2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-06-22 10:30:34 +03:00

fix: allow typeAheadSelect only when option is selectable (#1529)

This commit is contained in:
Jeff Sagal
2022-02-17 16:57:27 -08:00
committed by GitHub
parent 0c72638d5e
commit 115f24aba2
5 changed files with 28 additions and 13 deletions
+1 -1
View File
@@ -67,7 +67,7 @@ export default {
typeAheadSelect() { typeAheadSelect() {
const typeAheadOption = this.filteredOptions[this.typeAheadPointer] const typeAheadOption = this.filteredOptions[this.typeAheadPointer]
if (typeAheadOption) { if (typeAheadOption && this.selectable(typeAheadOption)) {
this.select(typeAheadOption) this.select(typeAheadOption)
} }
}, },
+8 -2
View File
@@ -9,11 +9,17 @@ import Vue from 'vue'
* @param Wrapper {Wrapper<Vue>} * @param Wrapper {Wrapper<Vue>}
* @param searchText * @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) { if (searchText) {
Wrapper.vm.search = 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()
} }
/** /**
+1 -1
View File
@@ -1,4 +1,4 @@
import { searchSubmit, selectTag, selectWithProps } from '../helpers' import { selectTag, selectWithProps } from '../helpers'
describe('CreateOption When Tagging Is Enabled', () => { describe('CreateOption When Tagging Is Enabled', () => {
it('can select the current search text as a string', async () => { it('can select the current search text as a string', async () => {
+16 -1
View File
@@ -1,4 +1,4 @@
import { selectWithProps } from '../helpers' import { searchSubmit, selectWithProps } from '../helpers'
describe('Selectable prop', () => { describe('Selectable prop', () => {
it('should select selectable option if clicked', async () => { it('should select selectable option if clicked', async () => {
@@ -56,4 +56,19 @@ describe('Selectable prop', () => {
expect(Select.vm.typeAheadPointer).toEqual(0) 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'])
})
}) })
+2 -8
View File
@@ -165,10 +165,7 @@ describe('When Tagging Is Enabled', () => {
options: [{ label: 'one' }, two], options: [{ label: 'one' }, two],
}) })
Select.vm.search = 'two' await searchSubmit(Select, 'two')
await Select.vm.$nextTick()
searchSubmit(Select)
expect(Select.vm.selectedValue).toEqual([two]) expect(Select.vm.selectedValue).toEqual([two])
}) })
@@ -180,10 +177,7 @@ describe('When Tagging Is Enabled', () => {
options: [{ label: 'one' }, two], options: [{ label: 'one' }, two],
}) })
Select.vm.search = 'two' await searchSubmit(Select, 'two')
await Select.vm.$nextTick()
searchSubmit(Select)
expect(Select.vm.selectedValue).toEqual([two]) expect(Select.vm.selectedValue).toEqual([two])
}) })