Skip to content

Svelte 5: Nested {#each} causes bind:group to improperly clear #10345

@mass8326

Description

@mass8326

Describe the bug

bind:group should only add or remove the single value that its element contains.

However, when updating an element in a nested {#each} block, it will clear any values added from other child {#each} blocks.

In the linked REPL:

  1. Select "1a"
  2. Select "2a"
  3. Expected output should be ["1a", "2a"]
  4. Actual output is ["2a"]

For anyone else running into this issue, this can be worked around in the meantime by using a manual onchange handler

import type { ChangeEventHandler } from "svelte/elements";
const onchange: ChangeEventHandler<HTMLInputElement> = ({ currentTarget }) => {
  if (currentTarget.checked) {
    group = [...group, currentTarget.value];
  } else {
    const index = group.indexOf(currentTarget.value);
    if (index !== -1)
      group = [...group.slice(0, index), ...group.slice(index + 1)];
  }
};

Reproduction

Bugged Svelte 5 REPL (Workaround)
Working Svelte 4 REPL

Logs

No response

System Info

N/A

Severity

annoyance

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions