Vue-select: Loosing existing selection when adding options not in list

Created on 8 Mar 2020  路  11Comments  路  Source: sagalbot/vue-select

Describe the bug
I have a block of code as below

<v-select
                    taggable
                    multiple
                    label="name"
                    id="keyword"
                    name="keyword"
                    v-validate="''"
                    data-vv-as="Skills"
                    v-model="search"
                    @search="getSkills"
                    :options="skills"
                    :reduce="skill => skill.name"
                    :create-option="skill => ({name: skill, description: null, id: null, slug: null, createdAt: null, updatedAt: null})"
                    placeholder="Select or enter your keywords"
                  >

                    <template slot="no-options">Type to search for keywords</template>
                  </v-select>

Everything works when when i select from the list of returned options, however i loose existing when immediately I try to add an option not in list.

Screenshots
Before adding option not in list

Screenshot 2020-03-08 at 04 46 15

After Adding option not in list

Screenshot 2020-03-08 at 04 46 32

Desktop (please complete the following information):

  • OS: macOs Catalina
  • Browser: Chrome
  • Version: 80.0.3987.132 (Official Build) (64-bit)
bug in progress reduce released tagging

All 11 comments

I have the same issue. In my case, it seems like the component does not run my custom create-option function: :create-option="name => ({ fullName: name, id: -1 })".

It does not return like the create-option tells it to.

Expected result:

[
 [0]: { fullName: 'John Doe', id: 2 },
 [1]: { fullName: 'myCreateOptionItem', id: -1 }
]

//... 
selectedValue: -1

Actual result:

[
 [0]: { fullName: 'John Doe', id: 2 },
 [1]: 'myCreateOptionItem',
]

//... 
selectedValue: undefined

You can see this in this provided CodePen example
It works by selecting the existing ones, but If you add a custom one, it does not work.

This issue is not affected in v3.7.0, so probably a break in 3.7.1

Locking your version to 3.7.0 solves this issue temporarily @papakay
_package.json_
"vue-select": "3.7.0"

Reduce works by taking the 'reduced' value, and finding the complete object within the options list. There's an internal method findOptionFromReducedValue that loops the options and tracks down the original.

I haven't tracked down what changed so that this started happening (I assume it has to do with the comparison changing to getOptionKey), but I've figured out a patch and should be able to get it out later today.

  • [x] delete maybePushTags, always push and track them
  • [x] findOptionFromReducedValue checks pushedTags and options
  • [x] modify optionList to conditionally concat pushedTags if pushTags is true
  • [x] add regression test

You are the hero we deserve 馃帠

I appreciate your great work @sagalbot

Duplicate #993

I have a fix for this ready to go, but there's an issue caused when create-option doesn't return a
value that will be unique when passed to reduce.

https://github.com/sagalbot/vue-select/pull/1091#issuecomment-596700873

Mar-09-2020 11-25-58

      <v-select
        v-model="selected"
        :options="[]"
        label="label"
        taggable
        push-tags
        :reduce="name => name.value"
        :create-option="label => ({ label, value: -1 })"
      />

@BlitZz96 you'd get bit by this issue in your example.

Now that I'm typing this out I might have a solution...

  1. findOptionFromReducedValue uses .filter() instead of .find()
  2. if the length of the array returned is 1, just return that value
  3. if the length of the array is > 1, compare the stringified return values against the pushedTags with .find().

That should give you the correct value, and then it doesn't matter if create-option generates unique reduced values. Can take a crack at that later today.

:tada: This issue has been resolved in version 3.8.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

This one should be solved. Let me know if you run into any issues! 馃帀

Thank you for your time @sagalbot. I figured I did not need push-tags after all, so this fix works perfect. I really appreciate your time fixing this. You should set up a patreon for the project. 馃挼

@BlitZz96 happy to help!

I'm signed up for GitHub sponsors https://github.com/sponsors/sagalbot, but I haven't advertised it much. Definitely need to advertise a bit more 鈥撀營 think I'll add the link to the automated release comment, as well as the docs and readme 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gilles6 picture gilles6  路  3Comments

lau-a picture lau-a  路  3Comments

theseawolves picture theseawolves  路  4Comments

xuwenhao picture xuwenhao  路  3Comments

PrimozRome picture PrimozRome  路  4Comments