Skip to content

Commit 764a002

Browse files
ellingemrchief
authored andcommitted
fix: Updated onAction to bubble up custom props (#230)
BREAKING CHANGE: Action Changes - The option to pass a local `onAction` handler on a node is now removed. Use the **global** `onAction` event instead. ```jsx <DropdownTreeSelect onAction={onAction} ... /> ``` - `onAction` signature is now consistent with signature for other event handlers such `onChange` and `onNodeToggle` ```js // before onAction = ({ action, id }) => { console.log(action, id) } // after onAction = (node, action) => { console.log(action, node.id) } ``` - Any custom props passed to `node` or `action` is accessible in the event properties. This will make it easier to add generic custom logic based on your custom data/properties which can be used instead of separate handlers. For example: ```js // node with action and custom props { id: 'mynode', propA: 'hello', propB: true, actions: [ { id: 'myaction', propX: {...}, propY: 12 } ] } onAction = (node, action) => { console.log(node.propA, node.propB, action.propX, action.propY) // prints hello true {...} 12 } ```
1 parent fb5beb3 commit 764a002

File tree

16 files changed

+70
-30
lines changed

16 files changed

+70
-30
lines changed

.github/prlint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"title": [
33
{
4-
"pattern": "^(feat|fix|docs|style|refactor|perf|test|chore|BREAKING CHANGE):\\s",
4+
"pattern": "^(feat|fix|docs|style|refactor|perf|test|chore):\\s",
55
"flags": ["i"],
66
"message": "PR title doesn't match conventional commit message specs. See https://conventionalcommits.org/"
77
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ const data = {
158158
const onChange = (currentNode, selectedNodes) => {
159159
console.log('onChange::', currentNode, selectedNodes)
160160
}
161-
const onAction = ({ action, id }) => {
162-
console.log(`onAction:: [${action}]`, id)
161+
const onAction = (node, action) => {
162+
console.log('onAction::', action, node)
163163
}
164164
const onNodeToggle = currentNode => {
165165
console.log('onNodeToggle::', currentNode)
@@ -228,8 +228,8 @@ Type: `function`
228228
Fires when a action is triggered. Example:
229229

230230
```jsx
231-
function onAction({ action, id }) {
232-
console.log(`onAction:: [${action}]`, id)
231+
function onAction(node, action) {
232+
console.log('onAction::', action, node)
233233
}
234234

235235
return <DropdownTreeSelect data={data} onAction={onAction} />

__snapshots__/src/index.test.js.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ Generated by [AVA](https://ava.li).
4343
],
4444
_depth: 0,
4545
_id: 'rdts-0',
46+
actions: [
47+
{
48+
className: 'fa fa-ban',
49+
id: 'NOT',
50+
title: 'NOT',
51+
},
52+
],
4653
children: undefined,
4754
label: 'item1',
4855
value: 'value1',
@@ -193,6 +200,13 @@ Generated by [AVA](https://ava.li).
193200
data={
194201
[
195202
{
203+
actions: [
204+
{
205+
className: 'fa fa-ban',
206+
id: 'NOT',
207+
title: 'NOT',
208+
},
209+
],
196210
children: [
197211
{
198212
children: [
@@ -322,6 +336,13 @@ Generated by [AVA](https://ava.li).
322336
data={
323337
[
324338
{
339+
actions: [
340+
{
341+
className: 'fa fa-ban',
342+
id: 'NOT',
343+
title: 'NOT',
344+
},
345+
],
325346
children: [
326347
{
327348
children: [
@@ -479,6 +500,13 @@ Generated by [AVA](https://ava.li).
479500
],
480501
_depth: 0,
481502
_id: 'rdts-0',
503+
actions: [
504+
{
505+
className: 'fa fa-ban',
506+
id: 'NOT',
507+
title: 'NOT',
508+
},
509+
],
482510
children: undefined,
483511
label: 'item1',
484512
value: 'value1',

__snapshots__/src/index.test.js.snap

157 Bytes
Binary file not shown.

__snapshots__/src/tree-node/actions.test.js.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ Generated by [AVA](https://ava.li).
1111
<Action
1212
actionData={
1313
{
14-
action: 'action-0',
15-
id: 'snapshot',
14+
action: {
15+
className: 'cn0-0-0',
16+
id: 'action-0',
17+
junk: '1',
18+
text: 'hello',
19+
title: 'action',
20+
},
21+
nodeId: 'snapshot',
1622
}
1723
}
1824
className="cn0-0-0"
46 Bytes
Binary file not shown.

docs/src/stories/BigData/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import bigData from './big-data.json'
88
const onChange = (curNode, selectedNodes) => {
99
console.log('onChange::', curNode, selectedNodes)
1010
}
11-
const onAction = ({ action, id }) => {
12-
console.log(`onAction:: [${action}]`, id)
11+
const onAction = (node, action) => {
12+
console.log('onAction::', action, node)
1313
}
1414
const onNodeToggle = curNode => {
1515
console.log('onNodeToggle::', curNode)

docs/src/stories/DefaultValues/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import data from './data.json'
99
const onChange = (curNode, selectedNodes) => {
1010
console.log('onChange::', curNode, selectedNodes)
1111
}
12-
const onAction = ({ action, id }) => {
13-
console.log(`onAction:: [${action}]`, id)
12+
const onAction = (node, action) => {
13+
console.log('onAction::', action, node)
1414
}
1515
const onNodeToggle = curNode => {
1616
console.log('onNodeToggle::', curNode)

docs/src/stories/Options/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class WithOptions extends PureComponent {
2626
onChange = (curNode, selectedNodes) => {
2727
console.log('onChange::', curNode, selectedNodes)
2828
}
29-
onAction = ({ action, id }) => {
30-
console.log(`onAction:: [${action}]`, id)
29+
onAction = (node, action) => {
30+
console.log('onAction::', action, node)
3131
}
3232
onNodeToggle = curNode => {
3333
console.log('onNodeToggle::', curNode)

docs/src/stories/Simple/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import data from './data.json'
88
const onChange = (curNode, selectedNodes) => {
99
console.log('onChange::', curNode, selectedNodes)
1010
}
11-
const onAction = ({ action, id }) => {
12-
console.log(`onAction:: [${action}]`, id)
11+
const onAction = (node, action) => {
12+
console.log('onAction::', action, node)
1313
}
1414
const onNodeToggle = curNode => {
1515
console.log('onNodeToggle::', curNode)

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ class DropdownTreeSelect extends Component {
198198
this.props.onChange(this.treeManager.getNodeById(id), tags)
199199
}
200200

201-
onAction = (actionId, nodeId) => {
202-
typeof this.props.onAction === 'function' && this.props.onAction(actionId, this.treeManager.getNodeById(nodeId))
201+
onAction = (nodeId, action) => {
202+
this.props.onAction(this.treeManager.getNodeById(nodeId), action)
203203
}
204204

205205
onInputFocus = () => {

src/index.test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ import { clientIdGenerator } from './utils'
88

99
const dropdownId = 'rdts'
1010

11+
const action = {
12+
id: 'NOT',
13+
title: 'NOT',
14+
className: 'fa fa-ban',
15+
}
16+
1117
const node0 = {
1218
_id: `${dropdownId}-0`,
1319
_children: [`${dropdownId}-0-0`, `${dropdownId}-0-1`],
1420
_depth: 0,
1521
label: 'item1',
1622
value: 'value1',
1723
children: undefined,
24+
actions: [action],
1825
}
1926

2027
test.beforeEach(t => {
@@ -30,6 +37,7 @@ test.beforeEach(t => {
3037
},
3138
{ label: 'item1-2', value: 'value1-2' },
3239
],
40+
actions: [action],
3341
},
3442
{
3543
label: 'item2',
@@ -88,9 +96,9 @@ test('keeps dropdown open for showDropdownAlways', t => {
8896
test('notifies on action', t => {
8997
const handler = spy()
9098
const { tree } = t.context
91-
const wrapper = shallow(<DropdownTreeSelect id={dropdownId} data={tree} onAction={handler} />)
92-
wrapper.instance().onAction('0', node0._id)
93-
t.true(handler.calledWithExactly('0', node0))
99+
const wrapper = mount(<DropdownTreeSelect id={dropdownId} data={tree} onAction={handler} showDropdown />)
100+
wrapper.find('i.fa-ban').simulate('click')
101+
t.true(handler.calledWithExactly(node0, action))
94102
})
95103

96104
test('notifies on node toggle', t => {

src/tree-node/action.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class Action extends PureComponent {
1616
}
1717

1818
handleClick = () => {
19-
this.props.onAction(this.props.actionData)
19+
const { onAction, actionData } = this.props
20+
if (onAction) {
21+
onAction(actionData.nodeId, actionData.action)
22+
}
2023
}
2124

2225
render() {

src/tree-node/action.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ test('notifies clicks if handler is passed', t => {
2525
title: 'action',
2626
className: 'cn0-0-0',
2727
onAction: onClick,
28-
actionData: { id: 'actionA' },
28+
actionData: { action: { id: 'actionA' }, nodeId: 'nodeId' },
2929
}
3030

3131
const wrapper = shallow(<Action {...props} />)
3232
wrapper.find('.cn0-0-0').simulate('click')
3333
t.true(onClick.calledOnce)
34-
t.true(onClick.calledWith(match({ id: 'actionA' })))
34+
t.true(onClick.calledWith(match('nodeId', { id: 'actionA' })))
3535
})

src/tree-node/actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Actions extends PureComponent {
1717

1818
return actions.map((a, idx) => {
1919
const actionId = a.id || `action-${idx}`
20-
return <Action key={actionId} {...rest} {...a} actionData={{ action: actionId, id }} />
20+
return <Action key={actionId} {...rest} {...a} actionData={{ action: { ...a, id: actionId }, nodeId: id }} />
2121
})
2222
}
2323
}

types/react-dropdown-tree-select.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ declare module "react-dropdown-tree-select" {
4040
*/
4141
onChange?: (currentNode: TreeNode, selectedNodes: TreeNode[]) => void;
4242
/** Fired on click of the action */
43-
onAction?: (event: ActionEvent) => void;
43+
onAction?: (currentNode: TreeNode, currentAction: NodeAction) => void;
4444
/** Fires when a node is expanded or collapsed.
4545
* Calls the handler with the current node object
4646
*/
@@ -148,11 +148,6 @@ declare module "react-dropdown-tree-select" {
148148
[property: string]: any;
149149
}
150150

151-
export interface ActionEvent {
152-
action: string;
153-
id: string;
154-
}
155-
156151
export interface NodeDataSet {
157152
[property: string]: any;
158153
}

0 commit comments

Comments
 (0)