Skip to content

Commit

Permalink
renderCaption
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jan 24, 2024
1 parent 75285d6 commit de588ff
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export const MRT_Table = <TData extends MRT_RowData>({
getFlatHeaders,
getState,
options: {
caption,
columns,
enableStickyHeader,
enableTableFooter,
enableTableHead,
layoutMode,
memoMode,
muiTableProps,
renderCaption,
},
} = table;
const { columnSizing, columnSizingInfo, columnVisibility, isFullScreen } =
Expand All @@ -38,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 @@ -68,7 +70,7 @@ export const MRT_Table = <TData extends MRT_RowData>({
...(parseFromValuesOrFunc(tableProps?.sx, theme) as any),
})}
>
{caption && (typeof caption === 'string' ? <caption>{caption}</caption> : caption)}
{!!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'}
/>
);

0 comments on commit de588ff

Please sign in to comment.