The problem is best shown with an actual example.
<script>
let searchInput = '';
let values = ['a', 'b', 'c', 'd', 'e', 'f'];
$: filteredValues = searchInput ? values.filter(v => v.toLowerCase().includes(searchInput.toLowerCase())) : values;
let chosenValues = [];
</script>
<input type="search" placeholder="Search" bind:value={searchInput} />
{#each filteredValues as value (value)}
<div class="checkbox">
<input type="checkbox" bind:group={chosenValues} value={value} />
{value}
</div>
{/each}
{chosenValues}
Also available on the REPL: https://svelte.dev/repl/5af87332d81e4d82835bcd0f47ff9d81?version=3.44.1.
The problem happens as follows:
- You select options a and b
- You search for c
- You select option c
Now the chosenValues only holds c; a and b are gone. I guess I could loop over all the values and hide the ones not part of hiddenValues, so that they're still part of the DOM, but why does this even happen, and is there an easier way to deal with this?