-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(Dropdown): blur dropdown on close() #628
Conversation
Cool, this just needs a test. it('closes on blur', () => { Named something like: it('blurs the Dropdown node on close', () => { |
Current coverage is 100% (diff: 100%)@@ master #628 diff @@
====================================
Files 131 131
Lines 2098 2099 +1
Methods 0 0
Messages 0 0
Branches 0 0
====================================
+ Hits 2098 2099 +1
Misses 0 0
Partials 0 0
|
I added a test but it fails. Could you take a look please and check if my test code is correct? It's the first test I've written so there's probably something I'm missing 😉 |
dropdownMenuIsClosed() | ||
|
||
const instance = wrapper.instance() | ||
sandbox.spy(instance._dropdown, 'blur') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So close, just need to start spying before closing the Dropdown. The spy will track all activity after it is initialized. I just moved the const instance
part above the dropdownMenuIs..
part:
wrapperMount(<Dropdown options={options} selection defaultOpen />)
const instance = wrapper.instance()
sandbox.spy(instance._dropdown, 'blur')
dropdownMenuIsOpen()
wrapper.simulate('click')
dropdownMenuIsClosed()
instance._dropdown.blur
.should.have.been.calledOnce()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your help, makes sense now 😄
I changed
instance._dropdown.blur
.should.have.been.calledOnce()
to
instance._dropdown.blur
.should.have.been.called()
because blur() was called twice. Is that alright?
Thanks for the update. I've marked this as needing review. I'll investigate why this is being called twice. It should only be called once. Pretty sure I know where the issue is. Will try to get this soon as I am able. |
Figured it out. The The Let's add a in componentDidUpdate() // opened / closed
if (!prevState.open && this.state.open) {
debug('dropdown opened')
- this.open()
document.addEventListener('keydown', this.closeOnEscape)
document.addEventListener('keydown', this.moveSelectionOnKeyDown)
document.addEventListener('keydown', this.selectItemOnEnter)
document.addEventListener('keydown', this.removeItemOnBackspace)
document.addEventListener('click', this.closeOnDocumentClick)
document.removeEventListener('keydown', this.openOnArrow)
document.removeEventListener('keydown', this.openOnSpace)
} else if (prevState.open && !this.state.open) {
debug('dropdown closed')
- this.close()
+ this.handleClose()
document.removeEventListener('keydown', this.closeOnEscape)
document.removeEventListener('keydown', this.moveSelectionOnKeyDown)
document.removeEventListener('keydown', this.selectItemOnEnter)
document.removeEventListener('keydown', this.removeItemOnBackspace)
document.removeEventListener('click', this.closeOnDocumentClick)
if (prevState.focus && this.state.focus) {
document.addEventListener('keydown', this.openOnArrow)
document.addEventListener('keydown', this.openOnSpace)
}
} add handleClose() close = () => {
debug('close()')
this.trySetState({ open: false })
- this._dropdown.blur()
}
+ handleClose = () => {
+ debug('handleClose()')
+ this._dropdown.blur()
+ } Then |
I updated the PR with the changes you suggested, works like a charm. |
Released in |
@levithomason @Chris-R3 from what I see, this change actually broke dropdown as menu item, reporting |
Indeed. A ref returns the DOM node for DOM components and a reference to the class/function for composite components. Because the Dropdown is rendering I've added #659 to address this issue. |
fixes #627