Skip to content

Commit 649f407

Browse files
committed
改变flatList类型
1 parent 0955b90 commit 649f407

File tree

5 files changed

+40
-44
lines changed

5 files changed

+40
-44
lines changed

src/components/VirtualTree/index.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export default defineComponent({
3636
setup(props, { emit, slots, expose }) {
3737
const loading = shallowRef(false);
3838
const selectedKey = ref<string | number>('');
39-
const flatList = ref<TreeNodeOptions[]>([]);
39+
const flatList = ref<Required<TreeNodeOptions>[]>([]);
4040
watch(() => props.source, newVal => {
4141
flatList.value = flattenTree(newVal);
4242
}, { immediate: true });
43-
const selectChange = (node: TreeNodeOptions) => {
43+
const selectChange = (node: Required<TreeNodeOptions>) => {
4444
node.selected = !node.selected;
4545
if (selectedKey.value !== node.nodeKey) {
4646
const preSelectedIndex = flatList.value.findIndex(item => item.nodeKey === selectedKey.value);
@@ -54,7 +54,7 @@ export default defineComponent({
5454
}
5555

5656

57-
const checkChange = ([checked, node]: [boolean, TreeNodeOptions]) => {
57+
const checkChange = ([checked, node]: [boolean, Required<TreeNodeOptions>]) => {
5858
node.checked = checked;
5959
if (!props.checkStrictly) {
6060
updateDownwards(checked, node);
@@ -63,7 +63,7 @@ export default defineComponent({
6363
emit('checkChange', node);
6464
}
6565

66-
const expandNode = (node: TreeNodeOptions, children: TreeNodeOptions[] = []) => {
66+
const expandNode = (node: Required<TreeNodeOptions>, children: TreeNodeOptions[] = []) => {
6767
const trueChildren = children.length ? children : cloneDeep(node.children)!;
6868
node.children = trueChildren.map(item => {
6969
item.loading = false;
@@ -78,18 +78,18 @@ export default defineComponent({
7878
return item;
7979
});
8080
const targetIndex = flatList.value.findIndex(item => item.nodeKey === node.nodeKey);
81-
flatList.value.splice(targetIndex + 1, 0, ...node.children);
81+
flatList.value.splice(targetIndex + 1, 0, ...(node.children as Required<TreeNodeOptions>[]));
8282
}
8383

84-
const collapseNode = (targetNode: TreeNodeOptions) => {
84+
const collapseNode = (targetNode: Required<TreeNodeOptions>) => {
8585
const delKeys: nodeKey[] = [];
86-
const recursion = (node: TreeNodeOptions) => {
86+
const recursion = (node: Required<TreeNodeOptions>) => {
8787
if (node.children?.length) {
8888
node.children.forEach(item => {
8989
delKeys.push(item.nodeKey);
9090
if (item.expanded) {
9191
item.expanded = false;
92-
recursion(item);
92+
recursion(item as Required<TreeNodeOptions>);
9393
}
9494
});
9595
}
@@ -100,7 +100,7 @@ export default defineComponent({
100100
}
101101
}
102102

103-
const toggleExpand = (node: TreeNodeOptions) => {
103+
const toggleExpand = (node: Required<TreeNodeOptions>) => {
104104
if (loading.value) return;
105105
// console.log('expand node');
106106
node.expanded = !node.expanded;
@@ -154,7 +154,7 @@ export default defineComponent({
154154
list: flatList.value,
155155
dataKey: 'nodeKey',
156156
}, {
157-
default: (data: { item: TreeNodeOptions, index: number }) => h(VirTreeNode, {
157+
default: (data: { item: Required<TreeNodeOptions>, index: number }) => h(VirTreeNode, {
158158
ref: setRef.bind(null, data.index),
159159
node: data.item,
160160
showCheckbox: props.showCheckbox,

src/components/VirtualTree/node.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineComponent({
77
name: 'VirTreeNode',
88
props: {
99
node: {
10-
type: Object as PropType<TreeNodeOptions>,
10+
type: Object as PropType<Required<TreeNodeOptions>>,
1111
required: true
1212
},
1313
iconSlot: Function as PropType<Slot>,

src/components/VirtualTree/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface TreeNodeOptions {
1616
hasChildren?: boolean;
1717
children?: TreeNodeOptions[];
1818
parentKey?: nodeKey | null;
19-
[key: string]: any;
19+
// [key: string]: any;
2020
}
2121

2222
interface TreeInstance {

src/components/VirtualTree/uses.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import { getCurrentInstance } from "vue";
22
import {TreeNodeOptions} from "./types";
33

4-
function flattenTree(source: TreeNodeOptions[]): TreeNodeOptions[] {
5-
const result: TreeNodeOptions[] = [];
6-
function recursion (list: TreeNodeOptions[], level = 0, parent: TreeNodeOptions | null = null) {
4+
function flattenTree(source: TreeNodeOptions[]): Required<TreeNodeOptions>[] {
5+
const result: Required<TreeNodeOptions>[] = [];
6+
function recursion (list: TreeNodeOptions[], level = 0, parent: Required<TreeNodeOptions> | null = null) {
77
return list.map(item => {
8-
const flatNode = {
8+
const flatNode: Required<TreeNodeOptions> = {
99
...item,
1010
level,
1111
loading: false,
1212
disabled: item.disabled || false,
13-
expand: item.expanded || false,
13+
expanded: item.expanded || false,
1414
selected: item.selected || false,
1515
checked: item.checked || parent?.checked || false,
1616
hasChildren: item.hasChildren || false,
17-
parentKey: parent?.nodeKey || null
17+
parentKey: parent?.nodeKey || null,
18+
children: item.children || []
1819
};
1920
result.push(flatNode);
2021
if (item.expanded && item.children?.length) {
2122
flatNode.children = recursion(item.children, level + 1, flatNode);
22-
} else {
23-
flatNode.children = flatNode.children || [];
2423
}
2524
return flatNode;
2625
});
@@ -31,22 +30,22 @@ function flattenTree(source: TreeNodeOptions[]): TreeNodeOptions[] {
3130
}
3231

3332

34-
function updateDownwards(checked: boolean, node: TreeNodeOptions) {
35-
const update = (children: TreeNodeOptions[]) => {
33+
function updateDownwards(checked: boolean, node: Required<TreeNodeOptions>) {
34+
const update = (children: Required<TreeNodeOptions>[]) => {
3635
if (children.length) {
3736
children.forEach(child => {
3837
child.checked = checked;
3938
if (child.children?.length) {
40-
update(child.children);
39+
update(child.children as Required<TreeNodeOptions>[]);
4140
}
4241
});
4342
}
4443
}
45-
update(node.children!);
44+
update(node.children as Required<TreeNodeOptions>[]);
4645
}
4746

48-
function updateUpwards(targetNode: TreeNodeOptions, flatList: TreeNodeOptions[]) {
49-
const update = (node: TreeNodeOptions) => {
47+
function updateUpwards(targetNode: Required<TreeNodeOptions>, flatList: Required<TreeNodeOptions>[]) {
48+
const update = (node: Required<TreeNodeOptions>) => {
5049
if (node.parentKey != null) { // 说明是子节点
5150
const parentNode = flatList.find(item => item.nodeKey == node.parentKey)!;
5251
// console.log('parentNode', parentNode);
@@ -60,12 +59,4 @@ function updateUpwards(targetNode: TreeNodeOptions, flatList: TreeNodeOptions[])
6059
update(targetNode);
6160
}
6261

63-
64-
function useExpose<T>(apis: T) {
65-
const instance = getCurrentInstance();
66-
if (instance) {
67-
Object.assign(instance.proxy, apis);
68-
}
69-
}
70-
71-
export { flattenTree, updateUpwards, updateDownwards, useExpose };
62+
export { flattenTree, updateUpwards, updateDownwards };

src/doc/SearchNodeDemo.vue

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@
1010
<script lang="tsx">
1111
import {defineComponent, onMounted, ref} from 'vue';
1212
import {TreeNodeOptions} from "../components/VirtualTree/types";
13+
14+
interface TreeNodeOptionsWithParentPath extends TreeNodeOptions {
15+
parentPath: Array<string | number>;
16+
}
17+
1318
const UNIQUE_WRAPPERS = ['##==-open_tag-==##', '##==-close_tag-==##'];
1419
let expandKeys: Array<string | number> = [];
15-
function getParentPath (parent: TreeNodeOptions | null): Array<string | number> {
16-
let result = [];
20+
function getParentPath (parent: TreeNodeOptionsWithParentPath | null): Array<string | number> {
21+
let result: Array<string | number> = [];
1722
if (parent) {
1823
const base = parent.parentPath || [];
1924
result = base.concat(parent.nodeKey);
2025
}
2126
return result;
2227
}
23-
function recursion(path = '0', level = 3, parent: TreeNodeOptions | null = null): TreeNodeOptions[] {
28+
function recursion(path = '0', level = 3, parent: TreeNodeOptionsWithParentPath | null = null): TreeNodeOptionsWithParentPath[] {
2429
const list = [];
2530
for (let i = 0; i < 10; i++) {
2631
const nodeKey = `${path}-${i}`;
27-
const treeNode: TreeNodeOptions = {
32+
const treeNode: TreeNodeOptionsWithParentPath = {
2833
nodeKey,
2934
name: nodeKey,
3035
children: [],
@@ -48,7 +53,7 @@
4853
name: 'SearchNodeDemo',
4954
setup(prop, {emit}) {
5055
const keywords = ref('');
51-
const list = ref<TreeNodeOptions[]>([]);
56+
const list = ref<TreeNodeOptionsWithParentPath[]>([]);
5257
5358
onMounted(() => {
5459
list.value = recursion();
@@ -57,9 +62,9 @@
5762
const formatSearchValue = (value: string) => {
5863
return new RegExp(value.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$&'), 'i');
5964
}
60-
const findMatchedNodes = (keywords: string): TreeNodeOptions[] => {
61-
const result: TreeNodeOptions[] = [];
62-
const recursion = (list: TreeNodeOptions[], parent: TreeNodeOptions | null = null) => {
65+
const findMatchedNodes = (keywords: string): TreeNodeOptionsWithParentPath[] => {
66+
const result: TreeNodeOptionsWithParentPath[] = [];
67+
const recursion = (list: TreeNodeOptionsWithParentPath[], parent: TreeNodeOptionsWithParentPath | null = null) => {
6368
for (const item of list) {
6469
const matched = formatSearchValue(keywords).test(item.name);
6570
if (matched) {
@@ -69,7 +74,7 @@
6974
parent.expanded = matched;
7075
}
7176
if (item.children?.length) {
72-
recursion(item.children, item);
77+
recursion(item.children as TreeNodeOptionsWithParentPath[], item);
7378
}
7479
}
7580
}

0 commit comments

Comments
 (0)