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

Feature: adding top content option #1355

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
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 @@ -7,6 +7,7 @@ import {
} from '../../types';
import { parseFromValuesOrFunc } from '../../utils/utils';
import { MRT_ToolbarAlertBanner } from '../toolbar/MRT_ToolbarAlertBanner';
import { getCommonToolbarStyles } from '../../utils/style.utils';

export interface MRT_TableHeadProps<TData extends MRT_RowData>
extends TableHeadProps {
Expand All @@ -23,6 +24,8 @@ export const MRT_TableHead = <TData extends MRT_RowData>({
getState,
options: {
enableStickyHeader,
enableTopContent,
enableTopToolbar,
layoutMode,
muiTableHeadProps,
positionToolbarAlertBanner,
Expand Down Expand Up @@ -52,7 +55,12 @@ export const MRT_TableHead = <TData extends MRT_RowData>({
display: layoutMode?.startsWith('grid') ? 'grid' : undefined,
opacity: 0.97,
position: stickyHeader ? 'sticky' : 'relative',
top: stickyHeader && layoutMode?.startsWith('grid') ? 0 : undefined,
top:
stickyHeader && layoutMode?.startsWith('grid')
? 0
: enableStickyHeader && enableTopContent && enableTopToolbar
? getCommonToolbarStyles({ table, theme }).minHeight
: undefined,
zIndex: stickyHeader ? 2 : undefined,
...(parseFromValuesOrFunc(tableHeadProps?.sx, theme) as any),
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { type MRT_RowData, type MRT_TableInstance } from '../../types';
import { parseFromValuesOrFunc } from '../../utils/utils';
import { MRT_CellActionMenu } from '../menus/MRT_CellActionMenu';
import { MRT_EditRowModal } from '../modals/MRT_EditRowModal';
import { MRT_TopContent } from '../toolbar/MRT_TopContent';

const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? useLayoutEffect : useEffect;
Expand All @@ -29,6 +30,7 @@ export const MRT_TableContainer = <TData extends MRT_RowData>({
enableCellActions,
enableStickyHeader,
muiTableContainerProps,
enableTopContent,
},
refs: { bottomToolbarRef, tableContainerRef, topToolbarRef },
} = table;
Expand Down Expand Up @@ -101,6 +103,7 @@ export const MRT_TableContainer = <TData extends MRT_RowData>({
})}
>
{loading ? <MRT_TableLoadingOverlay table={table} /> : null}
{enableTopContent ? <MRT_TopContent table={table} /> : null}
<MRT_Table table={table} />
{(createModalOpen || editModalOpen) && (
<MRT_EditRowModal open table={table} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const MRT_TablePaper = <TData extends MRT_RowData>({
getState,
options: {
enableBottomToolbar,
enableTopContent,
enableTopToolbar,
mrtTheme: { baseBackgroundColor },
muiTablePaperProps,
Expand Down Expand Up @@ -76,6 +77,7 @@ export const MRT_TablePaper = <TData extends MRT_RowData>({
})}
>
{enableTopToolbar &&
!enableTopContent &&
(parseFromValuesOrFunc(renderTopToolbar, { table }) ?? (
<MRT_TopToolbar table={table} />
))}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Box from '@mui/material/Box';
import { parseFromValuesOrFunc } from '../../utils/utils';
import { MRT_TopToolbar } from './MRT_TopToolbar';
import type { MRT_RowData, MRT_TableInstance } from '../../types';

export interface MRT_TopContentProps<TData extends MRT_RowData> {
table: MRT_TableInstance<TData>;
}

export const MRT_TopContent = <TData extends MRT_RowData>({
table,
}: MRT_TopContentProps<TData>) => {
const {
options: { renderTopContent, enableTopToolbar },
} = table;
return (
<>
<Box
sx={() => ({
position: 'sticky',
insetInlineStart: 0,
})}
>
{parseFromValuesOrFunc(renderTopContent, { table })}
</Box>
{enableTopToolbar && <MRT_TopToolbar table={table} />}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const MRT_TopToolbar = <TData extends MRT_RowData>({
options: {
enableGlobalFilter,
enablePagination,
enableTopContent,
enableToolbarInternalActions,
muiTopToolbarProps,
positionGlobalFilter,
Expand Down Expand Up @@ -66,9 +67,11 @@ export const MRT_TopToolbar = <TData extends MRT_RowData>({
}}
sx={(theme) => ({
...getCommonToolbarStyles({ table, theme }),
position: isFullScreen ? 'sticky' : 'relative',
top: isFullScreen ? '0' : undefined,
position: isFullScreen || enableTopContent ? 'sticky' : 'relative',
top: isFullScreen || enableTopContent ? '0' : undefined,
...(parseFromValuesOrFunc(toolbarProps?.sx, theme) as any),
insetInlineStart: enableTopContent ? 0 : undefined,
zIndex: enableTopContent ? 3 : undefined,
})}
>
{positionToolbarAlertBanner === 'top' && (
Expand Down
4 changes: 4 additions & 0 deletions packages/material-react-table/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ export interface MRT_TableOptions<TData extends MRT_RowData>
enableTableFooter?: boolean;
enableTableHead?: boolean;
enableToolbarInternalActions?: boolean;
enableTopContent?: boolean;
enableTopToolbar?: boolean;
expandRowsFn?: (dataRow: TData) => TData[];
getRowId?: (
Expand Down Expand Up @@ -1258,6 +1259,9 @@ export interface MRT_TableOptions<TData extends MRT_RowData>
renderToolbarInternalActions?: (props: {
table: MRT_TableInstance<TData>;
}) => ReactNode;
renderTopContent?:
| ((props: { table: MRT_TableInstance<TData> }) => ReactNode)
| ReactNode;
renderTopToolbar?:
| ((props: { table: MRT_TableInstance<TData> }) => ReactNode)
| ReactNode;
Expand Down
118 changes: 118 additions & 0 deletions packages/material-react-table/stories/styling/TopContent.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { type MRT_ColumnDef, MaterialReactTable } from '../../src';
import { faker } from '@faker-js/faker';
import { type Meta } from '@storybook/react';
import Paper from '@mui/material/Paper';

const meta: Meta = {
parameters: {
status: {
type: 'stable',
},
},
title: 'Styling/Top Content Examples',
};

export default meta;

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

const data = [...Array(88)].map(() => ({
address: faker.location.streetAddress(),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
phoneNumber: faker.phone.number(),
state: faker.location.state(),
}));

export const TopContentWithStaticHeaderDefault = () => (
<MaterialReactTable
columns={columns}
data={data}
initialState={{ pagination: { pageIndex: 0, pageSize: 25 } }}
enableTopContent={true}
renderTopContent={({ table }) => (
<Paper elevation={3} sx={{ padding: '0.5rem', margin: '0.5rem' }}>
<h2 style={{ margin: '2rem', textAlign: 'center' }}>
Total rows: {table.getRowCount()}
</h2>
</Paper>
)}
/>
);

export const TopContentWithStickyHeader = () => (
<MaterialReactTable
columns={columns}
data={data}
enableStickyHeader
initialState={{ pagination: { pageIndex: 0, pageSize: 25 } }}
enableTopContent={true}
renderTopContent={({ table }) => (
<Paper elevation={3} sx={{ padding: '0.5rem', margin: '0.5rem' }}>
<h2 style={{ margin: '2rem', textAlign: 'center' }}>
Total rows: {table.getRowCount()}
</h2>
</Paper>
)}
/>
);

export const TopContentWithStickyHeaderShorterTable = () => (
<MaterialReactTable
columns={columns}
data={data}
enableColumnPinning
enableRowSelection
enableStickyHeader
initialState={{ pagination: { pageIndex: 0, pageSize: 25 } }}
muiTableContainerProps={{ sx: { maxHeight: 400 } }}
enableTopContent={true}
renderTopContent={({ table }) => (
<Paper elevation={3} sx={{ padding: '0.5rem', margin: '0.5rem' }}>
<h2 style={{ margin: '2rem', textAlign: 'center' }}>
Total rows: {table.getRowCount()}
</h2>
</Paper>
)}
/>
);

export const TopContentWithHiddenTopToolbar = () => (
<MaterialReactTable
columns={columns}
data={data}
initialState={{ pagination: { pageIndex: 0, pageSize: 25 } }}
muiTableContainerProps={{ sx: { maxHeight: 400 } }}
enableStickyHeader
enableTopToolbar={false}
enableTopContent={true}
renderTopContent={({ table }) => (
<Paper elevation={3} sx={{ padding: '0.5rem', margin: '0.5rem' }}>
<h2 style={{ margin: '2rem', textAlign: 'center' }}>
Total rows: {table.getRowCount()}
</h2>
</Paper>
)}
/>
);