@lminii/material-react-table 4.0.0 → 4.0.1

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.
@@ -1,200 +1,200 @@
1
- ---
2
- name: state-and-server-data
3
- description: >
4
- Control Material React Table V4 state with initialState, the state option plus on*Change callbacks, MRT_Updater handlers, table.getState and table.state, and move filtering, sorting, pagination, grouping, or expanding to the server with manual* options and rowCount. Load for controlled tables, persisted state, side effects on state change, TanStack Query integration, or infinite scrolling.
5
- metadata:
6
- type: framework
7
- library: '@lminii/material-react-table'
8
- library_version: '4.0.0'
9
- framework: react
10
- requires:
11
- - getting-started
12
- - '@tanstack/table-core#client-vs-server'
13
- sources:
14
- - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/state-management.mdx'
15
- - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/pagination.mdx'
16
- - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/async-loading.mdx'
17
- - 'lminii/material-react-table:apps/material-react-table-docs/examples/react-query/sandbox/src/TS.tsx'
18
- - 'lminii/material-react-table:packages/material-react-table/src/types.ts'
19
- ---
20
-
21
- This skill builds on `getting-started` and `@tanstack/table-core#client-vs-server`. MRT manages every state slice internally by default. Take over only the slices you need to read or persist, and only through the `state` option paired with the matching `on*Change` callback.
22
-
23
- ## Setup
24
-
25
- ```tsx
26
- const [pagination, setPagination] = useState<MRT_PaginationState>({ pageIndex: 0, pageSize: 25 })
27
- const [sorting, setSorting] = useState<MRT_SortingState>([])
28
-
29
- const table = useMaterialReactTable({
30
- columns,
31
- data,
32
- initialState: { density: 'compact', showColumnFilters: true },
33
- state: { pagination, sorting },
34
- onPaginationChange: setPagination,
35
- onSortingChange: setSorting,
36
- })
37
- ```
38
-
39
- `initialState` seeds slices MRT keeps managing. `state` hands a slice over to you; from then on MRT calls `on<Slice>Change` and reads only what you pass back. Do not put the same slice in both.
40
-
41
- ## Core Patterns
42
-
43
- ### Handle updaters like React setState
44
-
45
- ```tsx
46
- import { type MRT_Updater, type MRT_RowSelectionState } from '@lminii/material-react-table'
47
-
48
- const handleRowSelectionChange = (updater: MRT_Updater<MRT_RowSelectionState>) => {
49
- setRowSelection((previous) => {
50
- const next = updater instanceof Function ? updater(previous) : updater
51
- onSelectionChanged(Object.keys(next)) // side effect runs once, even in Strict Mode
52
- return next
53
- })
54
- }
55
- ```
56
-
57
- Callbacks receive either a value or an updater function, exactly like `useState`. Use `getRowId` so selection keys are stable ids instead of row indexes.
58
-
59
- ### Server-side data with manual* options
60
-
61
- ```tsx
62
- const { data: page, isLoading, isError, isRefetching } = useQuery({
63
- queryKey: ['people', pagination, sorting, columnFilters, globalFilter],
64
- queryFn: () => fetchPeople({ pagination, sorting, columnFilters, globalFilter }),
65
- placeholderData: keepPreviousData,
66
- })
67
-
68
- const table = useMaterialReactTable({
69
- columns,
70
- data: page?.rows ?? EMPTY,
71
- rowCount: page?.total ?? 0,
72
- manualFiltering: true,
73
- manualPagination: true,
74
- manualSorting: true,
75
- state: { pagination, sorting, columnFilters, globalFilter, isLoading, showAlertBanner: isError, showProgressBars: isRefetching },
76
- onPaginationChange: setPagination,
77
- onSortingChange: setSorting,
78
- onColumnFiltersChange: setColumnFilters,
79
- onGlobalFilterChange: setGlobalFilter,
80
- muiToolbarAlertBannerProps: isError ? { color: 'error', children: 'Error loading data' } : undefined,
81
- })
82
- ```
83
-
84
- Each `manual*` option tells MRT that stage already happened on the server, so the client row model passes rows through. `rowCount` (or `pageCount`) drives the pagination controls. `isLoading`, `showProgressBars`, `showSkeletons`, and `showLoadingOverlay` are MRT state slices meant to be set from fetch status.
85
-
86
- ### Read state without controlling it
87
-
88
- ```tsx
89
- const table = useMaterialReactTable({ columns, data })
90
-
91
- <Button onClick={() => save(table.getState().columnVisibility)}>Save layout</Button>
92
- ```
93
-
94
- `table.getState()` returns the complete state, including MRT-only slices such as `density`, `isFullScreen`, `showColumnFilters`, `editingRow`, and `creatingRow`. In render code `table.state` is reactive. Setters such as `table.setPagination`, `table.resetSorting`, `table.setShowColumnFilters`, and `table.setDensity` exist for every slice.
95
-
96
- ### Persist state
97
-
98
- ```tsx
99
- const [columnVisibility, setColumnVisibility] = useState<MRT_VisibilityState>(
100
- () => JSON.parse(localStorage.getItem('people-columns') ?? '{}'),
101
- )
102
- useEffect(() => {
103
- localStorage.setItem('people-columns', JSON.stringify(columnVisibility))
104
- }, [columnVisibility])
105
-
106
- const table = useMaterialReactTable({
107
- columns,
108
- data,
109
- state: { columnVisibility },
110
- onColumnVisibilityChange: setColumnVisibility,
111
- })
112
- ```
113
-
114
- ## Common Mistakes
115
-
116
- ### HIGH Passing on*Change without state
117
-
118
- Wrong:
119
-
120
- ```tsx
121
- const table = useMaterialReactTable({ columns, data, onSortingChange: setSorting })
122
- ```
123
-
124
- Correct:
125
-
126
- ```tsx
127
- const table = useMaterialReactTable({
128
- columns,
129
- data,
130
- state: { sorting },
131
- onSortingChange: setSorting,
132
- })
133
- ```
134
-
135
- Once a callback is supplied, MRT stops updating that slice itself. Without `state`, the table never sorts.
136
-
137
- Source: `docs/guides/state-management.mdx`
138
-
139
- ### HIGH Toggling row selection with false
140
-
141
- Wrong:
142
-
143
- ```tsx
144
- setRowSelection((old) => ({ ...old, [id]: false }))
145
- ```
146
-
147
- Correct:
148
-
149
- ```tsx
150
- setRowSelection(({ [id]: _removed, ...rest }) => rest)
151
- ```
152
-
153
- TanStack Table V9 types `rowSelection` as `Record<string, true>`, so a `false` entry is a type error; absence of the key is the only deselected state.
154
-
155
- Source: `MIGRATION.md`
156
-
157
- ### MEDIUM Using onStateChange or table.setState
158
-
159
- Wrong:
160
-
161
- ```tsx
162
- onStateChange: (updater) => setTableState(updater)
163
- ```
164
-
165
- Correct:
166
-
167
- ```tsx
168
- state: { pagination, sorting },
169
- onPaginationChange: setPagination,
170
- onSortingChange: setSorting,
171
- ```
172
-
173
- `onStateChange` and `table.setState` were removed with TanStack Table V9. Control slices individually.
174
-
175
- Source: `MIGRATION.md`
176
-
177
- ### MEDIUM Forgetting rowCount with manualPagination
178
-
179
- Wrong:
180
-
181
- ```tsx
182
- manualPagination: true,
183
- data: page.rows,
184
- ```
185
-
186
- Correct:
187
-
188
- ```tsx
189
- manualPagination: true,
190
- data: page.rows,
191
- rowCount: page.total,
192
- ```
193
-
194
- Without a total, MRT assumes the current page is all the data and disables the next page button.
195
-
196
- Source: `docs/guides/pagination.mdx`
197
-
198
- ## API Discovery
199
-
200
- `MRT_TableState` in `node_modules/@lminii/material-react-table/dist/index.d.ts` lists every slice; each has an `on<Slice>Change` option and `table.set<Slice>` method. The State Options table at `/docs/api/state-options` shows defaults, and the `manual*` and `rowCount` options are in `/docs/api/table-options`.
1
+ ---
2
+ name: state-and-server-data
3
+ description: >
4
+ Control Material React Table V4 state with initialState, the state option plus on*Change callbacks, MRT_Updater handlers, table.getState and table.state, and move filtering, sorting, pagination, grouping, or expanding to the server with manual* options and rowCount. Load for controlled tables, persisted state, side effects on state change, TanStack Query integration, or infinite scrolling.
5
+ metadata:
6
+ type: framework
7
+ library: '@lminii/material-react-table'
8
+ library_version: '4.0.0'
9
+ framework: react
10
+ requires:
11
+ - getting-started
12
+ - '@tanstack/table-core#client-vs-server'
13
+ sources:
14
+ - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/state-management.mdx'
15
+ - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/pagination.mdx'
16
+ - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/async-loading.mdx'
17
+ - 'lminii/material-react-table:apps/material-react-table-docs/examples/react-query/sandbox/src/TS.tsx'
18
+ - 'lminii/material-react-table:packages/material-react-table/src/types.ts'
19
+ ---
20
+
21
+ This skill builds on `getting-started` and `@tanstack/table-core#client-vs-server`. MRT manages every state slice internally by default. Take over only the slices you need to read or persist, and only through the `state` option paired with the matching `on*Change` callback.
22
+
23
+ ## Setup
24
+
25
+ ```tsx
26
+ const [pagination, setPagination] = useState<MRT_PaginationState>({ pageIndex: 0, pageSize: 25 })
27
+ const [sorting, setSorting] = useState<MRT_SortingState>([])
28
+
29
+ const table = useMaterialReactTable({
30
+ columns,
31
+ data,
32
+ initialState: { density: 'compact', showColumnFilters: true },
33
+ state: { pagination, sorting },
34
+ onPaginationChange: setPagination,
35
+ onSortingChange: setSorting,
36
+ })
37
+ ```
38
+
39
+ `initialState` seeds slices MRT keeps managing. `state` hands a slice over to you; from then on MRT calls `on<Slice>Change` and reads only what you pass back. Do not put the same slice in both.
40
+
41
+ ## Core Patterns
42
+
43
+ ### Handle updaters like React setState
44
+
45
+ ```tsx
46
+ import { type MRT_Updater, type MRT_RowSelectionState } from '@lminii/material-react-table'
47
+
48
+ const handleRowSelectionChange = (updater: MRT_Updater<MRT_RowSelectionState>) => {
49
+ setRowSelection((previous) => {
50
+ const next = updater instanceof Function ? updater(previous) : updater
51
+ onSelectionChanged(Object.keys(next)) // side effect runs once, even in Strict Mode
52
+ return next
53
+ })
54
+ }
55
+ ```
56
+
57
+ Callbacks receive either a value or an updater function, exactly like `useState`. Use `getRowId` so selection keys are stable ids instead of row indexes.
58
+
59
+ ### Server-side data with manual* options
60
+
61
+ ```tsx
62
+ const { data: page, isLoading, isError, isRefetching } = useQuery({
63
+ queryKey: ['people', pagination, sorting, columnFilters, globalFilter],
64
+ queryFn: () => fetchPeople({ pagination, sorting, columnFilters, globalFilter }),
65
+ placeholderData: keepPreviousData,
66
+ })
67
+
68
+ const table = useMaterialReactTable({
69
+ columns,
70
+ data: page?.rows ?? EMPTY,
71
+ rowCount: page?.total ?? 0,
72
+ manualFiltering: true,
73
+ manualPagination: true,
74
+ manualSorting: true,
75
+ state: { pagination, sorting, columnFilters, globalFilter, isLoading, showAlertBanner: isError, showProgressBars: isRefetching },
76
+ onPaginationChange: setPagination,
77
+ onSortingChange: setSorting,
78
+ onColumnFiltersChange: setColumnFilters,
79
+ onGlobalFilterChange: setGlobalFilter,
80
+ muiToolbarAlertBannerProps: isError ? { color: 'error', children: 'Error loading data' } : undefined,
81
+ })
82
+ ```
83
+
84
+ Each `manual*` option tells MRT that stage already happened on the server, so the client row model passes rows through. `rowCount` (or `pageCount`) drives the pagination controls. `isLoading`, `showProgressBars`, `showSkeletons`, and `showLoadingOverlay` are MRT state slices meant to be set from fetch status.
85
+
86
+ ### Read state without controlling it
87
+
88
+ ```tsx
89
+ const table = useMaterialReactTable({ columns, data })
90
+
91
+ <Button onClick={() => save(table.getState().columnVisibility)}>Save layout</Button>
92
+ ```
93
+
94
+ `table.getState()` returns the complete state, including MRT-only slices such as `density`, `isFullScreen`, `showColumnFilters`, `editingRow`, and `creatingRow`. In render code `table.state` is reactive. Setters such as `table.setPagination`, `table.resetSorting`, `table.setShowColumnFilters`, and `table.setDensity` exist for every slice.
95
+
96
+ ### Persist state
97
+
98
+ ```tsx
99
+ const [columnVisibility, setColumnVisibility] = useState<MRT_VisibilityState>(
100
+ () => JSON.parse(localStorage.getItem('people-columns') ?? '{}'),
101
+ )
102
+ useEffect(() => {
103
+ localStorage.setItem('people-columns', JSON.stringify(columnVisibility))
104
+ }, [columnVisibility])
105
+
106
+ const table = useMaterialReactTable({
107
+ columns,
108
+ data,
109
+ state: { columnVisibility },
110
+ onColumnVisibilityChange: setColumnVisibility,
111
+ })
112
+ ```
113
+
114
+ ## Common Mistakes
115
+
116
+ ### HIGH Passing on*Change without state
117
+
118
+ Wrong:
119
+
120
+ ```tsx
121
+ const table = useMaterialReactTable({ columns, data, onSortingChange: setSorting })
122
+ ```
123
+
124
+ Correct:
125
+
126
+ ```tsx
127
+ const table = useMaterialReactTable({
128
+ columns,
129
+ data,
130
+ state: { sorting },
131
+ onSortingChange: setSorting,
132
+ })
133
+ ```
134
+
135
+ Once a callback is supplied, MRT stops updating that slice itself. Without `state`, the table never sorts.
136
+
137
+ Source: `docs/guides/state-management.mdx`
138
+
139
+ ### HIGH Toggling row selection with false
140
+
141
+ Wrong:
142
+
143
+ ```tsx
144
+ setRowSelection((old) => ({ ...old, [id]: false }))
145
+ ```
146
+
147
+ Correct:
148
+
149
+ ```tsx
150
+ setRowSelection(({ [id]: _removed, ...rest }) => rest)
151
+ ```
152
+
153
+ TanStack Table V9 types `rowSelection` as `Record<string, true>`, so a `false` entry is a type error; absence of the key is the only deselected state.
154
+
155
+ Source: `MIGRATION.md`
156
+
157
+ ### MEDIUM Using onStateChange or table.setState
158
+
159
+ Wrong:
160
+
161
+ ```tsx
162
+ onStateChange: (updater) => setTableState(updater)
163
+ ```
164
+
165
+ Correct:
166
+
167
+ ```tsx
168
+ state: { pagination, sorting },
169
+ onPaginationChange: setPagination,
170
+ onSortingChange: setSorting,
171
+ ```
172
+
173
+ `onStateChange` and `table.setState` were removed with TanStack Table V9. Control slices individually.
174
+
175
+ Source: `MIGRATION.md`
176
+
177
+ ### MEDIUM Forgetting rowCount with manualPagination
178
+
179
+ Wrong:
180
+
181
+ ```tsx
182
+ manualPagination: true,
183
+ data: page.rows,
184
+ ```
185
+
186
+ Correct:
187
+
188
+ ```tsx
189
+ manualPagination: true,
190
+ data: page.rows,
191
+ rowCount: page.total,
192
+ ```
193
+
194
+ Without a total, MRT assumes the current page is all the data and disables the next page button.
195
+
196
+ Source: `docs/guides/pagination.mdx`
197
+
198
+ ## API Discovery
199
+
200
+ `MRT_TableState` in `node_modules/@lminii/material-react-table/dist/index.d.ts` lists every slice; each has an `on<Slice>Change` option and `table.set<Slice>` method. The State Options table at `/docs/api/state-options` shows defaults, and the `manual*` and `rowCount` options are in `/docs/api/table-options`.