Skip to content

Commit

Permalink
Improve Accessibility - captions (#957)
Browse files Browse the repository at this point in the history
* Improve Accessibility - captions

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption

> A caption functions like a heading for a table. Most screen readers announce the content of captions. Captions help users to find a table and understand what it’s about and decide if they want to read it. If the user uses “Tables Mode”, captions are the primary mechanism to identify tables. The caption is provided by the <caption> element.

from: https://www.w3.org/WAI/tutorials/tables/caption-summary/

* renderCaption

---------

Co-authored-by: Kevin Van Cott <[email protected]>
  • Loading branch information
lalong13 and KevinVandy authored Jan 24, 2024
1 parent cdb9e18 commit 1a633e9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const MRT_Table = <TData extends MRT_RowData>({
layoutMode,
memoMode,
muiTableProps,
renderCaption,
},
} = table;
const { columnSizing, columnSizingInfo, columnVisibility, isFullScreen } =
Expand All @@ -37,6 +38,8 @@ export const MRT_Table = <TData extends MRT_RowData>({
...rest,
};

const caption = parseFromValuesOrFunc(renderCaption, { table });

const columnSizeVars = useMemo(() => {
const headers = getFlatHeaders();
const colSizes: { [key: string]: number } = {};
Expand Down Expand Up @@ -67,6 +70,7 @@ export const MRT_Table = <TData extends MRT_RowData>({
...(parseFromValuesOrFunc(tableProps?.sx, theme) as any),
})}
>
{!!caption && <caption>{caption}</caption>}
{enableTableHead && <MRT_TableHead {...commonTableGroupProps} />}
{memoMode === 'table-body' || columnSizingInfo.isResizingColumn ? (
<Memo_MRT_TableBody {...commonTableGroupProps} />
Expand Down
3 changes: 3 additions & 0 deletions packages/material-react-table/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,9 @@ export type MRT_TableOptions<TData extends MRT_RowData> = Omit<
renderBottomToolbarCustomActions?: (props: {
table: MRT_TableInstance<TData>;
}) => ReactNode;
renderCaption?:
| ((props: { table: MRT_TableInstance<TData> }) => ReactNode)
| ReactNode;
renderColumnActionsMenuItems?: (props: {
closeMenu: () => void;
column: MRT_Column<TData>;
Expand Down
66 changes: 66 additions & 0 deletions packages/material-react-table/stories/styling/Caption.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { type MRT_ColumnDef, MaterialReactTable } from '../../src';
import { faker } from '@faker-js/faker';
import { type Meta } from '@storybook/react';

const meta: Meta = {
title: 'Styling/Caption Examples',
};

export default meta;

const columns: MRT_ColumnDef<(typeof data)[0]>[] = [
{
accessorKey: 'firstName',
header: 'First Name',
},
{
accessorKey: 'lastName',
header: 'Last Name',
},
{
accessorKey: 'age',
header: 'Age',
},
{
accessorKey: 'address',
header: 'Address',
},
{
accessorKey: 'state',
header: 'State',
},
{
accessorKey: 'phoneNumber',
header: 'Phone Number',
},
];

const data = [...Array(25)].map(() => ({
address: faker.location.streetAddress(),
age: faker.number.int({ max: 60, min: 20 }),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
phoneNumber: faker.phone.number(),
state: faker.location.state(),
}));

export const BottomCaption = () => (
<MaterialReactTable
columns={columns}
data={data}
renderCaption={() => 'Table Caption'}
/>
);

export const TopCaption = () => (
<MaterialReactTable
columns={columns}
data={data}
muiTableProps={{
sx: {
captionSide: 'top',
},
}}
renderCaption={() => 'Table Caption'}
/>
);

2 comments on commit 1a633e9

@vercel
Copy link

@vercel vercel bot commented on 1a633e9 Jan 24, 2024

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 1a633e9 Jan 24, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.