Skip to content

Commit dd6cbac

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

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/DataTable/DataTable.svelte

Lines changed: 12 additions & 2 deletions
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
@@ -370,6 +374,8 @@
370374
<InlineCheckbox
371375
bind:ref={refSelectAll}
372376
aria-label="Select all rows"
377+
name="{id}-select-all"
378+
value="all"
373379
checked={selectAll}
374380
{indeterminate}
375381
on:change={(e) => {
@@ -497,9 +503,12 @@
497503
class:bx--table-column-radio={radio}
498504
>
499505
{#if !nonSelectableRowIds.includes(row.id)}
506+
{@const inputId = `${id}-${row.id}`}
507+
{@const inputName = `${id}-name`}
500508
{#if radio}
501509
<RadioButton
502-
name="select-row-{row.id}"
510+
id={inputId}
511+
name={inputName}
503512
checked={selectedRowIds.includes(row.id)}
504513
on:change={() => {
505514
selectedRowIds = [row.id];
@@ -508,7 +517,8 @@
508517
/>
509518
{:else}
510519
<InlineCheckbox
511-
name="select-row-{row.id}"
520+
id={inputId}
521+
name={inputName}
512522
checked={selectedRowIds.includes(row.id)}
513523
on:change={() => {
514524
if (selectedRowIds.includes(row.id)) {

tests/App.test.svelte

Lines changed: 6 additions & 0 deletions
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",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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} />
17+
18+
<DataTable batchSelection selectable {headers} {rows} />
19+
<DataTable batchSelection selectable {headers} {rows} />

0 commit comments

Comments
 (0)