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

How to make 2 way binding value[]<->options[] ? #249

Closed
michaltaberski opened this issue Jul 8, 2023 · 3 comments · Fixed by #250
Closed

How to make 2 way binding value[]<->options[] ? #249

michaltaberski opened this issue Jul 8, 2023 · 3 comments · Fixed by #250
Labels
DX Developer experience enhancement New feature or request

Comments

@michaltaberski
Copy link

michaltaberski commented Jul 8, 2023

I would like to keep 2 way binding with making mapping on the way. This is not pure svelte-multiselect issue, but it's easy to visualise it in the svelte-multiselect usecase:

I would like to use values as an array of strings (ids) and dynamically map those ids to provided options (if present). It's not a problem doing this 1-way, but how to maintain the 2-way binding?

// Superior component 
<script lang="ts">
  import MultiSelect from "svelte-multiselect";
  import { ExcludeFalsy } from "../typeScriptUtils";

  type Option = { label: string; value: string };

  export let values: string[];
  export let options: Option[] = [];

  $: {
   selected = values
    .map((val) => options.find(({ value }) => val === value))
    .filter(ExcludeFalsy);
   // OFC THIS IS NOT POSSBILE (CIRCULAR UPDATE), BUT HOW TO DO THING LIKE THAT?
   // values = selected.map(({ value }) => value);
  }
</script>

<MultiSelect {options} bind:selected />
@janosh
Copy link
Owner

janosh commented Jul 9, 2023

I only have bad news on this point. I'm not aware of a solution and I'm not even sure there is a way to break the recursion in the general case.

@janosh janosh added awaiting upstream Needs new release from an upstream dependency out of scope Not the goal or not doable labels Jul 9, 2023
@michaltaberski
Copy link
Author

I have a little update that might be useful for others. Stackoverflow has helped me a bit. It does the job, although it is a little bit complicated. Nevertheless for now it's good enough.

Thanks @janosh for feedback.

@janosh
Copy link
Owner

janosh commented Jul 9, 2023

Wow, that's a very cool REPL! I had no idea this was possible. Thanks for sharing. I'll definitely use this to make value and selected each reactive to the other. 👍

@janosh janosh added enhancement New feature or request DX Developer experience and removed awaiting upstream Needs new release from an upstream dependency out of scope Not the goal or not doable labels Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX Developer experience enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants