Skip to content

Commit c307dc0

Browse files
committed
Implements nodeData param for onClick hook; closes #21
1 parent 4600275 commit c307dc0

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class MyComponent extends React.Component {
8080
| Property | Type | Options | Required? | Default | Description |
8181
|:---------------------|:----------------|:--------------------------------------|:----------|:--------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
8282
| `data` | `array` | | required | `undefined` | Single-element array containing hierarchical object (see `myTreeData` above). <br /> Contains (at least) `name` and `parent` keys. |
83-
| `onClick` | `func` | | | `undefined` | Callback function to be called whenever a node is clicked. |
83+
| `onClick` | `func` | | | `(nodeData) => {}` | Callback function to be called whenever a node is clicked. <br /><br /> The clicked node's data object is passed to the callback function as the first parameter. |
8484
| `orientation` | `string` (enum) | `horizontal` `vertical` | | `horizontal` | `horizontal` - Tree expands left-to-right. <br /><br /> `vertical` - Tree expands top-to-bottom. |
8585
| `translate` | `object` | | | `{x: 0, y: 0}` | Translates the graph along the x/y axis by the specified amount of pixels (avoids the graph being stuck in the top left canvas corner). |
8686
| `pathFunc` | `string` (enum) | `diagonal` `elbow` | | `diagonal` | `diagonal` - Renders smooth, curved edges between parent-child nodes. <br /><br /> `elbow` - Renders sharp edges at right angles between parent-child nodes. |

src/Tree/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export default class Tree extends React.Component {
176176
* handleNodeToggle - Finds the node matching `nodeId` and
177177
* expands/collapses it, depending on the current state of
178178
* its `_collapsed` property.
179+
* Passes a copy of the target node to the top-level onClick handler.
179180
*
180181
* @param {string} nodeId A node object's `id` field.
181182
*
@@ -189,7 +190,9 @@ export default class Tree extends React.Component {
189190
targetNode._collapsed
190191
? this.expandNode(targetNode)
191192
: this.collapseNode(targetNode);
192-
this.setState({ data }, this.props.onClick);
193+
this.setState({ data }, () => {
194+
this.props.onClick(clone(targetNode));
195+
});
193196
}
194197
}
195198

@@ -291,7 +294,7 @@ export default class Tree extends React.Component {
291294
}
292295

293296
Tree.defaultProps = {
294-
onClick: undefined,
297+
onClick: () => {},
295298
orientation: 'horizontal',
296299
translate: { x: 0, y: 0 },
297300
pathFunc: 'diagonal',

src/Tree/tests/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,19 @@ describe('<Tree />', () => {
209209

210210
expect(onClickSpy).toHaveBeenCalledTimes(1);
211211
});
212+
213+
214+
it('passes the clicked node\'s data to the onClick callback', () => {
215+
const onClickSpy = jest.fn();
216+
const renderedComponent = mount(
217+
<Tree
218+
data={mockData}
219+
onClick={onClickSpy}
220+
/>
221+
);
222+
223+
renderedComponent.find(Node).first().simulate('click');
224+
225+
expect(onClickSpy).toHaveBeenCalledWith(renderedComponent.find(Node).first().prop('nodeData'));
226+
});
212227
});

0 commit comments

Comments
 (0)