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

Some more changes.

changed from actualValue to currentSelection
This commit is contained in:
Rajesh Akkineni
2016-10-22 17:55:17 +05:30
parent b01f1b21a7
commit 23355ea246
4 changed files with 98 additions and 58 deletions
+8
View File
@@ -164,6 +164,14 @@ From there you can use as normal. Here's an [example on JSBin](http://jsbin.com/
default: 'label' default: 'label'
}, },
/**
* An optional callback function that is called each time the selected
* value(s) change. When integrating with Vuex, use this callback to trigger
* an action, rather than using :value.sync to retreive the selected value.
* @type {Function}
* @default {null}
*/
onChange: Function,
/** /**
* Enable/disable creating options from searchInput. * Enable/disable creating options from searchInput.
+10
View File
@@ -78,6 +78,16 @@
label: { label: {
type: String, type: String,
default: 'label' default: 'label'
},
/**
* An optional callback function that is called each time the selected
* value(s) change. When integrating with Vuex, use this callback to trigger
* an action, rather than using :value.sync to retreive the selected value.
* @type {Function}
* @default {null}
*/
onChange: Function
} }
} }
+55 -33
View File
@@ -342,6 +342,16 @@
} }
}, },
/**
* An optional callback function that is called each time the selected
* value(s) change. When integrating with Vuex, use this callback to trigger
* an action, rather than using :value.sync to retreive the selected value.
* @type {Function}
* @default {null}
*/
onChange: Function,
/** /**
* Enable/disable creating options from searchInput. * Enable/disable creating options from searchInput.
* @type {Boolean} * @type {Boolean}
@@ -368,7 +378,7 @@
createOption: { createOption: {
type: Function, type: Function,
default: function (newOption) { default: function (newOption) {
if (typeof this.actualOptions[0] === 'object') { if (typeof this.currentOptions[0] === 'object') {
return {[this.label]: newOption} return {[this.label]: newOption}
} }
return newOption return newOption
@@ -389,27 +399,38 @@
return { return {
search: '', search: '',
open: false, open: false,
actualValue: null, currentSelection: null,
actualOptions: [], currentOptions: [],
showLoading: false showLoading: false
} }
}, },
watch: { watch: {
actualValue(val, old) { value(val, old) {
this.currentSelection = val
},
currentSelection(val, old) {
if (this.multiple) { if (this.multiple) {
this.onChange ? this.onChange(val) : null
this.$emit('change', val) this.$emit('change', val)
} else { } else {
if(val !== old) { if(val !== old) {
this.onChange? this.onChange(val) : null
this.$emit('change', val) this.$emit('change', val)
} }
} }
}, },
actualOptions() { options(val) {
this.currentOptions = val
},
currentOptions() {
if (!this.taggable && this.resetOnOptionsChange) { if (!this.taggable && this.resetOnOptionsChange) {
this.actualValue = this.multiple ? [] : null this.currentSelection = this.multiple ? [] : null
} }
} },
multiple(val) {
this.currentSelection = val ? [] : null
}
}, },
methods: { methods: {
@@ -427,18 +448,19 @@
option = this.createOption(option) option = this.createOption(option)
if (this.pushTags) { if (this.pushTags) {
this.actualOptions.push(option) console.log("adding " + option +" to "+ this.currentOptions)
this.currentOptions.push(option)
} }
} }
if (this.multiple) { if (this.multiple) {
if (!this.actualValue) { if (!this.currentSelection) {
this.actualValue = [option] this.currentSelection = [option]
} else { } else {
this.actualValue.push(option) this.currentSelection.push(option)
} }
} else { } else {
this.actualValue = option this.currentSelection = option
} }
} }
@@ -453,15 +475,15 @@
deselect(option) { deselect(option) {
if (this.multiple) { if (this.multiple) {
let ref = -1 let ref = -1
this.actualValue.forEach((val) => { this.currentSelection.forEach((val) => {
if (val === option || typeof val === 'object' && val[this.label] === option[this.label]) { if (val === option || typeof val === 'object' && val[this.label] === option[this.label]) {
ref = val ref = val
} }
}) })
var index = this.actualValue.indexOf(ref) var index = this.currentSelection.indexOf(ref)
this.actualValue.splice(index, 1) this.currentSelection.splice(index, 1)
} else { } else {
this.actualValue = null this.currentSelection = null
} }
}, },
@@ -503,9 +525,9 @@
* @return {Boolean} True when selected || False otherwise * @return {Boolean} True when selected || False otherwise
*/ */
isOptionSelected(option) { isOptionSelected(option) {
if (this.multiple && this.actualValue) { if (this.multiple && this.currentSelection) {
let selected = false let selected = false
this.actualValue.forEach(opt => { this.currentSelection.forEach(opt => {
if (typeof opt === 'object' && opt[this.label] === option[this.label]) { if (typeof opt === 'object' && opt[this.label] === option[this.label]) {
selected = true selected = true
} else if (opt === option) { } else if (opt === option) {
@@ -515,7 +537,7 @@
return selected return selected
} }
return this.actualValue === option return this.currentSelection === option
}, },
/** /**
@@ -537,14 +559,14 @@
* @return {this.value} * @return {this.value}
*/ */
maybeDeleteValue() { maybeDeleteValue() {
if (!this.$refs.search.value.length && this.actualValue) { if (!this.$refs.search.value.length && this.currentSelection) {
return this.multiple ? this.actualValue.pop() : this.actualValue = null return this.multiple ? this.currentSelection.pop() : this.currentSelection = null
} }
}, },
/** /**
* Determine if an option exists * Determine if an option exists
* within this.actualOptions array. * within this.currentOptions array.
* *
* @param {Object || String} option * @param {Object || String} option
* @return {boolean} * @return {boolean}
@@ -552,7 +574,7 @@
optionExists(option) { optionExists(option) {
let exists = false let exists = false
this.actualOptions.forEach(opt => { this.currentOptions.forEach(opt => {
if (typeof opt === 'object' && opt[this.label] === option) { if (typeof opt === 'object' && opt[this.label] === option) {
exists = true exists = true
} else if (opt === option) { } else if (opt === option) {
@@ -598,7 +620,7 @@
* @return {array} * @return {array}
*/ */
filteredOptions() { filteredOptions() {
let options = this.$options.filters.filterBy?this.$options.filters.filterBy(this.actualOptions, this.search):this.actualOptions let options = this.$options.filters.filterBy?this.$options.filters.filterBy(this.currentOptions, this.search):this.currentOptions
if (this.taggable && this.search.length && !this.optionExists(this.search)) { if (this.taggable && this.search.length && !this.optionExists(this.search)) {
options.unshift(this.search) options.unshift(this.search)
} }
@@ -610,11 +632,11 @@
* @return {Boolean} * @return {Boolean}
*/ */
isValueEmpty() { isValueEmpty() {
if (this.actualValue) { if (this.currentSelection) {
if (typeof this.actualValue === 'object') { if (typeof this.currentSelection === 'object') {
return !Object.keys(this.actualValue).length return !Object.keys(this.currentSelection).length
} }
return !this.actualValue.length return !this.currentSelection.length
} }
return true; return true;
@@ -626,17 +648,17 @@
*/ */
valueAsArray() { valueAsArray() {
if (this.multiple) { if (this.multiple) {
return this.actualValue return this.currentSelection
} else if (this.actualValue) { } else if (this.currentSelection) {
return [this.actualValue] return [this.currentSelection]
} }
return [] return []
} }
}, },
created: function() { created: function() {
this.actualValue = this.value this.currentSelection = this.value
this.actualOptions = this.options.slice(0) this.currentOptions = this.options.slice(0)
this.showLoading = this.loading this.showLoading = this.loading
} }
+25 -25
View File
@@ -68,7 +68,7 @@ describe('Select.vue', () => {
options: ['one', 'two', 'three'] options: ['one', 'two', 'three']
} }
}).$mount() }).$mount()
expect(vm.$children[0].actualValue).toEqual(vm.value) expect(vm.$children[0].currentSelection).toEqual(vm.value)
}) })
it('can accept an array of objects and pre-selected value (single)', () => { it('can accept an array of objects and pre-selected value (single)', () => {
@@ -80,7 +80,7 @@ describe('Select.vue', () => {
options: [{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() }).$mount()
expect(vm.$children[0].actualValue).toEqual(vm.value) expect(vm.$children[0].currentSelection).toEqual(vm.value)
}) })
it('can accept an array of objects and pre-selected values (multiple)', () => { it('can accept an array of objects and pre-selected values (multiple)', () => {
@@ -92,7 +92,7 @@ describe('Select.vue', () => {
options: [{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() }).$mount()
expect(vm.$children[0].actualValue).toEqual(vm.value) expect(vm.$children[0].currentSelection).toEqual(vm.value)
}) })
it('can deselect a pre-selected object', () => { it('can deselect a pre-selected object', () => {
@@ -104,7 +104,7 @@ describe('Select.vue', () => {
} }
}).$mount() }).$mount()
vm.$children[0].select({label: 'This is Foo', value: 'foo'}) vm.$children[0].select({label: 'This is Foo', value: 'foo'})
expect(vm.$children[0].actualValue.length).toEqual(1) expect(vm.$children[0].currentSelection.length).toEqual(1)
}) })
it('can deselect a pre-selected string', () => { it('can deselect a pre-selected string', () => {
@@ -116,7 +116,7 @@ describe('Select.vue', () => {
} }
}).$mount() }).$mount()
vm.$children[0].select('foo') vm.$children[0].select('foo')
expect(vm.$children[0].actualValue.length).toEqual(1) expect(vm.$children[0].currentSelection.length).toEqual(1)
}) })
it('can determine if the value prop is empty', () => { it('can determine if the value prop is empty', () => {
@@ -164,10 +164,10 @@ describe('Select.vue', () => {
vm.multiple = false vm.multiple = false
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual(null) expect(vm.$children[0].currentSelection).toEqual(null)
vm.multiple = true vm.multiple = true
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual([]) expect(vm.$children[0].currentSelection).toEqual([])
done() done()
}) })
}) })
@@ -183,7 +183,7 @@ describe('Select.vue', () => {
} }
}).$mount() }).$mount()
vm.$children[0].options = ['one', 'five', 'six'] vm.$children[0].options = ['one', 'five', 'six']
expect(vm.$children[0].actualValue).toEqual(['one']) expect(vm.$children[0].currentSelection).toEqual(['one'])
}) })
it('can determine if an object is already selected', () => { it('can determine if an object is already selected', () => {
@@ -228,7 +228,7 @@ describe('Select.vue', () => {
it('should run change when multiple is true and the value changes', (done) => { it('should run change when multiple is true and the value changes', (done) => {
const vm = new Vue({ const vm = new Vue({
template: `<div><v-select v-ref:select :value="['foo']" :options="['foo','bar','baz']" multiple v-on:change="cb"></v-select></div>`, template: `<div><v-select ref="select" :value="['foo']" :options="['foo','bar','baz']" multiple v-on:change="cb"></v-select></div>`,
methods: { methods: {
cb(val) { cb(val) {
} }
@@ -256,7 +256,7 @@ describe('Select.vue', () => {
describe('Toggling Dropdown', () => { describe('Toggling Dropdown', () => {
it('should open the dropdown when the el is clicked', (done) => { it('should open the dropdown when the el is clicked', (done) => {
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="value"></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: [{label: 'one'}], value: [{label: 'one'}],
@@ -292,7 +292,7 @@ describe('Select.vue', () => {
it('should close the dropdown on search blur', () => { it('should close the dropdown on search blur', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" multiple :value.sync="value"></v-select></div>', template: '<div><v-select :options="options" multiple :value="value"></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: [{label: 'one'}], value: [{label: 'one'}],
@@ -324,7 +324,7 @@ describe('Select.vue', () => {
it('should remove existing search text on escape keyup', () => { it('should remove existing search text on escape keyup', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :options="options" multiple :value.sync="value"></v-select></div>', template: '<div><v-select :options="options" multiple :value="value"></v-select></div>',
components: {vSelect}, components: {vSelect},
data: { data: {
value: [{label: 'one'}], value: [{label: 'one'}],
@@ -534,7 +534,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
vm.$children[0].$refs.toggle.querySelector('.close').click() vm.$children[0].$refs.toggle.querySelector('.close').click()
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual([]) expect(vm.$children[0].currentSelection).toEqual([])
done() done()
}) })
}) })
@@ -551,7 +551,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
vm.$children[0].maybeDeleteValue() vm.$children[0].maybeDeleteValue()
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual(['one']) expect(vm.$children[0].currentSelection).toEqual(['one'])
}) })
}) })
@@ -566,7 +566,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
vm.$children[0].maybeDeleteValue() vm.$children[0].maybeDeleteValue()
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual(null) expect(vm.$children[0].currentSelection).toEqual(null)
}) })
}) })
}) })
@@ -594,7 +594,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
expect(vm.$children[0].searchPlaceholder).toEqual('foo') expect(vm.$children[0].searchPlaceholder).toEqual('foo')
vm.$children[0].actualValue = {label: 'one'} vm.$children[0].currentSelection = {label: 'one'}
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].searchPlaceholder).not.toBeDefined() expect(vm.$children[0].searchPlaceholder).not.toBeDefined()
done() done()
@@ -670,7 +670,7 @@ describe('Select.vue', () => {
searchSubmit(vm, 'three') searchSubmit(vm, 'three')
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual(['one', 'three']) expect(vm.$children[0].currentSelection).toEqual(['one', 'three'])
done() done()
}) })
}) })
@@ -687,7 +687,7 @@ describe('Select.vue', () => {
searchSubmit(vm, 'two') searchSubmit(vm, 'two')
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual([{label: 'one'}, {label: 'two'}]) expect(vm.$children[0].currentSelection).toEqual([{label: 'one'}, {label: 'two'}])
done() done()
}) })
}) })
@@ -703,7 +703,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
searchSubmit(vm, 'three') searchSubmit(vm, 'three')
expect(vm.$children[0].actualOptions).toEqual(['one', 'two', 'three']) expect(vm.$children[0].currentOptions).toEqual(['one', 'two', 'three'])
}) })
it('wont add a freshly created option/tag to the options list when pushTags is false', () => { it('wont add a freshly created option/tag to the options list when pushTags is false', () => {
@@ -717,7 +717,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
searchSubmit(vm, 'three') searchSubmit(vm, 'three')
expect(vm.$children[0].actualOptions).toEqual(['one', 'two']) expect(vm.$children[0].currentOptions).toEqual(['one', 'two'])
}) })
it('should select an existing option if the search string matches a string from options', (done) => { it('should select an existing option if the search string matches a string from options', (done) => {
@@ -735,7 +735,7 @@ describe('Select.vue', () => {
searchSubmit(vm) searchSubmit(vm)
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue[0]).toBe(two) expect(vm.$children[0].currentSelection[0]).toBe(two)
done() done()
}) })
}) })
@@ -757,7 +757,7 @@ describe('Select.vue', () => {
// This needs to be wrapped in nextTick() twice so that filteredOptions can // This needs to be wrapped in nextTick() twice so that filteredOptions can
// calculate after setting the search text, and move the typeAheadPointer index to 0. // calculate after setting the search text, and move the typeAheadPointer index to 0.
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue.label).toBe(two.label) expect(vm.$children[0].currentSelection.label).toBe(two.label)
done() done()
}) })
}) })
@@ -773,7 +773,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
vm.$children[0].options = [{label: 'two'}] vm.$children[0].options = [{label: 'two'}]
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual([{label: 'one'}]) expect(vm.$children[0].currentSelection).toEqual([{label: 'one'}])
done() done()
}) })
}) })
@@ -876,7 +876,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
vm.$children[0].options = ['four', 'five', 'six'] vm.$children[0].options = ['four', 'five', 'six']
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual('one') expect(vm.$children[0].currentSelection).toEqual('one')
done() done()
}) })
}) })
@@ -892,7 +892,7 @@ describe('Select.vue', () => {
}).$mount() }).$mount()
vm.$children[0].options = ['four', 'five', 'six'] vm.$children[0].options = ['four', 'five', 'six']
Vue.nextTick(() => { Vue.nextTick(() => {
expect(vm.$children[0].actualValue).toEqual(null) expect(vm.$children[0].currentSelection).toEqual(null)
done() done()
}) })
}) })