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: [Table] Pagination input label's 'for' property needs to align with the input id #356

Merged
merged 6 commits into from
May 30, 2024
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
20 changes: 13 additions & 7 deletions src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import classNames from 'classnames';
import type { EventHandler, ReactElement, SyntheticEvent } from 'react';
import { useEffect, useState } from 'react';
import { useEffect, useId, useState } from 'react';
import { noOp } from '../../utils/noOp';
import { Icon } from '../Icon/Icon';
import { Label } from '../Label/Label';
import './pagination.less';
import { MIN_PAGE } from './paginationConstants';

export interface PaginationProperties {
/** Identifier of the table this element controls */
tableId?: string;
/** Currently displayed page number */
page: number;
/** Total number of available pages */
Expand Down Expand Up @@ -79,12 +81,14 @@ const PaginationNavButton = ({
};

interface PaginationInputProperties {
tableId: string;
page: number;
pageCount: number;
onChange: (value: number) => void;
}

const PaginationInput = ({
tableId,
page,
pageCount,
onChange
Expand All @@ -93,17 +97,15 @@ const PaginationInput = ({
onChange(Number.parseInt(event.currentTarget.value, 10));
};

const inputId = `${tableId}-pagination_current-page`;

return (
<Label
className='m-pagination_label'
htmlFor='m-pagination_current-page'
inline
>
<Label className='m-pagination_label' htmlFor={inputId} inline>
Page
<span className='u-visually-hidden'>number {page} out</span>
<input
className='m-pagination_current-page'
id='m-pagination_current-page-default'
id={inputId}
name='page'
type='number'
min='1'
Expand All @@ -124,6 +126,7 @@ const PaginationInput = ({
* Source: https://cfpb.github.io/design-system/components/pagination
*/
export const Pagination = ({
tableId,
page,
pageCount,
onClickPrevious = noOp,
Expand All @@ -143,6 +146,8 @@ export const Pagination = ({
onClickGo(targetPage);
};

const paginationId = useId();

const onInputChange = setPageNumber;

return (
Expand All @@ -168,6 +173,7 @@ export const Pagination = ({
onSubmit={onSubmit}
>
<PaginationInput
tableId={tableId ?? paginationId}
page={pageNumber}
pageCount={pageCount}
onChange={onInputChange}
Expand Down
10 changes: 8 additions & 2 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classNames from 'classnames';
import type { ForwardedRef, HTMLProps } from 'react';
import { forwardRef, type ReactNode } from 'react';
import { forwardRef, useId, type ReactNode } from 'react';
import type { JSXElement } from '~/src/types/jsxElement';
import { type WidthPercent } from '../../types/WidthPercent';
import { Pagination } from '../Pagination/Pagination';
Expand Down Expand Up @@ -28,6 +28,8 @@ export interface TableColumnConfiguration {
export type TableColumn = TableColumnConfiguration | string;

export interface TableProperties {
// Unique identifier
id?: string;
// Table description, displayed atop the table
caption?: ReactNode;
// Array of column headers or column configurations
Expand Down Expand Up @@ -66,6 +68,7 @@ export const Table = forwardRef<
>(
(
{
id,
caption,
columns,
rows,
Expand All @@ -90,6 +93,8 @@ export const Table = forwardRef<
perPage
});

const tableId = useId();

const tableClassnames = [];

if (isResponsive || isDirectory)
Expand All @@ -105,13 +110,14 @@ export const Table = forwardRef<
data-testid='table-testid'
className={classNames(tableClassnames)}
ref={tableRef}
id={id ?? tableId}
{...others}
>
<Caption>{caption}</Caption>
{buildColumnHeaders(columns)}
{buildRows(visibleRows, columns)}
</table>
{isPaginated ? <Pagination {...paginationProperties} /> : null}
{isPaginated ? <Pagination {...paginationProperties} tableId={id ?? tableId}/> : null}
shindigira marked this conversation as resolved.
Show resolved Hide resolved
</>
);

Expand Down