Skip to content

Commit aabc43c

Browse files
authored
Merge pull request sagalbot#172 from koriroys/master
Add option to allow closing a multi-select when a value is selected
2 parents 7f8c547 + 68b67e3 commit aabc43c

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

dev.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
<v-select placeholder="multiple" multiple :options="options"></v-select>
3535
<v-select placeholder="multiple, taggable" multiple taggable :options="options" no-drop></v-select>
3636
<v-select placeholder="multiple, taggable, push-tags" multiple push-tags taggable :options="[{label: 'Foo', value: 'foo'}]"></v-select>
37+
<v-select placeholder="multiple, closeOnSelect=true" multiple :options="['cat', 'dog', 'bear']"></v-select>
38+
<v-select placeholder="multiple, closeOnSelect=false" multiple :close-on-select="false" :options="['cat', 'dog', 'bear']"></v-select>
3739
<v-select placeholder="unsearchable" :options="options" :searchable="false"></v-select>
3840
<v-select placeholder="search github.." label="full_name" @search="search" :options="ajaxRes"></v-select>
3941
</div>

docs/components/Params.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@
7171
default: true
7272
},
7373

74+
/**
75+
* Close a dropdown when an option is chosen. Set to false to keep the dropdown
76+
* open (useful when combined with multi-select, for example)
77+
* @type {Boolean}
78+
*/
79+
closeOnSelect: {
80+
type: Boolean,
81+
default: true
82+
},
83+
7484
/**
7585
* Tells vue-select what key to use when generating option labels when
7686
* `option` is an object.

src/components/Select.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,16 @@
390390
default: true
391391
},
392392
393+
/**
394+
* Close a dropdown when an option is chosen. Set to false to keep the dropdown
395+
* open (useful when combined with multi-select, for example)
396+
* @type {Boolean}
397+
*/
398+
closeOnSelect: {
399+
type: Boolean,
400+
default: true
401+
},
402+
393403
/**
394404
* Tells vue-select what key to use when generating option
395405
* labels when each `option` is an object.
@@ -625,7 +635,7 @@
625635
* @return {void}
626636
*/
627637
onAfterSelect(option) {
628-
if (!this.multiple) {
638+
if (this.closeOnSelect) {
629639
this.open = !this.open
630640
this.$refs.search.blur()
631641
}

test/unit/specs/Select.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,46 @@ describe('Select.vue', () => {
351351
})
352352
})
353353

354+
355+
it('closes the dropdown when an option is selected, multiple is true, and closeOnSelect option is true', (done) => {
356+
const vm = new Vue({
357+
template: '<div><v-select ref="select" :options="options" multiple :value="value"></v-select></div>',
358+
components: {vSelect},
359+
data: {
360+
value: [],
361+
options: ['one', 'two', 'three']
362+
}
363+
}).$mount()
364+
365+
vm.$children[0].open = true
366+
vm.$refs.select.select('one')
367+
368+
Vue.nextTick(() => {
369+
expect(vm.$children[0].open).toEqual(false)
370+
done()
371+
})
372+
})
373+
374+
it('does not close the dropdown when the el is clicked, multiple is true, and closeOnSelect option is false', (done) => {
375+
const vm = new Vue({
376+
template: '<div><v-select ref="select" :options="options" multiple :closeOnSelect="false" :value="value"></v-select></div>',
377+
components: {vSelect},
378+
data: {
379+
value: [],
380+
options: ['one', 'two', 'three']
381+
}
382+
}).$mount()
383+
384+
vm.$children[0].open = true
385+
vm.$refs.select.select('one')
386+
387+
Vue.nextTick(() => {
388+
expect(vm.$children[0].open).toEqual(true)
389+
done()
390+
})
391+
})
392+
393+
354394
it('should close the dropdown on search blur', () => {
355395
const vm = new Vue({
356396
template: '<div><v-select :options="options" multiple :value="value"></v-select></div>',

0 commit comments

Comments
 (0)