Skip to content

Commit eb1567a

Browse files
authored
fix(data-table): prefix internal ID for radio button, checkbox (#2082)
Fixes #2081
1 parent f3a8d99 commit eb1567a

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/DataTable/DataTable.svelte

+6-2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@
170170
const batchSelectedIds = writable(false);
171171
const tableRows = writable(rows);
172172
173+
// Internal ID prefix for radio buttons, checkboxes, etc.
174+
// since there may be multiple `DataTable` instances that have overlapping row ids.
175+
const id = "ccs-" + Math.random().toString(36);
176+
173177
// Store a copy of the original rows for filter restoration.
174178
$: originalRows = [...rows];
175179
@@ -499,7 +503,7 @@
499503
{#if !nonSelectableRowIds.includes(row.id)}
500504
{#if radio}
501505
<RadioButton
502-
name="select-row-{row.id}"
506+
name="{id}-{row.id}"
503507
checked={selectedRowIds.includes(row.id)}
504508
on:change={() => {
505509
selectedRowIds = [row.id];
@@ -508,7 +512,7 @@
508512
/>
509513
{:else}
510514
<InlineCheckbox
511-
name="select-row-{row.id}"
515+
name="{id}-{row.id}"
512516
checked={selectedRowIds.includes(row.id)}
513517
on:change={() => {
514518
if (selectedRowIds.includes(row.id)) {

tests/App.test.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Accordion from "./Accordion/Accordion.test.svelte";
55
import AccordionProgrammatic from "./Accordion/Accordion.programmatic.test.svelte";
66
import AccordionDisabled from "./Accordion/Accordion.disabled.test.svelte";
7+
import DuplicateDataTables from "./DataTable/DuplicateDataTables.test.svelte";
78
import TreeView from "./TreeView/TreeView.test.svelte";
89
import TreeViewHierarchy from "./TreeView/TreeView.hierarchy.test.svelte";
910
import RecursiveList from "./RecursiveList/RecursiveList.test.svelte";
@@ -32,6 +33,11 @@
3233
name: "AccordionDisabled",
3334
component: AccordionDisabled,
3435
},
36+
{
37+
path: "/data-table",
38+
name: "DataTable",
39+
component: DuplicateDataTables,
40+
},
3541
{
3642
path: "/recursive-list",
3743
name: "RecursiveList",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">
2+
import { DataTable } from "carbon-components-svelte";
3+
4+
const headers = [
5+
{ key: "id", value: "id" },
6+
{ key: "contact.company", value: "Company name" },
7+
] as const;
8+
9+
const rows = [
10+
{ id: "1", contact: { company: "Company 1" } },
11+
{ id: "2", contact: { company: "Company 2" } },
12+
];
13+
</script>
14+
15+
<DataTable radio {headers} {rows} />
16+
<DataTable radio {headers} {rows} />

0 commit comments

Comments
 (0)