Skip to content

Commit 9d33175

Browse files
committed
add mrt hooks docs
1 parent 4fc1fad commit 9d33175

File tree

7 files changed

+427
-380
lines changed

7 files changed

+427
-380
lines changed

apps/material-react-table-docs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
"@types/node": "^20.10.6",
4545
"@types/react": "^18.2.46",
4646
"@types/react-dom": "^18.2.18",
47-
"@typescript-eslint/eslint-plugin": "^6.16.0",
48-
"@typescript-eslint/parser": "^6.16.0",
47+
"@typescript-eslint/eslint-plugin": "^6.17.0",
48+
"@typescript-eslint/parser": "^6.17.0",
4949
"eslint": "8.56.0",
5050
"eslint-config-next": "14.0.4",
5151
"next-plausible": "^3.12.0",

apps/material-react-table-docs/pages/docs/api/mrt-hooks.mdx

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,126 @@ import OptionsSwitcher from '../../../example-groups/OptionsSwitcher';
2828

2929
<br />
3030

31+
MRT has been adopting more and more of a hooks based approach for its internal logic. Its API is becoming more standardized so that you can use the same hooks that the MRT components use internally to build your own custom headless table if you want to.
32+
3133
### useMaterialReactTable
3234

33-
The main hook from Material React Table. It creates a TanStack Table instance with all of the features that MRT provides and that you enable.
35+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/useMaterialReactTable.ts)
36+
37+
> This is probably the only MRT hook you will need to use, unless you are writing a custom headless table.
38+
39+
This is the main hook from Material React Table. It creates a TanStack Table instance with all of the features that MRT provides and that you enable, and uses the proper default table options.
40+
41+
```tsx
42+
import { useMaterialReactTable } from 'material-react-table';
43+
44+
const table = useMaterialReactTable({
45+
...options,
46+
});
47+
```
48+
49+
This hook is the combination of 2 other internal MRT hooks: [`useMRT_TableOptions`](#usemrt_tableoptions), and [`useMRT_TableInstance`](#usemrt_tableinstance).
50+
51+
### useMRT_TableOptions
52+
53+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/hooks/useMRT_TableOptions.ts)
54+
55+
This hook simply takes in your custom table options and merges them with the default table options. It also does some extra logic to make sure that some default table options are set correctly based on other enabled features. For example, if you enable row virtualization features, the sticky header will also be enabled by default, and the layoutMode will adjust accordingly.
56+
57+
```tsx
58+
import { useMRT_TableOptions } from 'material-react-table';
59+
60+
const transformedTableOptions = useMRT_TableOptions({
61+
...options,
62+
});
63+
```
64+
65+
### useMRT_TableInstance
66+
67+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/hooks/useMRT_TableInstance.ts)
68+
69+
This is where most of the magic happens. This hook is responsible for creating the TanStack Table instance, and adding all of the MRT features to it. It needs table options to be passed in to it correctly, with good defaults, and it will return the table instance.
70+
71+
```tsx
72+
import { useMRT_TableInstance } from 'material-react-table';
73+
74+
const table = useMRT_TableInstance({
75+
...transformedTableOptions, //usually from useMRT_TableOptions
76+
});
77+
```
78+
79+
This hook also uses the [`useMRT_DisplayColumns`](#usemrt_displaycolumns) and [`useMRT_Effects`](#usemrt_effects) to generate the built-in display columns (row numbers, actions, selection, etc) and house some extra useEffect hooks needed by some MRT features on a table wide level.
80+
81+
### useMRT_DisplayColumns
82+
83+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/hooks/useMRT_DisplayColumns.tsx)
84+
85+
This hook is responsible for generating the built-in display columns (row numbers, actions, selection, etc) and adding them to the table instance's columns array, alongside your own defined columns.
86+
87+
### useMRT_Effects
88+
89+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/hooks/useMRT_Effects.ts)
90+
91+
This hook is responsible for adding some extra useEffect hooks to the table instance. These hooks are needed by some MRT features on a table wide level.
92+
93+
### useMRT_Rows
94+
95+
> New in v2.2.0
96+
97+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/hooks/useMRT_Rows.ts)
98+
99+
This hook is mostly a wrapper around `table.getRowModel`, but with a bit more custom logic for fuzzy ranking, row pinning, and more. It consumes a `table` instance and returns the rows that should be rendered in the main table body. This can be a useful hook if you are writing a custom headless table, but still want all of the extra MRT enhanced behavior for fuzzy ranking, row pinning, etc. Alternatively, you can just use `table.getRowModel()` for a more vanilla TanStack Table experience.
100+
101+
```tsx
102+
import { useMaterialReactTable, useMRT_Rows } from 'material-react-table';
103+
104+
const table = useMaterialReactTable({
105+
...options,
106+
});
107+
108+
const rows = useMRT_Rows(table);
109+
110+
return rows.map((row) => {
111+
//render row
112+
});
113+
```
114+
115+
### useMRT_ColumnVirtualizer
116+
117+
> New in v2.2.0
118+
119+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/hooks/useMRT_ColumnVirtualizer.ts)
120+
121+
This hook is a wrapper around the `useVirtualizer` hook from TanStack Virtual. It consumes a `table` instance and returns a Column Virtualizer instance that is optimized for MRT table columns, with considerations for other MRT features like column pinning, column resizing, column hiding, and more.
122+
123+
```tsx
124+
import { useMaterialReactTable, useMRT_ColumnVirtualizer } from 'material-react-table';
125+
126+
const table = useMaterialReactTable({
127+
...options,
128+
});
129+
130+
const columnVirtualizer = useMRT_ColumnVirtualizer(table);
131+
```
132+
133+
You would only need to use this hook if you are writing a custom headless table and want the same default column virtualization behavior that MRT provides. If you are using the MRT components, this hook is already used internally by the `MRT_Table` component.
134+
135+
### useMRT_RowVirtualizer
136+
137+
> New in v2.2.0
138+
139+
[Source Code](https://github.com/KevinVandy/material-react-table/blob/v2/packages/material-react-table/src/hooks/useMRT_RowVirtualizer.ts)
140+
141+
This hook is a wrapper around the `useVirtualizer` hook from TanStack Virtual. It consumes a `table` instance and returns a Row Virtualizer instance that is optimized for MRT table rows, with considerations for other MRT features like row pinning, row dragging, and more.
142+
143+
```tsx
144+
import { useMaterialReactTable, useMRT_RowVirtualizer } from 'material-react-table';
145+
146+
const table = useMaterialReactTable({
147+
...options,
148+
});
34149

35-
### useMaterialReactTableLight
150+
const rowVirtualizer = useMRT_RowVirtualizer(table);
151+
```
36152

37-
> Would you be interested in a "lightweight" version MRT that doesn't import all of the features from TanStack Table by default? If so, voice your opinion in the [Discord](https://discord.gg/5wqyRx6fnm). It may be added in a near future non-major feature release.
153+
You would only need to use this hook if you are writing a custom headless table and want the same default row virtualization behavior that MRT provides. If you are using the MRT components, this hook is already used internally by the `MRT_Table` component.

apps/test-vite/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"devDependencies": {
2323
"@types/react": "^18.2.46",
2424
"@types/react-dom": "^18.2.18",
25-
"@typescript-eslint/eslint-plugin": "^6.16.0",
26-
"@typescript-eslint/parser": "^6.16.0",
25+
"@typescript-eslint/eslint-plugin": "^6.17.0",
26+
"@typescript-eslint/parser": "^6.17.0",
2727
"@vitejs/plugin-react": "^4.2.1",
2828
"eslint": "^8.56.0",
2929
"eslint-plugin-react-hooks": "^4.6.0",

packages/material-react-table/package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@
7171
"@mui/x-date-pickers": "^6.18.6",
7272
"@rollup/plugin-typescript": "^11.1.5",
7373
"@size-limit/preset-small-lib": "^11.0.1",
74-
"@storybook/addon-a11y": "^7.6.6",
75-
"@storybook/addon-essentials": "^7.6.6",
76-
"@storybook/addon-interactions": "^7.6.6",
77-
"@storybook/addon-links": "^7.6.6",
78-
"@storybook/addon-storysource": "^7.6.6",
79-
"@storybook/blocks": "^7.6.6",
80-
"@storybook/react": "^7.6.6",
81-
"@storybook/react-vite": "^7.6.6",
74+
"@storybook/addon-a11y": "^7.6.7",
75+
"@storybook/addon-essentials": "^7.6.7",
76+
"@storybook/addon-interactions": "^7.6.7",
77+
"@storybook/addon-links": "^7.6.7",
78+
"@storybook/addon-storysource": "^7.6.7",
79+
"@storybook/blocks": "^7.6.7",
80+
"@storybook/react": "^7.6.7",
81+
"@storybook/react-vite": "^7.6.7",
8282
"@storybook/testing-library": "^0.2.2",
8383
"@types/node": "^20.10.6",
8484
"@types/react": "^18.2.46",
8585
"@types/react-dom": "^18.2.18",
86-
"@typescript-eslint/eslint-plugin": "^6.16.0",
87-
"@typescript-eslint/parser": "^6.16.0",
86+
"@typescript-eslint/eslint-plugin": "^6.17.0",
87+
"@typescript-eslint/parser": "^6.17.0",
8888
"@vitejs/plugin-react": "^4.2.1",
8989
"eslint": "^8.56.0",
9090
"eslint-plugin-mui-path-imports": "^0.0.15",
@@ -97,7 +97,7 @@
9797
"rollup-plugin-dts": "^6.1.0",
9898
"rollup-plugin-peer-deps-external": "^2.2.4",
9999
"size-limit": "^11.0.1",
100-
"storybook": "^7.6.6",
100+
"storybook": "^7.6.7",
101101
"storybook-dark-mode": "^3.0.3",
102102
"tslib": "^2.6.2",
103103
"typescript": "^5.3.3",

packages/material-react-table/src/body/MRT_TableBody.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import TableBody, { type TableBodyProps } from '@mui/material/TableBody';
44
import Typography from '@mui/material/Typography';
55
import { MRT_TableBodyRow, Memo_MRT_TableBodyRow } from './MRT_TableBodyRow';
66
import { parseFromValuesOrFunc } from '../column.utils';
7+
import { useMRT_RowVirtualizer } from '../hooks';
78
import { useMRT_Rows } from '../hooks/useMRT_Rows';
89
import {
910
type MRT_Row,
@@ -13,7 +14,6 @@ import {
1314

1415
interface Props<TData extends MRT_RowData> extends TableBodyProps {
1516
columnVirtualizer?: Virtualizer<HTMLDivElement, HTMLTableCellElement>;
16-
rowVirtualizer?: Virtualizer<HTMLDivElement, HTMLTableRowElement>;
1717
table: MRT_TableInstance<TData>;
1818
virtualColumns?: VirtualItem[];
1919
virtualPaddingLeft?: number;
@@ -22,7 +22,6 @@ interface Props<TData extends MRT_RowData> extends TableBodyProps {
2222

2323
export const MRT_TableBody = <TData extends MRT_RowData>({
2424
columnVirtualizer,
25-
rowVirtualizer,
2625
table,
2726
virtualColumns,
2827
virtualPaddingLeft,
@@ -73,6 +72,8 @@ export const MRT_TableBody = <TData extends MRT_RowData>({
7372

7473
const rows = useMRT_Rows(table);
7574

75+
const rowVirtualizer = useMRT_RowVirtualizer(table);
76+
7677
const virtualRows = rowVirtualizer
7778
? rowVirtualizer.getVirtualItems()
7879
: undefined;

packages/material-react-table/src/table/MRT_Table.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { parseFromValuesOrFunc } from '../column.utils';
55
import { MRT_TableFooter } from '../footer/MRT_TableFooter';
66
import { MRT_TableHead } from '../head/MRT_TableHead';
77
import { useMRT_ColumnVirtualizer } from '../hooks/useMRT_ColumnVirtualizer';
8-
import { useMRT_RowVirtualizer } from '../hooks/useMRT_RowVirtualizer';
98
import { parseCSSVarId } from '../style.utils';
109
import { type MRT_RowData, type MRT_TableInstance } from '../types';
1110

@@ -53,7 +52,6 @@ export const MRT_Table = <TData extends MRT_RowData>({
5352
}, [columns, columnSizing, columnSizingInfo, columnVisibility]);
5453

5554
const columnVirtualizer = useMRT_ColumnVirtualizer(table);
56-
const rowVirtualizer = useMRT_RowVirtualizer(table);
5755

5856
const { virtualPaddingLeft, virtualPaddingRight } = columnVirtualizer ?? {};
5957

@@ -71,7 +69,6 @@ export const MRT_Table = <TData extends MRT_RowData>({
7169
const commonTableBodyProps = {
7270
...commonTableGroupProps,
7371
columnVirtualizer,
74-
rowVirtualizer,
7572
};
7673

7774
return (

0 commit comments

Comments
 (0)