From cde2316e4f711f381033c3cacd7440e5b77882d9 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 9 Nov 2019 14:19:19 -0800 Subject: [PATCH] pull $emit out of create-option function --- src/components/Select.vue | 17 ++++++----------- tests/unit/Tagging.spec.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/components/Select.vue b/src/components/Select.vue index c9f5d8c..28cd342 100644 --- a/src/components/Select.vue +++ b/src/components/Select.vue @@ -296,8 +296,7 @@ `to generate unique key. Please provide'getOptionKey' prop ` + `to return a unique key for each option.\n` + 'https://vue-select.org/api/props.html#getoptionkey' - ) - return null + ); } } } @@ -402,14 +401,9 @@ */ createOption: { type: Function, - default(newOption) { - if (typeof this.optionList[0] === 'object') { - newOption = {[this.label]: newOption} - } - - this.$emit('option:created', newOption) - return newOption - } + default (option) { + return (typeof this.optionList[0] === 'object') ? {[this.label]: option} : option; + }, }, /** @@ -581,7 +575,8 @@ select(option) { if (!this.isOptionSelected(option)) { if (this.taggable && !this.optionExists(option)) { - option = this.createOption(option) + option = this.createOption(option); + this.$emit('option:created', option); } if (this.multiple) { option = this.selectedValue.concat(option) diff --git a/tests/unit/Tagging.spec.js b/tests/unit/Tagging.spec.js index d7c8de8..f0e2176 100755 --- a/tests/unit/Tagging.spec.js +++ b/tests/unit/Tagging.spec.js @@ -80,6 +80,20 @@ describe("When Tagging Is Enabled", () => { expect(Select.vm.optionList).toEqual(["one", "two", "three"]); }); + it("should pushTags even if the consumer has defined a createOption callback", () => { + const Select = selectWithProps({ + pushTags: true, + taggable: true, + createOption: option => option, + options: ["one", "two"] + }); + + searchSubmit(Select, "three"); + + expect(Select.vm.pushedTags).toEqual(["three"]); + expect(Select.vm.optionList).toEqual(["one", "two", "three"]); + }); + it("should add a freshly created option/tag to the options list when pushTags is true and filterable is false", () => { const Select = selectWithProps({ filterable: false,