Skip to content

Commit 08da11b

Browse files
committed
Add disabled and expandDisabled properties
References #35.
1 parent da79dbb commit 08da11b

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ All node objects **must** have a unique `value`. This value is serialized into t
9393
| ------------------ | -------- | ------------------------------------------------------------------------------------------------ | ----------- |
9494
| `nodes` | array | **Required**. Specifies the tree nodes and their children. | |
9595
| `checked` | array | An array of checked node values. | `[]` |
96+
| `disabled` | bool | If true, the component will be disabled and nodes cannot be checked. | `false` |
97+
| `expandDisabled` | bool | If true, the ability to expand nodes will be disabled. | `false` |
9698
| `expanded` | array | An array of expanded node values. | `[]` |
9799
| `name` | string | Optional name for the hidden `<input>` element. | `undefined` |
98100
| `nameAsArray` | bool | If true, the hidden `<input>` will encode its values as an array rather than a joined string. | `false` |

src/js/CheckboxTree.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import classNames from 'classnames';
12
import { isEqual } from 'lodash';
23
import PropTypes from 'prop-types';
34
import React from 'react';
@@ -11,6 +12,8 @@ class CheckboxTree extends React.Component {
1112
nodes: PropTypes.arrayOf(nodeShape).isRequired,
1213

1314
checked: PropTypes.arrayOf(PropTypes.string),
15+
disabled: PropTypes.bool,
16+
expandDisabled: PropTypes.bool,
1417
expanded: PropTypes.arrayOf(PropTypes.string),
1518
name: PropTypes.string,
1619
nameAsArray: PropTypes.bool,
@@ -23,6 +26,8 @@ class CheckboxTree extends React.Component {
2326

2427
static defaultProps = {
2528
checked: [],
29+
disabled: false,
30+
expandDisabled: false,
2631
expanded: [],
2732
name: undefined,
2833
nameAsArray: false,
@@ -180,7 +185,7 @@ class CheckboxTree extends React.Component {
180185
}
181186

182187
renderTreeNodes(nodes) {
183-
const { noCascade, optimisticToggle, showNodeIcon } = this.props;
188+
const { disabled, expandDisabled, noCascade, optimisticToggle, showNodeIcon } = this.props;
184189
const treeNodes = nodes.map((node) => {
185190
const key = `${node.value}`;
186191
const checked = this.getCheckState(node, noCascade);
@@ -191,6 +196,8 @@ class CheckboxTree extends React.Component {
191196
key={key}
192197
checked={checked}
193198
className={node.className}
199+
disabled={disabled}
200+
expandDisabled={expandDisabled}
194201
expanded={node.expanded}
195202
icon={node.icon}
196203
label={node.label}
@@ -251,9 +258,13 @@ class CheckboxTree extends React.Component {
251258
render() {
252259
const nodes = this.getFormattedNodes(this.props.nodes);
253260
const treeNodes = this.renderTreeNodes(nodes);
261+
const className = classNames({
262+
'react-checkbox-tree': true,
263+
'rct-disabled': this.props.disabled,
264+
});
254265

255266
return (
256-
<div className="react-checkbox-tree">
267+
<div className={className}>
257268
{this.renderHiddenInput()}
258269
{treeNodes}
259270
</div>

src/js/TreeNode.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import nodeShape from './nodeShape';
77
class TreeNode extends React.Component {
88
static propTypes = {
99
checked: PropTypes.number.isRequired,
10+
disabled: PropTypes.bool.isRequired,
11+
expandDisabled: PropTypes.bool.isRequired,
1012
expanded: PropTypes.bool.isRequired,
1113
label: PropTypes.string.isRequired,
1214
optimisticToggle: PropTypes.bool.isRequired,
@@ -68,6 +70,8 @@ class TreeNode extends React.Component {
6870
}
6971

7072
renderCollapseButton() {
73+
const { expandDisabled } = this.props;
74+
7175
if (!this.hasChildren()) {
7276
return (
7377
<span className="rct-collapse">
@@ -80,6 +84,7 @@ class TreeNode extends React.Component {
8084
<button
8185
aria-label="Toggle"
8286
className="rct-collapse rct-collapse-btn"
87+
disabled={expandDisabled}
8388
title="Toggle"
8489
type="button"
8590
onClick={this.onExpand}
@@ -134,7 +139,7 @@ class TreeNode extends React.Component {
134139
}
135140

136141
render() {
137-
const { checked, className, treeId, label, showNodeIcon, value } = this.props;
142+
const { checked, className, disabled, treeId, label, showNodeIcon, value } = this.props;
138143
const inputId = `${treeId}-${value}`;
139144
const nodeClass = classNames({
140145
'rct-node': true,
@@ -147,7 +152,13 @@ class TreeNode extends React.Component {
147152
<span className="rct-text">
148153
{this.renderCollapseButton()}
149154
<label htmlFor={inputId}>
150-
<input checked={checked === 1} id={inputId} type="checkbox" onChange={this.onCheck} />
155+
<input
156+
checked={checked === 1}
157+
disabled={disabled}
158+
id={inputId}
159+
type="checkbox"
160+
onChange={this.onCheck}
161+
/>
151162
<span className="rct-checkbox">
152163
{this.renderCheckboxIcon()}
153164
</span>

src/less/react-checkbox-tree.less

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
&:focus {
2222
outline: none;
2323
}
24+
25+
&:disabled {
26+
cursor: not-allowed;
27+
}
2428
}
2529

2630
label {
@@ -42,6 +46,18 @@
4246
}
4347
}
4448

49+
.rct-disabled {
50+
opacity: .75;
51+
52+
label {
53+
cursor: not-allowed;
54+
55+
&:hover {
56+
background: transparent;
57+
}
58+
}
59+
}
60+
4561
.rct-collapse,
4662
.rct-node-icon,
4763
.rct-checkbox {

src/scss/react-checkbox-tree.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ $rct-label-hover: rgba($rct-icon-color, .1) !default;
2121
&:focus {
2222
outline: none;
2323
}
24+
25+
&:disabled {
26+
cursor: not-allowed;
27+
}
2428
}
2529

2630
label {
@@ -42,6 +46,18 @@ $rct-label-hover: rgba($rct-icon-color, .1) !default;
4246
}
4347
}
4448

49+
.rct-disabled {
50+
opacity: .75;
51+
52+
label {
53+
cursor: not-allowed;
54+
55+
&:hover {
56+
background: transparent;
57+
}
58+
}
59+
}
60+
4561
.rct-collapse,
4662
.rct-node-icon,
4763
.rct-checkbox {

0 commit comments

Comments
 (0)