Skip to content

Commit f67a352

Browse files
committed
throw console warning if option[label] does not exist, prevents TypeError from breaking page
closes sagalbot#155
1 parent e1a100a commit f67a352

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

dist/vue-select.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-select.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-select",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A native Vue.js component that provides similar functionality to Select2 without the overhead of jQuery.",
55
"author": "Jeff Sagal <sagalbot@gmail.com>",
66
"private": false,

src/components/Select.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,10 @@
715715
*/
716716
filteredOptions() {
717717
let options = this.mutableOptions.filter((option) => {
718-
if (typeof option === 'object') {
718+
if (typeof option === 'object' && option.hasOwnProperty(this.label)) {
719719
return option[this.label].toLowerCase().indexOf(this.search.toLowerCase()) > -1
720+
} else if (typeof option === 'object' && !option.hasOwnProperty(this.label)) {
721+
return console.warn(`[vue-select warn]: Label key "option.${this.label}" does not exist in options object.\nhttp://sagalbot.github.io/vue-select/#ex-labels`)
720722
}
721723
return option.toLowerCase().indexOf(this.search.toLowerCase()) > -1
722724
})

test/unit/specs/Select.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,20 @@ describe('Select.vue', () => {
679679
expect(vm.$children[0].$refs.toggle.querySelector('.selected-tag').textContent).toContain('Baz')
680680
})
681681

682+
it('will console.warn when options contain objects without a valid label key', (done) => {
683+
spyOn(console, 'warn')
684+
const vm = new Vue({
685+
template: '<div><v-select :options="[{}]"></v-select></div>',
686+
}).$mount()
687+
Vue.nextTick(() => {
688+
expect(console.warn).toHaveBeenCalledWith(
689+
'[vue-select warn]: Label key "option.label" does not exist in options object.' +
690+
'\nhttp://sagalbot.github.io/vue-select/#ex-labels'
691+
)
692+
done()
693+
})
694+
})
695+
682696
it('should display a placeholder if the value is empty', (done) => {
683697
const vm = new Vue({
684698
template: '<div><v-select :options="options" placeholder="foo"></v-select></div>',

0 commit comments

Comments
 (0)