<template>
  <div>
    <el-popover placement="bottom-start" trigger="click">
      <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>
  </div>
</template>

<script>
export default {
  props: {
    treeData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    value: {
      default: "",
    },
  },
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "areaName",
      },
    };
  },
  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>
