<!--
 * @Descripttion:
 * @Version: 1.0
 * @Author: +C
 * @Date: 2022-02-15 08:47:06
 * @LastEditors: wufz
 * @LastEditTime: 2022-02-24 11:42:33
-->
<script>
import formComponents from "./formComponents"
export default {
  name: `DxxTreeSelect`,
  //继承表单父组件
  extends: formComponents,
  /* 这里的v-model的值将会传入这个名为 value的prop。同时当组件触发一个 change事件并附带一个新的值的时候，这个 value的 property将会被更新。 */
  model: {
    prop: 'value',
    event: 'change'
  },
  // functional: true, //函数式组件
  props: {
    value: { // v-model绑定的值
      type: String,
      required: false
    },
    replaceFields: {
      type: Object,
      required: false,
      default: () => { return { children: 'children', title: 'title', key: 'key', value: 'value' } }
    }
  },
  methods: {
    /**
     * @description: 让下拉框相对定位到表单组件而不是body
     * @author: wufz
     * @return {*}
     */
    getPopupContainer() {
      return this.$parent.$el
    }
  },
  //建立虚拟 DOM
  render: function (createElement) {
    /* let _this = Object.entries(this)
    console.log(_this); */
    //  详细：dom输出值
    let node = this.XEUtils.findTree(this.$attrs['tree-data'], (item) => item[this._props.replaceFields.value] === this._props.value, { children: this._props.replaceFields.children })
    let showStr = node ? node.item[this._props.replaceFields.title] : ''
    let drawText = createElement(`span`, {}, [showStr])
    this.$attrs.getPopupContainer = this.getPopupContainer

    //编辑：dom表单元素
    let drawFromComponents = createElement(
      // 标签名称
      `a-tree-select`,
      // 传递数据
      {
        attrs: {
          ...this.$attrs
        },
        props: {
          ...this._props
        },
        on: {
          ...this.$listeners
        },
        scopedSlots: {
          ...this.$scopedSlots
        }
        /* ...context.data,
        props: {
          ...context.props
        },
        class: (context.data.staticClass || "") + " dxx-select", //自定义class
        style: {
          ...context.data.staticStyle
        } */
      },
      // 子节点数组，createElement...
      [
        this.$slots.default
      ]
    );

    return this.isDetail ? drawText : drawFromComponents;
  }
};
</script>

<style scoped>
</style>