-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MRT Display Columns with better column resizing performance and less …
…memoization (#949) * display columns in their own hooks * progress, but will have to revert back to hooks again * refactor display columns away from hooks still * improve column resizing performance by caching column defs * Lint and Prettier --------- Co-authored-by: KevinVandy <kevinvandy@users.noreply.github.com>
- Loading branch information
1 parent
ec17f00
commit 185bcbd
Showing
20 changed files
with
595 additions
and
480 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/material-react-table/src/hooks/display-columns/getMRT_DisplayColumns.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
type MRT_ColumnDef, | ||
type MRT_RowData, | ||
type MRT_StatefulTableOptions, | ||
} from '../../types'; | ||
import { getMRT_RowActionsColumnDef } from './getMRT_RowActionsColumnDef'; | ||
import { getMRT_RowDragColumnDef } from './getMRT_RowDragColumnDef'; | ||
import { getMRT_RowExpandColumnDef } from './getMRT_RowExpandColumnDef'; | ||
import { getMRT_RowNumbersColumnDef } from './getMRT_RowNumbersColumnDef'; | ||
import { getMRT_RowPinningColumnDef } from './getMRT_RowPinningColumnDef'; | ||
import { getMRT_RowSelectColumnDef } from './getMRT_RowSelectColumnDef'; | ||
import { getMRT_RowSpacerColumnDef } from './getMRT_RowSpacerColumnDef'; | ||
|
||
export const getMRT_DisplayColumns = <TData extends MRT_RowData>( | ||
tableOptions: MRT_StatefulTableOptions<TData>, | ||
): MRT_ColumnDef<TData>[] => { | ||
return [ | ||
getMRT_RowNumbersColumnDef(tableOptions), | ||
getMRT_RowSelectColumnDef(tableOptions), | ||
getMRT_RowExpandColumnDef(tableOptions), | ||
getMRT_RowActionsColumnDef(tableOptions), | ||
getMRT_RowDragColumnDef(tableOptions), | ||
getMRT_RowPinningColumnDef(tableOptions), | ||
getMRT_RowSpacerColumnDef(tableOptions), | ||
].filter(Boolean) as MRT_ColumnDef<TData>[]; | ||
}; |
34 changes: 34 additions & 0 deletions
34
packages/material-react-table/src/hooks/display-columns/getMRT_RowActionsColumnDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { MRT_ToggleRowActionMenuButton } from '../../components/buttons/MRT_ToggleRowActionMenuButton'; | ||
import { | ||
type MRT_ColumnDef, | ||
type MRT_RowData, | ||
type MRT_StatefulTableOptions, | ||
} from '../../types'; | ||
import { | ||
defaultDisplayColumnProps, | ||
showRowActionsColumn, | ||
} from '../../utils/displayColumn.utils'; | ||
|
||
export const getMRT_RowActionsColumnDef = <TData extends MRT_RowData>( | ||
tableOptions: MRT_StatefulTableOptions<TData>, | ||
): MRT_ColumnDef<TData> | null => { | ||
if (!showRowActionsColumn(tableOptions)) { | ||
return null; | ||
} | ||
|
||
return { | ||
Cell: ({ cell, row, staticRowIndex, table }) => ( | ||
<MRT_ToggleRowActionMenuButton | ||
cell={cell} | ||
row={row} | ||
staticRowIndex={staticRowIndex} | ||
table={table} | ||
/> | ||
), | ||
...defaultDisplayColumnProps({ | ||
header: 'actions', | ||
id: 'mrt-row-actions', | ||
tableOptions, | ||
}), | ||
}; | ||
}; |
34 changes: 34 additions & 0 deletions
34
packages/material-react-table/src/hooks/display-columns/getMRT_RowDragColumnDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { type RefObject } from 'react'; | ||
import { MRT_TableBodyRowGrabHandle } from '../../components/body/MRT_TableBodyRowGrabHandle'; | ||
import { | ||
type MRT_ColumnDef, | ||
type MRT_RowData, | ||
type MRT_StatefulTableOptions, | ||
} from '../../types'; | ||
import { | ||
defaultDisplayColumnProps, | ||
showRowDragColumn, | ||
} from '../../utils/displayColumn.utils'; | ||
|
||
export const getMRT_RowDragColumnDef = <TData extends MRT_RowData>( | ||
tableOptions: MRT_StatefulTableOptions<TData>, | ||
): MRT_ColumnDef<TData> | null => { | ||
if (!showRowDragColumn(tableOptions)) { | ||
return null; | ||
} | ||
|
||
return { | ||
Cell: ({ row, rowRef, table }) => ( | ||
<MRT_TableBodyRowGrabHandle | ||
row={row} | ||
rowRef={rowRef as RefObject<HTMLTableRowElement>} | ||
table={table} | ||
/> | ||
), | ||
...defaultDisplayColumnProps({ | ||
header: 'move', | ||
id: 'mrt-row-drag', | ||
tableOptions, | ||
}), | ||
}; | ||
}; |
90 changes: 90 additions & 0 deletions
90
packages/material-react-table/src/hooks/display-columns/getMRT_RowExpandColumnDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { type ReactNode } from 'react'; | ||
import Stack from '@mui/material/Stack'; | ||
import Tooltip from '@mui/material/Tooltip'; | ||
import { MRT_ExpandAllButton } from '../../components/buttons/MRT_ExpandAllButton'; | ||
import { MRT_ExpandButton } from '../../components/buttons/MRT_ExpandButton'; | ||
import { | ||
type MRT_ColumnDef, | ||
type MRT_RowData, | ||
type MRT_StatefulTableOptions, | ||
} from '../../types'; | ||
import { | ||
defaultDisplayColumnProps, | ||
showRowExpandColumn, | ||
} from '../../utils/displayColumn.utils'; | ||
import { getCommonTooltipProps } from '../../utils/style.utils'; | ||
|
||
export const getMRT_RowExpandColumnDef = <TData extends MRT_RowData>( | ||
tableOptions: MRT_StatefulTableOptions<TData>, | ||
): MRT_ColumnDef<TData> | null => { | ||
if (!showRowExpandColumn(tableOptions)) { | ||
return null; | ||
} | ||
|
||
const { | ||
defaultColumn, | ||
enableExpandAll, | ||
groupedColumnMode, | ||
positionExpandColumn, | ||
state: { grouping }, | ||
} = tableOptions; | ||
|
||
const alignProps = | ||
positionExpandColumn === 'last' | ||
? ({ | ||
align: 'right', | ||
} as const) | ||
: undefined; | ||
|
||
return { | ||
Cell: ({ cell, column, row, staticRowIndex, table }) => { | ||
const expandButtonProps = { row, staticRowIndex, table }; | ||
const subRowsLength = row.subRows?.length; | ||
if (groupedColumnMode === 'remove' && row.groupingColumnId) { | ||
return ( | ||
<Stack alignItems="center" flexDirection="row" gap="0.25rem"> | ||
<MRT_ExpandButton {...expandButtonProps} /> | ||
<Tooltip | ||
{...getCommonTooltipProps('right')} | ||
title={table.getColumn(row.groupingColumnId).columnDef.header} | ||
> | ||
<span>{row.groupingValue as ReactNode}</span> | ||
</Tooltip> | ||
{!!subRowsLength && <span>({subRowsLength})</span>} | ||
</Stack> | ||
); | ||
} else { | ||
return ( | ||
<> | ||
<MRT_ExpandButton {...expandButtonProps} /> | ||
{column.columnDef.GroupedCell?.({ cell, column, row, table })} | ||
</> | ||
); | ||
} | ||
}, | ||
Header: enableExpandAll | ||
? ({ table }) => { | ||
return ( | ||
<> | ||
<MRT_ExpandAllButton table={table} /> | ||
{groupedColumnMode === 'remove' && | ||
grouping | ||
?.map( | ||
(groupedColumnId) => | ||
table.getColumn(groupedColumnId).columnDef.header, | ||
) | ||
?.join(', ')} | ||
</> | ||
); | ||
} | ||
: undefined, | ||
muiTableBodyCellProps: alignProps, | ||
muiTableHeadCellProps: alignProps, | ||
...defaultDisplayColumnProps({ | ||
header: 'expand', | ||
id: 'mrt-row-expand', | ||
size: groupedColumnMode === 'remove' ? defaultColumn?.size : 60, | ||
tableOptions, | ||
}), | ||
}; | ||
}; |
34 changes: 34 additions & 0 deletions
34
packages/material-react-table/src/hooks/display-columns/getMRT_RowNumbersColumnDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
type MRT_ColumnDef, | ||
type MRT_RowData, | ||
type MRT_StatefulTableOptions, | ||
} from '../../types'; | ||
import { | ||
defaultDisplayColumnProps, | ||
showRowNumbersColumn, | ||
} from '../../utils/displayColumn.utils'; | ||
|
||
export const getMRT_RowNumbersColumnDef = <TData extends MRT_RowData>( | ||
tableOptions: MRT_StatefulTableOptions<TData>, | ||
): MRT_ColumnDef<TData> | null => { | ||
if (!showRowNumbersColumn(tableOptions)) { | ||
return null; | ||
} | ||
|
||
const { rowNumberDisplayMode } = tableOptions; | ||
const { | ||
pagination: { pageIndex, pageSize }, | ||
} = tableOptions.state; | ||
|
||
return { | ||
Cell: ({ row, staticRowIndex }) => | ||
((rowNumberDisplayMode === 'static' | ||
? (staticRowIndex || 0) + (pageSize || 0) * (pageIndex || 0) | ||
: row.index) ?? 0) + 1, | ||
...defaultDisplayColumnProps({ | ||
header: 'rowNumbers', | ||
id: 'mrt-row-numbers', | ||
tableOptions, | ||
}), | ||
}; | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/material-react-table/src/hooks/display-columns/getMRT_RowPinningColumnDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { MRT_TableBodyRowPinButton } from '../../components/body/MRT_TableBodyRowPinButton'; | ||
import { | ||
type MRT_ColumnDef, | ||
type MRT_RowData, | ||
type MRT_StatefulTableOptions, | ||
} from '../../types'; | ||
import { | ||
defaultDisplayColumnProps, | ||
showRowPinningColumn, | ||
} from '../../utils/displayColumn.utils'; | ||
|
||
export const getMRT_RowPinningColumnDef = <TData extends MRT_RowData>( | ||
tableOptions: MRT_StatefulTableOptions<TData>, | ||
): MRT_ColumnDef<TData> | null => { | ||
if (!showRowPinningColumn(tableOptions)) { | ||
return null; | ||
} | ||
|
||
return { | ||
Cell: ({ row, table }) => ( | ||
<MRT_TableBodyRowPinButton row={row} table={table} /> | ||
), | ||
...defaultDisplayColumnProps({ | ||
header: 'pin', | ||
id: 'mrt-row-pin', | ||
tableOptions, | ||
}), | ||
}; | ||
}; |
39 changes: 39 additions & 0 deletions
39
packages/material-react-table/src/hooks/display-columns/getMRT_RowSelectColumnDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { MRT_SelectCheckbox } from '../../components/inputs/MRT_SelectCheckbox'; | ||
import { | ||
type MRT_ColumnDef, | ||
type MRT_RowData, | ||
type MRT_StatefulTableOptions, | ||
} from '../../types'; | ||
import { | ||
defaultDisplayColumnProps, | ||
showRowSelectionColumn, | ||
} from '../../utils/displayColumn.utils'; | ||
|
||
export const getMRT_RowSelectColumnDef = <TData extends MRT_RowData>( | ||
tableOptions: MRT_StatefulTableOptions<TData>, | ||
): MRT_ColumnDef<TData> | null => { | ||
if (!showRowSelectionColumn(tableOptions)) { | ||
return null; | ||
} | ||
|
||
const { enableMultiRowSelection, enableSelectAll } = tableOptions; | ||
|
||
return { | ||
Cell: ({ row, staticRowIndex, table }) => ( | ||
<MRT_SelectCheckbox | ||
row={row} | ||
staticRowIndex={staticRowIndex} | ||
table={table} | ||
/> | ||
), | ||
Header: | ||
enableSelectAll && enableMultiRowSelection | ||
? ({ table }) => <MRT_SelectCheckbox selectAll table={table} /> | ||
: undefined, | ||
...defaultDisplayColumnProps({ | ||
header: 'select', | ||
id: 'mrt-row-select', | ||
tableOptions, | ||
}), | ||
}; | ||
}; |
Oops, something went wrong.
185bcbd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
material-react-table-storybook – ./
material-react-table-storybook.vercel.app
www.material-react-table.dev
material-react-table-storybook-kevinvandy-s-team.vercel.app
material-react-table-storybook-git-v2-kevinvandy-s-team.vercel.app
material-react-table.dev