react-native-tree-multi-select 1.5.1 → 1.6.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.
- package/lib/commonjs/TreeView.js +22 -17
- package/lib/commonjs/TreeView.js.map +1 -1
- package/lib/commonjs/components/NodeList.js +10 -7
- package/lib/commonjs/components/NodeList.js.map +1 -1
- package/lib/commonjs/constants/tests.constants.js +8 -0
- package/lib/commonjs/constants/tests.constants.js.map +1 -0
- package/lib/commonjs/helpers/expandCollapse.helper.js +15 -10
- package/lib/commonjs/helpers/expandCollapse.helper.js.map +1 -1
- package/lib/commonjs/helpers/selectAll.helper.js +16 -12
- package/lib/commonjs/helpers/selectAll.helper.js.map +1 -1
- package/lib/commonjs/helpers/toggleCheckbox.helper.js +3 -2
- package/lib/commonjs/helpers/toggleCheckbox.helper.js.map +1 -1
- package/lib/commonjs/helpers/treeNode.helper.js +3 -2
- package/lib/commonjs/helpers/treeNode.helper.js.map +1 -1
- package/lib/commonjs/store/treeView.store.js +77 -65
- package/lib/commonjs/store/treeView.store.js.map +1 -1
- package/lib/module/TreeView.js +20 -17
- package/lib/module/TreeView.js.map +1 -1
- package/lib/module/components/NodeList.js +10 -7
- package/lib/module/components/NodeList.js.map +1 -1
- package/lib/module/constants/tests.constants.js +4 -0
- package/lib/module/constants/tests.constants.js.map +1 -0
- package/lib/module/helpers/expandCollapse.helper.js +16 -11
- package/lib/module/helpers/expandCollapse.helper.js.map +1 -1
- package/lib/module/helpers/selectAll.helper.js +17 -13
- package/lib/module/helpers/selectAll.helper.js.map +1 -1
- package/lib/module/helpers/toggleCheckbox.helper.js +4 -3
- package/lib/module/helpers/toggleCheckbox.helper.js.map +1 -1
- package/lib/module/helpers/treeNode.helper.js +4 -3
- package/lib/module/helpers/treeNode.helper.js.map +1 -1
- package/lib/module/store/treeView.store.js +75 -64
- package/lib/module/store/treeView.store.js.map +1 -1
- package/lib/typescript/TreeView.d.ts.map +1 -1
- package/lib/typescript/components/NodeList.d.ts.map +1 -1
- package/lib/typescript/constants/tests.constants.d.ts +2 -0
- package/lib/typescript/constants/tests.constants.d.ts.map +1 -0
- package/lib/typescript/helpers/expandCollapse.helper.d.ts +5 -5
- package/lib/typescript/helpers/expandCollapse.helper.d.ts.map +1 -1
- package/lib/typescript/helpers/selectAll.helper.d.ts +4 -4
- package/lib/typescript/helpers/selectAll.helper.d.ts.map +1 -1
- package/lib/typescript/helpers/toggleCheckbox.helper.d.ts +1 -1
- package/lib/typescript/helpers/toggleCheckbox.helper.d.ts.map +1 -1
- package/lib/typescript/helpers/treeNode.helper.d.ts +1 -1
- package/lib/typescript/helpers/treeNode.helper.d.ts.map +1 -1
- package/lib/typescript/store/treeView.store.d.ts +3 -1
- package/lib/typescript/store/treeView.store.d.ts.map +1 -1
- package/lib/typescript/types/treeView.types.d.ts +3 -1
- package/lib/typescript/types/treeView.types.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/TreeView.tsx +22 -17
- package/src/components/NodeList.tsx +13 -6
- package/src/constants/tests.constants.ts +1 -0
- package/src/helpers/expandCollapse.helper.ts +16 -11
- package/src/helpers/selectAll.helper.ts +17 -14
- package/src/helpers/toggleCheckbox.helper.ts +8 -3
- package/src/helpers/treeNode.helper.ts +4 -3
- package/src/store/treeView.store.ts +61 -46
- package/src/types/treeView.types.ts +3 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getTreeViewStore } from "../store/treeView.store";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Toggle the expanded state of a tree node by its ID.
|
|
@@ -8,12 +8,13 @@ import { useTreeViewStore } from "../store/treeView.store";
|
|
|
8
8
|
*
|
|
9
9
|
* @param id - The ID of the tree node to toggle.
|
|
10
10
|
*/
|
|
11
|
-
export function handleToggleExpand(id: string) {
|
|
11
|
+
export function handleToggleExpand(storeId: string, id: string) {
|
|
12
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
12
13
|
const {
|
|
13
14
|
expanded,
|
|
14
15
|
updateExpanded,
|
|
15
16
|
nodeMap
|
|
16
|
-
} =
|
|
17
|
+
} = treeViewStore.getState();
|
|
17
18
|
|
|
18
19
|
// Create a new Set based on the current expanded state
|
|
19
20
|
const newExpanded = new Set(expanded);
|
|
@@ -48,8 +49,9 @@ export function handleToggleExpand(id: string) {
|
|
|
48
49
|
/**
|
|
49
50
|
* Expand all nodes in the tree.
|
|
50
51
|
*/
|
|
51
|
-
export function expandAll() {
|
|
52
|
-
const
|
|
52
|
+
export function expandAll(storeId: string) {
|
|
53
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
54
|
+
const { nodeMap, updateExpanded } = treeViewStore.getState();
|
|
53
55
|
// Create a new Set containing the IDs of all nodes
|
|
54
56
|
const newExpanded = new Set(nodeMap.keys());
|
|
55
57
|
updateExpanded(newExpanded);
|
|
@@ -58,8 +60,9 @@ export function expandAll() {
|
|
|
58
60
|
/**
|
|
59
61
|
* Collapse all nodes in the tree.
|
|
60
62
|
*/
|
|
61
|
-
export function collapseAll() {
|
|
62
|
-
const
|
|
63
|
+
export function collapseAll(storeId: string) {
|
|
64
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
65
|
+
const { updateExpanded } = treeViewStore.getState();
|
|
63
66
|
// Clear the expanded state
|
|
64
67
|
updateExpanded(new Set<string>());
|
|
65
68
|
}
|
|
@@ -69,8 +72,9 @@ export function collapseAll() {
|
|
|
69
72
|
* its ancestors up to the root.
|
|
70
73
|
* @param ids - Ids of nodes to expand.
|
|
71
74
|
*/
|
|
72
|
-
export function expandNodes(ids: string[]) {
|
|
73
|
-
const
|
|
75
|
+
export function expandNodes(storeId: string, ids: string[]) {
|
|
76
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
77
|
+
const { expanded, updateExpanded, childToParentMap } = treeViewStore.getState();
|
|
74
78
|
const newExpanded = new Set(expanded);
|
|
75
79
|
const processedIds = new Set<string>();
|
|
76
80
|
|
|
@@ -91,8 +95,9 @@ export function expandNodes(ids: string[]) {
|
|
|
91
95
|
* its descendants.
|
|
92
96
|
* @param ids - Ids of nodes to collapse.
|
|
93
97
|
*/
|
|
94
|
-
export function collapseNodes(ids: string[]) {
|
|
95
|
-
const
|
|
98
|
+
export function collapseNodes(storeId: string, ids: string[]) {
|
|
99
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
100
|
+
const { expanded, updateExpanded, nodeMap } = treeViewStore.getState();
|
|
96
101
|
const newExpanded = new Set(expanded);
|
|
97
102
|
|
|
98
103
|
// Use an iterative approach to remove all descendants from the expanded set
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TreeNode } from "../types/treeView.types";
|
|
2
|
-
import {
|
|
2
|
+
import { getTreeViewStore } from "../store/treeView.store";
|
|
3
3
|
import { toggleCheckboxes } from "./toggleCheckbox.helper";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,16 +7,16 @@ import { toggleCheckboxes } from "./toggleCheckbox.helper";
|
|
|
7
7
|
*
|
|
8
8
|
* If there is no search text, then it selects all nodes; otherwise, it selects all visible nodes.
|
|
9
9
|
*/
|
|
10
|
-
export function selectAllFiltered() {
|
|
11
|
-
const
|
|
12
|
-
|
|
10
|
+
export function selectAllFiltered(storeId: string) {
|
|
11
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
12
|
+
const { searchText, innerMostChildrenIds } = treeViewStore.getState();
|
|
13
13
|
|
|
14
14
|
// If there's no search text, select all nodes
|
|
15
15
|
if (!searchText) {
|
|
16
|
-
selectAll();
|
|
16
|
+
selectAll(storeId);
|
|
17
17
|
} else {
|
|
18
18
|
// If there's search text, only select the visible nodes
|
|
19
|
-
toggleCheckboxes(innerMostChildrenIds, true);
|
|
19
|
+
toggleCheckboxes(storeId, innerMostChildrenIds, true);
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
22
|
|
|
@@ -25,15 +25,16 @@ export function selectAllFiltered() {
|
|
|
25
25
|
*
|
|
26
26
|
* If there is no search text, then it unselects all nodes; otherwise, it unselects all visible nodes.
|
|
27
27
|
*/
|
|
28
|
-
export function unselectAllFiltered() {
|
|
29
|
-
const
|
|
28
|
+
export function unselectAllFiltered(storeId: string) {
|
|
29
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
30
|
+
const { searchText, innerMostChildrenIds } = treeViewStore.getState();
|
|
30
31
|
|
|
31
32
|
// If there's no search text, unselect all nodes
|
|
32
33
|
if (!searchText) {
|
|
33
|
-
unselectAll();
|
|
34
|
+
unselectAll(storeId);
|
|
34
35
|
} else {
|
|
35
36
|
// If there's search text, only unselect the visible nodes
|
|
36
|
-
toggleCheckboxes(innerMostChildrenIds, false);
|
|
37
|
+
toggleCheckboxes(storeId, innerMostChildrenIds, false);
|
|
37
38
|
}
|
|
38
39
|
};
|
|
39
40
|
|
|
@@ -42,12 +43,13 @@ export function unselectAllFiltered() {
|
|
|
42
43
|
*
|
|
43
44
|
* This function selects all nodes by adding all node ids to the checked set and clearing the indeterminate set.
|
|
44
45
|
*/
|
|
45
|
-
export function selectAll() {
|
|
46
|
+
export function selectAll(storeId: string) {
|
|
47
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
46
48
|
const {
|
|
47
49
|
nodeMap,
|
|
48
50
|
updateChecked,
|
|
49
51
|
updateIndeterminate
|
|
50
|
-
} =
|
|
52
|
+
} = treeViewStore.getState();
|
|
51
53
|
|
|
52
54
|
// Create a new set containing the ids of all nodes
|
|
53
55
|
const newChecked = new Set(nodeMap.keys());
|
|
@@ -62,8 +64,9 @@ export function selectAll() {
|
|
|
62
64
|
*
|
|
63
65
|
* This function unselects all nodes by clearing both the checked and indeterminate sets.
|
|
64
66
|
*/
|
|
65
|
-
export function unselectAll() {
|
|
66
|
-
const
|
|
67
|
+
export function unselectAll(storeId: string) {
|
|
68
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
69
|
+
const { updateChecked, updateIndeterminate } = treeViewStore.getState();
|
|
67
70
|
// Update the state to mark all nodes as unchecked
|
|
68
71
|
|
|
69
72
|
updateChecked(new Set());
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getTreeViewStore } from "../store/treeView.store";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Function to toggle checkbox state for a tree structure.
|
|
@@ -7,7 +7,12 @@ import { useTreeViewStore } from "../store/treeView.store";
|
|
|
7
7
|
* @param {boolean} [forceCheck] - Optional. If provided, will force the check state of the nodes to be this value.
|
|
8
8
|
* If not provided, the check state will be toggled based on the current state.
|
|
9
9
|
*/
|
|
10
|
-
export function toggleCheckboxes(
|
|
10
|
+
export function toggleCheckboxes(
|
|
11
|
+
storeId: string,
|
|
12
|
+
ids: string[],
|
|
13
|
+
forceCheck?: boolean
|
|
14
|
+
) {
|
|
15
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
11
16
|
const {
|
|
12
17
|
checked,
|
|
13
18
|
updateChecked,
|
|
@@ -18,7 +23,7 @@ export function toggleCheckboxes(ids: string[], forceCheck?: boolean) {
|
|
|
18
23
|
nodeMap,
|
|
19
24
|
childToParentMap,
|
|
20
25
|
selectionPropagation
|
|
21
|
-
} =
|
|
26
|
+
} = treeViewStore.getState();
|
|
22
27
|
|
|
23
28
|
const { toChildren, toParents } = selectionPropagation;
|
|
24
29
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { TreeNode } from "../types/treeView.types";
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
getTreeViewStore,
|
|
4
4
|
} from "../store/treeView.store";
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -12,11 +12,12 @@ import {
|
|
|
12
12
|
* @param initialData - An array of TreeNode objects that represent the initial tree structure.
|
|
13
13
|
* @param preselectedIds - An optional array of TreeNode IDs that should be preselected.
|
|
14
14
|
*/
|
|
15
|
-
export function initializeNodeMaps(initialData: TreeNode[]) {
|
|
15
|
+
export function initializeNodeMaps(storeId: string, initialData: TreeNode[]) {
|
|
16
|
+
const treeViewStore = getTreeViewStore(storeId);
|
|
16
17
|
const {
|
|
17
18
|
updateNodeMap,
|
|
18
19
|
updateChildToParentMap
|
|
19
|
-
} =
|
|
20
|
+
} = treeViewStore.getState();
|
|
20
21
|
|
|
21
22
|
const tempNodeMap: Map<string, TreeNode> = new Map();;
|
|
22
23
|
const tempChildToParentMap: Map<string, string> = new Map();;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { SelectionPropagation, TreeNode } from "src/types/treeView.types";
|
|
2
|
-
|
|
3
|
-
import { create } from 'zustand';
|
|
2
|
+
import { create, StoreApi, UseBoundStore } from 'zustand';
|
|
4
3
|
|
|
5
4
|
export type TreeViewState = {
|
|
6
5
|
// Store ids of checked tree nodes
|
|
@@ -48,58 +47,74 @@ export type TreeViewState = {
|
|
|
48
47
|
cleanUpTreeViewStore: () => void;
|
|
49
48
|
};
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
updateChecked: (checked: Set<string>) => set({ checked }),
|
|
54
|
-
|
|
55
|
-
indeterminate: new Set(),
|
|
56
|
-
updateIndeterminate: (indeterminate: Set<string>) => set({ indeterminate }),
|
|
57
|
-
|
|
58
|
-
expanded: new Set<string>(),
|
|
59
|
-
updateExpanded: (expanded: Set<string>) => set({ expanded }),
|
|
60
|
-
|
|
61
|
-
initialTreeViewData: [],
|
|
62
|
-
updateInitialTreeViewData: (initialTreeViewData: TreeNode[]) => set({
|
|
63
|
-
initialTreeViewData
|
|
64
|
-
}),
|
|
65
|
-
|
|
66
|
-
nodeMap: new Map<string, TreeNode>(),
|
|
67
|
-
updateNodeMap: (nodeMap: Map<string, TreeNode>) => set({ nodeMap }),
|
|
68
|
-
|
|
69
|
-
childToParentMap: new Map<string, string>(),
|
|
70
|
-
updateChildToParentMap: (childToParentMap: Map<string, string>) => set({ childToParentMap }),
|
|
71
|
-
|
|
72
|
-
searchText: "",
|
|
73
|
-
updateSearchText: (searchText: string) => set({ searchText }),
|
|
74
|
-
|
|
75
|
-
searchKeys: [""],
|
|
76
|
-
updateSearchKeys: (searchKeys: string[]) => set({ searchKeys }),
|
|
77
|
-
|
|
78
|
-
innerMostChildrenIds: [],
|
|
79
|
-
updateInnerMostChildrenIds: (innerMostChildrenIds: string[]) => set({ innerMostChildrenIds }),
|
|
80
|
-
|
|
81
|
-
selectionPropagation: { toChildren: true, toParents: true },
|
|
82
|
-
setSelectionPropagation: (selectionPropagation) => set(
|
|
83
|
-
{
|
|
84
|
-
selectionPropagation: {
|
|
85
|
-
// Default selection propagation for parent and children to true if not mentioned
|
|
86
|
-
toChildren: selectionPropagation.toChildren ?? true,
|
|
87
|
-
toParents: selectionPropagation.toParents ?? true
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
),
|
|
50
|
+
// Map to store individual tree view stores by id
|
|
51
|
+
const treeViewStores = new Map<string, UseBoundStore<StoreApi<TreeViewState>>>();
|
|
91
52
|
|
|
92
|
-
|
|
93
|
-
|
|
53
|
+
export function getTreeViewStore(id: string): UseBoundStore<StoreApi<TreeViewState>> {
|
|
54
|
+
if (!treeViewStores.has(id)) {
|
|
55
|
+
const store = create<TreeViewState>((set) => ({
|
|
94
56
|
checked: new Set(),
|
|
57
|
+
updateChecked: (checked: Set<string>) => set({ checked }),
|
|
58
|
+
|
|
95
59
|
indeterminate: new Set(),
|
|
60
|
+
updateIndeterminate: (indeterminate: Set<string>) => set({ indeterminate }),
|
|
61
|
+
|
|
96
62
|
expanded: new Set<string>(),
|
|
63
|
+
updateExpanded: (expanded: Set<string>) => set({ expanded }),
|
|
64
|
+
|
|
97
65
|
initialTreeViewData: [],
|
|
66
|
+
updateInitialTreeViewData: (initialTreeViewData: TreeNode[]) => set({
|
|
67
|
+
initialTreeViewData
|
|
68
|
+
}),
|
|
69
|
+
|
|
98
70
|
nodeMap: new Map<string, TreeNode>(),
|
|
71
|
+
updateNodeMap: (nodeMap: Map<string, TreeNode>) => set({ nodeMap }),
|
|
72
|
+
|
|
99
73
|
childToParentMap: new Map<string, string>(),
|
|
74
|
+
updateChildToParentMap: (childToParentMap: Map<string, string>) => set({
|
|
75
|
+
childToParentMap
|
|
76
|
+
}),
|
|
77
|
+
|
|
100
78
|
searchText: "",
|
|
79
|
+
updateSearchText: (searchText: string) => set({ searchText }),
|
|
80
|
+
|
|
101
81
|
searchKeys: [""],
|
|
82
|
+
updateSearchKeys: (searchKeys: string[]) => set({ searchKeys }),
|
|
83
|
+
|
|
102
84
|
innerMostChildrenIds: [],
|
|
85
|
+
updateInnerMostChildrenIds: (innerMostChildrenIds: string[]) => set({
|
|
86
|
+
innerMostChildrenIds
|
|
87
|
+
}),
|
|
88
|
+
|
|
103
89
|
selectionPropagation: { toChildren: true, toParents: true },
|
|
104
|
-
|
|
105
|
-
|
|
90
|
+
setSelectionPropagation: (selectionPropagation) => set({
|
|
91
|
+
selectionPropagation: {
|
|
92
|
+
// Default selection propagation for parent and children to true if not specified
|
|
93
|
+
toChildren: selectionPropagation.toChildren ?? true,
|
|
94
|
+
toParents: selectionPropagation.toParents ?? true
|
|
95
|
+
}
|
|
96
|
+
}),
|
|
97
|
+
|
|
98
|
+
cleanUpTreeViewStore: () =>
|
|
99
|
+
set({
|
|
100
|
+
checked: new Set(),
|
|
101
|
+
indeterminate: new Set(),
|
|
102
|
+
expanded: new Set<string>(),
|
|
103
|
+
initialTreeViewData: [],
|
|
104
|
+
nodeMap: new Map<string, TreeNode>(),
|
|
105
|
+
childToParentMap: new Map<string, string>(),
|
|
106
|
+
searchText: "",
|
|
107
|
+
searchKeys: [""],
|
|
108
|
+
innerMostChildrenIds: [],
|
|
109
|
+
selectionPropagation: { toChildren: true, toParents: true },
|
|
110
|
+
}),
|
|
111
|
+
}));
|
|
112
|
+
|
|
113
|
+
treeViewStores.set(id, store);
|
|
114
|
+
}
|
|
115
|
+
return treeViewStores.get(id)!;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function useTreeViewStore(id: string) {
|
|
119
|
+
return getTreeViewStore(id);
|
|
120
|
+
}
|
|
@@ -59,13 +59,15 @@ export interface TreeItemCustomizations {
|
|
|
59
59
|
export interface NodeProps extends TreeItemCustomizations {
|
|
60
60
|
node: __FlattenedTreeNode__;
|
|
61
61
|
level: number;
|
|
62
|
+
storeId: string;
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
export interface NodeListProps extends TreeItemCustomizations {
|
|
65
66
|
treeFlashListProps?: TreeFlatListProps;
|
|
67
|
+
storeId: string;
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
export interface TreeViewProps extends NodeListProps {
|
|
70
|
+
export interface TreeViewProps extends Omit<NodeListProps, "storeId"> {
|
|
69
71
|
data: TreeNode[];
|
|
70
72
|
|
|
71
73
|
onCheck?: (checkedIds: string[], indeterminateIds: string[]) => void;
|