@mini_7/material-react-table-mcp 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/data/MIGRATION.md +136 -0
- package/data/api.json +5379 -0
- package/data/examples/advanced.tsx +309 -0
- package/data/examples/aggregation-and-grouping.tsx +153 -0
- package/data/examples/aggregation-multi.tsx +105 -0
- package/data/examples/alternate-column-filtering.tsx +49 -0
- package/data/examples/alternate-detail-panel.tsx +81 -0
- package/data/examples/alternate-pagination.tsx +23 -0
- package/data/examples/basic.tsx +109 -0
- package/data/examples/chart-detail-panel.tsx +89 -0
- package/data/examples/column-actions-space.tsx +44 -0
- package/data/examples/column-alignment.tsx +70 -0
- package/data/examples/custom-column-actions.tsx +109 -0
- package/data/examples/custom-column-filtering-ui.tsx +89 -0
- package/data/examples/custom-headless.tsx +125 -0
- package/data/examples/custom-top-toolbar.tsx +88 -0
- package/data/examples/customize-display-columns.tsx +85 -0
- package/data/examples/customize-filter-components.tsx +101 -0
- package/data/examples/customize-filter-modes.tsx +73 -0
- package/data/examples/customize-filter-variants.tsx +116 -0
- package/data/examples/customize-global-filter-component.tsx +52 -0
- package/data/examples/customize-remove-column-grouping.tsx +94 -0
- package/data/examples/customize-row-selection.tsx +54 -0
- package/data/examples/customize-table-styles.tsx +100 -0
- package/data/examples/disable-column-actions.tsx +37 -0
- package/data/examples/disable-column-hiding.tsx +90 -0
- package/data/examples/disable-density-toggle.tsx +49 -0
- package/data/examples/dynamic-columns.tsx +189 -0
- package/data/examples/editing-crud-cell.tsx +377 -0
- package/data/examples/editing-crud-modal.tsx +370 -0
- package/data/examples/editing-crud-row.tsx +333 -0
- package/data/examples/editing-crud-table.tsx +369 -0
- package/data/examples/editing-crud-tree.tsx +416 -0
- package/data/examples/enable-cell-actions.tsx +76 -0
- package/data/examples/enable-click-to-copy.tsx +39 -0
- package/data/examples/enable-column-grouping.tsx +120 -0
- package/data/examples/enable-column-ordering.tsx +48 -0
- package/data/examples/enable-column-pinning.tsx +69 -0
- package/data/examples/enable-column-resizing.tsx +56 -0
- package/data/examples/enable-column-virtualization.tsx +27 -0
- package/data/examples/enable-detail-panel-conditionally.tsx +78 -0
- package/data/examples/enable-detail-panel-virtualized.tsx +80 -0
- package/data/examples/enable-expanding-tree.tsx +110 -0
- package/data/examples/enable-filter-facet-values.tsx +65 -0
- package/data/examples/enable-row-dragging.tsx +137 -0
- package/data/examples/enable-row-numbers-original.tsx +48 -0
- package/data/examples/enable-row-numbers-static.tsx +48 -0
- package/data/examples/enable-row-ordering.tsx +57 -0
- package/data/examples/enable-row-pinning-select.tsx +76 -0
- package/data/examples/enable-row-pinning-static.tsx +50 -0
- package/data/examples/enable-row-pinning-sticky.tsx +69 -0
- package/data/examples/enable-row-selection.tsx +96 -0
- package/data/examples/enable-row-virtualization.tsx +101 -0
- package/data/examples/enable-sticky-header.tsx +59 -0
- package/data/examples/expanding-tree-expanded.tsx +121 -0
- package/data/examples/expanding-tree-flat-parse.tsx +125 -0
- package/data/examples/expanding-tree-root-expanded.tsx +151 -0
- package/data/examples/export-to-csv.tsx +114 -0
- package/data/examples/export-to-pdf.tsx +104 -0
- package/data/examples/external-toolbar.tsx +108 -0
- package/data/examples/font-awesome-icons.tsx +106 -0
- package/data/examples/infinite-scrolling.tsx +208 -0
- package/data/examples/lazy-detail-panel.tsx +262 -0
- package/data/examples/lazy-sub-rows.tsx +190 -0
- package/data/examples/linear-progress.tsx +66 -0
- package/data/examples/loading.tsx +50 -0
- package/data/examples/localization-i18n-ar.tsx +65 -0
- package/data/examples/localization-i18n-az.tsx +60 -0
- package/data/examples/localization-i18n-bg.tsx +60 -0
- package/data/examples/localization-i18n-cs.tsx +60 -0
- package/data/examples/localization-i18n-da.tsx +60 -0
- package/data/examples/localization-i18n-de.tsx +60 -0
- package/data/examples/localization-i18n-el.tsx +60 -0
- package/data/examples/localization-i18n-en.tsx +60 -0
- package/data/examples/localization-i18n-es.tsx +60 -0
- package/data/examples/localization-i18n-et.tsx +60 -0
- package/data/examples/localization-i18n-fa.tsx +66 -0
- package/data/examples/localization-i18n-fi.tsx +60 -0
- package/data/examples/localization-i18n-fr.tsx +60 -0
- package/data/examples/localization-i18n-he.tsx +66 -0
- package/data/examples/localization-i18n-hr.tsx +60 -0
- package/data/examples/localization-i18n-hu.tsx +60 -0
- package/data/examples/localization-i18n-hy.tsx +65 -0
- package/data/examples/localization-i18n-id.tsx +60 -0
- package/data/examples/localization-i18n-it.tsx +60 -0
- package/data/examples/localization-i18n-ja.tsx +60 -0
- package/data/examples/localization-i18n-ko.tsx +60 -0
- package/data/examples/localization-i18n-nl.tsx +60 -0
- package/data/examples/localization-i18n-no.tsx +60 -0
- package/data/examples/localization-i18n-np.tsx +60 -0
- package/data/examples/localization-i18n-pl.tsx +60 -0
- package/data/examples/localization-i18n-pt-BR.tsx +60 -0
- package/data/examples/localization-i18n-pt.tsx +60 -0
- package/data/examples/localization-i18n-ro.tsx +60 -0
- package/data/examples/localization-i18n-ru.tsx +60 -0
- package/data/examples/localization-i18n-sk.tsx +60 -0
- package/data/examples/localization-i18n-sr-Cyrl-RS.tsx +60 -0
- package/data/examples/localization-i18n-sr-Latn-RS.tsx +60 -0
- package/data/examples/localization-i18n-sv.tsx +60 -0
- package/data/examples/localization-i18n-tr.tsx +60 -0
- package/data/examples/localization-i18n-uk.tsx +60 -0
- package/data/examples/localization-i18n-vi.tsx +60 -0
- package/data/examples/localization-i18n-zh-hans.tsx +60 -0
- package/data/examples/localization-i18n-zh-hant.tsx +60 -0
- package/data/examples/manual-selection.tsx +105 -0
- package/data/examples/minimal.tsx +78 -0
- package/data/examples/mui-theme.tsx +136 -0
- package/data/examples/multi-sorting.tsx +58 -0
- package/data/examples/persistent-state.tsx +184 -0
- package/data/examples/react-query.tsx +182 -0
- package/data/examples/remote.tsx +150 -0
- package/data/examples/row-actions-buttons.tsx +89 -0
- package/data/examples/row-actions-menu-items.tsx +64 -0
- package/data/examples/single-row-selection.tsx +98 -0
- package/data/examples/virtualized.tsx +138 -0
- package/data/guides/accessibility.md +119 -0
- package/data/guides/aggregation.md +210 -0
- package/data/guides/async-loading.md +132 -0
- package/data/guides/best-practices.md +363 -0
- package/data/guides/cell-actions.md +139 -0
- package/data/guides/click-to-copy.md +67 -0
- package/data/guides/column-actions.md +69 -0
- package/data/guides/column-filtering.md +366 -0
- package/data/guides/column-grouping.md +192 -0
- package/data/guides/column-hiding.md +133 -0
- package/data/guides/column-ordering-dnd.md +99 -0
- package/data/guides/column-pinning.md +72 -0
- package/data/guides/column-resizing.md +157 -0
- package/data/guides/column-size.md +162 -0
- package/data/guides/customize-components.md +395 -0
- package/data/guides/customize-icons.md +35 -0
- package/data/guides/data-columns.md +292 -0
- package/data/guides/density-toggle.md +46 -0
- package/data/guides/detail-panel.md +195 -0
- package/data/guides/display-columns.md +135 -0
- package/data/guides/editing.md +367 -0
- package/data/guides/expanding-sub-rows.md +194 -0
- package/data/guides/full-screen-toggle.md +63 -0
- package/data/guides/global-filtering.md +237 -0
- package/data/guides/localization.md +57 -0
- package/data/guides/memoization.md +157 -0
- package/data/guides/pagination.md +171 -0
- package/data/guides/row-actions.md +116 -0
- package/data/guides/row-numbers.md +24 -0
- package/data/guides/row-ordering-dnd.md +51 -0
- package/data/guides/row-pinning.md +96 -0
- package/data/guides/row-selection.md +237 -0
- package/data/guides/sorting.md +190 -0
- package/data/guides/state-management.md +165 -0
- package/data/guides/sticky-header.md +41 -0
- package/data/guides/table-event-listeners.md +103 -0
- package/data/guides/toolbar-customization.md +251 -0
- package/data/guides/virtualization.md +183 -0
- package/data/index.json +1930 -0
- package/data/reference/mrt-components.md +183 -0
- package/data/reference/mrt-hooks.md +125 -0
- package/data/skills/composable-components/SKILL.md +189 -0
- package/data/skills/customization/SKILL.md +190 -0
- package/data/skills/drag-and-drop-ordering/SKILL.md +227 -0
- package/data/skills/editing/SKILL.md +243 -0
- package/data/skills/filtering/SKILL.md +213 -0
- package/data/skills/getting-started/SKILL.md +213 -0
- package/data/skills/localization/SKILL.md +182 -0
- package/data/skills/migrate-v3-to-v4/SKILL.md +161 -0
- package/data/skills/state-and-server-data/SKILL.md +200 -0
- package/data/skills/virtualization/SKILL.md +203 -0
- package/dist/data.js +137 -0
- package/dist/index.js +232 -0
- package/package.json +50 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
## Sorting Feature Guide
|
|
2
|
+
|
|
3
|
+
Material React Table supports almost any sorting scenario you may have. Client-side sorting is enabled by default, but you can opt to implement your own server-side sorting logic or even replace the default client-side sorting with your own implementation.
|
|
4
|
+
|
|
5
|
+
### Relevant Table Options
|
|
6
|
+
|
|
7
|
+
Relevant table options (get_mrt_api has full details):
|
|
8
|
+
|
|
9
|
+
- `enableGlobalFilterRankedResults` - type `boolean` - default `true`
|
|
10
|
+
- `enableMultiSort` - type `boolean`
|
|
11
|
+
- `enableSorting` - type `boolean` - default `true`
|
|
12
|
+
- `enableSortingRemoval` - type `boolean` - default `true`
|
|
13
|
+
- `isMultiSortEvent` - type `(e: unknown) => boolean` - Pass a custom function that will be used to determine if a multi-sort event should be triggered. It is passed the event from the sort toggle handler and should return true if the event should trigger a multi-sort.
|
|
14
|
+
- `manualSorting` - type `boolean` - Enables manual sorting for the table. If this is true, you will be expected to sort your data before it is passed to the table. This is useful if you are doing server-side sorting.
|
|
15
|
+
- `maxMultiSortColCount` - type `number` - Set a maximum number of columns that can be multi-sorted.
|
|
16
|
+
- `onSortingChange` - type `OnChangeFn<SortingState>` - If provided, this function will be called with an updaterFn when state.sorting changes. This overrides the default internal state management, so you will need to persist the state change either fully or partially outside of the table.
|
|
17
|
+
- `sortDescFirst` - type `boolean` - Set to true for sorting toggles on this column to start in the descending direction.
|
|
18
|
+
- `sortFns` - type `Record<string, SortFn>` - This option allows you to define custom sorting functions that can be referenced in a column's sortFn option by their key. Example:
|
|
19
|
+
|
|
20
|
+
### Relevant Column Options
|
|
21
|
+
|
|
22
|
+
Relevant column options (get_mrt_api has full details):
|
|
23
|
+
|
|
24
|
+
- `enableMultiSort` - type `boolean` - default `true`
|
|
25
|
+
- `enableSorting` - type `boolean`
|
|
26
|
+
- `invertSorting` - type `boolean` - default `false`
|
|
27
|
+
- `sortDescFirst` - type `boolean`
|
|
28
|
+
- `sortFn` - type `SortingFnOption`
|
|
29
|
+
- `sortUndefined` - type `'first' | 'last' | false | 1 | -1`
|
|
30
|
+
|
|
31
|
+
### Relevant State Options
|
|
32
|
+
|
|
33
|
+
Relevant state options (get_mrt_api has full details):
|
|
34
|
+
|
|
35
|
+
- `sorting` - type `Array<{ id: string, desc: boolean }>` - default `[]`
|
|
36
|
+
|
|
37
|
+
### Disable Sorting
|
|
38
|
+
|
|
39
|
+
Sorting can be disabled globally by setting the `enableSorting` table option to `false`. This will disable sorting for all columns. You can also disable sorting for individual columns by setting the `enableSorting` column option to `false`.
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
const columns = [
|
|
43
|
+
{
|
|
44
|
+
accessorKey: 'name',
|
|
45
|
+
header: 'Name',
|
|
46
|
+
enableSorting: false, // disable sorting for this column
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
const table = useMaterialReactTable({
|
|
51
|
+
columns,
|
|
52
|
+
data,
|
|
53
|
+
enableSorting: false, //disable sorting for all columns
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Initial/Default Sorting
|
|
58
|
+
|
|
59
|
+
You can sort by a column or multiple columns by default by setting the `sorting` state option in either the `initialState` or `state` props.
|
|
60
|
+
|
|
61
|
+
```jsx
|
|
62
|
+
const table = useMaterialReactTable({
|
|
63
|
+
columns,
|
|
64
|
+
data,
|
|
65
|
+
initialState: {
|
|
66
|
+
sorting: [
|
|
67
|
+
{
|
|
68
|
+
id: 'age', //sort by age by default on page load
|
|
69
|
+
desc: true,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: 'lastName', //then sort by lastName if age is the same
|
|
73
|
+
desc: true,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Default Sorting Features
|
|
81
|
+
|
|
82
|
+
Client-side sorting is enabled by default. When sorting is toggled on for a column, the table will be sorted by the [`basic` sorting algorithm](https://tanstack.com/table/v9/docs/framework/react/guide/sorting) by default.
|
|
83
|
+
|
|
84
|
+
#### Multi-Sorting
|
|
85
|
+
|
|
86
|
+
Multi-sorting is also enabled by default, which means you can sort by multiple columns at once. You can do this by clicking on a column header while holding down the `shift` key. The table will then be sorted by the previously sorted column, followed by the newly clicked column. Alternatively, if you want multi-sorting to be the default click behavior without the need to hold `shift`, you can set the `isMultiSortEvent` table option to `() => true`.
|
|
87
|
+
|
|
88
|
+
```jsx
|
|
89
|
+
const table = useMaterialReactTable({
|
|
90
|
+
columns,
|
|
91
|
+
data,
|
|
92
|
+
isMultiSortEvent: () => true, //multi-sorting will be the default click behavior without the need to hold shift
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
You can limit the number of columns that can be sorted at once by setting the `maxMultiSortColCount` prop, or you can disable multi-sorting entirely by setting the `enableMultiSort` table option to `false`.
|
|
97
|
+
|
|
98
|
+
#### Sorting Removal
|
|
99
|
+
|
|
100
|
+
By default, users can remove a sort on a column by clicking through the sort direction options or selecting "Clear Sort" from the column actions menu. You can disable this feature by setting the `enableSortingRemoval` table option to `false`.
|
|
101
|
+
|
|
102
|
+
```jsx
|
|
103
|
+
const table = useMaterialReactTable({
|
|
104
|
+
columns,
|
|
105
|
+
data,
|
|
106
|
+
enableSortingRemoval: false, //users will not be able to remove a sort on a column
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Sort Direction
|
|
111
|
+
|
|
112
|
+
By default, columns with `string` datatypes will sort alphabetically in ascending order, but columns with `number` datatypes will sort numerically in descending order. You can change the default sort direction per column by specifying the `sortDescFirst` column option to either `true` or `false`. You can also change the default sort direction globally by setting the `sortDescFirst` table option to either `true` or `false`.
|
|
113
|
+
|
|
114
|
+
> Live example: `multi-sorting` (call get_mrt_example with that id).
|
|
115
|
+
|
|
116
|
+
### Sorting Functions
|
|
117
|
+
|
|
118
|
+
By default, Material React Table will use the `basic` sorting function for all columns.
|
|
119
|
+
|
|
120
|
+
There are six built-in sorting functions you can choose from: ` alphanumeric`, `alphanumericCaseSensitive`, `text`, `textCaseSensitive`, `datetime`, and `basic`. You can learn more about these built-in sorting functions in the [TanStack Table Sorting API docs](https://tanstack.com/table/v9/docs/framework/react/guide/sorting).
|
|
121
|
+
|
|
122
|
+
#### Add Custom Sorting Functions
|
|
123
|
+
|
|
124
|
+
If none of these sorting functions meet your needs, you can add your own custom sorting functions by specifying more sorting functions in the `sortFns` table option.
|
|
125
|
+
|
|
126
|
+
```jsx
|
|
127
|
+
const table = useMaterialReactTable({
|
|
128
|
+
columns,
|
|
129
|
+
data,
|
|
130
|
+
sortFns: {
|
|
131
|
+
//will add a new sorting function to the list of other sorting functions already available
|
|
132
|
+
myCustomSortingFn: (rowA, rowB, columnId) => // your custom sorting logic
|
|
133
|
+
},
|
|
134
|
+
})
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### Change Sorting Function Per Column
|
|
138
|
+
|
|
139
|
+
You can now choose a sorting function for each column by either passing a string value of the built-in sorting function names to the `sortFn` column option or by passing a custom sorting function to the `sortFn` column option.
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
const columns = [
|
|
143
|
+
{
|
|
144
|
+
accessorKey: 'name',
|
|
145
|
+
header: 'Name',
|
|
146
|
+
sortFn: 'textCaseSensitive', //use the built-in textCaseSensitive sorting function instead of the default basic sorting function
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
accessorKey: 'age',
|
|
150
|
+
header: 'Age',
|
|
151
|
+
//use your own custom sorting function instead of any of the built-in sorting functions
|
|
152
|
+
sortFn: (rowA, rowB, columnId) => // your custom sorting logic
|
|
153
|
+
},
|
|
154
|
+
];
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Manual Server-Side Sorting
|
|
158
|
+
|
|
159
|
+
If you are working with large data sets, you may want to let your back-end APIs handle all of the sorting and pagination processing instead of doing it client-side. You can do this by setting the `manualSorting` table option to `true`. This will disable the default client-side sorting and pagination features and will let you implement your own sorting and pagination logic.
|
|
160
|
+
|
|
161
|
+
> When `manualSorting` is set to `true`, Material React Table assumes that your `data` is already sorted by the time you are passing it to the table.
|
|
162
|
+
|
|
163
|
+
If you need to sort your data in a back-end API, then you will also probably need access to the internal `sorting` state from the table. You can do this by managing the `sorting` state yourself and then passing it to the table via the `state` table option. You can also pass a callback function to the `onSortingChange` prop, which will be called whenever the `sorting` state changes internally in the table
|
|
164
|
+
|
|
165
|
+
```jsx
|
|
166
|
+
const [sorting, setSorting] = useState([]);
|
|
167
|
+
|
|
168
|
+
const table = useMaterialReactTable({
|
|
169
|
+
columns,
|
|
170
|
+
data,
|
|
171
|
+
manualSorting: true,
|
|
172
|
+
state: { sorting },
|
|
173
|
+
onSortingChange: setSorting,
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
//do something with the sorting state when it changes
|
|
178
|
+
//or use table.getState().sorting
|
|
179
|
+
}, [sorting]);
|
|
180
|
+
|
|
181
|
+
return <MaterialReactTable table={table} />;
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Remote Sorting Example
|
|
185
|
+
|
|
186
|
+
Here is the full Remote Data example showing how to implement server-side sorting, filtering, and pagination with Material React Table.
|
|
187
|
+
|
|
188
|
+
> Live example: `remote` (call get_mrt_example with that id).
|
|
189
|
+
|
|
190
|
+
View Extra Storybook **[Examples](https://www.material-react-table.dev/?path=/story/features-sorting-examples)**
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
## State Management Guide
|
|
2
|
+
|
|
3
|
+
> Note: `tableInstanceRef` has been removed in MRT v2 since the `useMaterialReactTable` hook now returns the table instance directly. See the [Migration Guide](/migrating-to-v2) for more information.
|
|
4
|
+
|
|
5
|
+
Material React Table does not try to hide any of its internal state from you. You can initialize state with custom initial values, manage individual states yourself as you discover the need to have access to them, or read any of the state from the table instance itself.
|
|
6
|
+
|
|
7
|
+
This is all optional, of course. If you do not need access to any of the internal state, you do not need to do anything and it will just automatically be managed internally.
|
|
8
|
+
|
|
9
|
+
See the [State Options API Docs](/docs/api/state-options) for more information on which states are available for you to manage.
|
|
10
|
+
|
|
11
|
+
### Relevant Table Options
|
|
12
|
+
|
|
13
|
+
Relevant table options (get_mrt_api has full details):
|
|
14
|
+
|
|
15
|
+
- `initialState` - type `Partial<MRT_TableState<TData>>` - Use this option to optionally pass initial state to the table. This state will be used when resetting various table states either automatically by the table (eg. options.autoResetPagination) or via functions like table.resetRowSelection(). Most reset function allow you optionally pass a flag to reset to a blank/default state instead of the initial state. Table state will not be reset when this object changes, which also means that the initial state object does not need to be stable.
|
|
16
|
+
- `state` - type `Partial<MRT_TableState<TData>>`
|
|
17
|
+
|
|
18
|
+
### Populate Initial State
|
|
19
|
+
|
|
20
|
+
If all you care about is setting parts of the initial or default state when the table mounts, then you may be able to specify that state in the `initialState` table option and not have to worry about managing the state yourself.
|
|
21
|
+
|
|
22
|
+
For example, let's say you do not need access to the `showColumnFilters` state, but you want to set the default value to `true` when the table mounts. You can do that with the `initialState` table option:
|
|
23
|
+
|
|
24
|
+
```jsx
|
|
25
|
+
const table = useMaterialReactTable({
|
|
26
|
+
columns,
|
|
27
|
+
data,
|
|
28
|
+
initialState: {
|
|
29
|
+
density: 'xs', //set default density to compact
|
|
30
|
+
expanded: true, //expand all rows by default
|
|
31
|
+
pagination: { pageIndex: 0, pageSize: 15 }, //set different default page size
|
|
32
|
+
showColumnFilters: true, //show filters by default
|
|
33
|
+
sorting: [{ id: 'name', desc: false }], //sort by name ascending by default
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return <MaterialReactTable table={table} />;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
> Note: If you use both `initialState` and `state`, the state initializer in `state` table option will take precedence and overwrite the same state values in `initialState`. So just use either `initialState` or `state`, not both for the same states.
|
|
41
|
+
|
|
42
|
+
### Manage Individual States as Needed
|
|
43
|
+
|
|
44
|
+
It is pretty common to need to manage certain state yourself, so that you can react to changes in that state, or have easy access to it when sending it to an API.
|
|
45
|
+
|
|
46
|
+
You can pass in any state that you are managing yourself to the `state` table option, and it will be used instead of the internal state. Each state property option also has a corresponding `on[StateName]Change` callback that you can use set/update your managed state as it changes internally in the table.
|
|
47
|
+
|
|
48
|
+
For example, let's say you need to store the pagination, sorting, and row selection states in a place where you can easily access it in order to use it in parameters for an API call.
|
|
49
|
+
|
|
50
|
+
```jsx
|
|
51
|
+
const [pagination, setPagination] = useState({
|
|
52
|
+
pageIndex: 0,
|
|
53
|
+
pageSize: 15, //set different default page size by initializing the state here
|
|
54
|
+
});
|
|
55
|
+
const [rowSelection, setRowSelection] = useState({});
|
|
56
|
+
const [sorting, setSorting] = useState([{ id: 'name', desc: false }]);
|
|
57
|
+
|
|
58
|
+
//see example at bottom of page for alternatives to useEffect here
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
//do something when the pagination state changes
|
|
61
|
+
}, [pagination]);
|
|
62
|
+
|
|
63
|
+
const table = useMaterialReactTable({
|
|
64
|
+
columns,
|
|
65
|
+
data,
|
|
66
|
+
getRowId: (originalRow) => row.username,
|
|
67
|
+
onPaginationChange: setPagination,
|
|
68
|
+
onRowSelectionChange: setRowSelection,
|
|
69
|
+
onSortingChange: setSorting,
|
|
70
|
+
state: { pagination, rowSelection, sorting }, //must pass states back down if using their on[StateName]Change callbacks
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return <MaterialReactTable table={table} />;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Add Side Effects in Set State Callbacks
|
|
77
|
+
|
|
78
|
+
In React 18 and beyond, it is becoming more discouraged to use `useEffect` to react to state changes, because in React Strict Mode (and maybe future versions of React), the useEffect hook may run twice per render. Instead, more event driven functions are recommended to be used. Here is an example for how that looks here. The callback signature for the `on[StateName]Change` works just like a React setState callback from the `useState` hook. This means that you have to check if the updater is a function or not, and then call the setState function with the updater callback if it is a function.
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
const [pagination, setPagination] = useState({
|
|
82
|
+
pageIndex: 0,
|
|
83
|
+
pageSize: 15,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const handlePaginationChange = (updater: MRT_Updater<PaginationState>) => {
|
|
87
|
+
//call the setState as normal, but need to check if using an updater callback with a previous state
|
|
88
|
+
setPagination((prevPagination) =>
|
|
89
|
+
//if updater is a function, call it with the previous state, otherwise just use the updater value
|
|
90
|
+
updater instanceof Function ? updater(prevPagination) : updater,
|
|
91
|
+
);
|
|
92
|
+
//put more code for your side effects here, guaranteed to only run once, even in React Strict Mode
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const table = useMaterialReactTable({
|
|
96
|
+
columns,
|
|
97
|
+
data,
|
|
98
|
+
onPaginationChange: handlePaginationChange,
|
|
99
|
+
state: { pagination },
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return <MaterialReactTable table={table} />;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Read From the Table Instance
|
|
106
|
+
|
|
107
|
+
> Note: Previously, in early MRT v1, you could use the `tableInstanceRef` table option to get access to the table instance. This is no longer necessary as the `useMaterialReactTable` hook now just returns the table instance directly.
|
|
108
|
+
|
|
109
|
+
The `useMaterialReactTable` hook returns the table instance. The `<MaterialReactTable />` needs the table instance for all of its internal logic, but you can also use it for your own purposes.
|
|
110
|
+
|
|
111
|
+
```jsx
|
|
112
|
+
const table = useMaterialReactTable({
|
|
113
|
+
columns,
|
|
114
|
+
data,
|
|
115
|
+
//...
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const someEventHandler = (event) => {
|
|
119
|
+
console.info(table.getRowModel().rows); //example - get access to all page rows in the table
|
|
120
|
+
console.info(table.getSelectedRowModel()); //example - get access to all selected rows in the table
|
|
121
|
+
console.info(table.getState().sorting); //example - get access to the current sorting state without having to manage it yourself
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<div>
|
|
126
|
+
<ExternalButton onClick={someEventHandler}>
|
|
127
|
+
Export or Something
|
|
128
|
+
</ExternalButton>
|
|
129
|
+
<MaterialReactTable table={table} />
|
|
130
|
+
</div>
|
|
131
|
+
);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The table instance is the same object that you will also see as a provided parameter in many of the other callback functions throughout Material React Table, such as all the `render...` props or the `Cell` or `Header` render overrides in the column definition options.
|
|
135
|
+
|
|
136
|
+
```jsx
|
|
137
|
+
const columns = useMemo(
|
|
138
|
+
() => [
|
|
139
|
+
{
|
|
140
|
+
Header: 'Name',
|
|
141
|
+
accessor: 'name',
|
|
142
|
+
Cell: ({ cell, table }) => <span>{cell.getValue()}</span>,
|
|
143
|
+
//The `table` parameter from the Cell option params and the `table` are the same object
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
[],
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const table = useMaterialReactTable({
|
|
150
|
+
columns,
|
|
151
|
+
data,
|
|
152
|
+
renderTopToolbarCustomActions: ({ table }) => {
|
|
153
|
+
//The `table` parameter here and the table returned from the hook are the same object
|
|
154
|
+
return <Button>Button</Button>;
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return <MaterialReactTable table={table} />;
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Persistent State
|
|
162
|
+
|
|
163
|
+
Persistent state is not a built-in feature of Material React Table, but it is an easy feature to implement yourself using the above patterns with the `state` table option and the `on[StateName]Change` callbacks. Here is an example of how you might implement persistent state using `sessionStorage`:
|
|
164
|
+
|
|
165
|
+
> Live example: `persistent-state` (call get_mrt_example with that id).
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
## Sticky Header and Footer Feature Guide
|
|
2
|
+
|
|
3
|
+
The sticky header and footer feature allows you to keep the header and footer of the table visible while scrolling through the table. This is useful when you have a large table and want to keep the header and footer visible at all times.
|
|
4
|
+
|
|
5
|
+
### Relevant Table Options
|
|
6
|
+
|
|
7
|
+
Relevant table options (get_mrt_api has full details):
|
|
8
|
+
|
|
9
|
+
- `enableStickyFooter` - type `boolean`
|
|
10
|
+
- `enableStickyHeader` - type `boolean`
|
|
11
|
+
|
|
12
|
+
### Enable Sticky Header
|
|
13
|
+
|
|
14
|
+
Enabling the sticky header is as simple as setting the `enableStickyHeader` table option to `true`. This will make the header of the table stick to the top and remain visible while scrolling through the table.
|
|
15
|
+
|
|
16
|
+
When the sticky header is enabled, you will probably also want to give the table a maxHeight so that the table can scroll vertically and keep the header visible. You can do this by styling the table container with the `muiTableContainerProps` table option.
|
|
17
|
+
|
|
18
|
+
> If no maxHeight is specified, the table container will default to a `100vh` maxHeight when `enableStickyHeader` is enabled.
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
<MaterialReactTable
|
|
22
|
+
columns={columns}
|
|
23
|
+
data={data}
|
|
24
|
+
enableStickyHeader
|
|
25
|
+
muiTableContainerProps={{ sx: { maxHeight: '500px' } }}
|
|
26
|
+
/>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Enable Sticky Footer
|
|
30
|
+
|
|
31
|
+
Similarly, enabling the sticky footer is as simple as setting the `enableStickyFooter` table option to `true`. This will make the footer of the table stick to the bottom of the table and always be visible, even before the table is scrolled to the bottom.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
<MaterialReactTable columns={columns} data={data} enableStickyFooter />
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Enable Sticky Header and Footer Demo
|
|
38
|
+
|
|
39
|
+
> Live example: `enable-sticky-header` (call get_mrt_example with that id).
|
|
40
|
+
|
|
41
|
+
View Extra Storybook **[Examples](https://www.material-react-table.dev/?path=/story/styling-sticky-header-examples)**
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
## Table Event Listeners Guide
|
|
2
|
+
|
|
3
|
+
You can pretty much add ANY event listener to ANY component in Material React Table.
|
|
4
|
+
|
|
5
|
+
You can do this by passing props to any of the `mui...Props` props. This guide gives a few examples of the most common use cases, but there are limitless possibilities.
|
|
6
|
+
|
|
7
|
+
### Add Event Listeners to any of the Material UI Components
|
|
8
|
+
|
|
9
|
+
In the [customize components docs](/docs/guides/customize-components#material-ui-prop-types), we explained how to pass any prop you need to pass to any exposed Material UI component that is in the table. The list of props that you can pass to Material UI components includes any event listener.
|
|
10
|
+
|
|
11
|
+
Here are a few common examples of some useful event listeners you might want to add to the table, although the possibilities are countless.
|
|
12
|
+
|
|
13
|
+
#### Add an onClick to a Table Row
|
|
14
|
+
|
|
15
|
+
```jsx
|
|
16
|
+
const table = useMaterialReactTable({
|
|
17
|
+
columns,
|
|
18
|
+
data,
|
|
19
|
+
muiTableBodyRowProps: ({ row }) => ({
|
|
20
|
+
onClick: (event) => {
|
|
21
|
+
console.info(event, row.id);
|
|
22
|
+
},
|
|
23
|
+
sx: {
|
|
24
|
+
cursor: 'pointer', //you might want to change the cursor too when adding an onClick
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
#### Add an onDoubleClick to a Table Cell
|
|
31
|
+
|
|
32
|
+
```jsx
|
|
33
|
+
const table = useMaterialReactTable({
|
|
34
|
+
columns,
|
|
35
|
+
data,
|
|
36
|
+
muiTableBodyCellProps: ({ cell }) => ({
|
|
37
|
+
onDoubleClick: (event) => {
|
|
38
|
+
console.info(event, cell.id);
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
#### Add an onBlur to an Edit TextField
|
|
45
|
+
|
|
46
|
+
```jsx
|
|
47
|
+
//add to every edit text field
|
|
48
|
+
const table = useMaterialReactTable({
|
|
49
|
+
columns,
|
|
50
|
+
data,
|
|
51
|
+
muiEditTextFieldProps: ({ cell }) => ({
|
|
52
|
+
onBlur: (event) => {
|
|
53
|
+
console.info(event, cell.id);
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```jsx
|
|
60
|
+
//or add to just edit text fields in a specific column
|
|
61
|
+
const columns = [
|
|
62
|
+
{
|
|
63
|
+
accessorKey: 'name',
|
|
64
|
+
header: 'Name',
|
|
65
|
+
muiEditTextFieldProps: ({ cell }) => ({
|
|
66
|
+
onBlur: (event) => {
|
|
67
|
+
console.info(event);
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
#### Add an onChange to an Edit TextField
|
|
75
|
+
|
|
76
|
+
> Warning: Be careful when using onChange, as it can trigger re-renders with every keystroke, which can lead to performance issues
|
|
77
|
+
|
|
78
|
+
```jsx
|
|
79
|
+
const table = useMaterialReactTable({
|
|
80
|
+
columns,
|
|
81
|
+
data,
|
|
82
|
+
muiEditTextFieldProps: ({ cell }) => ({
|
|
83
|
+
onChange: (event) => {
|
|
84
|
+
console.info(event, cell.id);
|
|
85
|
+
},
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```jsx
|
|
91
|
+
//or add to just edit text fields in a specific column
|
|
92
|
+
const columns = [
|
|
93
|
+
{
|
|
94
|
+
accessorKey: 'name',
|
|
95
|
+
header: 'Name',
|
|
96
|
+
muiEditTextFieldProps: ({ cell }) => ({
|
|
97
|
+
onChange: (event) => {
|
|
98
|
+
console.info(event);
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
```
|