Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton authored Feb 12, 2018
1 parent 02226cc commit a361f5d
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,55 @@ tree.on('willCheckNode', function(Node) {});

#### Creating tree nodes with checkboxes

Sets the checked attribute in your rowRenderer:

```js
tree.on('willSelectNode', function(node) {
node.props.checked = !node.props.checked;
tree.updateNode(node);
const tag = require('html5-tag');

const checkbox = tag('input', {
type: 'checkbox',
   checked: node.state.checked,
'class': 'checkbox',
'data-indeterminate': node.state.indeterminate
});
```

Sets the checked attribute in your rowRenderer:
In your tree, add 'click', 'contentDidUpdate', 'clusterDidChange' event listeners as below:

```js
const tag = require('html5-tag');
// `indeterminate` doesn't have a DOM attribute equivalent, so you need to update DOM on the fly.
const updateIndeterminateState = (tree) => {
const checkboxes = tree.contentElement.querySelectorAll('input[type="checkbox"]');
for (let i = 0; i < checkboxes.length; ++i) {
const checkbox = checkboxes[i];
if (checkbox.hasAttribute('data-indeterminate')) {
checkbox.indeterminate = true;
} else {
checkbox.indeterminate = false;
}
}
};

const checkbox = tag('input', {
type: 'checkbox',
checked: node.props.checked
}, '');
tree.on('click', function(node) {
const currentNode = tree.getNodeFromPoint(event.x, event.y);
if (!currentNode) {
return;
}

if (event.target.className === 'checkbox') {
event.stopPropagation();
tree.checkNode(currentNode);
return;
}
});

tree.on('contentDidUpdate', () => {
updateIndeterminateState(tree);
});

tree.on('clusterDidChange', () => {
updateIndeterminateState(tree);
});
```

#### How to attach click event listeners to nodes?
Expand Down

0 comments on commit a361f5d

Please sign in to comment.