Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit de11f04

Browse files
committed
Added multi select checkbox functionality
1 parent 9069701 commit de11f04

File tree

5 files changed

+87
-8
lines changed

5 files changed

+87
-8
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ Sets the treeview styling. Defaults to `src/themes/default`.
101101

102102
Sets the treeview animations. Set to `false` if you want to turn off animations. See [velocity-react](https://github.com/twitter-fabric/velocity-react) for more details. Defaults to `src/themes/animations`.
103103

104+
#### enableCheckbox
105+
`PropTypes.bool`
106+
Enable / disable checkbox element for each node. Defaults to `false`.
107+
108+
#### checkboxField
109+
`PropTypes.string`
110+
To set the checkbox field value which need to be used. Defaults to `name`.
111+
112+
#### handleCheckbox
113+
`PropTypes.func`
114+
115+
Callback function when a checbox is checked / unchecked. Passes 2 attributes: the data node and it's checked boolean value.
116+
117+
```
118+
handleCheckbox(node, isChecked) {
119+
node.checked = isChecked;
120+
this.setState({cursor: node});
121+
}
122+
```
123+
104124
#### decorators
105125
`PropTypes.object`
106126

@@ -157,6 +177,7 @@ const decorators = {
157177
loading: '[optional] boolean',
158178
decorators: '[optional] object',
159179
animations: '[optional] object'
180+
checked: '[optional] boolean'
160181
},
161182
```
162183
#### id
@@ -179,3 +200,6 @@ Loading flag. It will populate the treeview with the loading component. Useful w
179200

180201
#### decorators / animations
181202
Attach specific decorators / animations to a node. Provides the low level functionality to create visuals on a node-by-node basis. These structures are the same as the top level props, described above.
203+
204+
#### checked
205+
Checked flag. Validate node checkbox should display as checked or not .

example/app.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ class DemoTree extends React.Component {
4949
constructor() {
5050
super();
5151

52-
this.state = {data};
52+
this.state = {data, withCheckbox: false};
5353
this.onToggle = this.onToggle.bind(this);
54+
this.withCheckbox = this.withCheckbox.bind(this);
55+
this.handleCheckbox = this.handleCheckbox.bind(this);
56+
}
57+
58+
withCheckbox() {
59+
this.setState({ withCheckbox: !this.state.withCheckbox });
5460
}
5561

5662
onToggle(node, toggled) {
@@ -78,6 +84,11 @@ class DemoTree extends React.Component {
7884
this.setState({data: filtered});
7985
}
8086

87+
handleCheckbox(node, isChecked) {
88+
node.checked = isChecked;
89+
this.setState({cursor: node});
90+
}
91+
8192
render() {
8293
const {data: stateData, cursor} = this.state;
8394

@@ -95,9 +106,13 @@ class DemoTree extends React.Component {
95106
</div>
96107
</div>
97108
<div style={styles.component}>
109+
<input type="checkbox" onChange={this.withCheckbox} /> <span style={{ color: '#9da5ab' }}>With Checkbox?</span>
110+
<br />
98111
<Treebeard data={stateData}
99112
decorators={decorators}
100-
onToggle={this.onToggle}/>
113+
onToggle={this.onToggle}
114+
enableCheckbox={this.state.withCheckbox}
115+
handleCheckbox={this.handleCheckbox} />
101116
</div>
102117
<div style={styles.component}>
103118
<NodeViewer node={cursor}/>

src/components/node.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class TreeNode extends React.Component {
2222
}
2323
}
2424

25+
handleCheckbox(flagChecked) {
26+
if (this.props.handleCheckbox) {
27+
this.props.handleCheckbox(this.props.node, !flagChecked);
28+
}
29+
}
30+
2531
animations() {
2632
const {animations, node} = this.props;
2733

@@ -44,6 +50,20 @@ class TreeNode extends React.Component {
4450
return Object.assign({}, decorators, nodeDecorators);
4551
}
4652

53+
renderCheckbox(value, style, isChecked) {
54+
if (this.props.enableCheckbox) {
55+
return (
56+
<input
57+
type="checkbox"
58+
checked={isChecked}
59+
value={value}
60+
style={style}
61+
onChange={this.handleCheckbox.bind(this, isChecked)}
62+
/>
63+
);
64+
}
65+
}
66+
4767
render() {
4868
const {style} = this.props;
4969
const decorators = this.decorators();
@@ -52,6 +72,7 @@ class TreeNode extends React.Component {
5272
return (
5373
<li ref={ref => this.topLevelRef = ref}
5474
style={style.base}>
75+
{this.renderCheckbox(this.props.node[this.props.checkboxField], style.checkbox, this.props.node.checked)}
5576
{this.renderHeader(decorators, animations)}
5677

5778
{this.renderDrawer(decorators, animations)}
@@ -109,7 +130,10 @@ class TreeNode extends React.Component {
109130
decorators={propDecorators}
110131
key={child.id || index}
111132
node={child}
112-
style={style}/>
133+
style={style}
134+
enableCheckbox={this.props.enableCheckbox}
135+
checkboxField={this.props.checkboxField}
136+
handleCheckbox={this.props.handleCheckbox}/>
113137
)}
114138
</ul>
115139
);
@@ -144,7 +168,10 @@ TreeNode.propTypes = {
144168
PropTypes.object,
145169
PropTypes.bool
146170
]).isRequired,
147-
onToggle: PropTypes.func
171+
onToggle: PropTypes.func,
172+
enableCheckbox: PropTypes.bool,
173+
checkboxField: PropTypes.string,
174+
handleCheckbox: PropTypes.func
148175
};
149176

150177
export default TreeNode;

src/components/treebeard.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class TreeBeard extends React.Component {
2626
key={node.id || index}
2727
node={node}
2828
onToggle={onToggle}
29-
style={style.tree.node}/>
29+
style={style.tree.node}
30+
enableCheckbox={this.props.enableCheckbox}
31+
checkboxField={this.props.checkboxField}
32+
handleCheckbox={this.props.handleCheckbox}/>
3033
)}
3134
</ul>
3235
);
@@ -44,13 +47,18 @@ TreeBeard.propTypes = {
4447
PropTypes.bool
4548
]),
4649
onToggle: PropTypes.func,
47-
decorators: PropTypes.object
50+
decorators: PropTypes.object,
51+
enableCheckbox: PropTypes.bool,
52+
checkboxField: PropTypes.string,
53+
handleCheckbox: PropTypes.func
4854
};
4955

5056
TreeBeard.defaultProps = {
5157
style: defaultTheme,
5258
animations: defaultAnimations,
53-
decorators: defaultDecorators
59+
decorators: defaultDecorators,
60+
enableCheckbox: false,
61+
checkboxField: 'name'
5462
};
5563

5664
export default TreeBeard;

src/themes/default.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ export default {
1212
fontSize: '14px'
1313
},
1414
node: {
15+
checkbox: {
16+
float: 'left',
17+
marginRight: '5px',
18+
marginTop: '6px'
19+
},
1520
base: {
1621
position: 'relative'
1722
},
1823
link: {
1924
cursor: 'pointer',
2025
position: 'relative',
2126
padding: '0px 5px',
22-
display: 'block'
27+
display: 'inline-block'
2328
},
2429
activeLink: {
2530
background: '#31363F'

0 commit comments

Comments
 (0)