@tanstack/react-table 9.0.0-alpha.0 → 9.0.0-alpha.11
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/dist/esm/FlexRender.d.ts +46 -0
- package/dist/esm/FlexRender.js +39 -0
- package/dist/esm/FlexRender.js.map +1 -0
- package/dist/esm/Subscribe.d.ts +44 -0
- package/dist/esm/Subscribe.js +10 -0
- package/dist/esm/Subscribe.js.map +1 -0
- package/dist/esm/createTableHook.d.ts +349 -0
- package/dist/esm/createTableHook.js +130 -0
- package/dist/esm/createTableHook.js.map +1 -0
- package/dist/esm/index.d.ts +5 -8
- package/dist/esm/index.js +19 -50
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/useLegacyTable.d.ts +188 -0
- package/dist/esm/useLegacyTable.js +106 -0
- package/dist/esm/useLegacyTable.js.map +1 -0
- package/dist/esm/useTable.d.ts +52 -0
- package/dist/esm/useTable.js +51 -0
- package/dist/esm/useTable.js.map +1 -0
- package/package.json +21 -14
- package/src/FlexRender.tsx +118 -0
- package/src/Subscribe.ts +70 -0
- package/src/createTableHook.tsx +1112 -0
- package/src/index.ts +7 -0
- package/src/useLegacyTable.ts +370 -0
- package/src/useTable.ts +139 -0
- package/dist/cjs/index.cjs +0 -78
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -9
- package/src/index.tsx +0 -94
package/src/index.ts
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
aggregationFns,
|
|
5
|
+
createExpandedRowModel,
|
|
6
|
+
createFacetedMinMaxValues,
|
|
7
|
+
createFacetedRowModel,
|
|
8
|
+
createFacetedUniqueValues,
|
|
9
|
+
createFilteredRowModel,
|
|
10
|
+
createGroupedRowModel,
|
|
11
|
+
createPaginatedRowModel,
|
|
12
|
+
createSortedRowModel,
|
|
13
|
+
filterFns,
|
|
14
|
+
sortFns,
|
|
15
|
+
stockFeatures,
|
|
16
|
+
} from '@tanstack/table-core'
|
|
17
|
+
import { useMemo } from 'react'
|
|
18
|
+
import { useStore } from '@tanstack/react-store'
|
|
19
|
+
import { useTable } from './useTable'
|
|
20
|
+
import type {
|
|
21
|
+
CreateRowModels_All,
|
|
22
|
+
RowData,
|
|
23
|
+
RowModel,
|
|
24
|
+
StockFeatures,
|
|
25
|
+
Table,
|
|
26
|
+
TableOptions,
|
|
27
|
+
TableState,
|
|
28
|
+
} from '@tanstack/table-core'
|
|
29
|
+
import type { ReactTable } from './useTable'
|
|
30
|
+
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// V8-style row model factory functions
|
|
33
|
+
// These are stub functions that act as markers for useLegacyTable to know
|
|
34
|
+
// which row models to enable. They don't actually do anything - the real
|
|
35
|
+
// implementation is handled by useLegacyTable internally.
|
|
36
|
+
// =============================================================================
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @deprecated Use `createFilteredRowModel(filterFns)` with the new `useTable` hook instead.
|
|
40
|
+
*
|
|
41
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
42
|
+
* It acts as a marker to enable the filtered row model.
|
|
43
|
+
*/
|
|
44
|
+
export function getFilteredRowModel<
|
|
45
|
+
TData extends RowData,
|
|
46
|
+
>(): RowModelFactory<TData> {
|
|
47
|
+
return (() => () => {}) as unknown as RowModelFactory<TData>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated Use `createSortedRowModel(sortFns)` with the new `useTable` hook instead.
|
|
52
|
+
*
|
|
53
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
54
|
+
* It acts as a marker to enable the sorted row model.
|
|
55
|
+
*/
|
|
56
|
+
export function getSortedRowModel<
|
|
57
|
+
TData extends RowData,
|
|
58
|
+
>(): RowModelFactory<TData> {
|
|
59
|
+
return (() => () => {}) as unknown as RowModelFactory<TData>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @deprecated Use `createPaginatedRowModel()` with the new `useTable` hook instead.
|
|
64
|
+
*
|
|
65
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
66
|
+
* It acts as a marker to enable the paginated row model.
|
|
67
|
+
*/
|
|
68
|
+
export function getPaginationRowModel<
|
|
69
|
+
TData extends RowData,
|
|
70
|
+
>(): RowModelFactory<TData> {
|
|
71
|
+
return (() => () => {}) as unknown as RowModelFactory<TData>
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @deprecated Use `createExpandedRowModel()` with the new `useTable` hook instead.
|
|
76
|
+
*
|
|
77
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
78
|
+
* It acts as a marker to enable the expanded row model.
|
|
79
|
+
*/
|
|
80
|
+
export function getExpandedRowModel<
|
|
81
|
+
TData extends RowData,
|
|
82
|
+
>(): RowModelFactory<TData> {
|
|
83
|
+
return (() => () => {}) as unknown as RowModelFactory<TData>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @deprecated Use `createGroupedRowModel(aggregationFns)` with the new `useTable` hook instead.
|
|
88
|
+
*
|
|
89
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
90
|
+
* It acts as a marker to enable the grouped row model.
|
|
91
|
+
*/
|
|
92
|
+
export function getGroupedRowModel<
|
|
93
|
+
TData extends RowData,
|
|
94
|
+
>(): RowModelFactory<TData> {
|
|
95
|
+
return (() => () => {}) as unknown as RowModelFactory<TData>
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @deprecated Use `createFacetedRowModel()` with the new `useTable` hook instead.
|
|
100
|
+
*
|
|
101
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
102
|
+
* It acts as a marker to enable the faceted row model.
|
|
103
|
+
*/
|
|
104
|
+
export function getFacetedRowModel<
|
|
105
|
+
TData extends RowData,
|
|
106
|
+
>(): FacetedRowModelFactory<TData> {
|
|
107
|
+
return (() => () => {}) as unknown as FacetedRowModelFactory<TData>
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @deprecated Use `createFacetedMinMaxValues()` with the new `useTable` hook instead.
|
|
112
|
+
*
|
|
113
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
114
|
+
* It acts as a marker to enable the faceted min/max values.
|
|
115
|
+
*/
|
|
116
|
+
export function getFacetedMinMaxValues<
|
|
117
|
+
TData extends RowData,
|
|
118
|
+
>(): FacetedMinMaxValuesFactory<TData> {
|
|
119
|
+
return (() => () => undefined) as unknown as FacetedMinMaxValuesFactory<TData>
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @deprecated Use `createFacetedUniqueValues()` with the new `useTable` hook instead.
|
|
124
|
+
*
|
|
125
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
126
|
+
* It acts as a marker to enable the faceted unique values.
|
|
127
|
+
*/
|
|
128
|
+
export function getFacetedUniqueValues<
|
|
129
|
+
TData extends RowData,
|
|
130
|
+
>(): FacetedUniqueValuesFactory<TData> {
|
|
131
|
+
return (() => () => new Map()) as unknown as FacetedUniqueValuesFactory<TData>
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @deprecated The core row model is always created automatically in v9.
|
|
136
|
+
*
|
|
137
|
+
* This is a stub function for v8 API compatibility with `useLegacyTable`.
|
|
138
|
+
* It does nothing - the core row model is always available.
|
|
139
|
+
*/
|
|
140
|
+
export function getCoreRowModel<
|
|
141
|
+
TData extends RowData,
|
|
142
|
+
>(): RowModelFactory<TData> {
|
|
143
|
+
return (() => () => {}) as unknown as RowModelFactory<TData>
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// =============================================================================
|
|
147
|
+
// Type definitions
|
|
148
|
+
// =============================================================================
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Row model factory function type from v8 API
|
|
152
|
+
*/
|
|
153
|
+
type RowModelFactory<TData extends RowData> = (
|
|
154
|
+
table: Table<StockFeatures, TData>,
|
|
155
|
+
) => () => RowModel<StockFeatures, TData>
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Faceted row model factory function type from v8 API
|
|
159
|
+
*/
|
|
160
|
+
type FacetedRowModelFactory<TData extends RowData> = (
|
|
161
|
+
table: Table<StockFeatures, TData>,
|
|
162
|
+
columnId: string,
|
|
163
|
+
) => () => RowModel<StockFeatures, TData>
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Faceted min/max values factory function type from v8 API
|
|
167
|
+
*/
|
|
168
|
+
type FacetedMinMaxValuesFactory<TData extends RowData> = (
|
|
169
|
+
table: Table<StockFeatures, TData>,
|
|
170
|
+
columnId: string,
|
|
171
|
+
) => () => undefined | [number, number]
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Faceted unique values factory function type from v8 API
|
|
175
|
+
*/
|
|
176
|
+
type FacetedUniqueValuesFactory<TData extends RowData> = (
|
|
177
|
+
table: Table<StockFeatures, TData>,
|
|
178
|
+
columnId: string,
|
|
179
|
+
) => () => Map<any, number>
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Legacy v8-style row model options
|
|
183
|
+
*/
|
|
184
|
+
export interface LegacyRowModelOptions<TData extends RowData> {
|
|
185
|
+
/**
|
|
186
|
+
* Returns the core row model for the table.
|
|
187
|
+
* @deprecated This option is no longer needed in v9. The core row model is always created automatically.
|
|
188
|
+
*/
|
|
189
|
+
getCoreRowModel?: RowModelFactory<TData>
|
|
190
|
+
/**
|
|
191
|
+
* Returns the filtered row model for the table.
|
|
192
|
+
* @deprecated Use `_rowModels.filteredRowModel` with `createFilteredRowModel(filterFns)` instead.
|
|
193
|
+
*/
|
|
194
|
+
getFilteredRowModel?: RowModelFactory<TData>
|
|
195
|
+
/**
|
|
196
|
+
* Returns the sorted row model for the table.
|
|
197
|
+
* @deprecated Use `_rowModels.sortedRowModel` with `createSortedRowModel(sortFns)` instead.
|
|
198
|
+
*/
|
|
199
|
+
getSortedRowModel?: RowModelFactory<TData>
|
|
200
|
+
/**
|
|
201
|
+
* Returns the paginated row model for the table.
|
|
202
|
+
* @deprecated Use `_rowModels.paginatedRowModel` with `createPaginatedRowModel()` instead.
|
|
203
|
+
*/
|
|
204
|
+
getPaginationRowModel?: RowModelFactory<TData>
|
|
205
|
+
/**
|
|
206
|
+
* Returns the expanded row model for the table.
|
|
207
|
+
* @deprecated Use `_rowModels.expandedRowModel` with `createExpandedRowModel()` instead.
|
|
208
|
+
*/
|
|
209
|
+
getExpandedRowModel?: RowModelFactory<TData>
|
|
210
|
+
/**
|
|
211
|
+
* Returns the grouped row model for the table.
|
|
212
|
+
* @deprecated Use `_rowModels.groupedRowModel` with `createGroupedRowModel(aggregationFns)` instead.
|
|
213
|
+
*/
|
|
214
|
+
getGroupedRowModel?: RowModelFactory<TData>
|
|
215
|
+
/**
|
|
216
|
+
* Returns the faceted row model for a column.
|
|
217
|
+
* @deprecated Use `_rowModels.facetedRowModel` with `createFacetedRowModel()` instead.
|
|
218
|
+
*/
|
|
219
|
+
getFacetedRowModel?: FacetedRowModelFactory<TData>
|
|
220
|
+
/**
|
|
221
|
+
* Returns the faceted min/max values for a column.
|
|
222
|
+
* @deprecated Use `_rowModels.facetedMinMaxValues` with `createFacetedMinMaxValues()` instead.
|
|
223
|
+
*/
|
|
224
|
+
getFacetedMinMaxValues?: FacetedMinMaxValuesFactory<TData>
|
|
225
|
+
/**
|
|
226
|
+
* Returns the faceted unique values for a column.
|
|
227
|
+
* @deprecated Use `_rowModels.facetedUniqueValues` with `createFacetedUniqueValues()` instead.
|
|
228
|
+
*/
|
|
229
|
+
getFacetedUniqueValues?: FacetedUniqueValuesFactory<TData>
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Legacy v8-style table options that work with useLegacyTable.
|
|
234
|
+
*
|
|
235
|
+
* This type omits `_features` and `_rowModels` and instead accepts the v8-style
|
|
236
|
+
* `get*RowModel` function options.
|
|
237
|
+
*
|
|
238
|
+
* @deprecated This is a compatibility layer for migrating from v8. Use `useTable` with explicit `_features` and `_rowModels` instead.
|
|
239
|
+
*/
|
|
240
|
+
export type LegacyTableOptions<TData extends RowData> = Omit<
|
|
241
|
+
TableOptions<StockFeatures, TData>,
|
|
242
|
+
'_features' | '_rowModels'
|
|
243
|
+
> &
|
|
244
|
+
LegacyRowModelOptions<TData>
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Legacy table instance type that includes the v8-style `getState()` method.
|
|
248
|
+
*
|
|
249
|
+
* @deprecated Use `useTable` with explicit state selection instead.
|
|
250
|
+
*/
|
|
251
|
+
export type LegacyReactTable<TData extends RowData> = ReactTable<
|
|
252
|
+
StockFeatures,
|
|
253
|
+
TData,
|
|
254
|
+
TableState<StockFeatures>
|
|
255
|
+
> & {
|
|
256
|
+
/**
|
|
257
|
+
* Returns the current table state.
|
|
258
|
+
* @deprecated In v9, access state directly via `table.state` or use `table.store.state` for the full state.
|
|
259
|
+
*/
|
|
260
|
+
getState: () => TableState<StockFeatures>
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* @deprecated This hook is provided as a compatibility layer for migrating from TanStack Table v8.
|
|
265
|
+
*
|
|
266
|
+
* Use the new `useTable` hook instead with explicit `_features` and `_rowModels`:
|
|
267
|
+
*
|
|
268
|
+
* ```tsx
|
|
269
|
+
* // New v9 API
|
|
270
|
+
* const _features = tableFeatures({
|
|
271
|
+
* columnFilteringFeature,
|
|
272
|
+
* rowSortingFeature,
|
|
273
|
+
* rowPaginationFeature,
|
|
274
|
+
* })
|
|
275
|
+
*
|
|
276
|
+
* const table = useTable({
|
|
277
|
+
* _features,
|
|
278
|
+
* _rowModels: {
|
|
279
|
+
* filteredRowModel: createFilteredRowModel(filterFns),
|
|
280
|
+
* sortedRowModel: createSortedRowModel(sortFns),
|
|
281
|
+
* paginatedRowModel: createPaginatedRowModel(),
|
|
282
|
+
* },
|
|
283
|
+
* columns,
|
|
284
|
+
* data,
|
|
285
|
+
* })
|
|
286
|
+
* ```
|
|
287
|
+
*
|
|
288
|
+
* Key differences from v8:
|
|
289
|
+
* - Features are tree-shakeable - only import what you use
|
|
290
|
+
* - Row models are explicitly passed via `_rowModels`
|
|
291
|
+
* - Use `table.Subscribe` for fine-grained re-renders
|
|
292
|
+
* - State is accessed via `table.state` after selecting with the 2nd argument
|
|
293
|
+
*
|
|
294
|
+
* @param options - Legacy v8-style table options
|
|
295
|
+
* @returns A table instance with the full state subscribed and a `getState()` method
|
|
296
|
+
*/
|
|
297
|
+
export function useLegacyTable<TData extends RowData>(
|
|
298
|
+
options: LegacyTableOptions<TData>,
|
|
299
|
+
): LegacyReactTable<TData> {
|
|
300
|
+
const {
|
|
301
|
+
// Extract legacy row model options
|
|
302
|
+
getCoreRowModel: _getCoreRowModel,
|
|
303
|
+
getFilteredRowModel,
|
|
304
|
+
getSortedRowModel,
|
|
305
|
+
getPaginationRowModel,
|
|
306
|
+
getExpandedRowModel,
|
|
307
|
+
getGroupedRowModel,
|
|
308
|
+
getFacetedRowModel,
|
|
309
|
+
getFacetedMinMaxValues,
|
|
310
|
+
getFacetedUniqueValues,
|
|
311
|
+
// Rest of the options
|
|
312
|
+
...restOptions
|
|
313
|
+
} = options
|
|
314
|
+
|
|
315
|
+
// Build the _rowModels object based on which legacy options were provided
|
|
316
|
+
const _rowModels: CreateRowModels_All<StockFeatures, TData> = {}
|
|
317
|
+
|
|
318
|
+
// Map v8 row model factories to v9 _rowModels
|
|
319
|
+
// Note: getCoreRowModel is handled automatically in v9, so we ignore it
|
|
320
|
+
|
|
321
|
+
if (getFilteredRowModel) {
|
|
322
|
+
_rowModels.filteredRowModel = createFilteredRowModel(filterFns)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (getSortedRowModel) {
|
|
326
|
+
_rowModels.sortedRowModel = createSortedRowModel(sortFns)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (getPaginationRowModel) {
|
|
330
|
+
_rowModels.paginatedRowModel = createPaginatedRowModel()
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (getExpandedRowModel) {
|
|
334
|
+
_rowModels.expandedRowModel = createExpandedRowModel()
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (getGroupedRowModel) {
|
|
338
|
+
_rowModels.groupedRowModel = createGroupedRowModel(aggregationFns)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (getFacetedRowModel) {
|
|
342
|
+
_rowModels.facetedRowModel = createFacetedRowModel()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (getFacetedMinMaxValues) {
|
|
346
|
+
_rowModels.facetedMinMaxValues = createFacetedMinMaxValues()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (getFacetedUniqueValues) {
|
|
350
|
+
_rowModels.facetedUniqueValues = createFacetedUniqueValues()
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Call useTable with the v9 API, subscribing to all state changes
|
|
354
|
+
const table = useTable<StockFeatures, TData, TableState<StockFeatures>>({
|
|
355
|
+
...restOptions,
|
|
356
|
+
_features: stockFeatures,
|
|
357
|
+
_rowModels,
|
|
358
|
+
} as TableOptions<StockFeatures, TData>)
|
|
359
|
+
|
|
360
|
+
const state = useStore(table.store, (state) => state)
|
|
361
|
+
|
|
362
|
+
return useMemo(
|
|
363
|
+
() =>
|
|
364
|
+
({
|
|
365
|
+
...table,
|
|
366
|
+
getState: () => state,
|
|
367
|
+
}) as LegacyReactTable<TData>,
|
|
368
|
+
[table, state],
|
|
369
|
+
)
|
|
370
|
+
}
|
package/src/useTable.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { useEffect, useLayoutEffect, useMemo, useState } from 'react'
|
|
3
|
+
import { constructTable } from '@tanstack/table-core'
|
|
4
|
+
import { useStore } from '@tanstack/react-store'
|
|
5
|
+
import { FlexRender } from './FlexRender'
|
|
6
|
+
import { Subscribe } from './Subscribe'
|
|
7
|
+
import type {
|
|
8
|
+
CellData,
|
|
9
|
+
NoInfer,
|
|
10
|
+
RowData,
|
|
11
|
+
Table,
|
|
12
|
+
TableFeatures,
|
|
13
|
+
TableOptions,
|
|
14
|
+
TableState,
|
|
15
|
+
} from '@tanstack/table-core'
|
|
16
|
+
import type { FunctionComponent, ReactNode } from 'react'
|
|
17
|
+
import type { FlexRenderProps } from './FlexRender'
|
|
18
|
+
import type { SubscribeProps } from './Subscribe'
|
|
19
|
+
|
|
20
|
+
const useIsomorphicLayoutEffect =
|
|
21
|
+
typeof window !== 'undefined' ? useLayoutEffect : useEffect
|
|
22
|
+
|
|
23
|
+
export type ReactTable<
|
|
24
|
+
TFeatures extends TableFeatures,
|
|
25
|
+
TData extends RowData,
|
|
26
|
+
TSelected = {},
|
|
27
|
+
> = Table<TFeatures, TData> & {
|
|
28
|
+
/**
|
|
29
|
+
* A React HOC (Higher Order Component) that allows you to subscribe to the table state.
|
|
30
|
+
*
|
|
31
|
+
* This is useful for opting into state re-renders for specific parts of the table state.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
|
|
35
|
+
* {({ rowSelection }) => ( // important to include `{() => {()}}` syntax
|
|
36
|
+
* <tr key={row.id}>
|
|
37
|
+
// render the row
|
|
38
|
+
* </tr>
|
|
39
|
+
* ))}
|
|
40
|
+
* </table.Subscribe>
|
|
41
|
+
*/
|
|
42
|
+
Subscribe: <TSelected>(props: {
|
|
43
|
+
selector: (state: NoInfer<TableState<TFeatures>>) => TSelected
|
|
44
|
+
children: ((state: TSelected) => ReactNode) | ReactNode
|
|
45
|
+
}) => ReturnType<FunctionComponent>
|
|
46
|
+
/**
|
|
47
|
+
* A React component that renders headers, cells, or footers with custom markup.
|
|
48
|
+
* Use this utility component instead of manually calling flexRender.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```tsx
|
|
52
|
+
* <table.FlexRender cell={cell} />
|
|
53
|
+
* <table.FlexRender header={header} />
|
|
54
|
+
* <table.FlexRender footer={footer} />
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* This replaces calling `flexRender` directly like this:
|
|
58
|
+
* ```tsx
|
|
59
|
+
* flexRender(cell.column.columnDef.cell, cell.getContext())
|
|
60
|
+
* flexRender(header.column.columnDef.header, header.getContext())
|
|
61
|
+
* flexRender(footer.column.columnDef.footer, footer.getContext())
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
FlexRender: <TValue extends CellData = CellData>(
|
|
65
|
+
props: FlexRenderProps<TFeatures, TData, TValue>,
|
|
66
|
+
) => ReactNode
|
|
67
|
+
/**
|
|
68
|
+
* The selected state of the table. This state may not match the structure of `table.store.state` because it is selected by the `selector` function that you pass as the 2nd argument to `useTable`.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const table = useTable(options, (state) => ({ globalFilter: state.globalFilter })) // only globalFilter is part of the selected state
|
|
72
|
+
*
|
|
73
|
+
* console.log(table.state.globalFilter)
|
|
74
|
+
*/
|
|
75
|
+
readonly state: Readonly<TSelected>
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function useTable<
|
|
79
|
+
TFeatures extends TableFeatures,
|
|
80
|
+
TData extends RowData,
|
|
81
|
+
TSelected = {},
|
|
82
|
+
>(
|
|
83
|
+
tableOptions: TableOptions<TFeatures, TData>,
|
|
84
|
+
selector: (state: TableState<TFeatures>) => TSelected = () =>
|
|
85
|
+
({}) as TSelected,
|
|
86
|
+
): ReactTable<TFeatures, TData, TSelected> {
|
|
87
|
+
const [table] = useState(() => {
|
|
88
|
+
const tableInstance = constructTable(tableOptions) as ReactTable<
|
|
89
|
+
TFeatures,
|
|
90
|
+
TData,
|
|
91
|
+
TSelected
|
|
92
|
+
>
|
|
93
|
+
|
|
94
|
+
tableInstance.Subscribe = function SubscribeBound<TSelected>(
|
|
95
|
+
props: Omit<SubscribeProps<TFeatures, TData, TSelected>, 'table'>,
|
|
96
|
+
) {
|
|
97
|
+
return Subscribe({ ...props, table: tableInstance })
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
tableInstance.FlexRender = FlexRender
|
|
101
|
+
|
|
102
|
+
return tableInstance
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
// sync table options on every render
|
|
106
|
+
table.setOptions((prev) => ({
|
|
107
|
+
...prev,
|
|
108
|
+
...tableOptions,
|
|
109
|
+
}))
|
|
110
|
+
|
|
111
|
+
// Mount the derived store to register it on the dependency graph
|
|
112
|
+
useIsomorphicLayoutEffect(() => {
|
|
113
|
+
const cleanup = table.store.mount()
|
|
114
|
+
return cleanup
|
|
115
|
+
}, [table])
|
|
116
|
+
|
|
117
|
+
useIsomorphicLayoutEffect(() => {
|
|
118
|
+
// prevent race condition between table.setOptions and table.baseStore.setState
|
|
119
|
+
queueMicrotask(() => {
|
|
120
|
+
table.baseStore.setState((prev) => ({
|
|
121
|
+
...prev,
|
|
122
|
+
}))
|
|
123
|
+
})
|
|
124
|
+
}, [
|
|
125
|
+
table.options.columns, // re-render when columns change
|
|
126
|
+
table.options.data, // re-render when data changes
|
|
127
|
+
table.options.state, // sync react state to the table store
|
|
128
|
+
])
|
|
129
|
+
|
|
130
|
+
const state = useStore(table.store, selector)
|
|
131
|
+
|
|
132
|
+
return useMemo(
|
|
133
|
+
() => ({
|
|
134
|
+
...table,
|
|
135
|
+
state,
|
|
136
|
+
}),
|
|
137
|
+
[state, table],
|
|
138
|
+
)
|
|
139
|
+
}
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
-
const React = require("react");
|
|
5
|
-
const tableCore = require("@tanstack/table-core");
|
|
6
|
-
function _interopNamespaceDefault(e) {
|
|
7
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
8
|
-
if (e) {
|
|
9
|
-
for (const k in e) {
|
|
10
|
-
if (k !== "default") {
|
|
11
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: () => e[k]
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
n.default = e;
|
|
20
|
-
return Object.freeze(n);
|
|
21
|
-
}
|
|
22
|
-
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
23
|
-
function flexRender(Comp, props) {
|
|
24
|
-
return !Comp ? null : isReactComponent(Comp) ? /* @__PURE__ */ jsxRuntime.jsx(Comp, { ...props }) : Comp;
|
|
25
|
-
}
|
|
26
|
-
function isReactComponent(component) {
|
|
27
|
-
return isClassComponent(component) || typeof component === "function" || isExoticComponent(component);
|
|
28
|
-
}
|
|
29
|
-
function isClassComponent(component) {
|
|
30
|
-
return typeof component === "function" && (() => {
|
|
31
|
-
const proto = Object.getPrototypeOf(component);
|
|
32
|
-
return proto.prototype && proto.prototype.isReactComponent;
|
|
33
|
-
})();
|
|
34
|
-
}
|
|
35
|
-
function isExoticComponent(component) {
|
|
36
|
-
return typeof component === "object" && typeof component.$$typeof === "symbol" && ["react.memo", "react.forward_ref"].includes(component.$$typeof.description);
|
|
37
|
-
}
|
|
38
|
-
function useReactTable(options) {
|
|
39
|
-
const resolvedOptions = {
|
|
40
|
-
state: {},
|
|
41
|
-
// Dummy state
|
|
42
|
-
onStateChange: () => {
|
|
43
|
-
},
|
|
44
|
-
// noop
|
|
45
|
-
renderFallbackValue: null,
|
|
46
|
-
...options
|
|
47
|
-
};
|
|
48
|
-
const [tableRef] = React__namespace.useState(() => ({
|
|
49
|
-
current: tableCore.createTable(resolvedOptions)
|
|
50
|
-
}));
|
|
51
|
-
const [state, setState] = React__namespace.useState(() => tableRef.current.initialState);
|
|
52
|
-
tableRef.current.setOptions((prev) => ({
|
|
53
|
-
...prev,
|
|
54
|
-
...options,
|
|
55
|
-
state: {
|
|
56
|
-
...state,
|
|
57
|
-
...options.state
|
|
58
|
-
},
|
|
59
|
-
// Similarly, we'll maintain both our internal state and any user-provided
|
|
60
|
-
// state.
|
|
61
|
-
onStateChange: (updater) => {
|
|
62
|
-
var _a;
|
|
63
|
-
setState(updater);
|
|
64
|
-
(_a = options.onStateChange) == null ? void 0 : _a.call(options, updater);
|
|
65
|
-
}
|
|
66
|
-
}));
|
|
67
|
-
return tableRef.current;
|
|
68
|
-
}
|
|
69
|
-
exports.flexRender = flexRender;
|
|
70
|
-
exports.useReactTable = useReactTable;
|
|
71
|
-
Object.keys(tableCore).forEach((k) => {
|
|
72
|
-
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
73
|
-
Object.defineProperty(exports, k, {
|
|
74
|
-
enumerable: true,
|
|
75
|
-
get: () => tableCore[k]
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nexport * from '@tanstack/table-core'\n\nimport {\n TableOptions,\n TableOptionsResolved,\n RowData,\n createTable,\n} from '@tanstack/table-core'\n\nexport type Renderable<TProps> = React.ReactNode | React.ComponentType<TProps>\n\n//\n\n/**\n * If rendering headers, cells, or footers with custom markup, use flexRender instead of `cell.getValue()` or `cell.renderValue()`.\n */\nexport function flexRender<TProps extends object>(\n Comp: Renderable<TProps>,\n props: TProps\n): React.ReactNode | JSX.Element {\n return !Comp ? null : isReactComponent<TProps>(Comp) ? (\n <Comp {...props} />\n ) : (\n Comp\n )\n}\n\nfunction isReactComponent<TProps>(\n component: unknown\n): component is React.ComponentType<TProps> {\n return (\n isClassComponent(component) ||\n typeof component === 'function' ||\n isExoticComponent(component)\n )\n}\n\nfunction isClassComponent(component: any) {\n return (\n typeof component === 'function' &&\n (() => {\n const proto = Object.getPrototypeOf(component)\n return proto.prototype && proto.prototype.isReactComponent\n })()\n )\n}\n\nfunction isExoticComponent(component: any) {\n return (\n typeof component === 'object' &&\n typeof component.$$typeof === 'symbol' &&\n ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description)\n )\n}\n\nexport function useReactTable<TData extends RowData>(\n options: TableOptions<TData>\n) {\n // Compose in the generic options to the user options\n const resolvedOptions: TableOptionsResolved<TData> = {\n state: {}, // Dummy state\n onStateChange: () => {}, // noop\n renderFallbackValue: null,\n ...options,\n }\n\n // Create a new table and store it in state\n const [tableRef] = React.useState(() => ({\n current: createTable<TData>(resolvedOptions),\n }))\n\n // By default, manage table state here using the table's initial state\n const [state, setState] = React.useState(() => tableRef.current.initialState)\n\n // Compose the default state above with any user state. This will allow the user\n // to only control a subset of the state if desired.\n tableRef.current.setOptions(prev => ({\n ...prev,\n ...options,\n state: {\n ...state,\n ...options.state,\n },\n // Similarly, we'll maintain both our internal state and any user-provided\n // state.\n onStateChange: updater => {\n setState(updater)\n options.onStateChange?.(updater)\n },\n }))\n\n return tableRef.current\n}\n"],"names":["jsx","React","createTable"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAiBgB,SAAA,WACd,MACA,OAC+B;AACxB,SAAA,CAAC,OAAO,OAAO,iBAAyB,IAAI,IAChDA,+BAAA,MAAA,EAAM,GAAG,MAAO,CAAA,IAEjB;AAEJ;AAEA,SAAS,iBACP,WAC0C;AAC1C,SACE,iBAAiB,SAAS,KAC1B,OAAO,cAAc,cACrB,kBAAkB,SAAS;AAE/B;AAEA,SAAS,iBAAiB,WAAgB;AAEtC,SAAA,OAAO,cAAc,eACpB,MAAM;AACC,UAAA,QAAQ,OAAO,eAAe,SAAS;AACtC,WAAA,MAAM,aAAa,MAAM,UAAU;AAAA,EAAA;AAGhD;AAEA,SAAS,kBAAkB,WAAgB;AACzC,SACE,OAAO,cAAc,YACrB,OAAO,UAAU,aAAa,YAC9B,CAAC,cAAc,mBAAmB,EAAE,SAAS,UAAU,SAAS,WAAW;AAE/E;AAEO,SAAS,cACd,SACA;AAEA,QAAM,kBAA+C;AAAA,IACnD,OAAO,CAAC;AAAA;AAAA,IACR,eAAe,MAAM;AAAA,IAAC;AAAA;AAAA,IACtB,qBAAqB;AAAA,IACrB,GAAG;AAAA,EAAA;AAIL,QAAM,CAAC,QAAQ,IAAIC,iBAAM,SAAS,OAAO;AAAA,IACvC,SAASC,sBAAmB,eAAe;AAAA,EAC3C,EAAA;AAGI,QAAA,CAAC,OAAO,QAAQ,IAAID,iBAAM,SAAS,MAAM,SAAS,QAAQ,YAAY;AAInE,WAAA,QAAQ,WAAW,CAAS,UAAA;AAAA,IACnC,GAAG;AAAA,IACH,GAAG;AAAA,IACH,OAAO;AAAA,MACL,GAAG;AAAA,MACH,GAAG,QAAQ;AAAA,IACb;AAAA;AAAA;AAAA,IAGA,eAAe,CAAW,YAAA;;AACxB,eAAS,OAAO;AAChB,oBAAQ,kBAAR,iCAAwB;AAAA,IAC1B;AAAA,EACA,EAAA;AAEF,SAAO,SAAS;AAClB;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { TableOptions, RowData } from '@tanstack/table-core';
|
|
2
|
-
import * as React from 'react';
|
|
3
|
-
export * from '@tanstack/table-core';
|
|
4
|
-
export type Renderable<TProps> = React.ReactNode | React.ComponentType<TProps>;
|
|
5
|
-
/**
|
|
6
|
-
* If rendering headers, cells, or footers with custom markup, use flexRender instead of `cell.getValue()` or `cell.renderValue()`.
|
|
7
|
-
*/
|
|
8
|
-
export declare function flexRender<TProps extends object>(Comp: Renderable<TProps>, props: TProps): React.ReactNode | JSX.Element;
|
|
9
|
-
export declare function useReactTable<TData extends RowData>(options: TableOptions<TData>): import('@tanstack/table-core').Table<TData>;
|