1.2.7 • Published 3 days ago

react-native-tree-multi-select v1.2.7

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

react-native-tree-multi-select

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

npm version License Workflow Status Supported Platform Badge GitHub issues cov TS

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.

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[]) => {
    // NOTE: Do something with updated checkedIds 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?.();
  
  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
dataTreeNode[]YesAn array of TreeNode objects
onCheck(checkedIds: string[]) => voidNoCallback when a checkbox is checked
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
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>(React Native's TouchableOpacityProps)NoA custom expand/collapse touchable component
CustomNodeRowComponentReact.ComponentType<NodeRowProps>NoCustom row item component

ℹ️ 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
expandNodes() => voidExpands specified nodes
collapseAll() => voidCollapses all nodes
collapseNodes() => voidCollapses 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.

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


Contributing

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

License

MIT

Support the project

❤️ Thanks to