From 8cce5e0ea2bad37ff68acb18739c54dbf46debb3 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 7 Nov 2019 15:45:12 -0800 Subject: [PATCH] add support for composing #900 --- src/components/Select.vue | 16 +++++++++++----- tests/unit/Keydown.spec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/components/Select.vue b/src/components/Select.vue index 18a0edb..d7f3ef1 100644 --- a/src/components/Select.vue +++ b/src/components/Select.vue @@ -79,6 +79,9 @@ import ajax from '../mixins/ajax' import childComponents from './childComponents'; + /** + * @name VueSelect + */ export default { components: {...childComponents}, @@ -302,12 +305,12 @@ /** * Select the current value if selectOnTab is enabled - * @deprecated + * @deprecated since 3.3 */ onTab: { type: Function, default: function () { - if (this.selectOnTab) { + if (this.selectOnTab && !this.isComposing) { this.typeAheadSelect(); } }, @@ -450,7 +453,7 @@ /** * When true, hitting the 'tab' key will select the current select value * @type {Boolean} - * @deprecated + * @deprecated since 3.3 - use selectOnKeyCodes instead */ selectOnTab: { type: Boolean, @@ -500,6 +503,7 @@ return { search: '', open: false, + isComposing: false, pushedTags: [], _value: [] // Internal value managed by Vue Select if no `value` prop is passed } @@ -873,7 +877,7 @@ onSearchKeyDown (e) { const preventAndSelect = e => { e.preventDefault(); - return this.typeAheadSelect(); + return !this.isComposing && this.typeAheadSelect(); }; const defaults = { @@ -977,10 +981,12 @@ 'value': this.search, }, events: { + 'compositionstart': () => this.isComposing = true, + 'compositionend': () => this.isComposing = false, 'keydown': this.onSearchKeyDown, 'blur': this.onSearchBlur, 'focus': this.onSearchFocus, - 'input': (e) => this.search = e.target.value + 'input': (e) => this.search = e.target.value, }, }, spinner: { diff --git a/tests/unit/Keydown.spec.js b/tests/unit/Keydown.spec.js index 56d390b..4721719 100644 --- a/tests/unit/Keydown.spec.js +++ b/tests/unit/Keydown.spec.js @@ -41,4 +41,34 @@ describe('Custom Keydown Handlers', () => { expect(spy).toHaveBeenCalledTimes(1); }); + describe('CompositionEvent support', () => { + + it('will not select a value with enter if the user is composing', () => { + const Select = mountDefault(); + const spy = jest.spyOn(Select.vm, 'typeAheadSelect'); + + Select.find({ref: 'search'}).trigger('compositionstart'); + Select.find({ref: 'search'}).trigger('keydown.enter'); + expect(spy).toHaveBeenCalledTimes(0); + + Select.find({ref: 'search'}).trigger('compositionend'); + Select.find({ref: 'search'}).trigger('keydown.enter'); + expect(spy).toHaveBeenCalledTimes(1); + }); + + it('will not select a value with tab if the user is composing', () => { + const Select = mountDefault({selectOnTab: true}); + const spy = jest.spyOn(Select.vm, 'typeAheadSelect'); + + Select.find({ref: 'search'}).trigger('compositionstart'); + Select.find({ref: 'search'}).trigger('keydown.tab'); + expect(spy).toHaveBeenCalledTimes(0); + + Select.find({ref: 'search'}).trigger('compositionend'); + Select.find({ref: 'search'}).trigger('keydown.tab'); + expect(spy).toHaveBeenCalledTimes(1); + }); + + }); + });