diff --git a/src/components/Select.vue b/src/components/Select.vue index aebfa26..7f0b3de 100644 --- a/src/components/Select.vue +++ b/src/components/Select.vue @@ -383,7 +383,16 @@ } return newOption } - } + }, + + /** + * When false, updating the options will not reset the select value + * @type {Boolean} + */ + resetOnOptionsChange: { + type: Boolean, + default: true + }, }, data() { @@ -402,7 +411,7 @@ } }, options() { - if (!this.taggable) { + if (!this.taggable && this.resetOnOptionsChange) { this.$set('value', this.multiple ? [] : null) } }, diff --git a/test/unit/specs/Select.spec.js b/test/unit/specs/Select.spec.js index bec61aa..c416654 100644 --- a/test/unit/specs/Select.spec.js +++ b/test/unit/specs/Select.spec.js @@ -752,6 +752,21 @@ describe('Select.vue', () => { }) }) }) + it('should not reset the selected value when the options property changes', (done) => { + const vm = new Vue({ + template: '
', + components: {vSelect}, + data: { + value: [{label: 'one'}], + options: [{label: 'one'}] + } + }).$mount() + vm.$children[0].options = [{label: 'two'}] + Vue.nextTick(() => { + expect(vm.$children[0].value).toEqual([{label: 'one'}]) + done() + }) + }) }) describe('Asynchronous Loading', () => { @@ -839,4 +854,38 @@ describe('Select.vue', () => { }) }) }) + + describe('Reset on options change', () => { + it('should not reset the selected value when the options property changes', (done) => { + const vm = new Vue({ + template: '
', + components: {vSelect}, + data: { + value: 'one', + options: ['one', 'two', 'three'] + } + }).$mount() + vm.$children[0].options = ['four', 'five', 'six'] + Vue.nextTick(() => { + expect(vm.$children[0].value).toEqual('one') + done() + }) + }) + + it('should reset the selected value when the options property changes', (done) => { + const vm = new Vue({ + template: '
', + components: {vSelect}, + data: { + value: 'one', + options: ['one', 'two', 'three'] + } + }).$mount() + vm.$children[0].options = ['four', 'five', 'six'] + Vue.nextTick(() => { + expect(vm.$children[0].value).toEqual(null) + done() + }) + }) + }) }) \ No newline at end of file