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

[Bug] Selected options stays visible in dropdown if options are objects #234

Closed
tbydza opened this issue May 31, 2023 · 5 comments · Fixed by #238
Closed

[Bug] Selected options stays visible in dropdown if options are objects #234

tbydza opened this issue May 31, 2023 · 5 comments · Fixed by #238
Labels
bug Something isn't working discussion Gathering feedback on open questions dx Developer experience

Comments

@tbydza
Copy link

tbydza commented May 31, 2023

I define options using objects and after #217 there is problem with referential equality.

Data for my options and selected come from 2 different sources, but they have same properties.
Each option is identified by the label.
Because of that dropdown used to show only not selected options.

But change of this line caused that these option are no longer equal and they appear to be different options with same label.

And so the selected options stay in dropdown.

Example with screenshots

<script lang="ts">
  import MultiSelect from 'svelte-multiselect'
  const languages = [
  { label: `JavaScript`, value: `JavaScript` },
  { label: `TypeScript`, value: `TypeScript` },
  { label: `CoffeeScript`, value: `CoffeeScript`}
  ];

  const selectedLanguages = [{ label: `JavaScript`, value: `JavaScript` }]

</script>

<MultiSelect options={languages} selected={selectedLanguages}>
  <span slot="selected" let:option>{option.value}</span>
</MultiSelect>

svelte-multiselect-bug

Can you please fix this, or provide some workaround?

@tbydza tbydza added the bug Something isn't working label May 31, 2023
@janosh
Copy link
Owner

janosh commented May 31, 2023

Interesting edge case. The simplest workaround would be to JSON.stringify() options before comparing. But that could be a noticeable performance hit if your options contain deeply nested values or if you have 1000s of options. Maybe better would be to add a new prop equal: (op1: Option, op2: Option) => boolean that allows users to specify a custom comparison function. I'll get back to this tomorrow.

@janosh janosh added dx Developer experience discussion Gathering feedback on open questions labels May 31, 2023
@tbydza
Copy link
Author

tbydza commented May 31, 2023

equal: (op1: Option, op2: Option) => boolean would be good.
Some libraries use prop like: by: keyof Option to know which property is identifier.
But I think equal is simpler and more flexible.

For my particular use case I have 10s maybe lower 100s options with simple structure:

interface CustomOption {
    label: string
    value: string
    disabled: boolean,
    order: number
}

@janosh
Copy link
Owner

janosh commented Jun 14, 2023

@tbydza Sorry "tomorrow" turned into 2 weeks. Would be interested to hear your thoughts on #234 (if you have time to take a look). 🙏

@tbydza
Copy link
Author

tbydza commented Jun 15, 2023

@tbydza Sorry "tomorrow" turned into 2 weeks. Would be interested to hear your thoughts on #234 (if you have time to take a look). 🙏

Don't worry, I also forgot. I looked at it and tried it. It looks good and I think it will solve my issue. But I don't know how it will affect #217 use case.

@janosh
Copy link
Owner

janosh commented Jun 18, 2023

@tbydza Had a change of mind. A key function that maps options to a unique identifier seems like a much cleaner and more versatile API than having an equal() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working discussion Gathering feedback on open questions dx Developer experience
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants