Skip to content

Commit 6a25e99

Browse files
authored
fix: Delayed focused update when receiving data changes (#374)
Co-authored-by: Jesus Cabrera <jesus.cabrera@dowjones.com>
1 parent 02491b5 commit 6a25e99

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

src/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ class DropdownTreeSelect extends Component {
7575
rootPrefixId: this.clientId,
7676
searchPredicate,
7777
})
78-
// Restore focus-state
79-
const currentFocusNode = this.state.currentFocus && this.treeManager.getNodeById(this.state.currentFocus)
80-
if (currentFocusNode) {
81-
currentFocusNode._focused = true
82-
}
83-
this.setState(prevState => ({
84-
showDropdown: /initial|always/.test(showDropdown) || prevState.showDropdown === true,
85-
...this.treeManager.getTreeAndTags(),
86-
}))
78+
this.setState(prevState => {
79+
const currentFocusNode = prevState.currentFocus && this.treeManager.getNodeById(prevState.currentFocus)
80+
if (currentFocusNode) {
81+
currentFocusNode._focused = true
82+
}
83+
return {
84+
showDropdown: /initial|always/.test(showDropdown) || prevState.showDropdown === true,
85+
...this.treeManager.getTreeAndTags(),
86+
}
87+
})
8788
}
8889

8990
resetSearchState = () => {

src/index.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,44 @@ test('appends selected tags to aria-labelledby with text label', t => {
316316
t.deepEqual(wrapper.find('.dropdown-trigger').prop('aria-labelledby'), 'rdts_trigger rdts-0_tag')
317317
t.deepEqual(wrapper.find('.dropdown-trigger').prop('aria-label'), 'hello world')
318318
})
319+
320+
test('select correct focused node when using external state data container', t => {
321+
let data = [
322+
{
323+
label: 'All data',
324+
value: '0',
325+
checked: false,
326+
},
327+
{
328+
label: 'iWay',
329+
value: '1',
330+
checked: false,
331+
},
332+
]
333+
const wrapper = mount(<DropdownTreeSelect id={dropdownId} data={data} />)
334+
const nodeAllData = {
335+
_id: `${dropdownId}-0`,
336+
_depth: 0,
337+
label: 'All data',
338+
value: '0',
339+
children: undefined,
340+
actions: [action],
341+
}
342+
wrapper.instance().onCheckboxChange(nodeAllData._id, true)
343+
//simulate external change to the data props.
344+
wrapper.setProps({
345+
data: [
346+
{
347+
label: 'All data',
348+
value: '0',
349+
checked: true,
350+
},
351+
{
352+
label: 'iWay',
353+
value: '1',
354+
checked: false,
355+
},
356+
],
357+
})
358+
t.deepEqual(wrapper.state().currentFocus, nodeAllData._id)
359+
})

0 commit comments

Comments
 (0)