@snack-uikit/tree 0.10.2-preview-013112d8.0 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +0 -12
- package/dist/cjs/helpers/index.d.ts +0 -2
- package/dist/cjs/helpers/index.js +1 -3
- package/dist/cjs/helpers/sortTreeItemsByTitle.js +2 -2
- package/dist/cjs/helpers/traverse.d.ts +1 -7
- package/dist/cjs/helpers/traverse.js +2 -38
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +1 -26
- package/dist/cjs/types.d.ts +0 -12
- package/dist/esm/helpers/index.d.ts +0 -2
- package/dist/esm/helpers/index.js +0 -2
- package/dist/esm/helpers/sortTreeItemsByTitle.js +2 -2
- package/dist/esm/helpers/traverse.d.ts +1 -7
- package/dist/esm/helpers/traverse.js +0 -24
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +0 -1
- package/dist/esm/types.d.ts +0 -12
- package/package.json +6 -24
- package/src/helpers/index.ts +0 -2
- package/src/helpers/sortTreeItemsByTitle.ts +2 -2
- package/src/helpers/traverse.ts +1 -38
- package/src/index.ts +0 -1
- package/src/types.ts +0 -9
- package/dist/cjs/helpers/__tests__/collectEmptyNestedNodesInExpanded.spec.d.ts +0 -1
- package/dist/cjs/helpers/__tests__/collectEmptyNestedNodesInExpanded.spec.js +0 -80
- package/dist/cjs/helpers/__tests__/setChildrenOfTreeNode.spec.d.ts +0 -1
- package/dist/cjs/helpers/__tests__/setChildrenOfTreeNode.spec.js +0 -102
- package/dist/cjs/helpers/collectEmptyNestedNodesInExpanded.d.ts +0 -2
- package/dist/cjs/helpers/collectEmptyNestedNodesInExpanded.js +0 -17
- package/dist/cjs/helpers/setChildrenOfTreeNode.d.ts +0 -2
- package/dist/cjs/helpers/setChildrenOfTreeNode.js +0 -28
- package/dist/cjs/hooks/index.d.ts +0 -2
- package/dist/cjs/hooks/index.js +0 -26
- package/dist/cjs/hooks/useSearchableTree.d.ts +0 -33
- package/dist/cjs/hooks/useSearchableTree.js +0 -138
- package/dist/cjs/hooks/useTreeMultiSelection.d.ts +0 -12
- package/dist/cjs/hooks/useTreeMultiSelection.js +0 -82
- package/dist/esm/helpers/__tests__/collectEmptyNestedNodesInExpanded.spec.d.ts +0 -1
- package/dist/esm/helpers/__tests__/collectEmptyNestedNodesInExpanded.spec.js +0 -64
- package/dist/esm/helpers/__tests__/setChildrenOfTreeNode.spec.d.ts +0 -1
- package/dist/esm/helpers/__tests__/setChildrenOfTreeNode.spec.js +0 -50
- package/dist/esm/helpers/collectEmptyNestedNodesInExpanded.d.ts +0 -2
- package/dist/esm/helpers/collectEmptyNestedNodesInExpanded.js +0 -11
- package/dist/esm/helpers/setChildrenOfTreeNode.d.ts +0 -2
- package/dist/esm/helpers/setChildrenOfTreeNode.js +0 -20
- package/dist/esm/hooks/index.d.ts +0 -2
- package/dist/esm/hooks/index.js +0 -2
- package/dist/esm/hooks/useSearchableTree.d.ts +0 -33
- package/dist/esm/hooks/useSearchableTree.js +0 -98
- package/dist/esm/hooks/useTreeMultiSelection.d.ts +0 -12
- package/dist/esm/hooks/useTreeMultiSelection.js +0 -43
- package/src/helpers/__tests__/collectEmptyNestedNodesInExpanded.spec.ts +0 -88
- package/src/helpers/__tests__/setChildrenOfTreeNode.spec.ts +0 -68
- package/src/helpers/collectEmptyNestedNodesInExpanded.ts +0 -16
- package/src/helpers/setChildrenOfTreeNode.ts +0 -30
- package/src/hooks/index.ts +0 -2
- package/src/hooks/useSearchableTree.ts +0 -150
- package/src/hooks/useTreeMultiSelection.ts +0 -61
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.setChildrenOfTreeNode = void 0;
|
|
7
|
-
const traverse_1 = require("./traverse");
|
|
8
|
-
const setChildrenOfTreeNode = (tree, nodeId, children) => {
|
|
9
|
-
const result = [];
|
|
10
|
-
(0, traverse_1.traverseWithTarget)(tree, result, (source, _depth, targetList) => {
|
|
11
|
-
var _a;
|
|
12
|
-
const isTarget = source.id === nodeId;
|
|
13
|
-
const hasNested = Array.isArray(source.nested);
|
|
14
|
-
let newNested;
|
|
15
|
-
if (isTarget) {
|
|
16
|
-
newNested = children;
|
|
17
|
-
} else if (hasNested) {
|
|
18
|
-
newNested = [];
|
|
19
|
-
}
|
|
20
|
-
const newNode = newNested !== undefined ? Object.assign(Object.assign({}, source), {
|
|
21
|
-
nested: newNested
|
|
22
|
-
}) : Object.assign({}, source);
|
|
23
|
-
targetList.push(newNode);
|
|
24
|
-
return !isTarget && hasNested && ((_a = source.nested) === null || _a === void 0 ? void 0 : _a.length) ? newNested : undefined;
|
|
25
|
-
});
|
|
26
|
-
return result;
|
|
27
|
-
};
|
|
28
|
-
exports.setChildrenOfTreeNode = setChildrenOfTreeNode;
|
package/dist/cjs/hooks/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
|
|
4
|
-
if (k2 === undefined) k2 = k;
|
|
5
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
-
desc = {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: function () {
|
|
10
|
-
return m[k];
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
Object.defineProperty(o, k2, desc);
|
|
15
|
-
} : function (o, m, k, k2) {
|
|
16
|
-
if (k2 === undefined) k2 = k;
|
|
17
|
-
o[k2] = m[k];
|
|
18
|
-
});
|
|
19
|
-
var __exportStar = void 0 && (void 0).__exportStar || function (m, exports) {
|
|
20
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
|
-
};
|
|
22
|
-
Object.defineProperty(exports, "__esModule", {
|
|
23
|
-
value: true
|
|
24
|
-
});
|
|
25
|
-
__exportStar(require("./useSearchableTree"), exports);
|
|
26
|
-
__exportStar(require("./useTreeMultiSelection"), exports);
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { TreeNodeProps } from '../types';
|
|
2
|
-
export type SearchResult<TTreeNode extends TreeNodeProps> = {
|
|
3
|
-
tree: TTreeNode[];
|
|
4
|
-
needPreloadNodes: string[];
|
|
5
|
-
};
|
|
6
|
-
export type SearchParams = {
|
|
7
|
-
search: string;
|
|
8
|
-
expandedNodes: string[];
|
|
9
|
-
};
|
|
10
|
-
type UseSearchableTreeParams<TRecordValue, TTreeNode extends TreeNodeProps> = {
|
|
11
|
-
initTree: TTreeNode[];
|
|
12
|
-
onPreloadNode: (node: TreeNodeProps) => Promise<TTreeNode[]>;
|
|
13
|
-
onPreloadNodes: (nodes: string[]) => Promise<Record<string, TTreeNode[]>>;
|
|
14
|
-
onSearch: (params: SearchParams) => Promise<SearchResult<TTreeNode>>;
|
|
15
|
-
mapNodeToRecordItem: (node: TTreeNode) => TRecordValue;
|
|
16
|
-
};
|
|
17
|
-
export declare function useSearchableTree<TRecordValue, TTreeNode extends TreeNodeProps>({ initTree, onPreloadNode, onPreloadNodes, onSearch, mapNodeToRecordItem, }: UseSearchableTreeParams<TRecordValue, TTreeNode>): {
|
|
18
|
-
tree: import("@siberiacancode/reactuse").StateRef<TTreeNode[]>;
|
|
19
|
-
expandedNodes: import("@siberiacancode/reactuse").StateRef<string[]>;
|
|
20
|
-
loading: boolean;
|
|
21
|
-
treeItemsRecord: import("@siberiacancode/reactuse").StateRef<Record<string, TRecordValue>>;
|
|
22
|
-
search: {
|
|
23
|
-
value: string;
|
|
24
|
-
onChange: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
25
|
-
};
|
|
26
|
-
onExpand: (nodes: string[]) => void;
|
|
27
|
-
onDataLoad: (node: TreeNodeProps) => Promise<{
|
|
28
|
-
preloadedChildren: TTreeNode[];
|
|
29
|
-
updatedTree: TTreeNode[];
|
|
30
|
-
newTreeItemsRecord: Record<string, TRecordValue>;
|
|
31
|
-
}>;
|
|
32
|
-
};
|
|
33
|
-
export {};
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) {
|
|
5
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
6
|
-
resolve(value);
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
-
function fulfilled(value) {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.next(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function rejected(value) {
|
|
18
|
-
try {
|
|
19
|
-
step(generator["throw"](value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function step(result) {
|
|
25
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
26
|
-
}
|
|
27
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
Object.defineProperty(exports, "__esModule", {
|
|
31
|
-
value: true
|
|
32
|
-
});
|
|
33
|
-
exports.useSearchableTree = useSearchableTree;
|
|
34
|
-
const reactuse_1 = require("@siberiacancode/reactuse");
|
|
35
|
-
const cancelable_promise_1 = require("cancelable-promise");
|
|
36
|
-
const react_1 = require("react");
|
|
37
|
-
const helpers_1 = require("../helpers");
|
|
38
|
-
function useSearchableTree(_ref) {
|
|
39
|
-
let {
|
|
40
|
-
initTree,
|
|
41
|
-
onPreloadNode,
|
|
42
|
-
onPreloadNodes,
|
|
43
|
-
onSearch,
|
|
44
|
-
mapNodeToRecordItem
|
|
45
|
-
} = _ref;
|
|
46
|
-
const tree = (0, reactuse_1.useRefState)(initTree);
|
|
47
|
-
const treeItemsRecord = (0, reactuse_1.useRefState)({});
|
|
48
|
-
const expandedNodes = (0, reactuse_1.useRefState)([]);
|
|
49
|
-
const [search, setSearch] = (0, react_1.useState)('');
|
|
50
|
-
const debouncedSearch = (0, reactuse_1.useDebounceValue)(search, 500);
|
|
51
|
-
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
52
|
-
const searchPromiseRef = (0, react_1.useRef)(null);
|
|
53
|
-
const buildTreeItemsRecord = (0, react_1.useCallback)(nodes => {
|
|
54
|
-
const record = {};
|
|
55
|
-
(0, helpers_1.traverse)(nodes, node => {
|
|
56
|
-
record[node.id] = mapNodeToRecordItem(node);
|
|
57
|
-
});
|
|
58
|
-
return record;
|
|
59
|
-
}, [mapNodeToRecordItem]);
|
|
60
|
-
const onExpand = (0, react_1.useCallback)(nodes => {
|
|
61
|
-
expandedNodes.current = nodes;
|
|
62
|
-
}, [expandedNodes]);
|
|
63
|
-
const onDataLoad = (0, react_1.useCallback)(node => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
const preloadedChildren = yield onPreloadNode(node);
|
|
65
|
-
const updatedTree = (0, helpers_1.setChildrenOfTreeNode)(tree.current, node.id, preloadedChildren);
|
|
66
|
-
tree.current = updatedTree;
|
|
67
|
-
const newTreeItemsRecord = Object.assign({}, treeItemsRecord.current);
|
|
68
|
-
(0, helpers_1.traverse)(preloadedChildren, child => {
|
|
69
|
-
newTreeItemsRecord[child.id] = mapNodeToRecordItem(child);
|
|
70
|
-
});
|
|
71
|
-
treeItemsRecord.current = newTreeItemsRecord;
|
|
72
|
-
return {
|
|
73
|
-
preloadedChildren,
|
|
74
|
-
updatedTree,
|
|
75
|
-
newTreeItemsRecord
|
|
76
|
-
};
|
|
77
|
-
}), [mapNodeToRecordItem, onPreloadNode, tree, treeItemsRecord]);
|
|
78
|
-
const handleSearch = (0, react_1.useCallback)(searchQuery => __awaiter(this, void 0, void 0, function* () {
|
|
79
|
-
var _a;
|
|
80
|
-
(_a = searchPromiseRef.current) === null || _a === void 0 ? void 0 : _a.cancel();
|
|
81
|
-
setLoading(true);
|
|
82
|
-
const searchPromise = (0, cancelable_promise_1.cancelable)(onSearch({
|
|
83
|
-
search: searchQuery,
|
|
84
|
-
expandedNodes: expandedNodes.current
|
|
85
|
-
}));
|
|
86
|
-
searchPromiseRef.current = searchPromise;
|
|
87
|
-
try {
|
|
88
|
-
const {
|
|
89
|
-
tree: searchedTree,
|
|
90
|
-
needPreloadNodes
|
|
91
|
-
} = yield searchPromise;
|
|
92
|
-
if (!searchPromise.isCanceled()) {
|
|
93
|
-
tree.current = searchedTree;
|
|
94
|
-
treeItemsRecord.current = buildTreeItemsRecord(searchedTree);
|
|
95
|
-
const expandedSet = new Set(expandedNodes.current);
|
|
96
|
-
const toPreloadExpandableNodes = (0, helpers_1.collectEmptyNestedNodesInExpanded)(searchedTree, expandedSet);
|
|
97
|
-
const collectedNodesForPreload = Array.from(new Set([...toPreloadExpandableNodes.map(node => node.id), ...needPreloadNodes]));
|
|
98
|
-
if (!collectedNodesForPreload.length) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const preloadedNodes = yield onPreloadNodes(collectedNodesForPreload);
|
|
102
|
-
if (searchPromiseRef.current !== searchPromise) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
let tmpTree = [...searchedTree];
|
|
106
|
-
for (const [nodeId, children] of Object.entries(preloadedNodes)) {
|
|
107
|
-
tmpTree = (0, helpers_1.setChildrenOfTreeNode)(tmpTree, nodeId, children);
|
|
108
|
-
}
|
|
109
|
-
tree.current = tmpTree;
|
|
110
|
-
treeItemsRecord.current = buildTreeItemsRecord(tmpTree);
|
|
111
|
-
}
|
|
112
|
-
} finally {
|
|
113
|
-
if (searchPromiseRef.current === searchPromise) {
|
|
114
|
-
setLoading(false);
|
|
115
|
-
searchPromiseRef.current = null;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}), [buildTreeItemsRecord, expandedNodes, onPreloadNodes, onSearch, tree, treeItemsRecord]);
|
|
119
|
-
(0, reactuse_1.useDidUpdate)(() => {
|
|
120
|
-
handleSearch(debouncedSearch);
|
|
121
|
-
}, [debouncedSearch, handleSearch]);
|
|
122
|
-
(0, react_1.useEffect)(() => {
|
|
123
|
-
tree.current = initTree;
|
|
124
|
-
treeItemsRecord.current = buildTreeItemsRecord(initTree);
|
|
125
|
-
}, [buildTreeItemsRecord, initTree, tree, treeItemsRecord]);
|
|
126
|
-
return {
|
|
127
|
-
tree,
|
|
128
|
-
expandedNodes,
|
|
129
|
-
loading,
|
|
130
|
-
treeItemsRecord,
|
|
131
|
-
search: {
|
|
132
|
-
value: search,
|
|
133
|
-
onChange: setSearch
|
|
134
|
-
},
|
|
135
|
-
onExpand,
|
|
136
|
-
onDataLoad
|
|
137
|
-
};
|
|
138
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { PreloadNodeHandler, SelectHandler, TreeNodeProps } from '../types';
|
|
2
|
-
type UseTreeMultiSelectionParams<TTreeNode extends TreeNodeProps> = {
|
|
3
|
-
onDataLoad: PreloadNodeHandler<TTreeNode>;
|
|
4
|
-
onSelect: SelectHandler;
|
|
5
|
-
selected?: string[];
|
|
6
|
-
onChangeSelected?: (newSelected: string[]) => void;
|
|
7
|
-
};
|
|
8
|
-
export declare function useTreeMultiSelection<TTreeNode extends TreeNodeProps>({ onDataLoad, onSelect: onSelectProp, selected, onChangeSelected, }: UseTreeMultiSelectionParams<TTreeNode>): {
|
|
9
|
-
selected: string[];
|
|
10
|
-
onSelect: (selectedKeys: string[], node: TreeNodeProps) => Promise<void>;
|
|
11
|
-
};
|
|
12
|
-
export {};
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) {
|
|
5
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
6
|
-
resolve(value);
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
-
function fulfilled(value) {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.next(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function rejected(value) {
|
|
18
|
-
try {
|
|
19
|
-
step(generator["throw"](value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function step(result) {
|
|
25
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
26
|
-
}
|
|
27
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
Object.defineProperty(exports, "__esModule", {
|
|
31
|
-
value: true
|
|
32
|
-
});
|
|
33
|
-
exports.useTreeMultiSelection = useTreeMultiSelection;
|
|
34
|
-
const react_1 = require("react");
|
|
35
|
-
const utils_1 = require("@snack-uikit/utils");
|
|
36
|
-
const getNewSelectedIds = (selectedIds, added, removed) => {
|
|
37
|
-
const result = new Set(selectedIds);
|
|
38
|
-
added.forEach(id => {
|
|
39
|
-
result.add(id);
|
|
40
|
-
});
|
|
41
|
-
removed.forEach(id => {
|
|
42
|
-
result.delete(id);
|
|
43
|
-
});
|
|
44
|
-
return Array.from(result);
|
|
45
|
-
};
|
|
46
|
-
function useTreeMultiSelection(_ref) {
|
|
47
|
-
let {
|
|
48
|
-
onDataLoad,
|
|
49
|
-
onSelect: onSelectProp,
|
|
50
|
-
selected,
|
|
51
|
-
onChangeSelected
|
|
52
|
-
} = _ref;
|
|
53
|
-
const [selectedIds = [], setSelectedIds] = (0, utils_1.useValueControl)({
|
|
54
|
-
value: selected,
|
|
55
|
-
defaultValue: [],
|
|
56
|
-
onChange: onChangeSelected
|
|
57
|
-
});
|
|
58
|
-
const onSelect = (0, react_1.useCallback)((selectedKeys, node) => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
const isSelected = selectedKeys.includes(node.id);
|
|
60
|
-
const clonedNode = structuredClone(node);
|
|
61
|
-
if (node.nested && !node.nested.length) {
|
|
62
|
-
const {
|
|
63
|
-
preloadedChildren
|
|
64
|
-
} = yield onDataLoad(node);
|
|
65
|
-
clonedNode.nested = preloadedChildren;
|
|
66
|
-
}
|
|
67
|
-
const {
|
|
68
|
-
added,
|
|
69
|
-
removed
|
|
70
|
-
} = onSelectProp({
|
|
71
|
-
selectedKeys,
|
|
72
|
-
node: clonedNode,
|
|
73
|
-
isSelected
|
|
74
|
-
});
|
|
75
|
-
const updatedSelectedIds = getNewSelectedIds(selectedIds, added, removed);
|
|
76
|
-
setSelectedIds(updatedSelectedIds);
|
|
77
|
-
}), [onDataLoad, onSelectProp, selectedIds, setSelectedIds]);
|
|
78
|
-
return {
|
|
79
|
-
selected: selectedIds,
|
|
80
|
-
onSelect
|
|
81
|
-
};
|
|
82
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { collectEmptyNestedNodesInExpanded } from '../collectEmptyNestedNodesInExpanded';
|
|
3
|
-
const node = (id, nested) => (Object.assign({ id, title: id }, (nested !== undefined && { nested })));
|
|
4
|
-
describe('collectEmptyNestedNodesInExpanded', () => {
|
|
5
|
-
it('returns nodes with empty nested array that are in expandedIds', () => {
|
|
6
|
-
const tree = [node('a', []), node('b', [node('b1')])];
|
|
7
|
-
const expandedIds = new Set(['a']);
|
|
8
|
-
const result = collectEmptyNestedNodesInExpanded(tree, expandedIds);
|
|
9
|
-
expect(result).toHaveLength(1);
|
|
10
|
-
expect(result[0]).toMatchObject({ id: 'a', nested: [] });
|
|
11
|
-
});
|
|
12
|
-
it('does not return nodes with empty nested if not in expandedIds', () => {
|
|
13
|
-
const tree = [node('a', []), node('b', [])];
|
|
14
|
-
const expandedIds = new Set(['b']);
|
|
15
|
-
const result = collectEmptyNestedNodesInExpanded(tree, expandedIds);
|
|
16
|
-
expect(result).toHaveLength(1);
|
|
17
|
-
expect(result[0].id).toBe('b');
|
|
18
|
-
});
|
|
19
|
-
it('does not return nodes that have non-empty nested', () => {
|
|
20
|
-
const tree = [node('a', [node('a1')]), node('b', [])];
|
|
21
|
-
const expandedIds = new Set(['a', 'b']);
|
|
22
|
-
const result = collectEmptyNestedNodesInExpanded(tree, expandedIds);
|
|
23
|
-
expect(result).toHaveLength(1);
|
|
24
|
-
expect(result[0].id).toBe('b');
|
|
25
|
-
});
|
|
26
|
-
it('does not return nodes without nested property (leaf)', () => {
|
|
27
|
-
const tree = [node('leaf'), node('empty', [])];
|
|
28
|
-
const expandedIds = new Set(['leaf', 'empty']);
|
|
29
|
-
const result = collectEmptyNestedNodesInExpanded(tree, expandedIds);
|
|
30
|
-
expect(result).toHaveLength(1);
|
|
31
|
-
expect(result[0].id).toBe('empty');
|
|
32
|
-
});
|
|
33
|
-
it('returns empty array for empty tree', () => {
|
|
34
|
-
const result = collectEmptyNestedNodesInExpanded([], new Set(['any']));
|
|
35
|
-
expect(result).toEqual([]);
|
|
36
|
-
});
|
|
37
|
-
it('returns empty array when expandedIds is empty', () => {
|
|
38
|
-
const tree = [node('a', []), node('b', [])];
|
|
39
|
-
const result = collectEmptyNestedNodesInExpanded(tree, new Set());
|
|
40
|
-
expect(result).toEqual([]);
|
|
41
|
-
});
|
|
42
|
-
it('collects from deeply nested nodes', () => {
|
|
43
|
-
const child = node('child', []);
|
|
44
|
-
const tree = [node('root', [node('mid', [child])])];
|
|
45
|
-
const expandedIds = new Set(['root', 'mid', 'child']);
|
|
46
|
-
const result = collectEmptyNestedNodesInExpanded(tree, expandedIds);
|
|
47
|
-
expect(result).toHaveLength(1);
|
|
48
|
-
expect(result[0]).toMatchObject({ id: 'child', nested: [] });
|
|
49
|
-
});
|
|
50
|
-
it('returns all matching nodes from different levels', () => {
|
|
51
|
-
const tree = [node('a', []), node('b', [node('b1', []), node('b2', [])])];
|
|
52
|
-
const expandedIds = new Set(['a', 'b', 'b1', 'b2']);
|
|
53
|
-
const result = collectEmptyNestedNodesInExpanded(tree, expandedIds);
|
|
54
|
-
expect(result).toHaveLength(3);
|
|
55
|
-
expect(result.map(n => n.id).sort()).toEqual(['a', 'b1', 'b2']);
|
|
56
|
-
});
|
|
57
|
-
it('only includes node if both empty nested and expanded', () => {
|
|
58
|
-
const tree = [node('expanded', []), node('collapsed', [])];
|
|
59
|
-
const expandedIds = new Set(['expanded']);
|
|
60
|
-
const result = collectEmptyNestedNodesInExpanded(tree, expandedIds);
|
|
61
|
-
expect(result).toHaveLength(1);
|
|
62
|
-
expect(result[0].id).toBe('expanded');
|
|
63
|
-
});
|
|
64
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { setChildrenOfTreeNode } from '../setChildrenOfTreeNode';
|
|
3
|
-
const node = (id, nested) => (Object.assign({ id, title: id }, (nested !== undefined && { nested: nested.map(n => (Object.assign(Object.assign({}, n), { title: n.id }))) })));
|
|
4
|
-
describe('setChildrenOfTreeNode', () => {
|
|
5
|
-
it('replaces nested for root-level node', () => {
|
|
6
|
-
const tree = [node('a', [{ id: 'a1', title: 'a1' }]), node('b', [{ id: 'b1', title: 'b1' }])];
|
|
7
|
-
const children = [node('new1'), node('new2')];
|
|
8
|
-
const result = setChildrenOfTreeNode(tree, 'b', children);
|
|
9
|
-
expect(result).toHaveLength(2);
|
|
10
|
-
expect(result[0]).toMatchObject({ id: 'a', nested: [{ id: 'a1' }] });
|
|
11
|
-
expect(result[1]).toMatchObject({ id: 'b', nested: [{ id: 'new1' }, { id: 'new2' }] });
|
|
12
|
-
});
|
|
13
|
-
it('replaces nested for deeply nested node', () => {
|
|
14
|
-
const childWithNested = node('child');
|
|
15
|
-
childWithNested.nested = [{ id: 'grand', title: 'grand' }];
|
|
16
|
-
const tree = [node('root', [childWithNested])];
|
|
17
|
-
const children = [node('replacement')];
|
|
18
|
-
const result = setChildrenOfTreeNode(tree, 'child', children);
|
|
19
|
-
const root = result[0];
|
|
20
|
-
expect(root.nested).toHaveLength(1);
|
|
21
|
-
expect(root.nested[0]).toMatchObject({ id: 'child', nested: [{ id: 'replacement' }] });
|
|
22
|
-
});
|
|
23
|
-
it('returns cloned tree when node is not found', () => {
|
|
24
|
-
const tree = [node('a'), node('b')];
|
|
25
|
-
const result = setChildrenOfTreeNode(tree, 'missing', [node('x')]);
|
|
26
|
-
expect(result).toHaveLength(2);
|
|
27
|
-
expect(result[0]).toMatchObject({ id: 'a' });
|
|
28
|
-
expect(result[1]).toMatchObject({ id: 'b' });
|
|
29
|
-
expect(result).not.toBe(tree);
|
|
30
|
-
});
|
|
31
|
-
it('returns empty array for empty tree', () => {
|
|
32
|
-
const result = setChildrenOfTreeNode([], 'any', [node('x')]);
|
|
33
|
-
expect(result).toEqual([]);
|
|
34
|
-
});
|
|
35
|
-
it('sets children for target node that had no nested (leaf)', () => {
|
|
36
|
-
const tree = [node('leaf'), node('other')];
|
|
37
|
-
const children = [node('new1')];
|
|
38
|
-
const result = setChildrenOfTreeNode(tree, 'leaf', children);
|
|
39
|
-
expect(result[0]).toMatchObject({ id: 'leaf', nested: [{ id: 'new1' }] });
|
|
40
|
-
expect(result[1]).toMatchObject({ id: 'other' });
|
|
41
|
-
});
|
|
42
|
-
it('preserves order of root nodes when target is second', () => {
|
|
43
|
-
const tree = [node('first'), node('second', [{ id: 's1', title: 's1' }]), node('third')];
|
|
44
|
-
const children = [node('new1'), node('new2')];
|
|
45
|
-
const result = setChildrenOfTreeNode(tree, 'second', children);
|
|
46
|
-
expect(result.map(n => n.id)).toEqual(['first', 'second', 'third']);
|
|
47
|
-
expect(result[1].nested).toHaveLength(2);
|
|
48
|
-
expect(result[1].nested.map(n => n.id)).toEqual(['new1', 'new2']);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { traverse } from './traverse';
|
|
2
|
-
export function collectEmptyNestedNodesInExpanded(nodes, expandedIds) {
|
|
3
|
-
const result = [];
|
|
4
|
-
traverse(nodes, node => {
|
|
5
|
-
const hasEmptyNested = Array.isArray(node.nested) && node.nested.length === 0;
|
|
6
|
-
if (hasEmptyNested && expandedIds.has(node.id)) {
|
|
7
|
-
result.push(node);
|
|
8
|
-
}
|
|
9
|
-
});
|
|
10
|
-
return result;
|
|
11
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { traverseWithTarget } from './traverse';
|
|
2
|
-
export const setChildrenOfTreeNode = (tree, nodeId, children) => {
|
|
3
|
-
const result = [];
|
|
4
|
-
traverseWithTarget(tree, result, (source, _depth, targetList) => {
|
|
5
|
-
var _a;
|
|
6
|
-
const isTarget = source.id === nodeId;
|
|
7
|
-
const hasNested = Array.isArray(source.nested);
|
|
8
|
-
let newNested;
|
|
9
|
-
if (isTarget) {
|
|
10
|
-
newNested = children;
|
|
11
|
-
}
|
|
12
|
-
else if (hasNested) {
|
|
13
|
-
newNested = [];
|
|
14
|
-
}
|
|
15
|
-
const newNode = (newNested !== undefined ? Object.assign(Object.assign({}, source), { nested: newNested }) : Object.assign({}, source));
|
|
16
|
-
targetList.push(newNode);
|
|
17
|
-
return !isTarget && hasNested && ((_a = source.nested) === null || _a === void 0 ? void 0 : _a.length) ? newNested : undefined;
|
|
18
|
-
});
|
|
19
|
-
return result;
|
|
20
|
-
};
|
package/dist/esm/hooks/index.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { TreeNodeProps } from '../types';
|
|
2
|
-
export type SearchResult<TTreeNode extends TreeNodeProps> = {
|
|
3
|
-
tree: TTreeNode[];
|
|
4
|
-
needPreloadNodes: string[];
|
|
5
|
-
};
|
|
6
|
-
export type SearchParams = {
|
|
7
|
-
search: string;
|
|
8
|
-
expandedNodes: string[];
|
|
9
|
-
};
|
|
10
|
-
type UseSearchableTreeParams<TRecordValue, TTreeNode extends TreeNodeProps> = {
|
|
11
|
-
initTree: TTreeNode[];
|
|
12
|
-
onPreloadNode: (node: TreeNodeProps) => Promise<TTreeNode[]>;
|
|
13
|
-
onPreloadNodes: (nodes: string[]) => Promise<Record<string, TTreeNode[]>>;
|
|
14
|
-
onSearch: (params: SearchParams) => Promise<SearchResult<TTreeNode>>;
|
|
15
|
-
mapNodeToRecordItem: (node: TTreeNode) => TRecordValue;
|
|
16
|
-
};
|
|
17
|
-
export declare function useSearchableTree<TRecordValue, TTreeNode extends TreeNodeProps>({ initTree, onPreloadNode, onPreloadNodes, onSearch, mapNodeToRecordItem, }: UseSearchableTreeParams<TRecordValue, TTreeNode>): {
|
|
18
|
-
tree: import("@siberiacancode/reactuse").StateRef<TTreeNode[]>;
|
|
19
|
-
expandedNodes: import("@siberiacancode/reactuse").StateRef<string[]>;
|
|
20
|
-
loading: boolean;
|
|
21
|
-
treeItemsRecord: import("@siberiacancode/reactuse").StateRef<Record<string, TRecordValue>>;
|
|
22
|
-
search: {
|
|
23
|
-
value: string;
|
|
24
|
-
onChange: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
25
|
-
};
|
|
26
|
-
onExpand: (nodes: string[]) => void;
|
|
27
|
-
onDataLoad: (node: TreeNodeProps) => Promise<{
|
|
28
|
-
preloadedChildren: TTreeNode[];
|
|
29
|
-
updatedTree: TTreeNode[];
|
|
30
|
-
newTreeItemsRecord: Record<string, TRecordValue>;
|
|
31
|
-
}>;
|
|
32
|
-
};
|
|
33
|
-
export {};
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { useDebounceValue, useDidUpdate, useRefState } from '@siberiacancode/reactuse';
|
|
11
|
-
import { cancelable } from 'cancelable-promise';
|
|
12
|
-
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
13
|
-
import { collectEmptyNestedNodesInExpanded, setChildrenOfTreeNode, traverse } from '../helpers';
|
|
14
|
-
export function useSearchableTree({ initTree, onPreloadNode, onPreloadNodes, onSearch, mapNodeToRecordItem, }) {
|
|
15
|
-
const tree = useRefState(initTree);
|
|
16
|
-
const treeItemsRecord = useRefState({});
|
|
17
|
-
const expandedNodes = useRefState([]);
|
|
18
|
-
const [search, setSearch] = useState('');
|
|
19
|
-
const debouncedSearch = useDebounceValue(search, 500);
|
|
20
|
-
const [loading, setLoading] = useState(false);
|
|
21
|
-
const searchPromiseRef = useRef(null);
|
|
22
|
-
const buildTreeItemsRecord = useCallback((nodes) => {
|
|
23
|
-
const record = {};
|
|
24
|
-
traverse(nodes, node => {
|
|
25
|
-
record[node.id] = mapNodeToRecordItem(node);
|
|
26
|
-
});
|
|
27
|
-
return record;
|
|
28
|
-
}, [mapNodeToRecordItem]);
|
|
29
|
-
const onExpand = useCallback((nodes) => {
|
|
30
|
-
expandedNodes.current = nodes;
|
|
31
|
-
}, [expandedNodes]);
|
|
32
|
-
const onDataLoad = useCallback((node) => __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
const preloadedChildren = yield onPreloadNode(node);
|
|
34
|
-
const updatedTree = setChildrenOfTreeNode(tree.current, node.id, preloadedChildren);
|
|
35
|
-
tree.current = updatedTree;
|
|
36
|
-
const newTreeItemsRecord = Object.assign({}, treeItemsRecord.current);
|
|
37
|
-
traverse(preloadedChildren, child => {
|
|
38
|
-
newTreeItemsRecord[child.id] = mapNodeToRecordItem(child);
|
|
39
|
-
});
|
|
40
|
-
treeItemsRecord.current = newTreeItemsRecord;
|
|
41
|
-
return { preloadedChildren, updatedTree, newTreeItemsRecord };
|
|
42
|
-
}), [mapNodeToRecordItem, onPreloadNode, tree, treeItemsRecord]);
|
|
43
|
-
const handleSearch = useCallback((searchQuery) => __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
var _a;
|
|
45
|
-
(_a = searchPromiseRef.current) === null || _a === void 0 ? void 0 : _a.cancel();
|
|
46
|
-
setLoading(true);
|
|
47
|
-
const searchPromise = cancelable(onSearch({ search: searchQuery, expandedNodes: expandedNodes.current }));
|
|
48
|
-
searchPromiseRef.current = searchPromise;
|
|
49
|
-
try {
|
|
50
|
-
const { tree: searchedTree, needPreloadNodes } = yield searchPromise;
|
|
51
|
-
if (!searchPromise.isCanceled()) {
|
|
52
|
-
tree.current = searchedTree;
|
|
53
|
-
treeItemsRecord.current = buildTreeItemsRecord(searchedTree);
|
|
54
|
-
const expandedSet = new Set(expandedNodes.current);
|
|
55
|
-
const toPreloadExpandableNodes = collectEmptyNestedNodesInExpanded(searchedTree, expandedSet);
|
|
56
|
-
const collectedNodesForPreload = Array.from(new Set([...toPreloadExpandableNodes.map(node => node.id), ...needPreloadNodes]));
|
|
57
|
-
if (!collectedNodesForPreload.length) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const preloadedNodes = yield onPreloadNodes(collectedNodesForPreload);
|
|
61
|
-
if (searchPromiseRef.current !== searchPromise) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
let tmpTree = [...searchedTree];
|
|
65
|
-
for (const [nodeId, children] of Object.entries(preloadedNodes)) {
|
|
66
|
-
tmpTree = setChildrenOfTreeNode(tmpTree, nodeId, children);
|
|
67
|
-
}
|
|
68
|
-
tree.current = tmpTree;
|
|
69
|
-
treeItemsRecord.current = buildTreeItemsRecord(tmpTree);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
finally {
|
|
73
|
-
if (searchPromiseRef.current === searchPromise) {
|
|
74
|
-
setLoading(false);
|
|
75
|
-
searchPromiseRef.current = null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}), [buildTreeItemsRecord, expandedNodes, onPreloadNodes, onSearch, tree, treeItemsRecord]);
|
|
79
|
-
useDidUpdate(() => {
|
|
80
|
-
handleSearch(debouncedSearch);
|
|
81
|
-
}, [debouncedSearch, handleSearch]);
|
|
82
|
-
useEffect(() => {
|
|
83
|
-
tree.current = initTree;
|
|
84
|
-
treeItemsRecord.current = buildTreeItemsRecord(initTree);
|
|
85
|
-
}, [buildTreeItemsRecord, initTree, tree, treeItemsRecord]);
|
|
86
|
-
return {
|
|
87
|
-
tree,
|
|
88
|
-
expandedNodes,
|
|
89
|
-
loading,
|
|
90
|
-
treeItemsRecord,
|
|
91
|
-
search: {
|
|
92
|
-
value: search,
|
|
93
|
-
onChange: setSearch,
|
|
94
|
-
},
|
|
95
|
-
onExpand,
|
|
96
|
-
onDataLoad,
|
|
97
|
-
};
|
|
98
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { PreloadNodeHandler, SelectHandler, TreeNodeProps } from '../types';
|
|
2
|
-
type UseTreeMultiSelectionParams<TTreeNode extends TreeNodeProps> = {
|
|
3
|
-
onDataLoad: PreloadNodeHandler<TTreeNode>;
|
|
4
|
-
onSelect: SelectHandler;
|
|
5
|
-
selected?: string[];
|
|
6
|
-
onChangeSelected?: (newSelected: string[]) => void;
|
|
7
|
-
};
|
|
8
|
-
export declare function useTreeMultiSelection<TTreeNode extends TreeNodeProps>({ onDataLoad, onSelect: onSelectProp, selected, onChangeSelected, }: UseTreeMultiSelectionParams<TTreeNode>): {
|
|
9
|
-
selected: string[];
|
|
10
|
-
onSelect: (selectedKeys: string[], node: TreeNodeProps) => Promise<void>;
|
|
11
|
-
};
|
|
12
|
-
export {};
|