Skip to content

Commit

Permalink
Merge pull request #295 from monterail/fix-value-clear-error
Browse files Browse the repository at this point in the history
Fix clearing the `value` of single select with null
  • Loading branch information
Damian Dulisz authored Mar 5, 2017
2 parents 9f0f775 + c02008f commit f22988f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/multiselectMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,31 @@ export default {
this.$emit('search-change', this.search, this.id)
},
'value' (value) {
this.internalValue = deepClone(Array.isArray(value) ? value : [value])
this.internalValue = this.getInternalValue()
}
},
methods: {
/**
* Converts the internal value to the external value
* @returns {Object||Array||String||Integer} returns the external value
*/
getValue () {
return this.multiple
? deepClone(this.internalValue)
: deepClone(this.internalValue[0])
: this.internalValue.length === 0
? null
: deepClone(this.internalValue[0])
},
/**
* Converts the external value to the internal value
* @returns {Array} returns the internal value
*/
getInternalValue () {
return this.multiple
? deepClone(this.value)
: this.value === null || this.value === undefined
? []
: deepClone([this.value])
},
/**
* Filters and then flattens the options list
Expand Down
67 changes: 67 additions & 0 deletions test/unit/specs/Multiselect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2815,4 +2815,71 @@ describe('Multiselect.vue', () => {
expect(vm.$children[0].search).to.equal('test')
})
})
describe('#getInternalValue', () => {
describe('when multiple == TRUE', () => {
it('should return the external value', () => {
const vm = new Vue({
render (h) {
return h(Multiselect, {
props: {
options: this.source,
value: this.value,
multiple: true
}
})
},
components: { Multiselect },
data: {
value: ['1', '2', '3'],
source: ['1', '2', '3', '4', '5']
}
}).$mount()
expect(vm.$children[0].getInternalValue()).to.deep.equal(['1', '2', '3'])
})
})
describe('when multiple == FALSE', () => {
describe('and an option is selected', () => {
it('should return the exernal value in an array', () => {
const vm = new Vue({
render (h) {
return h(Multiselect, {
props: {
options: this.source,
value: this.value,
multiple: false
}
})
},
components: { Multiselect },
data: {
value: '1',
source: ['1', '2', '3', '4', '5']
}
}).$mount()
expect(vm.$children[0].getInternalValue()).to.deep.equal(['1'])
})
})
describe('and the selection is empty', () => {
it('should return an empty array', () => {
const vm = new Vue({
render (h) {
return h(Multiselect, {
props: {
options: this.source,
value: this.value,
multiple: false
}
})
},
components: { Multiselect },
data: {
value: null,
source: ['1', '2', '3', '4', '5']
}
}).$mount()
expect(vm.$children[0].getInternalValue()).to.deep.equal([])
})
})
})
})
})

0 comments on commit f22988f

Please sign in to comment.