Skip to content

Commit

Permalink
fix(data-table): prefix internal ID for radio button, checkbox
Browse files Browse the repository at this point in the history
Fixes #2081
  • Loading branch information
metonym committed Jan 8, 2025
1 parent f3a8d99 commit 53ebf78
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/DataTable/DataTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@
const batchSelectedIds = writable(false);
const tableRows = writable(rows);
// Internal ID prefix for radio buttons, checkboxes, etc.
// since there may be multiple `DataTable` instances that have overlapping row ids.
const id = "ccs-" + Math.random().toString(36);
// Store a copy of the original rows for filter restoration.
$: originalRows = [...rows];
Expand Down Expand Up @@ -499,7 +503,7 @@
{#if !nonSelectableRowIds.includes(row.id)}
{#if radio}
<RadioButton
name="select-row-{row.id}"
name="{id}-{row.id}"
checked={selectedRowIds.includes(row.id)}
on:change={() => {
selectedRowIds = [row.id];
Expand All @@ -508,7 +512,7 @@
/>
{:else}
<InlineCheckbox
name="select-row-{row.id}"
name="{id}-{row.id}"
checked={selectedRowIds.includes(row.id)}
on:change={() => {
if (selectedRowIds.includes(row.id)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/App.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Accordion from "./Accordion/Accordion.test.svelte";
import AccordionProgrammatic from "./Accordion/Accordion.programmatic.test.svelte";
import AccordionDisabled from "./Accordion/Accordion.disabled.test.svelte";
import DuplicateDataTables from "./DataTable/DuplicateDataTables.test.svelte";
import TreeView from "./TreeView/TreeView.test.svelte";
import TreeViewHierarchy from "./TreeView/TreeView.hierarchy.test.svelte";
import RecursiveList from "./RecursiveList/RecursiveList.test.svelte";
Expand Down Expand Up @@ -32,6 +33,11 @@
name: "AccordionDisabled",
component: AccordionDisabled,
},
{
path: "/data-table",
name: "DataTable",
component: DuplicateDataTables,
},
{
path: "/recursive-list",
name: "RecursiveList",
Expand Down
16 changes: 16 additions & 0 deletions tests/DataTable/DuplicateDataTables.test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts">
import { DataTable } from "carbon-components-svelte";
const headers = [
{ key: "id", value: "id" },
{ key: "contact.company", value: "Company name" },
] as const;
const rows = [
{ id: "abc123", contact: { company: "Super Wheels" } },
{ id: "321baf", contact: { company: "Mini Wheels" } },
];
</script>

<DataTable radio {headers} {rows} />
<DataTable radio {headers} {rows} />

0 comments on commit 53ebf78

Please sign in to comment.