Skip to content
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): more logic for clearing search #1956

Merged
merged 3 commits into from
Aug 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,17 +591,21 @@ export default class Dropdown extends Component {

selectItemOnEnter = (e) => {
debug('selectItemOnEnter()', keyboardKey.getName(e))
const { multiple, search } = this.props
const { multiple, onAddItem, search } = this.props

if (keyboardKey.getCode(e) !== keyboardKey.Enter) return
e.preventDefault()

if (search && _.isEmpty(this.getMenuOptions())) return
const optionSize = _.size(this.getMenuOptions())
if (search && optionSize === 0) return

const item = this.getSelectedItem()
const isAdditionItem = onAddItem && item['data-additional']

this.makeSelectedItemActive(e)
this.closeOnChange(e)

if (!multiple) this.clearSearchQuery()
if (!multiple || isAdditionItem || optionSize === 1) this.clearSearchQuery()
if (search && this.searchRef) this.searchRef.focus()
}

Expand Down Expand Up @@ -695,14 +699,17 @@ export default class Dropdown extends Component {
if (item.disabled) return

// notify the onAddItem prop if this is a new value
if (onAddItem && item['data-additional']) onAddItem(e, { ...this.props, value })
const isAdditionItem = onAddItem && item['data-additional']
if (isAdditionItem) onAddItem(e, { ...this.props, value })

const newValue = multiple ? _.union(this.state.value, [value]) : value

// notify the onChange prop that the user is trying to change value
this.setValue(newValue)
this.setSelectedIndex(value)
if (!multiple) this.clearSearchQuery()

const optionSize = _.size(this.getMenuOptions())
if (!multiple || isAdditionItem || optionSize === 1) this.clearSearchQuery()

this.handleChange(e, newValue)
this.closeOnChange(e)
Expand Down
13 changes: 12 additions & 1 deletion test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ describe('Dropdown', () => {
domEvent.keyDown(document, { key: 'Enter' })
dropdownMenuIsClosed()
})

it('keeps value of the searchQuery when selection is changed', () => {
wrapperMount(<Dropdown options={options} selection search />)

Expand Down Expand Up @@ -2258,6 +2257,18 @@ describe('Dropdown', () => {
spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch({}, { value: 'boo' })
})

it('clears value of the searchQuery when selection is only option', () => {
const search = wrapperMount(
<Dropdown options={customOptions} selection search allowAdditions />
)
.find('input.search')

search.simulate('change', { target: { value: 'boo' } })
domEvent.keyDown(document, { key: 'Enter' })

wrapper.should.have.state('searchQuery', '')
})
})

describe('header', () => {
Expand Down