<template>
  <ul class="uif-tree-list">
    <template v-for="(data, i) in dataList">
      <u-tree-item :key="i" v-if="data" v-bind="$attrs" :slot-function="slotFunction" :data="data" :active="active" :level="0"
                   @select="onSelect" @click="onClick"></u-tree-item>
    </template>
  </ul>
</template>
<script>
import {findSlot} from '@uif/hq/lib/utils/findComponents'
import UTreeItem from './tree-item'

export default {
  inheritAttrs: false,
  name: 'u-tree-list',
  components: {
    UTreeItem
  },
  props: {
    // 节点数据
    dataList: {
      type: Array,
      default () {
        return []
      }
    },
    // 选中的数据
    value: {
      default: null
    }
  },
  data () {
    return {
      active: this.value,
      slotFunction: null
    }
  },
  computed: {
  },
  watch: {
    value () {
      if (this.active !== this.value) {
        this.active = this.value
      }
    }
  },
  mounted () {
    this.slotFunction = findSlot(this, 'tree-item')
  },
  methods: {
    // 点击事件
    onClick (data) {
      this.$emit('click', data)
    },
    onSelect (value) {
      this.active = value
      this.$emit('input', value)
    }
  }
}
</script>
