<template>
  <el-popover placement="bottom-start" trigger="click" width="100%">
    <el-tree
      :data="treeData"
      :props="defaultProps"
      node-key="id"
      ref="tree"
      highlight-current
      accordion
      :expand-on-click-node="false"
      :current-node-key="value"
      @node-click="handleNodeClick"
    ></el-tree>
    <el-input
      slot="reference"
      :value="value"
      placeholder="请选择区域"
      clearable
      @clear="handleClear"
    ></el-input>
  </el-popover>
</template>

<script>
export default {
  name: "InputTree",
  props: {
    treeData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    value: {
      default: "",
    },
    defaultProps: {
      type: Object,
      default: () => {
        return {
          children: "children",
          label: "areaName",
        };
      },
    },
  },
  data() {
    return {};
  },
  methods: {
    handleClear() {
      this.$emit("input", "");
      this.$emit("change", {});
      this.$refs.tree.setCurrentKey(null);
    },
    handleNodeClick(row) {
      this.$emit("input", row.areaName);
      this.$emit("change", row);
    },
  },
};
</script>

<style lang="less" scoped></style>
