|
| 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