Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sibiraj-s/use-checkbox-tree
React hook for checkbox tree component
https://github.com/sibiraj-s/use-checkbox-tree
checkbox-tree react react-hooks
Last synced: about 1 month ago
JSON representation
React hook for checkbox tree component
- Host: GitHub
- URL: https://github.com/sibiraj-s/use-checkbox-tree
- Owner: sibiraj-s
- License: mit
- Created: 2022-09-02T08:24:00.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-12T11:36:40.000Z (3 months ago)
- Last Synced: 2024-12-26T12:42:02.715Z (about 2 months ago)
- Topics: checkbox-tree, react, react-hooks
- Language: TypeScript
- Homepage: https://sibiraj-s.github.io/use-checkbox-tree/
- Size: 1.69 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# use-checkbox-tree
> React hook for checkbox tree component
[](https://github.com/sibiraj-s/use-checkbox-tree/actions/workflows/tests.yml)
[](https://npmjs.com/use-checkbox-tree)
[](https://github.com/sibiraj-s/use-checkbox-tree/blob/master/LICENSE)## Installation
```bash
npm i use-checkbox-tree
```## Usage
```jsx
import ReactDOM from 'react-dom';
import useCheckboxTree from 'use-checkbox-tree';const nodes = [
{
id: 1,
children: [
{
id: 1.1,
},
{
id: 1.2,
},
],
},
];const initialValue = [];
const TreeComponent = () => {
const { checked, indeterminates, state, selectNode } = useCheckboxTree(nodes, initialValue);// checked -> [1.2]
// indeterminates -> [1]
// state.get(1) -> 'indeterminate'return (
// iterate through nodes and create your own tree view
);
};const root = ReactDOM.createRoot(document.getElementById('root'));
root.render();
```### Return values
- checked - array of selected node ids
- selectNode - a function to select node
- deSelectNode - a function to deselect node```js
selectNode(1); // select a node
deSelectNode(1); // deselect a node
selectNode(1, true); // select a node
selectNode(1, false); // deselect a node
```- clear - a function to clear all selected items
- state - object with id as key and value as `boolean | indeterminate`
```js
Map{
1: "indeterminate",
1.1: true,
1.2: false
}
```