Skip to content

Commit 16565cb

Browse files
author
Damian Dulisz
committed
Improve getOptionLabel method
1 parent d44d394 commit 16565cb

3 files changed

Lines changed: 23 additions & 27 deletions

File tree

docs/partials/examples/_single-select-primitve.jade

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ p.typo__p Disabled all highlight label text.
1212
:show-labels="false"
1313
@update="updateValuePrimitive"
1414
placeholder="Select one"
15-
label="name"
1615
)
1716
pre.language-json
1817
code.

src/multiselectMixin.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ module.exports = {
55
return {
66
search: '',
77
isOpen: false,
8-
value: this.selected ? deepClone(this.selected) : this.multiple ? [] : null
8+
value: this.selected || this.selected === 0
9+
? deepClone(this.selected)
10+
: this.multiple ? [] : null
911
}
1012
},
1113
props: {
@@ -142,7 +144,6 @@ module.exports = {
142144
customLabel: {
143145
type: Function,
144146
default (option, label) {
145-
if (option && option.isTag) return option.label
146147
return label ? option[label] : option
147148
}
148149
},
@@ -223,8 +224,7 @@ module.exports = {
223224
: this.options
224225
},
225226
currentOptionLabel () {
226-
const label = this.getOptionLabel(this.value)
227-
return label ? label.toString() : ''
227+
return this.getOptionLabel(this.value)
228228
}
229229
},
230230
watch: {
@@ -238,11 +238,11 @@ module.exports = {
238238
},
239239
'search' () {
240240
/* istanbul ignore else */
241-
if (this.search !== this.currentOptionLabel) {
242-
this.$emit('search-change', this.search, this.id)
243-
}
241+
if (this.search === this.currentOptionLabel) return
242+
243+
this.$emit('search-change', this.search, this.id)
244244
},
245-
'selected' (newVal, oldVal) {
245+
'selected' () {
246246
this.value = deepClone(this.selected)
247247
}
248248
},
@@ -287,19 +287,16 @@ module.exports = {
287287
return !this.isSelected(option)
288288
},
289289
/**
290-
* Returns the option[this.label]
291-
* if option is Object. Otherwise check for option.label.
292-
* If non is found, return entrie option.
293-
*
290+
* Returns empty string when options is null/undefined
291+
* Returns tag query if option is tag.
292+
* Returns the customLabel() results and casts it to string.
294293
* @param {Object||String||Integer} Passed option
295294
* @returns {Object||String}
296295
*/
297296
getOptionLabel (option) {
298-
if (typeof option === 'object' && option !== null) {
299-
return this.customLabel(option, this.label)
300-
} else {
301-
return option ? this.customLabel(option) : ''
302-
}
297+
if (!option && option !== 0) return ''
298+
if (option.isTag) return option.label
299+
return this.customLabel(option, this.label) + ''
303300
},
304301
/**
305302
* Add the given option to the list of selected options

test/unit/specs/Multiselect.spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,18 @@ describe('Multiselect.vue', () => {
378378

379379
it('should set search value to equal to passed value', (done) => {
380380
const vm = new Vue({
381-
template: '<multiselect :selected="value" :options="source" label="id" key="id"></multiselect>',
381+
template: '<multiselect :selected="value" :options="source"></multiselect>',
382382
components: { Multiselect },
383383
data: {
384-
value: 2,
385-
source: [1, 2, 3]
384+
value: 0,
385+
source: [0, 1, 2, 3]
386386
}
387387
}).$mount()
388388
const comp = vm.$children[0]
389389
comp.$nextTick(() => {
390390
comp.$nextTick(() => {
391-
expect(comp.search).to.equal('2')
392-
expect(comp.$els.search.value).to.equal('2')
391+
expect(comp.search).to.equal('0')
392+
expect(comp.$els.search.value).to.equal('0')
393393
done()
394394
})
395395
})
@@ -1088,17 +1088,17 @@ describe('Multiselect.vue', () => {
10881088
expect(vm.$children[0].getOptionLabel(option)).to.equal('2')
10891089
})
10901090

1091-
it('should return option’s label when custom label is set', () => {
1091+
it('should return tag’s query when option is a tag', () => {
10921092
const vm = new Vue({
10931093
template: '<multiselect :selected="value" :options="source" :multiple="true" label="id" key="id"></multiselect>',
10941094
components: { Multiselect },
10951095
data: {
10961096
value: [],
1097-
source: [{ id: '1' }, { id: '2' }, { id: '3' }]
1097+
source: [{ isTag: true, label: 'isTag' }, { id: '2' }, { id: '3' }]
10981098
}
10991099
}).$mount()
1100-
const option = vm.$children[0].options[2]
1101-
expect(vm.$children[0].getOptionLabel(option)).to.equal('3')
1100+
const option = vm.$children[0].options[0]
1101+
expect(vm.$children[0].getOptionLabel(option)).to.equal('isTag')
11021102
})
11031103

11041104
it('should return customLabel’s interpolation if set', () => {

0 commit comments

Comments
 (0)