DeeCustomCmp.vue 1.69 KB
<template>

  <component
    :is="allComps"
    :key="key"
    v-model="cptValue"
    :read-only="readOnly"
    :form="form"
    :prop="prop"
    v-bind="config"
    :size="size"
    :item="item"
  />
</template>

<script>
import customCmps from '@/customCompontents/index'
export default {
  name: 'DeeCustom',
  props: {
    readOnly: {
      type: Boolean,
      default: () => false
    },
    value: {
      type: [String, Number, Array, Object, Boolean],
      default: () => ''
    },
    cptName: {
      type: String,
      default: () => ''
    },
    form: {
      type: Object,
      default: () => null
    },
    item: {
      type: Object,
      default: () => null
    },
    size: {
      type: String,
      default: () => 'small'
    }
  },
  data() {
    return {
      customCmps,
      key: '',
      cptValue: this.value
    }
  },
  computed: {
    allComps() {
      return this.customCmps[this.cptName] || this.cptName
    },
    prop() {
      return this.$parent.prop
    },
    config() {
      if (!this.item || !this.item.component || !this.item.component.props) {
        return {}
      }
      let config = {}
      try {
        if (typeof (this.item.component.props) === 'string') {
          // eslint-disable-next-line no-eval
          eval(`config = ${this.item.component.props || {}}`)
        } else {
          config = { ...this.item.component.props }
        }
      } catch {
        config = {}
      }
      return config
    }
  },
  watch: {
    value(val) {
      this.cptValue = val
    },
    cptValue(val) {
      if (val === this.value) {
        return
      }
      this.$emit('input', val)
    }
  },
  mounted() {

  },
  methods: {

  }

}
</script>

<style>

</style>