Skip to content

Commit 420a60c

Browse files
committed
Add showNodeIcon property
Resolves #30.
1 parent 05f1878 commit 420a60c

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## (TBD)
4+
5+
### New Features
6+
7+
* [#30]: Add `showNodeIcon` property to optionally remove node icons
8+
39
## [v0.5.2](https://github.com/jakezatecky/react-checkbox-tree/compare/v0.5.1...v0.5.2) (2017-05-03)
410

511
### Bug Fixes

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ class Widget extends React.Component {
9393
| `checked` | array | An array of checked node values. | `[]` |
9494
| `expanded` | array | An array of expanded node values. | `[]` |
9595
| `onCheck` | function | onCheck handler: `function(checked) {}` | `() => {}` |
96-
| `onExpand` | function | onExpand handler: `function(expanded) {}` | `() => {}` |
96+
| `onExpand` | function | onExpand handler: `function(expanded) {}` | `() => {}` |
9797
| `name` | string | Optional name for the hidden `<input>` element. | `undefined` |
9898
| `nameAsArray` | bool | If true, the hidden `<input>` will encode its values as an array rather than a joined string. | `false` |
9999
| `optimisticToggle` | bool | If true, toggling a partially-checked node will select all children. If false, it will deselect. | `true` |
100+
| `showNodeIcon` | bool | If true, each node will show a parent or leaf icon. | `true` |
100101

101102
#### Node Properties
102103

src/js/CheckboxTree.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ class CheckboxTree extends React.Component {
99
static propTypes = {
1010
nodes: PropTypes.arrayOf(nodeShape).isRequired,
1111

12-
checked: PropTypes.arrayOf(React.PropTypes.string),
13-
expanded: PropTypes.arrayOf(React.PropTypes.string),
12+
checked: PropTypes.arrayOf(PropTypes.string),
13+
expanded: PropTypes.arrayOf(PropTypes.string),
1414
name: PropTypes.string,
1515
nameAsArray: PropTypes.bool,
1616
optimisticToggle: PropTypes.bool,
17+
showNodeIcon: PropTypes.bool,
1718
onCheck: PropTypes.func,
1819
onExpand: PropTypes.func,
1920
};
@@ -25,6 +26,7 @@ class CheckboxTree extends React.Component {
2526
nameAsArray: false,
2627
nodes: [],
2728
optimisticToggle: true,
29+
showNodeIcon: true,
2830
onCheck: () => {},
2931
onExpand: () => {},
3032
};
@@ -146,6 +148,7 @@ class CheckboxTree extends React.Component {
146148
label={node.label}
147149
optimisticToggle={this.props.optimisticToggle}
148150
rawChildren={node.children}
151+
showNodeIcon={this.props.showNodeIcon}
149152
treeId={this.id}
150153
value={node.value}
151154
onCheck={this.onCheck}

src/js/TreeNode.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TreeNode extends React.Component {
1010
expanded: PropTypes.bool.isRequired,
1111
label: PropTypes.string.isRequired,
1212
optimisticToggle: PropTypes.bool.isRequired,
13+
showNodeIcon: PropTypes.bool.isRequired,
1314
treeId: PropTypes.string.isRequired,
1415
value: PropTypes.string.isRequired,
1516
onCheck: PropTypes.func.isRequired,
@@ -64,14 +65,6 @@ class TreeNode extends React.Component {
6465
return this.props.rawChildren !== null;
6566
}
6667

67-
renderCollapseIcon() {
68-
if (!this.props.expanded) {
69-
return <span className="rct-icon rct-icon-expand-close" />;
70-
}
71-
72-
return <span className="rct-icon rct-icon-expand-open" />;
73-
}
74-
7568
renderCollapseButton() {
7669
if (!this.hasChildren()) {
7770
return (
@@ -94,6 +87,14 @@ class TreeNode extends React.Component {
9487
);
9588
}
9689

90+
renderCollapseIcon() {
91+
if (!this.props.expanded) {
92+
return <span className="rct-icon rct-icon-expand-close" />;
93+
}
94+
95+
return <span className="rct-icon rct-icon-expand-open" />;
96+
}
97+
9798
renderCheckboxIcon() {
9899
if (this.props.checked === 0) {
99100
return <span className="rct-icon rct-icon-uncheck" />;
@@ -131,7 +132,7 @@ class TreeNode extends React.Component {
131132
}
132133

133134
render() {
134-
const { checked, treeId, label, value } = this.props;
135+
const { checked, treeId, label, showNodeIcon, value } = this.props;
135136
const inputId = `${treeId}-${value}`;
136137
const nodeClass = classNames({
137138
'rct-node': true,
@@ -148,9 +149,11 @@ class TreeNode extends React.Component {
148149
<span className="rct-checkbox">
149150
{this.renderCheckboxIcon()}
150151
</span>
151-
<span className="rct-node-icon">
152-
{this.renderNodeIcon()}
153-
</span>
152+
{showNodeIcon ? (
153+
<span className="rct-node-icon">
154+
{this.renderNodeIcon()}
155+
</span>
156+
) : null}
154157
<span className="rct-title">
155158
{label}
156159
</span>

test/TreeNode.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const baseProps = {
99
expanded: false,
1010
label: 'Jupiter',
1111
optimisticToggle: true,
12+
showNodeIcon: true,
1213
treeId: 'id',
1314
value: 'jupiter',
1415
onCheck: () => {},
@@ -139,6 +140,32 @@ describe('<TreeNode />', () => {
139140
});
140141
});
141142

143+
describe('showNodeIcon', () => {
144+
it('should render the node icon when true', () => {
145+
const wrapper = shallow(
146+
<TreeNode {...baseProps} />,
147+
);
148+
149+
assert.isTrue(wrapper.contains(
150+
<span className="rct-node-icon">
151+
<span className="rct-icon rct-icon-leaf" />
152+
</span>,
153+
));
154+
});
155+
156+
it('should not render the node icon when false', () => {
157+
const wrapper = shallow(
158+
<TreeNode {...baseProps} showNodeIcon={false} />,
159+
);
160+
161+
assert.isFalse(wrapper.contains(
162+
<span className="rct-node-icon">
163+
<span className="rct-icon rct-icon-leaf" />
164+
</span>,
165+
));
166+
});
167+
});
168+
142169
describe('onCheck', () => {
143170
it('should pass the current node\'s value', () => {
144171
let actual = {};

0 commit comments

Comments
 (0)