Skip to content

Commit 0e19b46

Browse files
committed
Add title node property and showNodeTitle tree property
Resolves #103.
1 parent 764fdeb commit 0e19b46

File tree

7 files changed

+67
-3
lines changed

7 files changed

+67
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New Features
66

77
* [#102]: Add `icons` property to allow specification of icon components
8+
* [#103]: Add `title` node property and `showNodeTitle` tree property
89

910
## [v1.2.4](https://github.com/jakezatecky/react-checkbox-tree/compare/v1.2.3...v1.2.4) (2018-08-29)
1011

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ All node objects **must** have a unique `value`. This value is serialized into t
105105
| `onlyLeafCheckboxes` | bool | If true, checkboxes will only be shown for leaf nodes. | `false` |
106106
| `optimisticToggle` | bool | If true, toggling a partially-checked node will select all children. If false, it will deselect. | `true` |
107107
| `showNodeIcon` | bool | If true, each node will show a parent or leaf icon. | `true` |
108+
| `showNodeTitle` | bool | If true, the `label` of each node will become the `title` of the resulting DOM node. Overridden by `node.title`. | `false` |
108109
| `onCheck` | function | onCheck handler: `function(checked) {}` | `() => {}` |
109110
| `onClick` | function | onClick handler: `function(clicked) {}`. If set, it will be called when clicked on a node label. | `() => {}` |
110111
| `onExpand` | function | onExpand handler: `function(expanded) {}` | `() => {}` |
@@ -122,3 +123,4 @@ Individual nodes within the `nodes` property can have the following structure:
122123
| `disabled` | bool | Whether the node should be disabled. | `false` |
123124
| `icon` | mixed | A custom icon for the node. | `null` |
124125
| `showCheckbox` | bool | Whether the node should show a checkbox. | `true` |
126+
| `title` | string | A custom `title` attribute for the node. | `null` |

src/js/CheckboxTree.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class CheckboxTree extends React.Component {
2626
onlyLeafCheckboxes: PropTypes.bool,
2727
optimisticToggle: PropTypes.bool,
2828
showNodeIcon: PropTypes.bool,
29+
showNodeTitle: PropTypes.bool,
2930
onCheck: PropTypes.func,
3031
onClick: PropTypes.func,
3132
onExpand: PropTypes.func,
@@ -54,6 +55,7 @@ class CheckboxTree extends React.Component {
5455
onlyLeafCheckboxes: false,
5556
optimisticToggle: true,
5657
showNodeIcon: true,
58+
showNodeTitle: false,
5759
onCheck: () => {},
5860
onClick: () => {},
5961
onExpand: () => {},
@@ -229,6 +231,7 @@ class CheckboxTree extends React.Component {
229231
noCascade,
230232
onlyLeafCheckboxes,
231233
optimisticToggle,
234+
showNodeTitle,
232235
showNodeIcon,
233236
onClick,
234237
} = this.props;
@@ -258,6 +261,7 @@ class CheckboxTree extends React.Component {
258261
rawChildren={node.children}
259262
showCheckbox={showCheckbox}
260263
showNodeIcon={showNodeIcon}
264+
title={showNodeTitle ? node.title || node.label : node.title}
261265
treeId={this.id}
262266
value={node.value}
263267
onCheck={this.onCheck}

src/js/TreeNode.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TreeNode extends React.Component {
3030
icon: PropTypes.node,
3131
rawChildren: PropTypes.arrayOf(nodeShape),
3232
showCheckbox: PropTypes.bool,
33+
title: PropTypes.string,
3334
onClick: PropTypes.func,
3435
};
3536

@@ -40,6 +41,7 @@ class TreeNode extends React.Component {
4041
icon: null,
4142
rawChildren: null,
4243
showCheckbox: true,
44+
title: null,
4345
onClick: () => {},
4446
};
4547

@@ -174,12 +176,12 @@ class TreeNode extends React.Component {
174176
}
175177

176178
renderBareLabel(children) {
177-
const { onClick } = this.props;
179+
const { onClick, title } = this.props;
178180

179181
const clickable = onClick.toString() !== TreeNode.defaultProps.onClick.toString();
180182

181183
return (
182-
<span className="rct-bare-label">
184+
<span className="rct-bare-label" title={title}>
183185
{clickable ? (
184186
<span
185187
className="rct-node-clickable"
@@ -200,6 +202,7 @@ class TreeNode extends React.Component {
200202
checked,
201203
disabled,
202204
label,
205+
title,
203206
treeId,
204207
value,
205208
onClick,
@@ -209,7 +212,7 @@ class TreeNode extends React.Component {
209212
const inputId = `${treeId}-${String(value).split(' ').join('_')}`;
210213

211214
const render = [(
212-
<label key={0} htmlFor={inputId}>
215+
<label key={0} htmlFor={inputId} title={title}>
213216
<NativeCheckbox
214217
checked={checked === 1}
215218
disabled={disabled}

src/js/nodeShape.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const nodeShape = {
99

1010
icon: PropTypes.node,
1111
showCheckbox: PropTypes.bool,
12+
title: PropTypes.string,
1213
};
1314

1415
const nodeShapeWithChildren = PropTypes.oneOfType([

test/CheckboxTree.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,41 @@ describe('<CheckboxTree />', () => {
297297
});
298298
});
299299

300+
describe('showNodeTitle', () => {
301+
it('should add `title` properties to a TreeNode from the `label` property when set', () => {
302+
const wrapper = shallow(
303+
<CheckboxTree
304+
nodes={[
305+
{
306+
value: 'jupiter',
307+
label: 'Jupiter',
308+
},
309+
]}
310+
showNodeTitle
311+
/>,
312+
);
313+
314+
assert.equal('Jupiter', wrapper.find('TreeNode').prop('title'));
315+
});
316+
317+
it('should prioritize the node `title` over the `label', () => {
318+
const wrapper = shallow(
319+
<CheckboxTree
320+
nodes={[
321+
{
322+
value: 'jupiter',
323+
label: 'Jupiter',
324+
title: 'That Big Failed Star',
325+
},
326+
]}
327+
showNodeTitle
328+
/>,
329+
);
330+
331+
assert.equal('That Big Failed Star', wrapper.find('TreeNode').prop('title'));
332+
});
333+
});
334+
300335
describe('onCheck', () => {
301336
it('should pass the node toggled as the second parameter', () => {
302337
let actualNode = null;

test/TreeNode.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,24 @@ describe('<TreeNode />', () => {
266266
});
267267
});
268268

269+
describe('title', () => {
270+
it('should add the `title` property to the label when set', () => {
271+
const wrapper = shallow(
272+
<TreeNode {...baseProps} title="Some extra text" />,
273+
);
274+
275+
assert.equal('Some extra text', wrapper.find('label').prop('title'));
276+
});
277+
278+
it('should add the `title` property to the bare label when set on a checkbox-less node', () => {
279+
const wrapper = shallow(
280+
<TreeNode {...baseProps} showCheckbox={false} title="Some extra text" />,
281+
);
282+
283+
assert.equal('Some extra text', wrapper.find('.rct-bare-label').prop('title'));
284+
});
285+
});
286+
269287
describe('onCheck', () => {
270288
it('should pass the current node\'s value', () => {
271289
let actual = {};

0 commit comments

Comments
 (0)