2021-11-19 01:16:09 +00:00
|
|
|
import { TreeEmits, TreeProps } from './tree.type'
|
2021-10-17 16:55:58 +00:00
|
|
|
import { computed, ComputedRef, watch } from 'vue'
|
2021-11-19 01:16:09 +00:00
|
|
|
import { Tree, TreeData } from './tree'
|
2021-10-17 16:55:58 +00:00
|
|
|
|
|
|
|
export declare type UseTree = (
|
|
|
|
props: TreeProps,
|
|
|
|
emit: TreeEmits
|
|
|
|
) => {
|
2021-10-19 10:22:46 +00:00
|
|
|
tree: Tree
|
2021-10-17 16:55:58 +00:00
|
|
|
nodeList: ComputedRef<TreeData[]>
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useTree: UseTree = (props: TreeProps, emit: TreeEmits) => {
|
2021-10-19 10:22:46 +00:00
|
|
|
const tree = new Tree(
|
|
|
|
{
|
|
|
|
nodeMap: new Map(),
|
|
|
|
originMap: new Map(),
|
|
|
|
replaceFields: {
|
|
|
|
id: 'id',
|
|
|
|
title: 'title',
|
|
|
|
children: 'children',
|
2021-10-17 16:55:58 +00:00
|
|
|
},
|
2021-10-19 10:22:46 +00:00
|
|
|
showCheckbox: props.showCheckbox ?? false,
|
|
|
|
checkedKeys: props.checkedKeys ?? [],
|
|
|
|
expandKeys: props.expandKeys ?? [],
|
|
|
|
},
|
|
|
|
props.data
|
|
|
|
)
|
|
|
|
|
2021-10-17 16:55:58 +00:00
|
|
|
const nodeList = computed(() => {
|
2021-10-19 10:22:46 +00:00
|
|
|
const nodes = tree.getData()
|
2021-10-17 16:55:58 +00:00
|
|
|
return nodes
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => nodeList,
|
|
|
|
(list) => {
|
2021-10-19 10:22:46 +00:00
|
|
|
const { checkedKeys, expandKeys } = tree.getKeys()
|
|
|
|
emit('update:checkedKeys', checkedKeys)
|
|
|
|
// emit('update:expandKeys', expandKeys)
|
2021-10-17 16:55:58 +00:00
|
|
|
},
|
|
|
|
{ deep: true }
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
2021-10-19 10:22:46 +00:00
|
|
|
tree,
|
2021-10-17 16:55:58 +00:00
|
|
|
nodeList,
|
|
|
|
}
|
|
|
|
}
|