### Start Example App Packager Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/CONTRIBUTING.md Start the Metro server for the example application. This is necessary to run the example app on various platforms. ```sh yarn example start ``` -------------------------------- ### Run Example App on Web Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/CONTRIBUTING.md Build and run the example application in a web browser. Ensure the Metro server is running. ```sh yarn example web ``` -------------------------------- ### Basic TreeView Usage Example Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/README.md Demonstrates the fundamental setup for the TreeView component, including data binding, event handlers for selection and expansion, and ref usage for programmatic control. Ensure the parent container has a fixed height. ```tsx import { TreeView, type TreeNode, type TreeViewRef } from 'react-native-tree-multi-select'; // Refer to the Properties table below or the example app for the TreeNode type const myData: TreeNode[] = [...]; export function TreeViewUsageExample(){ const treeViewRef = React.useRef(null); // It's recommended to use debounce for the search function (refer to the example app) function triggerSearch(text: string){ // Pass search text to the tree along with the keys on which search is to be done(optional) treeViewRef.current?.setSearchText(text, ["name"]); } // Callback functions for check and expand state changes: const handleSelectionChange = ( _checkedIds: string[], _indeterminateIds: string[] ) => { // NOTE: Handle _checkedIds and _indeterminateIds here }; const handleExpanded = (expandedIds: string[]) => { // NOTE: Do something with updated expandedIds here }; // Expand collapse calls using ref const expandAllPress = () => treeViewRef.current?.expandAll?.(); const collapseAllPress = () => treeViewRef.current?.collapseAll?.(); const expandNodes = (idsToExpand: string[]) => treeViewRef.current?.expandNodes?.( idsToExpand ); const collapseNodes = (idsToCollapse: string[]) => treeViewRef.current?.collapseNodes?.( idsToCollapse ); // Multi-selection function calls using ref const onSelectAllPress = () => treeViewRef.current?.selectAll?.(); const onUnselectAllPress = () => treeViewRef.current?.unselectAll?.(); const onSelectAllFilteredPress = () => treeViewRef.current?.selectAllFiltered?.(); const onUnselectAllFilteredPress = () => treeViewRef.current?.unselectAllFiltered?.(); const selectNodes = (idsToExpand: string[]) => treeViewRef.current?.selectNodes?.( idsToExpand ); const unselectNodes = (idsToCollapse: string[]) => treeViewRef.current?.unselectNodes?.( idsToUnselect ); return( // ... Remember to keep a fixed height for the parent. Read Flash List docs to know why ); } ``` -------------------------------- ### Install react-native-tree-multi-select Source: https://context7.com/jairajjangle/react-native-tree-multi-select/llms.txt Commands to install the library and its required peer dependencies. ```bash # Using yarn yarn add react-native-tree-multi-select # Using npm npm install react-native-tree-multi-select # Required peer dependency yarn add @shopify/flash-list # For non-Expo apps, also install: yarn add react-native-vector-icons ``` -------------------------------- ### Run Example App on iOS Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/CONTRIBUTING.md Build and run the example application on an iOS simulator or device. Ensure the Metro server is running. ```sh yarn example ios ``` -------------------------------- ### Install react-native-tree-multi-select Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/README.md Commands to install the package using common package managers. ```sh yarn add react-native-tree-multi-select ``` ```sh npm install react-native-tree-multi-select ``` -------------------------------- ### Run Example App on Android Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/CONTRIBUTING.md Build and run the example application on an Android device or emulator. Ensure the Metro server is running. ```sh yarn example android ``` -------------------------------- ### Install and Use Node.js Version Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/CONTRIBUTING.md Ensure Node.js 22.12.0 or higher is installed and active using nvm. This is a prerequisite for the project's development environment. ```sh nvm install 22.12.0 nvm use 22.12.0 ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/jairajjangle/react-native-tree-multi-select/blob/main/CONTRIBUTING.md Install all project dependencies for all packages within the monorepo. This command should be run after setting up Yarn. ```sh yarn ``` -------------------------------- ### Implement Basic TreeView Component Source: https://context7.com/jairajjangle/react-native-tree-multi-select/llms.txt Example of rendering a hierarchical tree structure with checkboxes and handling selection and expansion events. ```tsx import React, { useRef, useCallback } from "react"; import { SafeAreaView, View, Button, StyleSheet } from "react-native"; import { TreeView, type TreeNode, type TreeViewRef } from "react-native-tree-multi-select"; import debounce from "lodash/debounce"; // Define your tree data structure const treeData: TreeNode[] = [ { id: "1", name: "Documents", children: [ { id: "1.1", name: "Work", children: [ { id: "1.1.1", name: "Reports" }, { id: "1.1.2", name: "Presentations" } ] }, { id: "1.2", name: "Personal", children: [ { id: "1.2.1", name: "Photos" }, { id: "1.2.2", name: "Videos" } ] } ] }, { id: "2", name: "Downloads", children: [ { id: "2.1", name: "Software" }, { id: "2.2", name: "Music" } ] } ]; export default function BasicTreeViewExample() { const treeViewRef = useRef(null); // Handle checkbox state changes const handleSelectionChange = ( checkedIds: string[], indeterminateIds: string[] ) => { console.log("Checked nodes:", checkedIds); console.log("Indeterminate nodes:", indeterminateIds); }; // Handle expand/collapse state changes const handleExpanded = (expandedIds: string[]) => { console.log("Expanded nodes:", expandedIds); }; return ( ); } const styles = StyleSheet.create({ container: { flex: 1 }, treeContainer: { flex: 1 } }); ``` -------------------------------- ### Custom ID Types with Numeric Identifiers Source: https://context7.com/jairajjangle/react-native-tree-multi-select/llms.txt Utilize generic ID types to support non-string identifiers like numbers for node IDs. This example demonstrates using numeric IDs and interacting with the TreeView via its ref to select and expand nodes. ```tsx import React, { useRef } from "react"; import { View, Button, StyleSheet } from "react-native"; import { TreeView, type TreeNode, type TreeViewRef } from "react-native-tree-multi-select"; // Define tree with numeric IDs const treeData: TreeNode[] = [ { id: 1, name: "Category 1", children: [ { id: 101, name: "Item 101" }, { id: 102, name: "Item 102" } ] }, { id: 2, name: "Category 2", children: [ { id: 201, name: "Item 201" }, { id: 202, name: "Item 202" } ] } ]; export default function NumericIdExample() { // Specify the ID type in the ref const treeViewRef = useRef | null>(null); const handleSelectionChange = ( checkedIds: number[], indeterminateIds: number[] ) => { console.log("Selected IDs:", checkedIds); // [101, 201] }; const selectSpecificItems = () => { treeViewRef.current?.selectNodes([101, 201]); }; const expandCategories = () => { treeViewRef.current?.expandNodes([1, 2]); }; return (