Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-tree-multi-select

Package Overview
Dependencies
Maintainers
0
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-tree-multi-select

A super-fast, customizable tree view component for React Native with multi-selection, checkboxes, and search filtering capabilities.

  • 1.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

react-native-tree-multi-select

⚡️Super-fast Tree view with multi-selection capabilities, using checkboxes and search filtering.

npm version License Workflow Status cov Android iOS Web GitHub issues TS Known Vulnerabilities Expo Snack

Expand/collapse demo Search demo Customization demo

Installation

Using yarn

yarn add react-native-tree-multi-select

using npm:

npm install react-native-tree-multi-select

Dependencies that need to be installed for this library to work:

  1. @shopify/flash-list
  2. react-native-paper
  3. react-native-vector-icons

Make sure to follow the native-related installation instructions for these dependencies.

Highlighted Features

  • Fast: Designed with performance in mind for smooth scrolling and quick selections.
  • 🛠️ Highly Customizable: Modify styles, behavior, and use your custom list component to suit your application's needs.
  • 🔍 Filterable: Quickly filter through tree nodes and option to select and un-select only the filtered tree nodes.
  • Well Tested: Comprehensive test coverage to ensure reliability and stability.
  • 📚 Well Documented: Detailed documentation to get you started and an example app to demo all the features.
  • 🌳 Multi-Level Selection: Select individual nodes or entire branches with ease.
  • 📦 Supports Large Datasets: Efficiently handles large trees without much performance degradation.
  • 🔒 TypeScript Support: Full TypeScript support for better developer experience.
  • 💻 Cross-Platform: Works seamlessly across iOS, Android, and web (with React Native Web).

Usage

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<TreeViewRef | null>(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?.(
    idsToSelect
  );
  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
    <TreeView
      ref={treeViewRef}
      data={myData}
      onCheck={handleSelectionChange}
      onExpand={handleExpanded}
    />
  );
}

Properties

PropertyTypeRequiredDescription
dataTreeNodeYesAn array of TreeNode objects
onCheck(checkedIds: string[], indeterminateIds: string[]) => voidNoCallback when a checkbox state changes
onExpand(expandedIds: string[]) => voidNoCallback when a node is expanded
preselectedIdsstring[]NoAn array of ids that should be pre-selected
preExpandedIdsstring[]NoAn array of ids that should be pre-expanded
selectionPropagationSelectionPropagationNoControl Selection Propagation Behavior. Choose whether you want to auto-select children or parents.
indentationMultipliernumberNoIndentation (marginStart) per level (defaults to 15)
treeFlashListPropsTreeFlatListPropsNoProps for the flash list
checkBoxViewStylePropsBuiltInCheckBoxViewStylePropsNoProps for the checkbox view
CheckboxComponentComponentType<CheckBoxViewProps>NoA custom checkbox component. Defaults to React Native Paper's Checkbox
ExpandCollapseIconComponentComponentType<ExpandIconProps>NoA custom expand/collapse icon component
ExpandCollapseTouchableComponentComponentType<TouchableOpacityProps>NoA custom expand/collapse touchable component
CustomNodeRowComponentReact.ComponentType<NodeRowProps>NoCustom row item component

ℹ️ data prop is not stateful! You will need to update the key prop to force TreeView to re-render.

ℹ️ If CustomNodeRowComponent is provided then below props are not applied:

  1. indentationMultiplier
  2. checkBoxViewStyleProps
  3. CheckboxComponent
  4. ExpandCollapseIconComponent
  5. ExpandCollapseTouchableComponent.

⚠️ If the tree view doesn't scroll if rendered in a complex nested scroll view/s then try setting the renderScrollComponent value in treeFlashListProps to ScrollView from react-native-gesture-handler.


TreeNode
PropertyTypeRequiredDescription
idstringYesUnique identifier for the node
namestringYesThe display name of the node
childrenTreeNode[]NoAn array of child TreeNode objects
[key: string]anyNoAny additional properties for the node
(May be useful to perform search on)

TreeViewRef
PropertyTypeDescription
selectAll() => voidSelects all nodes
unselectAll() => voidUnselects all nodes
selectAllFiltered() => voidSelects all filtered nodes
unselectAllFiltered() => voidUnselects all filtered nodes
expandAll() => voidExpands all nodes
collapseAll() => voidCollapses all nodes
expandNodes(ids: string[]) => voidExpands specified nodes
collapseNodes(ids: string[]) => voidCollapses specified nodes
selectNodes(ids: string[]) => voidSelects specified nodes
unSelectNodes(ids: string[]) => voidUnselects specified nodes
setSearchText(searchText: string, searchKeys?: string[]) => voidSet the search text and optionally the search keys. Default search key is "name"

Recommended to call this inside a debounced function if you find any performance issue otherwise.

SelectionPropagation
PropertyTypeRequiredDescription
toChildrenbooleanNoWhether to propagate selection to children nodes. Defaults to true.
toParentsbooleanNoWhether to propagate selection to parent nodes. Defaults to true.

TreeFlatListProps

All properties of FlashListProps(from @shopify/flash-list) except for data and renderItem


BuiltInCheckBoxViewStyleProps
PropertyTypeRequiredDescription
outermostParentViewStyleStyleProp<ViewStyle>NoOptional style modifier for the outermost parent view.
checkboxParentViewStyleStyleProp<ViewStyle>NoOptional style modifier for the checkbox parent view.
textTouchableStyleStyleProp<ViewStyle>NoOptional style modifier for the text touchable style.
checkboxPropsCheckboxPropsNoOptional props for the checkbox component.
textPropsTextProps
(React Native)
NoOptional props for the text component.
CheckboxProps

All properties of RNPaperCheckboxAndroidProps(from react-native-paper) except for onPress and status


CheckBoxViewProps
PropertyTypeRequiredDescription
valueCheckboxValueTypeYesThe current value of the checkbox
onValueChange(value: boolean) => voidYesFunction to be called when the checkbox is pressed
textstringYesThe display text besides the checkbox
CheckboxValueType

Type: boolean OR "indeterminate"


ExpandIconProps
PropertyTypeRequiredDescription
isExpandedbooleanYesIndicates if the icon is expanded

NodeRowProps
PropertyTypeRequiredDescription
nodeTreeNodeYesThe node to be rendered
levelnumberYesThe depth of the node in the tree
checkedValueCheckboxValueTypeYesThe current value of the checkbox
isExpandedbooleanYesWhether the node is expanded or not
onCheck() => voidYesFunction to be called when the checkbox is pressed
onExpand() => voidYesFunction to be called when the expand button is pressed

🙌 Planned features

  • Row Item full-customization
  • Prop to control auto children and parents selection. Can now be done using selectionPropagation prop 🎉
  • Prop to set the maximum checked item limit
  • Prop to disable certain nodes from getting checked
  • Ref function to programatically expand/collapse a certain node
  • Ref function to programatically un/check a certain node
  • Ref function to auto-scroll to a certain node's position

If you do not see what you want in the planned feature list, raise a feature request.


💡 Some Expo Snack Examples

  1. Radio button like selection in tree view: Snack link
  2. Display count of number of checked nodes: Snack link

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

🙏 Support the project

LiberPay_Donation_Button           Paypal_Donation_Button           Paypal_Donation_Button

❤️ Thanks to


Keywords

FAQs

Package last updated on 20 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc