Skip to content

Commit 51cf427

Browse files
committed
Add expandOnClick property
1 parent 82ec4d3 commit 51cf427

File tree

8 files changed

+22
-3
lines changed

8 files changed

+22
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ All node objects **must** have a unique `value`. This value is serialized into t
9595
| `checked` | array | An array of checked node values. | `[]` |
9696
| `disabled` | bool | If true, the component will be disabled and nodes cannot be checked. | `false` |
9797
| `expandDisabled` | bool | If true, the ability to expand nodes will be disabled. | `false` |
98+
| `expandOnClick` | bool | If true, nodes will be expanded by clicking on labels. Requires a non empty `onClick` function. | `false` |
9899
| `expanded` | array | An array of expanded node values. | `[]` |
99100
| `name` | string | Optional name for the hidden `<input>` element. | `undefined` |
100101
| `nameAsArray` | bool | If true, the hidden `<input>` will encode its values as an array rather than a joined string. | `false` |

examples/dist/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ <h2>Clickable Labels Example</h2>
6969
By default, clicking on the node label texts toggle the checkbox value. Providing an <code>onClick</code> property the checkbox will toggle
7070
only when clicking on the checkbox and the provided function will be called when clicking on the node label text.
7171
</p>
72+
<p>
73+
When the <code>onClick</code> function is defined passing the <code>expandOnClick</code> property will expand the clicked node automatically.
74+
</p>
7275
<div id="clickable-labels-example"></div>
7376

7477
<footer class="site-footer">

examples/dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/dist/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@
9696
}
9797

9898
.rct-node-clickable:focus {
99-
background: rgba(51, 51, 204, 0.5);
10099
outline: 0;
100+
background: rgba(51, 51, 204, 0.5);
101101
}
102102

103103
.rct-collapse {

examples/src/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ <h2>Clickable Labels Example</h2>
6969
By default, clicking on the node label texts toggle the checkbox value. Providing an <code>onClick</code> property the checkbox will toggle
7070
only when clicking on the checkbox and the provided function will be called when clicking on the node label text.
7171
</p>
72+
<p>
73+
When the <code>onClick</code> function is defined passing the <code>expandOnClick</code> property will expand the clicked node automatically.
74+
</p>
7275
<div id="clickable-labels-example"></div>
7376

7477
<footer class="site-footer">

examples/src/js/ClickableLabelsExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class ClickExample extends React.Component {
127127
checked={checked}
128128
expanded={expanded}
129129
nodes={nodes}
130+
expandOnClick
130131
onCheck={this.onCheck}
131132
onClick={this.onClick}
132133
onExpand={this.onExpand}

src/js/CheckboxTree.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CheckboxTree extends React.Component {
1414
checked: PropTypes.arrayOf(PropTypes.string),
1515
disabled: PropTypes.bool,
1616
expandDisabled: PropTypes.bool,
17+
expandOnClick: PropTypes.bool,
1718
expanded: PropTypes.arrayOf(PropTypes.string),
1819
name: PropTypes.string,
1920
nameAsArray: PropTypes.bool,
@@ -31,6 +32,7 @@ class CheckboxTree extends React.Component {
3132
checked: [],
3233
disabled: false,
3334
expandDisabled: false,
35+
expandOnClick: false,
3436
expanded: [],
3537
name: undefined,
3638
nameAsArray: false,
@@ -209,6 +211,7 @@ class CheckboxTree extends React.Component {
209211
const {
210212
disabled,
211213
expandDisabled,
214+
expandOnClick,
212215
noCascade,
213216
onlyLeafCheckboxes,
214217
optimisticToggle,
@@ -231,6 +234,7 @@ class CheckboxTree extends React.Component {
231234
className={node.className}
232235
disabled={nodeDisabled}
233236
expandDisabled={expandDisabled}
237+
expandOnClick={expandOnClick}
234238
expanded={node.expanded}
235239
icon={node.icon}
236240
label={node.label}

src/js/TreeNode.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TreeNode extends React.Component {
2121

2222
children: PropTypes.node,
2323
className: PropTypes.string,
24+
expandOnClick: PropTypes.bool,
2425
icon: PropTypes.node,
2526
rawChildren: PropTypes.arrayOf(nodeShape),
2627
showCheckbox: PropTypes.bool,
@@ -30,6 +31,7 @@ class TreeNode extends React.Component {
3031
static defaultProps = {
3132
children: null,
3233
className: null,
34+
expandOnClick: false,
3335
icon: null,
3436
rawChildren: null,
3537
showCheckbox: true,
@@ -76,6 +78,11 @@ class TreeNode extends React.Component {
7678
isChecked = this.props.optimisticToggle;
7779
}
7880

81+
// Auto expand if enabled
82+
if (this.hasChildren() && this.props.expandOnClick) {
83+
this.onExpand();
84+
}
85+
7986
this.props.onClick({
8087
value: this.props.value,
8188
checked: isChecked,

0 commit comments

Comments
 (0)