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