Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(data-table): prefix internal ID for radio button, checkbox #2082

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
Comment on lines 505 to 509
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can, I suggest taking this opportunity to provide a solution that affords progressive enhancement:

  • Have the name attribute be defined by the component consumers; perhaps exposed as a prop, that way every Radio button in the table has a matching name to be gathered server-side via formData.get('school')
  • Have the value attribute match the given Row's ID, available in row.id

In this way, a DataTable can be wrapped in <form> and be submittable without JS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. You're right – the name should in fact be the same for all rows in the table (barring the select all).

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: "1", contact: { company: "Company 1" } },
{ id: "2", contact: { company: "Company 2" } },
];
</script>

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