Skip to content

Commit

Permalink
fix(Dropdown): add event handlers when opened via defaultOpen (#2334)
Browse files Browse the repository at this point in the history
This attaches the event handlers to deal with closing the Dropdown such as onBlue and pressing the esc key.

fixes #2332
  • Loading branch information
sean-mckenna authored and levithomason committed Nov 25, 2017
1 parent da959f1 commit 4f66e7d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ export default class Dropdown extends Component {
this.setValue(value)
this.setSelectedIndex(value)

if (open) this.open()
if (open) {
this.open()
this.attachHandlersOnOpen()
}
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -479,14 +482,7 @@ export default class Dropdown extends Component {
// opened / closed
if (!prevState.open && this.state.open) {
debug('dropdown opened')
eventStack.sub('keydown', [
this.closeOnEscape,
this.moveSelectionOnKeyDown,
this.selectItemOnEnter,
this.removeItemOnBackspace,
])
eventStack.sub('click', this.closeOnDocumentClick)
eventStack.unsub('keydown', [this.openOnArrow, this.openOnSpace])
this.attachHandlersOnOpen()
this.scrollSelectedItemIntoView()
} else if (prevState.open && !this.state.open) {
debug('dropdown closed')
Expand Down Expand Up @@ -652,6 +648,17 @@ export default class Dropdown extends Component {
this.close()
}

attachHandlersOnOpen = () => {
eventStack.sub('keydown', [
this.closeOnEscape,
this.moveSelectionOnKeyDown,
this.selectItemOnEnter,
this.removeItemOnBackspace,
])
eventStack.sub('click', this.closeOnDocumentClick)
eventStack.unsub('keydown', [this.openOnArrow, this.openOnSpace])
}

// ----------------------------------------
// Component Event Handlers
// ----------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ describe('Dropdown', () => {
.should.have.been.calledOnce()
})

it('blurs the Dropdown node on close by clicking outside component', () => {
wrapperMount(<Dropdown options={options} selection defaultOpen />)

const instance = wrapper.instance()
sandbox.spy(instance.ref, 'blur')

dropdownMenuIsOpen()
document.body.click()
dropdownMenuIsClosed()

instance.ref.blur
.should.have.been.calledOnce()
})

it('does not close on click when search is true and options are empty', () => {
wrapperMount(<Dropdown options={{}} search selection defaultOpen />)

Expand Down

0 comments on commit 4f66e7d

Please sign in to comment.