Skip to content

Commit

Permalink
Small tweaks to null handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Dulisz committed Mar 8, 2017
1 parent f22988f commit 63879c4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/partials/examples/SingleSelectObject.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ div
placeholder="Select one",
:options="options",
:searchable="false",
:allow-empty="false"
:allow-empty="true"
)
pre.language-json
code.
Expand All @@ -26,7 +26,7 @@ export default {
},
data () {
return {
value: { name: 'Vue.js', language: 'JavaScript' },
value: null,
options: [
{ name: 'Vue.js', language: 'JavaScript' },
{ name: 'Rails', language: 'Ruby' },
Expand Down
14 changes: 7 additions & 7 deletions src/multiselectMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export default {
this.$emit('search-change', this.search, this.id)
},
'value' (value) {
this.internalValue = this.getInternalValue()
this.internalValue = this.getInternalValue(value)
}
},
methods: {
Expand All @@ -350,12 +350,12 @@ export default {
* 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])
getInternalValue (value) {
return value === null || value === undefined
? []
: this.multiple
? deepClone(value)
: deepClone([value])
},
/**
* Filters and then flattens the options list
Expand Down
32 changes: 30 additions & 2 deletions test/unit/specs/Multiselect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ describe('Multiselect.vue', () => {
expect(vm.value).to.deep.equal([])
})
})
describe(':value', () => {
it('should work when initial value is null', () => {
const vm = new Vue({
template: `<div><multiselect
v-model="value"
:options="source"
track-by="val"
label="label"
>
</multiselect></div>
`,
components: { Multiselect },
data: {
value: null,
source: [{ val: 1, label: '1' }, { val: 2, label: '2' }]
},
methods: {
resetValue () {
this.value = null
}
}
}).$mount()

const comp = vm.$children[0]

expect(comp.internalValue).to.deep.equal([])
})
})
describe('Events emitting', () => {
describe('@input', () => {
it('should be called whenever the value changes passing the new value and id', () => {
Expand Down Expand Up @@ -2834,7 +2862,7 @@ describe('Multiselect.vue', () => {
source: ['1', '2', '3', '4', '5']
}
}).$mount()
expect(vm.$children[0].getInternalValue()).to.deep.equal(['1', '2', '3'])
expect(vm.$children[0].getInternalValue(vm.value)).to.deep.equal(['1', '2', '3'])
})
})
describe('when multiple == FALSE', () => {
Expand All @@ -2856,7 +2884,7 @@ describe('Multiselect.vue', () => {
source: ['1', '2', '3', '4', '5']
}
}).$mount()
expect(vm.$children[0].getInternalValue()).to.deep.equal(['1'])
expect(vm.$children[0].getInternalValue(vm.value)).to.deep.equal(['1'])
})
})
describe('and the selection is empty', () => {
Expand Down

0 comments on commit 63879c4

Please sign in to comment.