react-native-tree-multi-select 1.1.2 → 1.2.1

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 (62) hide show
  1. package/README.md +12 -3
  2. package/lib/commonjs/TreeView.js +14 -5
  3. package/lib/commonjs/TreeView.js.map +1 -1
  4. package/lib/commonjs/components/CheckboxView.js.map +1 -1
  5. package/lib/commonjs/components/CustomExpandCollapseIcon.js.map +1 -1
  6. package/lib/commonjs/components/NodeList.js +3 -4
  7. package/lib/commonjs/components/NodeList.js.map +1 -1
  8. package/lib/commonjs/constants/treeView.constants.js.map +1 -1
  9. package/lib/commonjs/helpers/expandCollapse.helper.js +88 -21
  10. package/lib/commonjs/helpers/expandCollapse.helper.js.map +1 -1
  11. package/lib/commonjs/helpers/flattenTree.helper.js +1 -2
  12. package/lib/commonjs/helpers/flattenTree.helper.js.map +1 -1
  13. package/lib/commonjs/helpers/index.js.map +1 -1
  14. package/lib/commonjs/helpers/initNodeMap.helper.js +1 -2
  15. package/lib/commonjs/helpers/initNodeMap.helper.js.map +1 -1
  16. package/lib/commonjs/helpers/search.helper.js.map +1 -1
  17. package/lib/commonjs/helpers/selectAll.helper.js.map +1 -1
  18. package/lib/commonjs/helpers/toggleCheckbox.helper.js +0 -1
  19. package/lib/commonjs/helpers/toggleCheckbox.helper.js.map +1 -1
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/commonjs/jest.setup.js.map +1 -1
  22. package/lib/commonjs/store/treeView.store.js.map +1 -1
  23. package/lib/commonjs/types/treeView.types.js.map +1 -1
  24. package/lib/commonjs/utils/usePreviousState.js +20 -0
  25. package/lib/commonjs/utils/usePreviousState.js.map +1 -0
  26. package/lib/module/TreeView.js +15 -6
  27. package/lib/module/TreeView.js.map +1 -1
  28. package/lib/module/components/CheckboxView.js.map +1 -1
  29. package/lib/module/components/CustomExpandCollapseIcon.js.map +1 -1
  30. package/lib/module/components/NodeList.js +3 -4
  31. package/lib/module/components/NodeList.js.map +1 -1
  32. package/lib/module/constants/treeView.constants.js.map +1 -1
  33. package/lib/module/helpers/expandCollapse.helper.js +86 -21
  34. package/lib/module/helpers/expandCollapse.helper.js.map +1 -1
  35. package/lib/module/helpers/flattenTree.helper.js +1 -2
  36. package/lib/module/helpers/flattenTree.helper.js.map +1 -1
  37. package/lib/module/helpers/index.js.map +1 -1
  38. package/lib/module/helpers/initNodeMap.helper.js +1 -2
  39. package/lib/module/helpers/initNodeMap.helper.js.map +1 -1
  40. package/lib/module/helpers/search.helper.js.map +1 -1
  41. package/lib/module/helpers/selectAll.helper.js.map +1 -1
  42. package/lib/module/helpers/toggleCheckbox.helper.js +0 -1
  43. package/lib/module/helpers/toggleCheckbox.helper.js.map +1 -1
  44. package/lib/module/index.js.map +1 -1
  45. package/lib/module/jest.setup.js.map +1 -1
  46. package/lib/module/store/treeView.store.js.map +1 -1
  47. package/lib/module/types/treeView.types.js.map +1 -1
  48. package/lib/module/utils/usePreviousState.js +15 -0
  49. package/lib/module/utils/usePreviousState.js.map +1 -0
  50. package/lib/typescript/TreeView.d.ts.map +1 -1
  51. package/lib/typescript/helpers/expandCollapse.helper.d.ts +12 -0
  52. package/lib/typescript/helpers/expandCollapse.helper.d.ts.map +1 -1
  53. package/lib/typescript/types/treeView.types.d.ts +3 -0
  54. package/lib/typescript/types/treeView.types.d.ts.map +1 -1
  55. package/lib/typescript/utils/usePreviousState.d.ts +7 -0
  56. package/lib/typescript/utils/usePreviousState.d.ts.map +1 -0
  57. package/package.json +2 -2
  58. package/src/TreeView.tsx +25 -4
  59. package/src/helpers/expandCollapse.helper.ts +82 -21
  60. package/src/helpers/toggleCheckbox.helper.ts +1 -1
  61. package/src/types/treeView.types.ts +5 -0
  62. package/src/utils/usePreviousState.ts +16 -0
package/src/TreeView.tsx CHANGED
@@ -14,9 +14,12 @@ import {
14
14
  initializeNodeMaps,
15
15
  expandAll,
16
16
  collapseAll,
17
- toggleCheckboxes
17
+ toggleCheckboxes,
18
+ expandNodes,
19
+ collapseNodes
18
20
  } from './helpers';
19
21
  import { useTreeViewStore } from './store/treeView.store';
22
+ import usePreviousState from './utils/usePreviousState';
20
23
 
21
24
  const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(
22
25
  (props, ref) => {
@@ -28,6 +31,8 @@ const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(
28
31
 
29
32
  preselectedIds = [],
30
33
 
34
+ preExpandedIds = [],
35
+
31
36
  treeFlashListProps,
32
37
  checkBoxViewStyleProps,
33
38
  indentationMultiplier,
@@ -66,9 +71,14 @@ const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(
66
71
  expandAll,
67
72
  collapseAll,
68
73
 
74
+ expandNodes,
75
+ collapseNodes,
76
+
69
77
  setSearchText
70
78
  }));
71
79
 
80
+ const prevSearchText = usePreviousState(searchText);
81
+
72
82
  function setSearchText(text: string, keys: string[] = ["name"]) {
73
83
  updateSearchText(text);
74
84
  updateSearchKeys(keys);
@@ -79,8 +89,11 @@ const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(
79
89
 
80
90
  initializeNodeMaps(data);
81
91
 
82
- // Check any preselected nodes
92
+ // Check any pre-selected nodes
83
93
  toggleCheckboxes(preselectedIds, true);
94
+
95
+ // Expand pre-expanded nodes
96
+ expandNodes(preExpandedIds);
84
97
  // eslint-disable-next-line react-hooks/exhaustive-deps
85
98
  }, []);
86
99
 
@@ -108,12 +121,20 @@ const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(
108
121
  )));
109
122
  });
110
123
  }
111
- else {
124
+ else if (prevSearchText && prevSearchText !== "") {
125
+ /* Collapse all nodes only if previous search query was non-empty: this is
126
+ done to prevent node collapse on first render if preExpandedIds is provided */
112
127
  InteractionManager.runAfterInteractions(() => {
113
128
  updateExpanded(new Set());
114
129
  });
115
130
  }
116
- }, [getIds, initialTreeViewData, searchText, updateExpanded]);
131
+ }, [
132
+ getIds,
133
+ initialTreeViewData,
134
+ prevSearchText,
135
+ searchText,
136
+ updateExpanded
137
+ ]);
117
138
 
118
139
  React.useEffect(() => {
119
140
  return () => {
@@ -33,28 +33,8 @@ export function handleToggleExpand(id: string) {
33
33
  }
34
34
  }
35
35
 
36
- /**
37
- * Finds a node in the tree by its ID.
38
- *
39
- * @param nodes - The array of tree nodes to search through.
40
- * @returns The found tree node, or undefined if not found.
41
- */
42
- function findNode(nodes: TreeNode[]): TreeNode | undefined {
43
- for (let node of nodes) {
44
- if (node.id === id) {
45
- return node;
46
- } else if (node.children) {
47
- const found = findNode(node.children);
48
- if (found) {
49
- return found;
50
- }
51
- }
52
- }
53
- return undefined;
54
- }
55
-
56
36
  // Find the node to expand or collapse
57
- const node = findNode(initialTreeViewData);
37
+ const node = findNode(initialTreeViewData, id);
58
38
 
59
39
  if (expanded.has(id)) {
60
40
  // If the node is currently expanded, collapse it and its descendants
@@ -97,3 +77,84 @@ export function collapseAll() {
97
77
  const newExpanded = new Set<string>();
98
78
  updateExpanded(newExpanded);
99
79
  };
80
+
81
+ /**
82
+ * Expand tree nodes of given ids. If the id is of a child, it also expands
83
+ * the parent which it belongs to.
84
+ * @param ids Ids of nodes to expand.
85
+ */
86
+ export function expandNodes(ids: string[]) {
87
+ const { expanded, updateExpanded, childToParentMap } = useTreeViewStore.getState();
88
+ const newExpanded = new Set(expanded);
89
+ const processedParents = new Set(); // To track already processed parents
90
+
91
+ ids.forEach(id => {
92
+ newExpanded.add(id); // Start by adding the node ID to the set
93
+ let currentId = id;
94
+
95
+ while (currentId && childToParentMap.has(currentId) && !processedParents.has(currentId)) {
96
+ const parentId = childToParentMap.get(currentId);
97
+ if (parentId) {
98
+ if (!newExpanded.has(parentId)) {
99
+ newExpanded.add(parentId); // Add the parent ID only if not already processed
100
+ processedParents.add(parentId);
101
+ }
102
+ currentId = parentId; // Move up to the next parent
103
+ } else {
104
+ break; // Break the loop if there's no further parent
105
+ }
106
+ }
107
+ });
108
+
109
+ updateExpanded(newExpanded);
110
+ }
111
+
112
+ /**
113
+ * Collapse tree nodes of given ids. If the id is of a parent, it also collapses
114
+ * the children inside it.
115
+ * @param ids Ids of nodes to collapse.
116
+ */
117
+ export function collapseNodes(ids: string[]) {
118
+ const { expanded, updateExpanded, nodeMap } = useTreeViewStore.getState();
119
+ const newExpanded = new Set(expanded);
120
+
121
+ // Function to recursively remove child nodes from the expanded set
122
+ const deleteChildrenFromExpanded = (nodeId: string, visited = new Set()) => {
123
+ if (visited.has(nodeId)) return; // Prevent redundant processing
124
+ visited.add(nodeId);
125
+
126
+ const node = nodeMap.get(nodeId);
127
+ node?.children?.forEach(child => {
128
+ newExpanded.delete(child.id);
129
+ deleteChildrenFromExpanded(child.id, visited);
130
+ });
131
+ };
132
+
133
+ ids.forEach(id => {
134
+ // Remove the node ID from the set and all its children
135
+ newExpanded.delete(id);
136
+ deleteChildrenFromExpanded(id);
137
+ });
138
+
139
+ updateExpanded(newExpanded);
140
+ }
141
+
142
+ /**
143
+ * Finds a node in the tree by its ID.
144
+ *
145
+ * @param nodes - The array of tree nodes to search through.
146
+ * @returns The found tree node, or undefined if not found.
147
+ */
148
+ function findNode(nodes: TreeNode[], parentId: string): TreeNode | undefined {
149
+ for (let node of nodes) {
150
+ if (node.id === parentId) {
151
+ return node;
152
+ } else if (node.children) {
153
+ const found = findNode(node.children, parentId);
154
+ if (found) {
155
+ return found;
156
+ }
157
+ }
158
+ }
159
+ return undefined;
160
+ }
@@ -163,4 +163,4 @@ export function toggleCheckboxes(ids: string[], forceCheck?: boolean) {
163
163
  // Update the state object with the new checked and indeterminate sets.
164
164
  updateChecked(tempChecked);
165
165
  updateIndeterminate(tempIndeterminate);
166
- };
166
+ }
@@ -72,6 +72,8 @@ export interface TreeViewProps extends NodeListProps {
72
72
  onExpand?: (expandedIds: string[]) => void;
73
73
 
74
74
  preselectedIds?: string[];
75
+
76
+ preExpandedIds?: string[];
75
77
  }
76
78
 
77
79
  type CheckboxProps = Omit<RNPaperCheckboxAndroidProps, "onPress" | "status">;
@@ -107,5 +109,8 @@ export interface TreeViewRef {
107
109
  expandAll: () => void;
108
110
  collapseAll: () => void;
109
111
 
112
+ expandNodes: (ids: string[]) => void;
113
+ collapseNodes: (ids: string[]) => void;
114
+
110
115
  setSearchText: (searchText: string, searchKeys?: string[]) => void;
111
116
  }
@@ -0,0 +1,16 @@
1
+ import { useEffect, useRef } from "react";
2
+
3
+ /**
4
+ * Get previous value of a state
5
+ * @param value state
6
+ * @returns previous value of @param value after it's updated
7
+ */
8
+ export default function usePreviousState<T>(value: T) {
9
+ const ref = useRef<T>();
10
+
11
+ useEffect(() => {
12
+ ref.current = value;
13
+ }, [value]);
14
+
15
+ return ref.current;
16
+ }