Skip to content

Commit

Permalink
fix(Dropdown): retain focus on the input if the Dropdown receives a c…
Browse files Browse the repository at this point in the history
…lick (#3422)

* fix(Dropdown): retain focus on the input if the Dropdown root element receives a click.

* add a test case

* Update Dropdown-test.js

* Update Dropdown.js

* Update Dropdown.js
  • Loading branch information
jongsue authored and layershifter committed Feb 11, 2019
1 parent a4bf431 commit ab582ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,10 @@ export default class Dropdown extends Component {
e.stopPropagation()

if (!search) return this.toggle(e)
if (open) return
if (open) {
if (this.searchRef) this.searchRef.focus()
return
}
if (searchQuery.length >= minCharacters || minCharacters === 1) {
this.open(e)
return
Expand Down
11 changes: 11 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,17 @@ describe('Dropdown', () => {
)
})

it('sets focus to the search input on click Dropdown when is opened', () => {
wrapperMount(<Dropdown open options={options} multiple selection search />)
wrapper.simulate('click')

const activeElement = document.activeElement
const searchIsFocused = activeElement === document.querySelector('input.search')
searchIsFocused.should.be.true(
`Expected "input.search" to be the active element but found ${activeElement} instead.`,
)
})

it('clears the search query when an item is selected', () => {
// search for random item
const searchQuery = _.sample(options).text
Expand Down

0 comments on commit ab582ed

Please sign in to comment.