Skip to content

Commit 795feab

Browse files
authored
fix(1735): use keypress event for space (#1736)
1 parent 6de1375 commit 795feab

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

src/components/Select.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,6 @@ export default {
574574
default: () => [
575575
// enter
576576
13,
577-
// space
578-
32,
579577
],
580578
},
581579
@@ -773,6 +771,7 @@ export default {
773771
compositionstart: () => (this.isComposing = true),
774772
compositionend: () => (this.isComposing = false),
775773
keydown: this.onSearchKeyDown,
774+
keypress: this.onSearchKeyPress,
776775
blur: this.onSearchBlur,
777776
focus: this.onSearchFocus,
778777
input: (e) => (this.search = e.target.value),
@@ -1358,6 +1357,17 @@ export default {
13581357
return handlers[e.keyCode](e)
13591358
}
13601359
},
1360+
1361+
/**
1362+
* @todo: Probably want to add a mapKeyPress method just like we have for keydown.
1363+
* @param {KeyboardEvent} e
1364+
*/
1365+
onSearchKeyPress(e) {
1366+
if (!this.open && e.keyCode === 32) {
1367+
e.preventDefault()
1368+
this.open = true
1369+
}
1370+
},
13611371
},
13621372
}
13631373
</script>

tests/unit/Dropdown.spec.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { selectWithProps } from '../helpers'
1+
import { mountDefault, selectWithProps } from '../helpers'
22
import OpenIndicator from '../../src/components/OpenIndicator'
3+
import { shallowMount } from '@vue/test-utils'
4+
import VueSelect from '../../src/components/Select.vue'
35

46
const preventDefault = jest.fn()
57

@@ -49,6 +51,40 @@ describe('Toggling Dropdown', () => {
4951
expect(Select.vm.open).toEqual(true)
5052
})
5153

54+
it('will open the dropdown when: the input has focus, space is pressed, menu is closed', async () => {
55+
const Select = mountDefault()
56+
const input = Select.findComponent({ ref: 'search' })
57+
58+
input.trigger('focus')
59+
Select.vm.open = false
60+
input.trigger('keypress.space')
61+
62+
expect(Select.vm.open).toEqual(true)
63+
expect(Select.vm.search).toEqual('')
64+
})
65+
66+
it('should open dropdown on alphabetic input', async () => {
67+
const Select = mountDefault()
68+
const input = Select.findComponent({ ref: 'search' })
69+
70+
input.element.value = 'a'
71+
input.trigger('input')
72+
await Select.vm.$nextTick()
73+
74+
expect(Select.vm.open).toEqual(true)
75+
})
76+
77+
it('should open dropdown on numeric input', async () => {
78+
const Select = shallowMount(VueSelect)
79+
const input = Select.findComponent({ ref: 'search' })
80+
81+
input.element.value = 1
82+
input.trigger('input')
83+
await Select.vm.$nextTick()
84+
85+
expect(Select.vm.open).toEqual(true)
86+
})
87+
5288
it('can close the dropdown when the el is clicked', () => {
5389
const Select = selectWithProps()
5490
const spy = jest.spyOn(Select.vm.$refs.search, 'blur')

tests/unit/Filtering.spec.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ describe('Filtering Options', () => {
2121
expect(Select.vm.filteredOptions).toEqual(['bar', 'baz'])
2222
})
2323

24+
it('can filter items with spaces', () => {
25+
const Select = shallowMount(VueSelect, {
26+
propsData: { options: ['foo bar', 'baz'] },
27+
})
28+
Select.vm.search = ' '
29+
expect(Select.vm.filteredOptions).toEqual(['foo bar'])
30+
})
31+
2432
it('should not filter the array of strings if filterable is false', () => {
2533
const Select = shallowMount(VueSelect, {
2634
propsData: { options: ['foo', 'bar', 'baz'], filterable: false },
@@ -83,26 +91,4 @@ describe('Filtering Options', () => {
8391
Select.vm.search = '1'
8492
expect(Select.vm.filteredOptions).toEqual([1, 10])
8593
})
86-
87-
it('should open dropdown on alphabetic input', async () => {
88-
const Select = shallowMount(VueSelect)
89-
90-
const input = Select.find('.vs__search')
91-
input.element.value = 'a'
92-
input.trigger('input')
93-
await Select.vm.$nextTick()
94-
95-
expect(Select.vm.open).toEqual(true)
96-
})
97-
98-
it('should open dropdown on numeric input', async () => {
99-
const Select = shallowMount(VueSelect)
100-
101-
const input = Select.find('.vs__search')
102-
input.element.value = 1
103-
input.trigger('input')
104-
await Select.vm.$nextTick()
105-
106-
expect(Select.vm.open).toEqual(true)
107-
})
10894
})

0 commit comments

Comments
 (0)