Skip to content

Commit bae2b43

Browse files
refactor: add screen to demonstrate a tree of nodes with numeric IDs
1 parent fd4f4a8 commit bae2b43

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

example/src/App.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from './screens/SelectionPropagationScreens';
1818
import packageJson from '../../package.json';
1919
import { TwoTreeViewsScreen } from "./screens/TwoTreeViewsScreen";
20+
import CustomNodeID from './screens/CustomNodeIDScreen';
2021

2122
const data: ShowcaseExampleScreenSectionType[] = [
2223
{
@@ -57,6 +58,11 @@ const data: ShowcaseExampleScreenSectionType[] = [
5758
slug: 'custom-row-item',
5859
getScreen: () => CustomNodeRowViewScreen,
5960
},
61+
{
62+
name: 'Custom Node ID',
63+
slug: 'custom-node-id',
64+
getScreen: () => CustomNodeID
65+
}
6066
],
6167
},
6268
{
@@ -104,4 +110,4 @@ export default function App() {
104110
data={data}
105111
/>
106112
);
107-
}
113+
}
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { debounce } from "lodash";
2+
import React, { useEffect, useRef } from "react";
3+
import { SafeAreaView, View, Button } from "react-native";
4+
import { SelectionPropagation, TreeViewRef, TreeView } from "react-native-tree-multi-select";
5+
import SearchInput from "../components/SearchInput";
6+
import { generateTreeList, TreeNode } from "../utils/sampleDataGenerator";
7+
import { styles } from "./screens.styles";
8+
import { CustomNodeRowView } from "../components/CustomNodeRowView";
9+
10+
interface Props {
11+
selectionPropagation?: SelectionPropagation;
12+
}
13+
14+
const customMapper: (parentName?: string) => (it: TreeNode<number>, idx: number) => TreeNode<number> = (parentStr?: string) => (it: TreeNode<number>, idx: number) => {
15+
const name = `${parentStr ? `${parentStr}.` : ''}${idx + 1}`;
16+
return {
17+
...it,
18+
name,
19+
children: it.children?.map(customMapper(name)) ?? []
20+
} as TreeNode<number>
21+
}
22+
23+
export default function CustomNodeID(props: Props) {
24+
const { selectionPropagation } = props;
25+
26+
const idRef = useRef<number>(1);
27+
28+
useEffect(() => {
29+
return () => {
30+
idRef.current = 1
31+
};
32+
}, [])
33+
34+
const sampleData = React.useMemo(() => generateTreeList<number>(30, 4, 5, (_prev, _idx) => idRef.current++, 1).map(customMapper()), []);
35+
console.log(sampleData);
36+
const treeViewRef = React.useRef<TreeViewRef<number> | null>(null);
37+
38+
// eslint-disable-next-line react-hooks/exhaustive-deps
39+
const debouncedSetSearchText = React.useCallback(
40+
debounce((text) => treeViewRef.current?.setSearchText(text), 375, {
41+
leading: true,
42+
trailing: true,
43+
maxWait: 750
44+
}),
45+
[]
46+
);
47+
48+
const handleSelectionChange = (
49+
_checkedIds: number[],
50+
_indeterminateIds: number[]
51+
) => {
52+
// NOTE: Handle _checkedIds and _indeterminateIds here
53+
};
54+
const handleExpanded = (_expandedIds: number[]) => {
55+
// NOTE: Handle _expandedIds here
56+
};
57+
58+
// Expand collapse calls using ref
59+
const expandAllPress = () => treeViewRef.current?.expandAll?.();
60+
const collapseAllPress = () => treeViewRef.current?.collapseAll?.();
61+
62+
// Multi-select calls using ref
63+
const onSelectAllPress = () => treeViewRef.current?.selectAll?.();
64+
const onUnselectAllPress = () => treeViewRef.current?.unselectAll?.();
65+
const onSelectAllFilteredPress = () => treeViewRef.current?.selectAllFiltered?.();
66+
const onUnselectAllFilteredPress = () => treeViewRef.current?.unselectAllFiltered?.();
67+
68+
return (
69+
<SafeAreaView
70+
style={styles.mainView}>
71+
<SearchInput onChange={debouncedSetSearchText} />
72+
<View
73+
style={styles.selectionButtonRow}>
74+
<Button
75+
title='Select All'
76+
onPress={onSelectAllPress} />
77+
<Button
78+
title='Unselect All'
79+
onPress={onUnselectAllPress} />
80+
</View>
81+
<View
82+
style={styles.selectionButtonRow}>
83+
<Button
84+
title='Select Filtered'
85+
onPress={onSelectAllFilteredPress} />
86+
<Button
87+
title='Unselect Filtered'
88+
onPress={onUnselectAllFilteredPress} />
89+
</View>
90+
91+
<View
92+
style={[styles.selectionButtonRow, styles.selectionButtonBottom]}>
93+
<Button
94+
title='Expand All'
95+
onPress={expandAllPress} />
96+
<Button
97+
title='Collapse All'
98+
onPress={collapseAllPress} />
99+
</View>
100+
101+
<View
102+
style={styles.treeViewParent}>
103+
<TreeView<number>
104+
ref={treeViewRef}
105+
data={sampleData}
106+
onCheck={handleSelectionChange}
107+
onExpand={handleExpanded}
108+
selectionPropagation={selectionPropagation}
109+
CustomNodeRowComponent={CustomNodeRowView}
110+
/>
111+
</View>
112+
</SafeAreaView>
113+
);
114+
}

0 commit comments

Comments
 (0)