Skip to content

Commit

Permalink
fix(Dropdown): search does not preserve value (#3584)
Browse files Browse the repository at this point in the history
* Fix.

* add UT to cover case
  • Loading branch information
mihai-dinculescu authored and layershifter committed Jun 9, 2019
1 parent 5ae8576 commit f3abc92
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ export default class Dropdown extends Component {
this.handleChange(e, newValue)
}

this.clearSearchQuery()
this.clearSearchQuery(value)
this.closeOnChange(e)

// Heads up! This event handler should be called after `onChange`
Expand Down Expand Up @@ -774,9 +774,12 @@ export default class Dropdown extends Component {

// There are times when we need to calculate the options based on a value
// that hasn't yet been persisted to state.
getMenuOptions = (value = this.state.value, options = this.props.options) => {
getMenuOptions = (
value = this.state.value,
options = this.props.options,
searchQuery = this.state.searchQuery,
) => {
const { additionLabel, additionPosition, allowAdditions, deburr, multiple, search } = this.props
const { searchQuery } = this.state

let filteredOptions = options

Expand Down Expand Up @@ -890,20 +893,29 @@ export default class Dropdown extends Component {
// Setters
// ----------------------------------------

clearSearchQuery = () => {
clearSearchQuery = (value) => {
debug('clearSearchQuery()')

const { searchQuery } = this.state
if (searchQuery === undefined || searchQuery === '') return

this.trySetState({ searchQuery: '' })
this.setSelectedIndex(value, undefined, '')
}

setValue = (value) => {
debug('setValue()', value)
this.trySetState({ value })
}

setSelectedIndex = (value = this.state.value, optionsProps = this.props.options) => {
setSelectedIndex = (
value = this.state.value,
optionsProps = this.props.options,
searchQuery = this.state.searchQuery,
) => {
const { multiple } = this.props
const { selectedIndex } = this.state
const options = this.getMenuOptions(value, optionsProps)
const options = this.getMenuOptions(value, optionsProps, searchQuery)
const enabledIndicies = this.getEnabledIndices(options)

let newSelectedIndex
Expand Down
20 changes: 20 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,26 @@ describe('Dropdown', () => {
})
})

describe('selectedIndex', () => {
it('sets "selectedIndex" when an item was selected', () => {
const option = _.last(options)

wrapperMount(<Dropdown options={options} search selection />)
const input = wrapper.find('input.search')

// open, simulate search and select option
wrapper.simulate('click')
input.simulate('change', { target: { value: option.text } })
domEvent.keyDown(document, { key: 'Enter' })

wrapper.should.have.state('selectedIndex', 4)

// open again
wrapper.simulate('click')
wrapper.should.have.state('selectedIndex', 4)
})
})

describe('isMouseDown', () => {
it('tracks when the mouse is down', () => {
wrapperMount(<Dropdown />).simulate('mousedown')
Expand Down

0 comments on commit f3abc92

Please sign in to comment.