<template>
	<view class="tm-trees">
		<tm-trees-row v-for="(item,index) in options" 
			:option="item" 
			:openKeys="openKeys"
			:checkedKeys="checkedKeys"
			v-on:open-keys="openKeysHandle"
			v-on:checked-keys="checkedKeysHandle"
			:key="index"/>
	</view>
</template>

<script>
	/**
	 * 树形控件，支持多选
	 * 千里及
	 * 2021.2.1
	 * props: {
		 * options：树节点数据，
		 * defaultOpen：默认展开的项（节点key），
		 * defaultChecked：默认选中的项（节点key），
		 * open-keys：展开事件监听，会返回展开项的key，
		 * checked-keys：选中事件监听，返回选中项的key
		 * }
	 */
	export default {
		data() {
			return {
				openKeys: [],
				checkedKeys: []
			};
		},
		props: ['options', 'defaultOpen', 'defaultChecked'],
		created:function(){
			this.openKeys = this.defaultOpen || [];
			this.checkedKeys = this.defaultChecked || [];
		},
		watch: {
			defaultOpen: function(open) {
				this.openKeys = open;
			},
			defaultChecked: function(checked) {
				this.checkedKeys = checked;
			}
		},
		methods: {
			openKeysHandle: function(keys) {
				this.openKeys = keys;
				this.$emit('open-keys', keys);
			},
			checkedKeysHandle: function(keys) {
				console.log('已选中');
				this.checkedKeys = keys;
				this.$emit('checked-keys', keys);
			}
		}
	}
</script>

<style lang="less">
	.tm-trees {
		width: 100%;
		height: 100%;
	}
</style>
