layui/src/module/tree/useTree.ts

51 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-11-19 01:16:09 +00:00
import { TreeEmits, TreeProps } from './tree.type'
import { computed, ComputedRef, watch } from 'vue'
2021-11-19 01:16:09 +00:00
import { Tree, TreeData } from './tree'
export declare type UseTree = (
props: TreeProps,
emit: TreeEmits
) => {
2021-10-19 10:22:46 +00:00
tree: Tree
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-19 10:22:46 +00:00
showCheckbox: props.showCheckbox ?? false,
checkedKeys: props.checkedKeys ?? [],
expandKeys: props.expandKeys ?? [],
},
props.data
)
const nodeList = computed(() => {
2021-10-19 10:22:46 +00:00
const nodes = tree.getData()
2021-12-17 05:15:44 +00:00
console.log(nodes);
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)
},
{ deep: true }
)
return {
2021-10-19 10:22:46 +00:00
tree,
nodeList,
}
}