2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-06-22 10:30:34 +03:00

manually track down options when deselecting instead of relying on array.$remove, fixes #74

This commit is contained in:
Jeff Sagal
2016-07-11 22:13:43 -07:00
parent b9302e7de3
commit afdc4da785
5 changed files with 52 additions and 10 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "vue-select", "name": "vue-select",
"version": "1.3.1", "version": "1.3.2",
"description": "A native Vue.js component that provides similar functionality to Select2 without the overhead of jQuery.", "description": "A native Vue.js component that provides similar functionality to Select2 without the overhead of jQuery.",
"author": "Jeff Sagal <sagalbot@gmail.com>", "author": "Jeff Sagal <sagalbot@gmail.com>",
"private": false, "private": false,
+23 -6
View File
@@ -428,7 +428,9 @@
* @return {void} * @return {void}
*/ */
select(option) { select(option) {
if (!this.isOptionSelected(option)) { if (this.isOptionSelected(option)) {
this.deselect(option)
} else {
if (this.taggable && !this.optionExists(option)) { if (this.taggable && !this.optionExists(option)) {
option = this.createOption(option) option = this.createOption(option)
@@ -446,15 +448,30 @@
} else { } else {
this.value = option this.value = option
} }
} else {
if (this.multiple) {
this.value.$remove(option)
}
} }
this.onAfterSelect(option) this.onAfterSelect(option)
}, },
/**
* De-select a given option.
* @param {Object||String} option
* @return {void}
*/
deselect(option) {
if (this.multiple) {
let ref = -1
this.value.forEach((val) => {
if (val === option || typeof val === 'object' && val[this.label] === option[this.label]) {
ref = val
}
})
this.value.$remove(ref)
} else {
this.value = null
}
},
/** /**
* Called from this.select after each selection. * Called from this.select after each selection.
* @param {Object||String} option * @param {Object||String} option
@@ -496,7 +513,7 @@
if (this.multiple && this.value) { if (this.multiple && this.value) {
let selected = false let selected = false
this.value.forEach(opt => { this.value.forEach(opt => {
if (typeof opt === 'object' && opt[this.label] === option) { if (typeof opt === 'object' && opt[this.label] === option[this.label]) {
selected = true selected = true
} else if (opt === option) { } else if (opt === option) {
selected = true selected = true
+26 -1
View File
@@ -2,6 +2,7 @@
import Vue from 'vue' import Vue from 'vue'
import vSelect from 'src/components/Select.vue' import vSelect from 'src/components/Select.vue'
// import vSelect from '../../../dist/vue-select'
import pointerScroll from 'src/mixins/pointerScroll.js' import pointerScroll from 'src/mixins/pointerScroll.js'
Vue.component('v-select', vSelect) Vue.component('v-select', vSelect)
@@ -94,6 +95,30 @@ describe('Select.vue', () => {
expect(vm.$children[0].value).toEqual(vm.value) expect(vm.$children[0].value).toEqual(vm.value)
}) })
it('can deselect a pre-selected object', () => {
const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value" :multiple="true"></v-select></div>',
data: {
value: [{label: 'This is Foo', value: 'foo'}, {label: 'This is Bar', value: 'bar'}],
options: [{label: 'This is Foo', value: 'foo'}, {label: 'This is Bar', value: 'bar'}]
}
}).$mount()
vm.$children[0].select({label: 'This is Foo', value: 'foo'})
expect(vm.$children[0].value.length).toEqual(1)
})
it('can deselect a pre-selected string', () => {
const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value" :multiple="true"></v-select></div>',
data: {
value: ['foo', 'bar'],
options: ['foo','bar']
}
}).$mount()
vm.$children[0].select('foo')
expect(vm.$children[0].value.length).toEqual(1)
})
it('can determine if the value prop is empty', () => { it('can determine if the value prop is empty', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" :value.sync="value"></v-select></div>', template: '<div><v-select :options="options" :value.sync="value"></v-select></div>',
@@ -171,7 +196,7 @@ describe('Select.vue', () => {
} }
}).$mount() }).$mount()
expect(vm.$children[0].isOptionSelected('one')).toEqual(true) expect(vm.$children[0].isOptionSelected({label: 'one'})).toEqual(true)
}) })
describe('onChange Callback', () => { describe('onChange Callback', () => {