Code Monkey home page Code Monkey logo

jairajjangle / react-native-tree-multi-select Goto Github PK

View Code? Open in Web Editor NEW
40.0 2.0 5.0 3.15 MB

Super-fast tree view with multi-selection capabilities, using checkboxes and search filtering.

License: MIT License

JavaScript 2.15% Ruby 1.85% Java 5.77% Swift 0.05% C 0.08% Objective-C 1.86% Objective-C++ 0.61% TypeScript 87.62%
react-native react-native-tree-select react-tree-view tree-select tree-view react-checkbox-tree react-native-tree react-native-tree-checkbox react-native-tree-view checkbox-treeview

react-native-tree-multi-select's Introduction

react-native-tree-multi-select

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

npm version License Workflow Status Android iOS Web GitHub issues cov TS 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.

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?.();
  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

Property Type Required Description
data TreeNode Yes An array of TreeNode objects
onCheck (checkedIds: string[]) => void No Callback when a checkbox is checked
onExpand (expandedIds: string[]) => void No Callback when a node is expanded
preselectedIds string[] No An array of ids that should be pre-selected
preExpandedIds string[] No An array of ids that should be pre-expanded
indentationMultiplier number No Indentation (marginStart) per level (defaults to 15)
treeFlashListProps TreeFlatListProps No Props for the flash list
checkBoxViewStyleProps BuiltInCheckBoxViewStyleProps No Props for the checkbox view
CheckboxComponent ComponentType<CheckBoxViewProps> No A custom checkbox component. Defaults to React Native Paper's Checkbox
ExpandCollapseIconComponent ComponentType<ExpandIconProps> No A custom expand/collapse icon component
ExpandCollapseTouchableComponent ComponentType<TouchableOpacityProps> No A custom expand/collapse touchable component
CustomNodeRowComponent React.ComponentType<NodeRowProps> No Custom 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

Property Type Required Description
id string Yes Unique identifier for the node
name string Yes The display name of the node
children TreeNode[] No An array of child TreeNode objects
[key: string] any No Any additional properties for the node
(May be useful to perform search on)

TreeViewRef

Property Type Description
selectAll () => void Selects all nodes
unselectAll () => void Unselects all nodes
selectAllFiltered () => void Selects all filtered nodes
unselectAllFiltered () => void Unselects all filtered nodes
expandAll () => void Expands all nodes
collapseAll () => void Collapses all nodes
expandNodes (ids: string[]) => void Expands specified nodes
collapseNodes (ids: string[]) => void Collapses specified nodes
selectNodes (ids: string[]) => void Selects specified nodes
unSelectNodes (ids: string[]) => void Unselects specified nodes
setSearchText (searchText: string, searchKeys?: string[]) => void Set 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

Property Type Required Description
outermostParentViewStyle StyleProp<ViewStyle> No Optional style modifier for the outermost parent view.
checkboxParentViewStyle StyleProp<ViewStyle> No Optional style modifier for the checkbox parent view.
textTouchableStyle StyleProp<ViewStyle> No Optional style modifier for the text touchable style.
checkboxProps CheckboxProps No Optional props for the checkbox component.
textProps TextProps
(React Native)
No Optional props for the text component.

CheckboxProps

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


CheckBoxViewProps

Property Type Required Description
value CheckboxValueType Yes The current value of the checkbox
onValueChange (value: boolean) => void Yes Function to be called when the checkbox is pressed
text string Yes The display text besides the checkbox

CheckboxValueType

Type: boolean OR "indeterminate"


ExpandIconProps

Property Type Required Description
isExpanded boolean Yes Indicates if the icon is expanded

NodeRowProps

Property Type Required Description
node TreeNode Yes The node to be rendered
level number Yes The depth of the node in the tree
checkedValue CheckboxValueType Yes The current value of the checkbox
isExpanded boolean Yes Whether the node is expanded or not
onCheck () => void Yes Function to be called when the checkbox is pressed
onExpand () => void Yes Function 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

LiberPay_Donation_Button           Paypal_Donation_Button           Paypal_Donation_Button

❤️ Thanks to


react-native-tree-multi-select's People

Contributors

actions-user avatar dependabot[bot] avatar jairajjangle avatar renovate[bot] avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-native-tree-multi-select's Issues

How to get value onCheck

const handleSelectionChange = _checkedIds => {
    // const slug = _checkedIds;
    console.log('slug:', _checkedIds);
  };

how to get data _checkedIds, i can't using setState for set value _checkedId, i need list value using to call api

[FEAT-REQ] Handle empty children array like no children array attribute.

Is your feature request related to a problem? Please describe.
I was getting weird behavior, checking sort-of working but unable to un-check, when there were empty arrays of children, if I recursively delete any empty arrays of "children" attributes it works as intended, the library should gracefully handle an empty array.

Describe the solution you'd like
The library should handle no "children" attribute just as well / the same as an empty array children.

Describe alternatives you've considered
I had to add this to my function:

// Step 3: Remove empty children arrays
function removeEmptyChildren(rmArr: any[]) {
  rmArr.forEach(rm => {
      if (rm.children.length === 0) {
          delete rm.children;
      } else {
          removeEmptyChildren(rm.children);
      }
  });
}

removeEmptyChildren(tree);

Screenshots or screen records
NA

Additional context
NA

Would you like to work on this feature?
Potentially, havn't really dug into the code yet.

[FEAT-REQ] Custom ID type for TreeNode

Is your feature request related to a problem? Please describe.
I tried changing an existing data type in my project to extend TreeNode. However, since the ID is assumed to be a string, typescript complains.

Describe the solution you'd like
Change the TreeNode interface to be generic

export interface TreeNode<T> {
  id: T,
  // ...
}

Describe alternatives you've considered
N/A

Screenshots or screen records
N/A

Additional context
N/A

Would you like to work on this feature?
Absolutely!

[FEAT-REQ] key

It is really useful and most update.

I have very small enhancement request. If you accept name and children from props. It could be easy for us to pass any kind of data in our own structure.

it will reduce conversion to name and children.

[FEAT-REQ] Single node selection like radio button

Hi,

well done! Your control looks great, but is it possible to adapt your solution to single node selection? I need something exactly like this, but instead of checkboxes, for example, a radiobutton, where you can select only one node.

Thanks in advance for your answer!

[FEAT-REQ] option for nodes to be reorderable via dragging

Is your feature request related to a problem? Please describe.
I'd like to be able to create a reorderable tree view. Moving items with drag-and-drop gestures is more intuitive for the end user than, say, using a context menu

Describe the solution you'd like
new props that can be passed to the TreeView component:

  • canDragNodes: boolean
  • onDragFinished: (oldIndex: number, newIndex: number, draggedItem: TreeNode) => void

Describe alternatives you've considered
N/A

Screenshots or screen records
N/A

Additional context
There is an implementation of a draggable flashlist here that i think could be integrated

Would you like to work on this feature?
If you wouldn't mind adding another dependency, then sure :)

[FEAT-REQ]: count of checked items in a parent node

Hey!
Thanks for this incredible library!

Is there an approach for getting the count of checked subNodes under a node?

For example

└── Cars (2 out of 3)/
    ├── [x] Hyundai
    ├── [x] Benz
    └── [ ] Tesla

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Updating multiple npm lock files is deprecated and support will be removed in future versions.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency react-native-builder-bob to ^0.23.0
  • fix(deps): update dependency org.jetbrains.kotlin:kotlin-bom to v1.9.24
  • fix(deps): update flipper_version to v0.250.0 (com.facebook.flipper:flipper-network-plugin, com.facebook.flipper:flipper)
  • chore(deps): update commitlint monorepo to v19 (major) (@commitlint/config-conventional, commitlint)
  • chore(deps): update dependency @release-it/conventional-changelog to v8
  • chore(deps): update dependency @types/react to v18
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency eslint-config-prettier to v9
  • chore(deps): update dependency eslint-plugin-prettier to v5
  • chore(deps): update dependency gradle to v8
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency release-it to v17
  • chore(deps): update dependency semantic-release to v24
  • chore(deps): update dependency turbo to v2
  • fix(deps): update dependency @gorhom/showcase-template to v3
  • fix(deps): update dependency org.jetbrains.kotlin:kotlin-bom to v2
  • fix(deps): update dependency react-native-vector-icons to v10
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

bundler
example/Gemfile
  • ruby >= 2.6.10
  • cocoapods '~> 1.12'
cocoapods
example/ios/Podfile
github-actions
.github/actions/setup/action.yml
  • actions/setup-node v4
  • actions/cache v4
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/checkout v4
  • actions/checkout v4
  • actions/checkout v4
  • actions/cache v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/checkout v4
  • actions/cache v4
  • actions/cache v4
  • actions/upload-artifact v4
  • actions/checkout v4
  • actions/setup-node v4
gradle
example/android/gradle.properties
  • com.facebook.flipper:flipper 0.182.0
  • com.facebook.flipper:flipper-network-plugin 0.182.0
  • com.facebook.flipper:flipper-fresco-plugin 0.182.0
example/android/settings.gradle
example/android/build.gradle
example/android/app/build.gradle
  • org.jetbrains.kotlin:kotlin-bom 1.8.0
gradle-wrapper
example/android/gradle/wrapper/gradle-wrapper.properties
  • gradle 7.6.4
npm
example/package.json
  • @gorhom/showcase-template ^2.1.0
  • @react-navigation/native ^6.1.7
  • @react-navigation/native-stack ^6.9.15
  • @react-navigation/stack ^6.3.17
  • @shopify/flash-list ^1.4.3
  • lodash ^4.17.21
  • react 18.3.1
  • react-native 0.72.1
  • react-native-gesture-handler ^2.12.0
  • react-native-paper ^5.9.1
  • react-native-safe-area-context ^4.7.1
  • react-native-screens ^3.22.1
  • react-native-vector-icons ^9.2.0
  • @babel/core ^7.20.0
  • @babel/preset-env ^7.20.0
  • @babel/runtime ^7.20.0
  • @react-native/eslint-config ^0.74.0
  • @react-native/metro-config ^0.74.0
  • @types/lodash ^4.14.195
  • @types/metro-config ^0.76.3
  • babel-plugin-module-resolver ^5.0.0
  • metro-react-native-babel-preset 0.77.0
  • node >=16
package.json
  • zustand ^4.3.9
  • @commitlint/config-conventional ^17.0.2
  • @evilmartians/lefthook ^1.2.2
  • @react-native-community/eslint-config ^3.0.2
  • @release-it/conventional-changelog ^5.0.0
  • @semantic-release/changelog ^6.0.3
  • @semantic-release/git ^10.0.1
  • @semantic-release/github ^10.0.3
  • @semantic-release/npm ^12.0.0
  • @shopify/flash-list 1.x.x
  • @testing-library/jest-native ^5.4.2
  • @testing-library/react-native ^12.1.2
  • @types/color ^3.0.3
  • @types/jest ^29.5.3
  • @types/react ~17.0.21
  • @types/react-native 0.70.0
  • @types/react-native-vector-icons ^6.4.13
  • @types/react-test-renderer 18.3.0
  • commitlint ^17.0.2
  • del-cli ^5.0.0
  • eslint ^8.4.1
  • eslint-config-prettier ^8.5.0
  • eslint-plugin-prettier ^4.0.0
  • jest ^29.6.1
  • pod-install ^0.1.0
  • prettier ^2.0.5
  • react 18.3.1
  • react-native 0.72.1
  • react-native-builder-bob ^0.20.0
  • react-native-paper 5.x.x
  • react-test-renderer 18.3.1
  • release-it ^15.0.0
  • semantic-release ^23.0.8
  • ts-jest ^29.1.1
  • turbo ^1.10.7
  • typescript ^5.0.2
  • @shopify/flash-list 1.x.x
  • react *
  • react-native *
  • react-native-paper 5.x.x
  • react-native-vector-icons *
  • node >= 16.0.0
  • @types/react 17.0.80
  • yarn 1.22.22
nvm
.nvmrc
  • node 20.14.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.