Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loosing existing selection when adding options not in list #1089

Closed
papakay opened this issue Mar 8, 2020 · 11 comments · Fixed by #1091
Closed

Loosing existing selection when adding options not in list #1089

papakay opened this issue Mar 8, 2020 · 11 comments · Fixed by #1091
Assignees
Labels
bug in progress reduce Bug related to `reduce` prop released tagging Relates to the `taggable` prop

Comments

@papakay
Copy link

papakay commented Mar 8, 2020

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)
@papakay papakay changed the title Loosing existing selection when using adding options not in list Loosing existing selection when adding options not in list Mar 8, 2020
@kamerat
Copy link
Contributor

kamerat commented Mar 9, 2020

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

@kamerat
Copy link
Contributor

kamerat commented Mar 9, 2020

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

@sagalbot
Copy link
Owner

sagalbot commented Mar 9, 2020

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.

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

@sagalbot sagalbot added the bug label Mar 9, 2020
@sagalbot sagalbot self-assigned this Mar 9, 2020
@kamerat
Copy link
Contributor

kamerat commented Mar 9, 2020

You are the hero we deserve 🎖

@papakay
Copy link
Author

papakay commented Mar 9, 2020

I appreciate your great work @sagalbot

@sagalbot sagalbot added reduce Bug related to `reduce` prop tagging Relates to the `taggable` prop labels Mar 10, 2020
@sagalbot
Copy link
Owner

Duplicate #993

@sagalbot
Copy link
Owner

sagalbot commented Mar 10, 2020

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.

#1091 (comment)

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.

sagalbot added a commit that referenced this issue Mar 11, 2020
@github-actions
Copy link

🎉 This issue has been resolved in version 3.8.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@sagalbot
Copy link
Owner

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

@kamerat
Copy link
Contributor

kamerat commented Mar 12, 2020

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. 💵

@sagalbot
Copy link
Owner

@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 – I think I'll add the link to the automated release comment, as well as the docs and readme 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug in progress reduce Bug related to `reduce` prop released tagging Relates to the `taggable` prop
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants