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

Improve Accessibility - captions #957

Merged
merged 2 commits into from
Jan 24, 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
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'}
/>
);
Loading