Skip to content

Commit

Permalink
improve sort with hints
Browse files Browse the repository at this point in the history
  • Loading branch information
esizer committed Jan 10, 2025
1 parent de0ce61 commit 2372f18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ const ResponsiveTable = <TData extends object, TFilters = object>({
};
}

const canSort = table
.getFlatHeaders()
.some((header) => header.column.getCanSort());

return (
<>
<Table.Controls add={add}>
Expand Down Expand Up @@ -353,7 +357,7 @@ const ResponsiveTable = <TData extends object, TFilters = object>({
{table.getHeaderGroups().map((headerGroup) => (
<Table.HeadRow key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<Table.HeadCell key={header.id} header={header} />
<Table.HeadCell key={header.id} id={id} header={header} />
))}
</Table.HeadRow>
))}
Expand Down Expand Up @@ -397,6 +401,15 @@ const ResponsiveTable = <TData extends object, TFilters = object>({
)}
</>
)}
{canSort && (
<span id={`sortHint-${id}`} data-h2-display="base(none)">
{intl.formatMessage({
defaultMessage: "Sort",
id: "LwruRb",
description: "Hint to let users know a table column can be sorted",
})}
</span>
)}
</>
);
};
Expand Down
14 changes: 12 additions & 2 deletions apps/web/src/components/Table/ResponsiveTable/SortButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ import ArrowDownIcon from "@heroicons/react/20/solid/ArrowDownIcon";
import ArrowUpIcon from "@heroicons/react/20/solid/ArrowUpIcon";
import ArrowsUpDownIcon from "@heroicons/react/20/solid/ArrowsUpDownIcon";
import LockClosedIcon from "@heroicons/react/24/solid/LockClosedIcon";
import { ReactNode } from "react";
import { ReactNode, useId } from "react";

import { Button } from "@gc-digital-talent/ui";

interface SortButtonProps<T> {
column: Column<T, unknown>;
children: ReactNode;
locked?: boolean;
tableId: string;
}

const SortButton = <T,>({ column, locked, children }: SortButtonProps<T>) => {
const SortButton = <T,>({
column,
locked,
tableId,
children,
}: SortButtonProps<T>) => {
const intl = useIntl();
const id = useId();
const ariaId = `${column.id}-${id}`;

if (!column.getCanSort()) {
// eslint-disable-next-line react/jsx-no-useless-fragment
Expand All @@ -30,6 +38,8 @@ const SortButton = <T,>({ column, locked, children }: SortButtonProps<T>) => {

return (
<Button
id={ariaId}
aria-labelledby={`${ariaId} sortHint-${tableId}`}
mode="inline"
color="whiteFixed"
fontSize="caption"
Expand Down
7 changes: 4 additions & 3 deletions apps/web/src/components/Table/ResponsiveTable/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ type CellHTMLProps = DetailedHTMLProps<

type HeadCellProps<T> = {
header: Header<T, unknown>;
id: string;
} & CellHTMLProps;

const HeadCell = <T,>({ header, ...rest }: HeadCellProps<T>) => {
const HeadCell = <T,>({ header, id, ...rest }: HeadCellProps<T>) => {
const isRowSelect = header.column.columnDef.meta?.isRowSelect;
const shouldShrink = header.column.columnDef.meta?.shrink;
const sortingLocked = header.column.columnDef.meta?.sortingLocked;
const sortDirection = header.column.getIsSorted();
let ariaSort: AriaSort = "none";
let ariaSort: AriaSort = undefined;
if (sortDirection) {
ariaSort = sortDirection === "asc" ? "ascending" : "descending";
}
Expand All @@ -151,7 +152,7 @@ const HeadCell = <T,>({ header, ...rest }: HeadCellProps<T>) => {
{...rest}
>
{header.isPlaceholder ? null : (
<SortButton column={header.column} locked={sortingLocked}>
<SortButton tableId={id} column={header.column} locked={sortingLocked}>
{flexRender(header.column.columnDef.header, header.getContext())}
</SortButton>
)}
Expand Down

0 comments on commit 2372f18

Please sign in to comment.