Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/disabled checkbox #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A versatile and customizable react treeview library. Features:
✅ inline add, modify, and delete tree nodes
✅ checkbox with half check (indeterminate check)
✅ read-only mode
✅ disable individual node checkboxes

It uses [use-tree-state](https://www.npmjs.com/package/use-tree-state) hook internally for convenient state management.
### Quick Preview
Expand Down Expand Up @@ -48,6 +49,7 @@ Initial tree state is an object that describes a nested tree node structure, whi
name: 'root node',
checked (optional): 0 (unchecked, default) | 0.5 (half checked) | 1(checked),
isOpen (optional): true (default) | false,
isDisabled (optional): true | false (default),
children (optional): [array of treenode],

// internal keys (auto generated), plz don't include them in the initial data
Expand Down Expand Up @@ -76,6 +78,7 @@ const treeState = {
children: [
{ name: 'children 2-1 [not checked]', checked: 0 },
{ name: 'children 2-2 [checked]', checked: 1 },
{ name: 'children 2-3 [not checked] [disabled]', checked: 0, isDisabled: true },
],
},
],
Expand Down
4 changes: 3 additions & 1 deletion src/components/CheckBox/CheckBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {
} from 'react';
import PropTypes from 'prop-types';

const CheckBox = ({ status, onChange }) => {
const CheckBox = ({ status, onChange, isDisabled = false }) => {
const isChecked = status === 1;
const isHalfChecked = status === 0.5;

Expand All @@ -23,6 +23,7 @@ const CheckBox = ({ status, onChange }) => {
checked={ isChecked }
onChange={ onChange }
ref={ checkboxRef }
disabled={ isDisabled }
/>
</div>
);
Expand All @@ -31,6 +32,7 @@ const CheckBox = ({ status, onChange }) => {
CheckBox.propTypes = {
status: PropTypes.number.isRequired,
onChange: PropTypes.func.isRequired,
isDisabled: PropTypes.bool,
};

export default CheckBox;
5 changes: 4 additions & 1 deletion src/components/TreeNode/TreeNode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ const TreeNode = ({
name,
checked,
isOpen,
isDisabled,
children,
...restData
}) => {
const nodeData = {
path, name, checked, isOpen, ...restData,
path, name, checked, isOpen, isDisabled, ...restData,
};

const {
Expand Down Expand Up @@ -189,6 +190,7 @@ const TreeNode = ({
<CheckBox
status={ checked }
onChange={ handleCheckBoxChange }
disabled={ nodeData.isDisabled }
/>
)}

Expand Down Expand Up @@ -237,6 +239,7 @@ TreeNode.propTypes = {
name: PropTypes.string.isRequired,
checked: PropTypes.number.isRequired,
isOpen: PropTypes.bool,
isDisabled: PropTypes.bool,

children: PropTypes.array,
};
Expand Down