<template>
	<div class="tree-choice">
		<el-input placeholder="输入名称搜索" v-model="filterText" style="margin-bottom: 10px"></el-input>
		<el-tree :data="dataList" node-key="id" default-expand-all :highlight-current="true" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" @node-click="nodeClick">
			<span class="custom-tree-node" slot-scope="{ data }">
				<span>{{ data.menuName }}</span>
			</span>
		</el-tree>
	</div>
</template>

<script>
	export default {
		props: {
			dataList: Array,
			nodeKey: {
				type: String,
				default: 'id'
			},
			nodeVal: {
				type: String,
				default: 'name'
			}
		},
		data() {
			return {
				//搜索字段
				filterText: ''
			}
		},
		watch: {
			filterText(val) {
				this.$refs.tree.filter(val)
			}
		},
		methods: {
			//搜索过滤节点
			filterNode(value, data) {
				if (!value) return true
				return data[this.nodeVal].indexOf(value) !== -1
			},
			nodeClick(data) {
				this.$emit('itemClick', data)
			}
		}
	}
</script>

<style scoped lang="scss">
	.tree-choice {
		background-color: #fff;
		padding: 10px 15px;
		width: 280px;
		margin-right: 7px;
		flex-shrink: 0;
	}
</style>
