Skip to content

Commit

Permalink
fix: allow typeAheadSelect only when option is selectable (#1529)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagalbot authored Feb 18, 2022
1 parent 0c72638 commit 115f24a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/mixins/typeAheadPointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default {
typeAheadSelect() {
const typeAheadOption = this.filteredOptions[this.typeAheadPointer]

if (typeAheadOption) {
if (typeAheadOption && this.selectable(typeAheadOption)) {
this.select(typeAheadOption)
}
},
Expand Down
10 changes: 8 additions & 2 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import Vue from 'vue'
* @param Wrapper {Wrapper<Vue>}
* @param searchText
*/
export const searchSubmit = (Wrapper, searchText = false) => {
export const searchSubmit = async (Wrapper, searchText = false) => {
const search = Wrapper.findComponent({ ref: 'search' })
await search.trigger('focus')

if (searchText) {
Wrapper.vm.search = searchText
await Wrapper.vm.$nextTick()
}
Wrapper.findComponent({ ref: 'search' }).trigger('keydown.enter')

await search.trigger('keydown.enter')
await Wrapper.vm.$nextTick()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/CreateOption.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { searchSubmit, selectTag, selectWithProps } from '../helpers'
import { selectTag, selectWithProps } from '../helpers'

describe('CreateOption When Tagging Is Enabled', () => {
it('can select the current search text as a string', async () => {
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/Selectable.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { selectWithProps } from '../helpers'
import { searchSubmit, selectWithProps } from '../helpers'

describe('Selectable prop', () => {
it('should select selectable option if clicked', async () => {
Expand Down Expand Up @@ -56,4 +56,19 @@ describe('Selectable prop', () => {

expect(Select.vm.typeAheadPointer).toEqual(0)
})

it('should not let the user select an unselectable option with return', async () => {
const Select = selectWithProps({
options: ['one', 'two'],
multiple: true,
selectable: (option) => option !== 'two',
})

// this sets the typeAheadPointer to 0
await searchSubmit(Select, 'one')
expect(Select.vm.selectedValue).toEqual(['one'])

await searchSubmit(Select, 'two')
expect(Select.vm.selectedValue).toEqual(['one'])
})
})
10 changes: 2 additions & 8 deletions tests/unit/Tagging.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ describe('When Tagging Is Enabled', () => {
options: [{ label: 'one' }, two],
})

Select.vm.search = 'two'
await Select.vm.$nextTick()

searchSubmit(Select)
await searchSubmit(Select, 'two')
expect(Select.vm.selectedValue).toEqual([two])
})

Expand All @@ -180,10 +177,7 @@ describe('When Tagging Is Enabled', () => {
options: [{ label: 'one' }, two],
})

Select.vm.search = 'two'
await Select.vm.$nextTick()

searchSubmit(Select)
await searchSubmit(Select, 'two')
expect(Select.vm.selectedValue).toEqual([two])
})

Expand Down

0 comments on commit 115f24a

Please sign in to comment.