@snack-uikit/tree 0.9.37 → 0.10.1-preview-0aba136f.0

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.
Files changed (83) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/cjs/helpers/__tests__/collectIds.spec.d.ts +1 -0
  3. package/dist/cjs/helpers/__tests__/collectIds.spec.js +14 -0
  4. package/dist/cjs/helpers/__tests__/constants.d.ts +2 -0
  5. package/dist/cjs/helpers/__tests__/constants.js +42 -0
  6. package/dist/cjs/helpers/__tests__/getSearchedTreeItems.spec.d.ts +1 -0
  7. package/dist/cjs/helpers/__tests__/getSearchedTreeItems.spec.js +183 -0
  8. package/dist/cjs/helpers/__tests__/getSearchedTreeNodeById.spec.d.ts +1 -0
  9. package/dist/cjs/helpers/__tests__/getSearchedTreeNodeById.spec.js +123 -0
  10. package/dist/cjs/helpers/checkNestedNodesSelection.d.ts +5 -0
  11. package/dist/cjs/helpers/checkNestedNodesSelection.js +17 -0
  12. package/dist/cjs/helpers/collectIds.d.ts +2 -0
  13. package/dist/cjs/helpers/collectIds.js +13 -0
  14. package/dist/cjs/helpers/extractTreeNodeTitle.d.ts +2 -0
  15. package/dist/cjs/helpers/extractTreeNodeTitle.js +15 -0
  16. package/dist/cjs/helpers/findAllChildNodeIds.d.ts +2 -0
  17. package/dist/cjs/helpers/findAllChildNodeIds.js +21 -0
  18. package/dist/cjs/helpers/findAllExpandedChildNodeIds.d.ts +2 -0
  19. package/dist/cjs/helpers/findAllExpandedChildNodeIds.js +19 -0
  20. package/dist/cjs/helpers/getSearchedTreeItems.d.ts +10 -0
  21. package/dist/cjs/helpers/getSearchedTreeItems.js +43 -0
  22. package/dist/cjs/helpers/getSearchedTreeNodeById.d.ts +11 -0
  23. package/dist/cjs/helpers/getSearchedTreeNodeById.js +39 -0
  24. package/dist/cjs/helpers/index.d.ts +8 -0
  25. package/dist/cjs/helpers/index.js +32 -0
  26. package/dist/cjs/helpers/lookupTreeForSelectedNodes.d.ts +6 -0
  27. package/dist/cjs/{helpers.js → helpers/lookupTreeForSelectedNodes.js} +5 -43
  28. package/dist/cjs/helpers/sortTreeItemsByTitle.d.ts +2 -0
  29. package/dist/cjs/helpers/sortTreeItemsByTitle.js +13 -0
  30. package/dist/cjs/helpers/traverse.d.ts +2 -0
  31. package/dist/cjs/helpers/traverse.js +40 -0
  32. package/dist/cjs/types.d.ts +3 -0
  33. package/dist/esm/helpers/__tests__/collectIds.spec.d.ts +1 -0
  34. package/dist/esm/helpers/__tests__/collectIds.spec.js +20 -0
  35. package/dist/esm/helpers/__tests__/constants.d.ts +2 -0
  36. package/dist/esm/helpers/__tests__/constants.js +52 -0
  37. package/dist/esm/helpers/__tests__/getSearchedTreeItems.spec.d.ts +1 -0
  38. package/dist/esm/helpers/__tests__/getSearchedTreeItems.spec.js +165 -0
  39. package/dist/esm/helpers/__tests__/getSearchedTreeNodeById.spec.d.ts +1 -0
  40. package/dist/esm/helpers/__tests__/getSearchedTreeNodeById.spec.js +112 -0
  41. package/dist/esm/helpers/checkNestedNodesSelection.d.ts +5 -0
  42. package/dist/esm/helpers/checkNestedNodesSelection.js +11 -0
  43. package/dist/esm/helpers/collectIds.d.ts +2 -0
  44. package/dist/esm/helpers/collectIds.js +6 -0
  45. package/dist/esm/helpers/extractTreeNodeTitle.d.ts +2 -0
  46. package/dist/esm/helpers/extractTreeNodeTitle.js +1 -0
  47. package/dist/esm/helpers/findAllChildNodeIds.d.ts +2 -0
  48. package/dist/esm/helpers/findAllChildNodeIds.js +15 -0
  49. package/dist/esm/helpers/findAllExpandedChildNodeIds.d.ts +2 -0
  50. package/dist/esm/helpers/findAllExpandedChildNodeIds.js +13 -0
  51. package/dist/esm/helpers/getSearchedTreeItems.d.ts +10 -0
  52. package/dist/esm/helpers/getSearchedTreeItems.js +31 -0
  53. package/dist/esm/helpers/getSearchedTreeNodeById.d.ts +11 -0
  54. package/dist/esm/helpers/getSearchedTreeNodeById.js +23 -0
  55. package/dist/esm/helpers/index.d.ts +8 -0
  56. package/dist/esm/helpers/index.js +8 -0
  57. package/dist/esm/helpers/lookupTreeForSelectedNodes.d.ts +6 -0
  58. package/dist/esm/{helpers.js → helpers/lookupTreeForSelectedNodes.js} +2 -38
  59. package/dist/esm/helpers/sortTreeItemsByTitle.d.ts +2 -0
  60. package/dist/esm/helpers/sortTreeItemsByTitle.js +6 -0
  61. package/dist/esm/helpers/traverse.d.ts +2 -0
  62. package/dist/esm/helpers/traverse.js +19 -0
  63. package/dist/esm/types.d.ts +3 -0
  64. package/package.json +4 -3
  65. package/src/helperComponents/TreeNode/TreeNode.tsx +2 -1
  66. package/src/helpers/__tests__/collectIds.spec.ts +23 -0
  67. package/src/helpers/__tests__/constants.ts +54 -0
  68. package/src/helpers/__tests__/getSearchedTreeItems.spec.ts +191 -0
  69. package/src/helpers/__tests__/getSearchedTreeNodeById.spec.ts +138 -0
  70. package/src/helpers/checkNestedNodesSelection.ts +15 -0
  71. package/src/helpers/collectIds.ts +10 -0
  72. package/src/helpers/extractTreeNodeTitle.ts +4 -0
  73. package/src/helpers/findAllChildNodeIds.ts +20 -0
  74. package/src/helpers/findAllExpandedChildNodeIds.ts +18 -0
  75. package/src/helpers/getSearchedTreeItems.ts +50 -0
  76. package/src/helpers/getSearchedTreeNodeById.ts +46 -0
  77. package/src/helpers/index.ts +8 -0
  78. package/src/{helpers.ts → helpers/lookupTreeForSelectedNodes.ts} +3 -50
  79. package/src/helpers/sortTreeItemsByTitle.ts +10 -0
  80. package/src/helpers/traverse.ts +27 -0
  81. package/src/types.ts +4 -0
  82. package/dist/cjs/helpers.d.ts +0 -11
  83. package/dist/esm/helpers.d.ts +0 -11
@@ -3,47 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.checkNestedNodesSelection = checkNestedNodesSelection;
7
- exports.findAllExpandedChildNodeIds = findAllExpandedChildNodeIds;
8
6
  exports.lookupTreeForSelectedNodes = lookupTreeForSelectedNodes;
9
- function findAllChildNodeIds(nodes) {
10
- var _a;
11
- const stack = [...nodes];
12
- const ids = [];
13
- let node;
14
- while (node = stack.pop()) {
15
- if (!node.disabled) {
16
- ids.push(node.id);
17
- if ((_a = node.nested) === null || _a === void 0 ? void 0 : _a.length) {
18
- stack.push(...node.nested);
19
- }
20
- }
21
- }
22
- return ids;
23
- }
24
- function checkNestedNodesSelection(nodes, selectedKeys) {
25
- const allIds = findAllChildNodeIds(nodes);
26
- const selected = allIds.filter(id => selectedKeys.includes(id));
27
- const someSelected = selected.length > 0;
28
- const allSelected = someSelected && allIds.length === selected.length;
29
- return {
30
- someSelected: !allSelected && someSelected,
31
- allSelected
32
- };
33
- }
34
- function findAllExpandedChildNodeIds(nodes, expandedNodes) {
35
- var _a;
36
- const stack = [...nodes];
37
- const ids = [];
38
- let node;
39
- while (node = stack.shift()) {
40
- ids.push(node.id);
41
- if (((_a = node.nested) === null || _a === void 0 ? void 0 : _a.length) && expandedNodes.includes(node.id)) {
42
- stack.unshift(...node.nested);
43
- }
44
- }
45
- return ids;
46
- }
7
+ const checkNestedNodesSelection_1 = require("./checkNestedNodesSelection");
8
+ const findAllChildNodeIds_1 = require("./findAllChildNodeIds");
47
9
  function lookupTreeForSelectedNodes(_ref) {
48
10
  let {
49
11
  node,
@@ -54,12 +16,12 @@ function lookupTreeForSelectedNodes(_ref) {
54
16
  const {
55
17
  nested
56
18
  } = node;
57
- const childSelection = (nested === null || nested === void 0 ? void 0 : nested.length) ? checkNestedNodesSelection(nested, selectedNodes) : undefined;
19
+ const childSelection = (nested === null || nested === void 0 ? void 0 : nested.length) ? (0, checkNestedNodesSelection_1.checkNestedNodesSelection)(nested, selectedNodes) : undefined;
58
20
  const isSelected = childSelection ? childSelection.someSelected || childSelection.allSelected : selectedNodes.includes(node.id);
59
21
  let updatedSelectedNodes = [];
60
22
  const allIdsFromNode = [node.id];
61
23
  if (nested === null || nested === void 0 ? void 0 : nested.length) {
62
- allIdsFromNode.push(...findAllChildNodeIds(nested));
24
+ allIdsFromNode.push(...(0, findAllChildNodeIds_1.findAllChildNodeIds)(nested));
63
25
  }
64
26
  if (isSelected) {
65
27
  updatedSelectedNodes = selectedNodes.filter(id => !allIdsFromNode.includes(id));
@@ -70,7 +32,7 @@ function lookupTreeForSelectedNodes(_ref) {
70
32
  let parent = parentNode;
71
33
  while (parent) {
72
34
  if ((_a = parent.nested) === null || _a === void 0 ? void 0 : _a.length) {
73
- const parentNestedSelection = checkNestedNodesSelection(parent.nested, updatedSelectedNodes);
35
+ const parentNestedSelection = (0, checkNestedNodesSelection_1.checkNestedNodesSelection)(parent.nested, updatedSelectedNodes);
74
36
  const parentId = parent.id;
75
37
  if (isSelected) {
76
38
  if (!parentNestedSelection.allSelected) {
@@ -0,0 +1,2 @@
1
+ import { ExtendedTreeNodeProps } from '../types';
2
+ export declare const sortTreeItemsByTitle: (items: ExtendedTreeNodeProps[]) => ExtendedTreeNodeProps[];
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.sortTreeItemsByTitle = void 0;
7
+ const extractTreeNodeTitle_1 = require("./extractTreeNodeTitle");
8
+ const sortTreeItemsByTitle = items => items === null || items === void 0 ? void 0 : items.toSorted((itemA, itemB) => {
9
+ const valueA = (0, extractTreeNodeTitle_1.extractTreeNodeTitle)(itemA);
10
+ const valueB = (0, extractTreeNodeTitle_1.extractTreeNodeTitle)(itemB);
11
+ return valueA.localeCompare(valueB);
12
+ });
13
+ exports.sortTreeItemsByTitle = sortTreeItemsByTitle;
@@ -0,0 +1,2 @@
1
+ import { TreeNodeProps } from '../';
2
+ export declare const traverse: <T extends TreeNodeProps>(nodes: T[], callback: (node: T, depth: number) => void) => void;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
+ return mod && mod.__esModule ? mod : {
5
+ "default": mod
6
+ };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", {
9
+ value: true
10
+ });
11
+ exports.traverse = void 0;
12
+ const queue_fifo_1 = __importDefault(require("queue-fifo"));
13
+ const traverse = (nodes, callback) => {
14
+ var _a;
15
+ const queue = new queue_fifo_1.default();
16
+ for (const node of nodes) {
17
+ queue.enqueue({
18
+ node,
19
+ depth: 0
20
+ });
21
+ }
22
+ while (!queue.isEmpty()) {
23
+ const nodeWithDepth = queue.dequeue();
24
+ if (!nodeWithDepth) {
25
+ continue;
26
+ }
27
+ const {
28
+ node,
29
+ depth
30
+ } = nodeWithDepth;
31
+ callback(node, depth);
32
+ for (const child of (_a = node.nested) !== null && _a !== void 0 ? _a : []) {
33
+ queue.enqueue({
34
+ node: child,
35
+ depth: depth + 1
36
+ });
37
+ }
38
+ }
39
+ };
40
+ exports.traverse = traverse;
@@ -100,3 +100,6 @@ export type TreeMultiSelect = Omit<TreeCommonProps, 'selected'> & {
100
100
  onSelect?(selectedKeys: TreeNodeId[], node: TreeNodeProps): void;
101
101
  };
102
102
  export type TreeBaseProps = TreeView | TreeMultiSelect | TreeSingleSelect;
103
+ export type ExtendedTreeNodeProps = TreeNodeProps & {
104
+ getTitle?(): void;
105
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,20 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { collectIds } from '../collectIds';
3
+ import { tree } from './constants';
4
+ describe('collect tree ids', () => {
5
+ it('find all ids', () => {
6
+ const expected = [
7
+ 'customer 1',
8
+ 'customer 2',
9
+ 'same_hierarchy customer',
10
+ 'department 1.1',
11
+ 'department 1.2',
12
+ 'department 2.1',
13
+ 'same_hierarchy department',
14
+ 'project 1.1.1',
15
+ 'project 1.1.2',
16
+ 'same_hierarchy project',
17
+ ];
18
+ expect(collectIds(tree)).toEqual(expected);
19
+ });
20
+ });
@@ -0,0 +1,2 @@
1
+ import { TreeNodeProps } from '../../types';
2
+ export declare const tree: TreeNodeProps[];
@@ -0,0 +1,52 @@
1
+ export const tree = [
2
+ {
3
+ id: 'customer 1',
4
+ title: 'customer 1',
5
+ nested: [
6
+ {
7
+ id: 'department 1.1',
8
+ title: 'department 1.1',
9
+ nested: [
10
+ {
11
+ id: 'project 1.1.1',
12
+ title: 'project 1.1.1',
13
+ },
14
+ {
15
+ id: 'project 1.1.2',
16
+ title: 'project 1.1.2',
17
+ },
18
+ ],
19
+ },
20
+ {
21
+ id: 'department 1.2',
22
+ title: 'department 1.2',
23
+ },
24
+ ],
25
+ },
26
+ {
27
+ id: 'customer 2',
28
+ title: 'customer 2',
29
+ nested: [
30
+ {
31
+ id: 'department 2.1',
32
+ title: 'department 2.1',
33
+ },
34
+ ],
35
+ },
36
+ {
37
+ id: 'same_hierarchy customer',
38
+ title: 'same_hierarchy customer',
39
+ nested: [
40
+ {
41
+ id: 'same_hierarchy department',
42
+ title: 'same_hierarchy department',
43
+ nested: [
44
+ {
45
+ id: 'same_hierarchy project',
46
+ title: 'same_hierarchy project',
47
+ },
48
+ ],
49
+ },
50
+ ],
51
+ },
52
+ ];
@@ -0,0 +1,165 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { getSearchedTreeItems } from '../getSearchedTreeItems';
3
+ import { tree } from './constants';
4
+ describe('search tree items', () => {
5
+ it('find items if 1 project is matched', () => {
6
+ const expected = [
7
+ {
8
+ id: 'customer 1',
9
+ title: 'customer 1',
10
+ nested: [
11
+ {
12
+ id: 'department 1.1',
13
+ title: 'department 1.1',
14
+ nested: [
15
+ {
16
+ id: 'project 1.1.1',
17
+ title: 'project 1.1.1',
18
+ },
19
+ ],
20
+ },
21
+ ],
22
+ },
23
+ ];
24
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'project 1.1.1' } })).toEqual(expected);
25
+ });
26
+ it('find items if 2 projects are matched', () => {
27
+ const expected = [
28
+ {
29
+ id: 'customer 1',
30
+ title: 'customer 1',
31
+ nested: [
32
+ {
33
+ id: 'department 1.1',
34
+ title: 'department 1.1',
35
+ nested: [
36
+ {
37
+ id: 'project 1.1.1',
38
+ title: 'project 1.1.1',
39
+ },
40
+ {
41
+ id: 'project 1.1.2',
42
+ title: 'project 1.1.2',
43
+ },
44
+ ],
45
+ },
46
+ ],
47
+ },
48
+ ];
49
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'project 1.1' } })).toEqual(expected);
50
+ });
51
+ it('find items if 1 department is matched', () => {
52
+ const expected = [
53
+ {
54
+ id: 'customer 1',
55
+ title: 'customer 1',
56
+ nested: [
57
+ {
58
+ id: 'department 1.1',
59
+ title: 'department 1.1',
60
+ },
61
+ ],
62
+ },
63
+ ];
64
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'department 1.1' } })).toEqual(expected);
65
+ });
66
+ it('find items if 2 departments are matched', () => {
67
+ const expected = [
68
+ {
69
+ id: 'customer 1',
70
+ title: 'customer 1',
71
+ nested: [
72
+ {
73
+ id: 'department 1.1',
74
+ title: 'department 1.1',
75
+ },
76
+ {
77
+ id: 'department 1.2',
78
+ title: 'department 1.2',
79
+ },
80
+ ],
81
+ },
82
+ ];
83
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'department 1.' } })).toEqual(expected);
84
+ });
85
+ it('find items if 1 customer is matched', () => {
86
+ const expected = [
87
+ {
88
+ id: 'customer 1',
89
+ title: 'customer 1',
90
+ },
91
+ ];
92
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'customer 1' } })).toEqual(expected);
93
+ });
94
+ it('find items if not one items are matched', () => {
95
+ const expected = [];
96
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'test' } })).toEqual(expected);
97
+ });
98
+ it('find items if empty query', () => {
99
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: '' } })).toEqual(tree);
100
+ });
101
+ it('find items if empty searchOptions', () => {
102
+ expect(getSearchedTreeItems({ tree })).toEqual(tree);
103
+ });
104
+ it('find matches in the same hierarchy', () => {
105
+ const expected = [
106
+ {
107
+ id: 'same_hierarchy customer',
108
+ title: 'same_hierarchy customer',
109
+ nested: [
110
+ {
111
+ id: 'same_hierarchy department',
112
+ title: 'same_hierarchy department',
113
+ nested: [
114
+ {
115
+ id: 'same_hierarchy project',
116
+ title: 'same_hierarchy project',
117
+ },
118
+ ],
119
+ },
120
+ ],
121
+ },
122
+ ];
123
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'same_hierarchy' } })).toEqual(expected);
124
+ });
125
+ it('find all items from customer 2', () => {
126
+ const expected = [
127
+ {
128
+ id: 'customer 2',
129
+ title: 'customer 2',
130
+ nested: [
131
+ {
132
+ id: 'department 2.1',
133
+ title: 'department 2.1',
134
+ },
135
+ ],
136
+ },
137
+ ];
138
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'customer 2', includeChildrenMatchedParent: true } })).toEqual(expected);
139
+ });
140
+ it('find all items from department 1.1', () => {
141
+ const expected = [
142
+ {
143
+ id: 'customer 1',
144
+ title: 'customer 1',
145
+ nested: [
146
+ {
147
+ id: 'department 1.1',
148
+ title: 'department 1.1',
149
+ nested: [
150
+ {
151
+ id: 'project 1.1.1',
152
+ title: 'project 1.1.1',
153
+ },
154
+ {
155
+ id: 'project 1.1.2',
156
+ title: 'project 1.1.2',
157
+ },
158
+ ],
159
+ },
160
+ ],
161
+ },
162
+ ];
163
+ expect(getSearchedTreeItems({ tree, searchOptions: { query: 'department 1.1', includeChildrenMatchedParent: true } })).toEqual(expected);
164
+ });
165
+ });
@@ -0,0 +1,112 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { getSearchedTreeNodeById } from '../getSearchedTreeNodeById';
3
+ import { tree } from './constants';
4
+ describe('get tree items by id', () => {
5
+ it('should return the item with nested when it exists', () => {
6
+ const expected = {
7
+ id: 'customer 1',
8
+ title: 'customer 1',
9
+ nested: [
10
+ {
11
+ id: 'department 1.1',
12
+ title: 'department 1.1',
13
+ nested: [
14
+ {
15
+ id: 'project 1.1.1',
16
+ title: 'project 1.1.1',
17
+ },
18
+ {
19
+ id: 'project 1.1.2',
20
+ title: 'project 1.1.2',
21
+ },
22
+ ],
23
+ },
24
+ {
25
+ id: 'department 1.2',
26
+ title: 'department 1.2',
27
+ },
28
+ ],
29
+ };
30
+ expect(getSearchedTreeNodeById({ tree, searchOptions: { id: 'customer 1' } })).toEqual(expected);
31
+ });
32
+ it('should return the item without nested when it exists', () => {
33
+ const expected = {
34
+ id: 'customer 1',
35
+ title: 'customer 1',
36
+ };
37
+ expect(getSearchedTreeNodeById({ tree, searchOptions: { id: 'customer 1', includeNested: false } })).toEqual(expected);
38
+ });
39
+ it('should return null or undefined when the item does not exist', () => {
40
+ const expected = null;
41
+ expect(getSearchedTreeNodeById({ tree, searchOptions: { id: 'customer none' } })).toEqual(expected);
42
+ });
43
+ });
44
+ describe('get tree items by ids', () => {
45
+ it('should return the item when only 1 item exists with nested', () => {
46
+ const expected = [
47
+ {
48
+ id: 'department 1.1',
49
+ title: 'department 1.1',
50
+ nested: [
51
+ {
52
+ id: 'project 1.1.1',
53
+ title: 'project 1.1.1',
54
+ },
55
+ {
56
+ id: 'project 1.1.2',
57
+ title: 'project 1.1.2',
58
+ },
59
+ ],
60
+ },
61
+ ];
62
+ expect(getSearchedTreeNodeById({
63
+ tree,
64
+ searchOptions: { id: ['department 1.1'] },
65
+ })).toEqual(expected);
66
+ });
67
+ it('should return the item when only 1 item exists without nested', () => {
68
+ const expected = [
69
+ {
70
+ id: 'department 1.1',
71
+ title: 'department 1.1',
72
+ },
73
+ ];
74
+ expect(getSearchedTreeNodeById({
75
+ tree,
76
+ searchOptions: { id: ['department 1.1'], includeNested: false },
77
+ })).toEqual(expected);
78
+ });
79
+ it('should return both items when 2 items exist', () => {
80
+ const expected = [
81
+ {
82
+ id: 'department 1.1',
83
+ title: 'department 1.1',
84
+ nested: [
85
+ {
86
+ id: 'project 1.1.1',
87
+ title: 'project 1.1.1',
88
+ },
89
+ {
90
+ id: 'project 1.1.2',
91
+ title: 'project 1.1.2',
92
+ },
93
+ ],
94
+ },
95
+ {
96
+ id: 'department 1.2',
97
+ title: 'department 1.2',
98
+ },
99
+ ];
100
+ expect(getSearchedTreeNodeById({
101
+ tree,
102
+ searchOptions: { id: ['department 1.1', 'department 1.2'] },
103
+ })).toEqual(expected);
104
+ });
105
+ it('should return an empty array or null when no items exist', () => {
106
+ const expected = [];
107
+ expect(getSearchedTreeNodeById({
108
+ tree,
109
+ searchOptions: { id: ['department 1.1test', 'department 1.2test'] },
110
+ })).toEqual(expected);
111
+ });
112
+ });
@@ -0,0 +1,5 @@
1
+ import { TreeNodeId, TreeNodeProps } from '../types';
2
+ export declare function checkNestedNodesSelection(nodes: TreeNodeProps[], selectedKeys: TreeNodeId[]): {
3
+ someSelected: boolean;
4
+ allSelected: boolean;
5
+ };
@@ -0,0 +1,11 @@
1
+ import { findAllChildNodeIds } from './findAllChildNodeIds';
2
+ export function checkNestedNodesSelection(nodes, selectedKeys) {
3
+ const allIds = findAllChildNodeIds(nodes);
4
+ const selected = allIds.filter(id => selectedKeys.includes(id));
5
+ const someSelected = selected.length > 0;
6
+ const allSelected = someSelected && allIds.length === selected.length;
7
+ return {
8
+ someSelected: !allSelected && someSelected,
9
+ allSelected,
10
+ };
11
+ }
@@ -0,0 +1,2 @@
1
+ import { TreeNodeProps } from '../types';
2
+ export declare const collectIds: (treeNodes: TreeNodeProps[]) => string[];
@@ -0,0 +1,6 @@
1
+ import { traverse } from './traverse';
2
+ export const collectIds = (treeNodes) => {
3
+ const ids = [];
4
+ traverse(treeNodes, node => ids.push(node.id));
5
+ return ids;
6
+ };
@@ -0,0 +1,2 @@
1
+ import { ExtendedTreeNodeProps } from '../types';
2
+ export declare const extractTreeNodeTitle: ({ title, getTitle }: ExtendedTreeNodeProps) => string;
@@ -0,0 +1 @@
1
+ export const extractTreeNodeTitle = ({ title, getTitle }) => { var _a; return typeof title === 'string' ? title : ((_a = getTitle === null || getTitle === void 0 ? void 0 : getTitle()) !== null && _a !== void 0 ? _a : ''); };
@@ -0,0 +1,2 @@
1
+ import { TreeNodeId, TreeNodeProps } from '../types';
2
+ export declare function findAllChildNodeIds(nodes: TreeNodeProps[]): TreeNodeId[];
@@ -0,0 +1,15 @@
1
+ export function findAllChildNodeIds(nodes) {
2
+ var _a;
3
+ const stack = [...nodes];
4
+ const ids = [];
5
+ let node;
6
+ while ((node = stack.pop())) {
7
+ if (!node.disabled) {
8
+ ids.push(node.id);
9
+ if ((_a = node.nested) === null || _a === void 0 ? void 0 : _a.length) {
10
+ stack.push(...node.nested);
11
+ }
12
+ }
13
+ }
14
+ return ids;
15
+ }
@@ -0,0 +1,2 @@
1
+ import { TreeNodeId, TreeNodeProps } from '../types';
2
+ export declare function findAllExpandedChildNodeIds(nodes: TreeNodeProps[], expandedNodes: TreeNodeId[]): string[];
@@ -0,0 +1,13 @@
1
+ export function findAllExpandedChildNodeIds(nodes, expandedNodes) {
2
+ var _a;
3
+ const stack = [...nodes];
4
+ const ids = [];
5
+ let node;
6
+ while ((node = stack.shift())) {
7
+ ids.push(node.id);
8
+ if (((_a = node.nested) === null || _a === void 0 ? void 0 : _a.length) && expandedNodes.includes(node.id)) {
9
+ stack.unshift(...node.nested);
10
+ }
11
+ }
12
+ return ids;
13
+ }
@@ -0,0 +1,10 @@
1
+ import { ExtendedTreeNodeProps } from '../types';
2
+ type SearchParams = {
3
+ tree: ExtendedTreeNodeProps[];
4
+ searchOptions?: Partial<{
5
+ query: string;
6
+ includeChildrenMatchedParent: boolean;
7
+ }>;
8
+ };
9
+ export declare const getSearchedTreeItems: ({ tree, searchOptions }: SearchParams) => ExtendedTreeNodeProps[];
10
+ export {};
@@ -0,0 +1,31 @@
1
+ import { extractTreeNodeTitle } from './extractTreeNodeTitle';
2
+ const isMatchedTreeItem = (search) => {
3
+ const searchLower = search.toLocaleLowerCase();
4
+ return (treeItem) => {
5
+ const currentValue = extractTreeNodeTitle(treeItem);
6
+ return currentValue.toLocaleLowerCase().includes(searchLower);
7
+ };
8
+ };
9
+ export const getSearchedTreeItems = ({ tree, searchOptions }) => {
10
+ if (!(searchOptions === null || searchOptions === void 0 ? void 0 : searchOptions.query))
11
+ return tree;
12
+ const { query = '', includeChildrenMatchedParent } = searchOptions;
13
+ const matchFunc = isMatchedTreeItem(query);
14
+ const searchItems = (treeItems) => treeItems.reduce((acc, item) => {
15
+ const hasMatchingTitle = matchFunc(item);
16
+ const needDeepSearch = !(hasMatchingTitle && includeChildrenMatchedParent);
17
+ const matchedChildren = item.nested && (needDeepSearch ? searchItems(item.nested) : item.nested);
18
+ if (hasMatchingTitle || (matchedChildren === null || matchedChildren === void 0 ? void 0 : matchedChildren.length)) {
19
+ const newItem = Object.assign({}, item);
20
+ if (matchedChildren === null || matchedChildren === void 0 ? void 0 : matchedChildren.length) {
21
+ newItem.nested = matchedChildren;
22
+ }
23
+ else {
24
+ delete newItem.nested;
25
+ }
26
+ acc.push(newItem);
27
+ }
28
+ return acc;
29
+ }, []);
30
+ return searchItems(tree);
31
+ };
@@ -0,0 +1,11 @@
1
+ import { ExtendedTreeNodeProps } from '../types';
2
+ type ReturnSearch<I, T> = I extends string ? T | null : T[];
3
+ type SearchByIdParams<I, T> = {
4
+ tree: T[];
5
+ searchOptions: {
6
+ id: I;
7
+ includeNested?: boolean;
8
+ };
9
+ };
10
+ export declare const getSearchedTreeNodeById: <I extends string | string[], T extends ExtendedTreeNodeProps>({ tree, searchOptions: { id, includeNested }, }: SearchByIdParams<I, T>) => ReturnSearch<I, T>;
11
+ export {};
@@ -0,0 +1,23 @@
1
+ export const getSearchedTreeNodeById = ({ tree, searchOptions: { id, includeNested = true }, }) => {
2
+ const isArrayIds = Array.isArray(id);
3
+ if (!id || (isArrayIds && id.length === 0)) {
4
+ return (isArrayIds ? [] : null);
5
+ }
6
+ const foundItems = [];
7
+ const searchNodes = (nodes) => {
8
+ for (const node of nodes) {
9
+ const matchCondition = isArrayIds ? id.includes(node.id) : node.id === id;
10
+ if (matchCondition) {
11
+ foundItems.push(includeNested ? node : Object.assign(Object.assign({}, node), { nested: undefined }));
12
+ if (!isArrayIds) {
13
+ return;
14
+ }
15
+ }
16
+ if (node.nested) {
17
+ searchNodes(node.nested);
18
+ }
19
+ }
20
+ };
21
+ searchNodes(tree);
22
+ return (isArrayIds ? foundItems : foundItems[0] || null);
23
+ };