From 361aba64d974c7e22d025c6c0055bbae18cbf5ee Mon Sep 17 00:00:00 2001 From: Blaine Ehrhart Date: Mon, 20 Jun 2016 16:17:00 -0700 Subject: [PATCH] Added resetOnOptionsChange parameter to allow changing the options of the select without resetting the value. This is extra useful for ajax request, especially slow ajax requests --- src/components/Select.vue | 13 ++++++- test/unit/coverage/lcov-report/base.css | 13 ++++--- test/unit/specs/Select.spec.js | 49 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 8 deletions(-) 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/coverage/lcov-report/base.css b/test/unit/coverage/lcov-report/base.css index 417c7ad..29737bc 100644 --- a/test/unit/coverage/lcov-report/base.css +++ b/test/unit/coverage/lcov-report/base.css @@ -166,10 +166,13 @@ table.coverage td span.cline-any { /* dark green */ .status-line.high, .high .cover-fill { background:rgb(77,146,33) } .high .chart { border:1px solid rgb(77,146,33) } - - -.medium .chart { border:1px solid #666; } -.medium .cover-fill { background: #666; } +/* dark yellow (gold) */ +.medium .chart { border:1px solid #f9cd0b; } +.status-line.medium, .medium .cover-fill { background: #f9cd0b; } +/* light yellow */ +.medium { background: #fff4c2; } +/* light gray */ +span.cline-neutral { background: #eaeaea; } .cbranch-no { background: yellow !important; color: #111; } @@ -177,8 +180,6 @@ table.coverage td span.cline-any { .fstat-skip { background: #ddd; color: #111 !important; } .cbranch-skip { background: #ddd !important; color: #111; } -span.cline-neutral { background: #eaeaea; } -.medium { background: #eaeaea; } .cover-fill, .cover-empty { display:inline-block; 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