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

Added unique id to name in TableSelectRow #17015

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 3 additions & 2 deletions packages/react/src/components/DataTable/TableSelectRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import PropTypes from 'prop-types';
import React from 'react';
import React, { useId } from 'react';
import classNames from 'classnames';
import InlineCheckbox from '../InlineCheckbox';
import RadioButton from '../RadioButton';
Expand Down Expand Up @@ -85,9 +85,10 @@ const TableSelectRow = ({
className,
}: TableSelectRowProps) => {
const prefix = usePrefix();
const uniqueNameId = useId();
const selectionInputProps = {
id,
name,
name: name ? name : uniqueNameId,
onClick: onSelect,
onChange,
checked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ exports[`DataTable behaves as expected selection -- radio buttons should not hav
>
<h4
class="cds--data-table-header__title"
id="tc-:r63:-title"
id="tc-:r71:-title"
>
DataTable with selection
</h4>
<p
class="cds--data-table-header__description"
id="tc-:r63:-description"
id="tc-:r71:-description"
/>
</div>
<div
class="cds--data-table-content"
>
<table
aria-labelledby="tc-:r63:-title"
aria-labelledby="tc-:r71:-title"
class="cds--data-table cds--data-table--lg"
>
<thead>
Expand Down Expand Up @@ -182,20 +182,20 @@ exports[`DataTable behaves as expected selection -- radio buttons should render
>
<h4
class="cds--data-table-header__title"
id="tc-:r5t:-title"
id="tc-:r6o:-title"
>
DataTable with selection
</h4>
<p
class="cds--data-table-header__description"
id="tc-:r5t:-description"
id="tc-:r6o:-description"
/>
</div>
<div
class="cds--data-table-content"
>
<table
aria-labelledby="tc-:r5t:-title"
aria-labelledby="tc-:r6o:-title"
class="cds--data-table cds--data-table--lg"
>
<thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,135 @@ export default {
},
};

export const Test = () => {
const headers1 = [
{ key: 'id', header: 'ID' },
{ key: 'name', header: 'Name' },
];
const rows1 = [
{ id: 'person1', name: 'Ivan' },
{ id: 'person2', name: 'Salvador' },
];

const headers2 = [
{ key: 'id', header: 'ID' },
{ key: 'country', header: 'Country' },
];
const rows2 = [
{ id: 'country1', country: 'Switzerland' },
{ id: 'country2', country: 'Mexico' },
];

return (
<div>
<DataTable rows={rows1} headers={headers1} radio>
{({
rows,
headers,
getHeaderProps,
getRowProps,
getSelectionProps,
getTableProps,
getTableContainerProps,
}) => (
<TableContainer
title="DataTable"
description="With radio selection"
{...getTableContainerProps()}>
<Table {...getTableProps()} aria-label="sample table">
<TableHead>
<TableRow>
<th scope="col" />
{headers.map((header, i) => (
<TableHeader
key={'header1' + i}
{...getHeaderProps({
header,
})}>
{header.header}
</TableHeader>
))}
</TableRow>
</TableHead>
<TableBody>
{rows.map((row, i) => (
<TableRow
key={'row1' + i}
{...getRowProps({
row,
})}>
<TableSelectRow
{...getSelectionProps({
row,
})}
// name="select-1"
/>
{row.cells.map((cell) => (
<TableCell key={cell.id}>{cell.value}</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
)}
</DataTable>
<DataTable rows={rows2} headers={headers2} radio>
{({
rows,
headers,
getHeaderProps,
getRowProps,
getSelectionProps,
getTableProps,
getTableContainerProps,
}) => (
<TableContainer
title="DataTable"
description="With radio selection"
{...getTableContainerProps()}>
<Table {...getTableProps()} aria-label="sample table">
<TableHead>
<TableRow>
<th scope="col" />
{headers.map((header, i) => (
<TableHeader
key={'header2' + i}
{...getHeaderProps({
header,
})}>
{header.header}
</TableHeader>
))}
</TableRow>
</TableHead>
<TableBody>
{rows.map((row, i) => (
<TableRow
key={'row2' + i}
{...getRowProps({
row,
})}>
<TableSelectRow
{...getSelectionProps({
row,
})}
name="select-2"
/>
{row.cells.map((cell) => (
<TableCell key={cell.id}>{cell.value}</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
)}
</DataTable>
</div>
);
};

export const Default = () => (
<DataTable rows={rows} headers={headers}>
{({
Expand Down
Loading