Skip to content

Commit

Permalink
fix: Remove resetting of dropdown on init props (#265) 馃悰
Browse files Browse the repository at this point in the history
Keeps dropdown open if updating with new props. Only updates/sets state open if new props cause the dropdown to show (a.k.a `initial`/`always`)

#253 introduced resetting of state for shopDropdown on new props:
  • Loading branch information
ellinge authored and mrchief committed Jun 16, 2019
1 parent 881f74c commit 866af6e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ class DropdownTreeSelect extends Component {
if (currentFocusNode) {
currentFocusNode._focused = true
}
this.setState({
showDropdown: /initial|always/.test(showDropdown) || false,
this.setState(prevState => ({
showDropdown: /initial|always/.test(showDropdown) || prevState.showDropdown === true,
...this.treeManager.getTreeAndTags(),
})
}))
}

resetSearchState = () => {
Expand Down
22 changes: 22 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ test('toggles dropdown', t => {
t.false(wrapper.state().showDropdown)
})

test('keeps dropdown open on props update', t => {
const { tree } = t.context
const wrapper = mount(<DropdownTreeSelect data={tree} />)
wrapper.instance().handleClick()
t.true(wrapper.state().showDropdown)
wrapper.setProps({ data: tree })
t.true(wrapper.state().showDropdown)
})

test('opens dropdown on props update with show intention', t => {
const { tree } = t.context
const wrapper = mount(<DropdownTreeSelect data={tree} />)
t.false(wrapper.state().showDropdown)
wrapper.setProps({ data: tree, showDropdown: 'initial' })
t.true(wrapper.state().showDropdown)
wrapper.instance().handleClick()
t.false(wrapper.state().showDropdown)
wrapper.setProps({ data: tree, showDropdown: 'always' })
wrapper.instance().handleClick()
t.true(wrapper.state().showDropdown)
})

test('sets unique ids on dropdowns', t => {
const { tree } = t.context
clientIdGenerator.reset()
Expand Down

0 comments on commit 866af6e

Please sign in to comment.