@tanstack/react-table 9.0.0-alpha.4 → 9.0.0-alpha.40

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.
Files changed (65) hide show
  1. package/README.md +117 -0
  2. package/dist/FlexRender.cjs +61 -0
  3. package/dist/FlexRender.cjs.map +1 -0
  4. package/dist/FlexRender.d.cts +51 -0
  5. package/dist/FlexRender.d.ts +51 -0
  6. package/dist/FlexRender.js +58 -0
  7. package/dist/FlexRender.js.map +1 -0
  8. package/dist/Subscribe.cjs +14 -0
  9. package/dist/Subscribe.cjs.map +1 -0
  10. package/dist/Subscribe.d.cts +103 -0
  11. package/dist/Subscribe.d.ts +103 -0
  12. package/dist/Subscribe.js +13 -0
  13. package/dist/Subscribe.js.map +1 -0
  14. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  15. package/dist/createTableHook.cjs +313 -0
  16. package/dist/createTableHook.cjs.map +1 -0
  17. package/dist/createTableHook.d.cts +358 -0
  18. package/dist/createTableHook.d.ts +358 -0
  19. package/dist/createTableHook.js +311 -0
  20. package/dist/createTableHook.js.map +1 -0
  21. package/dist/flex-render.cjs +5 -0
  22. package/dist/flex-render.d.cts +2 -0
  23. package/dist/flex-render.d.ts +2 -0
  24. package/dist/flex-render.js +3 -0
  25. package/dist/index.cjs +18 -0
  26. package/dist/index.d.cts +6 -0
  27. package/dist/index.d.ts +6 -0
  28. package/dist/index.js +8 -0
  29. package/dist/legacy.cjs +14 -0
  30. package/dist/legacy.d.cts +2 -0
  31. package/dist/legacy.d.ts +2 -0
  32. package/dist/legacy.js +3 -0
  33. package/dist/static-functions.cjs +9 -0
  34. package/dist/static-functions.d.cts +1 -0
  35. package/dist/static-functions.d.ts +1 -0
  36. package/dist/static-functions.js +3 -0
  37. package/dist/useLegacyTable.cjs +193 -0
  38. package/dist/useLegacyTable.cjs.map +1 -0
  39. package/dist/useLegacyTable.d.cts +234 -0
  40. package/dist/useLegacyTable.d.ts +234 -0
  41. package/dist/useLegacyTable.js +182 -0
  42. package/dist/useLegacyTable.js.map +1 -0
  43. package/dist/useTable.cjs +41 -0
  44. package/dist/useTable.cjs.map +1 -0
  45. package/dist/useTable.d.cts +92 -0
  46. package/dist/useTable.d.ts +92 -0
  47. package/dist/useTable.js +40 -0
  48. package/dist/useTable.js.map +1 -0
  49. package/package.json +37 -20
  50. package/src/FlexRender.tsx +147 -0
  51. package/src/Subscribe.ts +199 -0
  52. package/src/createTableHook.tsx +1118 -0
  53. package/src/flex-render.ts +1 -0
  54. package/src/index.ts +6 -0
  55. package/src/legacy.ts +3 -0
  56. package/src/static-functions.ts +1 -0
  57. package/src/useLegacyTable.ts +489 -0
  58. package/src/useTable.ts +160 -0
  59. package/dist/cjs/index.cjs +0 -78
  60. package/dist/cjs/index.cjs.map +0 -1
  61. package/dist/cjs/index.d.cts +0 -9
  62. package/dist/esm/index.d.ts +0 -9
  63. package/dist/esm/index.js +0 -55
  64. package/dist/esm/index.js.map +0 -1
  65. package/src/index.tsx +0 -94
@@ -0,0 +1 @@
1
+ export * from './FlexRender'
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from '@tanstack/table-core'
2
+
3
+ export * from './FlexRender'
4
+ export * from './Subscribe'
5
+ export * from './createTableHook'
6
+ export * from './useTable'
package/src/legacy.ts ADDED
@@ -0,0 +1,3 @@
1
+ // Legacy entry - v8-style API for incremental migration
2
+ // Import flexRender, types (ColumnFiltersState, etc.) from '@tanstack/react-table'
3
+ export * from './useLegacyTable'
@@ -0,0 +1 @@
1
+ export * from '@tanstack/table-core/static-functions'
@@ -0,0 +1,489 @@
1
+ 'use client'
2
+
3
+ import {
4
+ aggregationFns,
5
+ createColumnHelper,
6
+ createExpandedRowModel,
7
+ createFacetedMinMaxValues,
8
+ createFacetedRowModel,
9
+ createFacetedUniqueValues,
10
+ createFilteredRowModel,
11
+ createGroupedRowModel,
12
+ createPaginatedRowModel,
13
+ createSortedRowModel,
14
+ filterFns,
15
+ sortFns,
16
+ stockFeatures,
17
+ } from '@tanstack/table-core'
18
+ import { useCallback, useMemo } from 'react'
19
+ import { useTable } from './useTable'
20
+ import type {
21
+ AggregationFns,
22
+ Cell,
23
+ Column,
24
+ ColumnDef,
25
+ CreateRowModels_All,
26
+ FilterFns,
27
+ Header,
28
+ HeaderGroup,
29
+ Row,
30
+ RowData,
31
+ RowModel,
32
+ SortFns,
33
+ StockFeatures,
34
+ Table,
35
+ TableOptions,
36
+ TableState,
37
+ } from '@tanstack/table-core'
38
+ import type { ReactTable } from './useTable'
39
+
40
+ // =============================================================================
41
+ // V8-style row model factory functions
42
+ // These are stub functions that act as markers for useLegacyTable to know
43
+ // which row models to enable. They don't actually do anything - the real
44
+ // implementation is handled by useLegacyTable internally.
45
+ // =============================================================================
46
+
47
+ /**
48
+ * @deprecated Use `createFilteredRowModel(filterFns)` with the new `useTable` hook instead.
49
+ *
50
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
51
+ * It acts as a marker to enable the filtered row model.
52
+ */
53
+ export function getFilteredRowModel<
54
+ TData extends RowData,
55
+ >(): RowModelFactory<TData> {
56
+ return (() => () => {}) as unknown as RowModelFactory<TData>
57
+ }
58
+
59
+ /**
60
+ * @deprecated Use `createSortedRowModel(sortFns)` with the new `useTable` hook instead.
61
+ *
62
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
63
+ * It acts as a marker to enable the sorted row model.
64
+ */
65
+ export function getSortedRowModel<
66
+ TData extends RowData,
67
+ >(): RowModelFactory<TData> {
68
+ return (() => () => {}) as unknown as RowModelFactory<TData>
69
+ }
70
+
71
+ /**
72
+ * @deprecated Use `createPaginatedRowModel()` with the new `useTable` hook instead.
73
+ *
74
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
75
+ * It acts as a marker to enable the paginated row model.
76
+ */
77
+ export function getPaginationRowModel<
78
+ TData extends RowData,
79
+ >(): RowModelFactory<TData> {
80
+ return (() => () => {}) as unknown as RowModelFactory<TData>
81
+ }
82
+
83
+ /**
84
+ * @deprecated Use `createExpandedRowModel()` with the new `useTable` hook instead.
85
+ *
86
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
87
+ * It acts as a marker to enable the expanded row model.
88
+ */
89
+ export function getExpandedRowModel<
90
+ TData extends RowData,
91
+ >(): RowModelFactory<TData> {
92
+ return (() => () => {}) as unknown as RowModelFactory<TData>
93
+ }
94
+
95
+ /**
96
+ * @deprecated Use `createGroupedRowModel(aggregationFns)` with the new `useTable` hook instead.
97
+ *
98
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
99
+ * It acts as a marker to enable the grouped row model.
100
+ */
101
+ export function getGroupedRowModel<
102
+ TData extends RowData,
103
+ >(): RowModelFactory<TData> {
104
+ return (() => () => {}) as unknown as RowModelFactory<TData>
105
+ }
106
+
107
+ /**
108
+ * @deprecated Use `createFacetedRowModel()` with the new `useTable` hook instead.
109
+ *
110
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
111
+ * It acts as a marker to enable the faceted row model.
112
+ */
113
+ export function getFacetedRowModel<
114
+ TData extends RowData,
115
+ >(): FacetedRowModelFactory<TData> {
116
+ return (() => () => {}) as unknown as FacetedRowModelFactory<TData>
117
+ }
118
+
119
+ /**
120
+ * @deprecated Use `createFacetedMinMaxValues()` with the new `useTable` hook instead.
121
+ *
122
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
123
+ * It acts as a marker to enable the faceted min/max values.
124
+ */
125
+ export function getFacetedMinMaxValues<
126
+ TData extends RowData,
127
+ >(): FacetedMinMaxValuesFactory<TData> {
128
+ return () => () => undefined
129
+ }
130
+
131
+ /**
132
+ * @deprecated Use `createFacetedUniqueValues()` with the new `useTable` hook instead.
133
+ *
134
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
135
+ * It acts as a marker to enable the faceted unique values.
136
+ */
137
+ export function getFacetedUniqueValues<
138
+ TData extends RowData,
139
+ >(): FacetedUniqueValuesFactory<TData> {
140
+ return () => () => new Map()
141
+ }
142
+
143
+ /**
144
+ * @deprecated The core row model is always created automatically in v9.
145
+ *
146
+ * This is a stub function for v8 API compatibility with `useLegacyTable`.
147
+ * It does nothing - the core row model is always available.
148
+ */
149
+ export function getCoreRowModel<
150
+ TData extends RowData,
151
+ >(): RowModelFactory<TData> {
152
+ return (() => () => {}) as unknown as RowModelFactory<TData>
153
+ }
154
+
155
+ // =============================================================================
156
+ // Type definitions
157
+ // =============================================================================
158
+
159
+ /**
160
+ * Row model factory function type from v8 API
161
+ */
162
+ export type RowModelFactory<TData extends RowData> = (
163
+ table: Table<StockFeatures, TData>,
164
+ ) => () => RowModel<StockFeatures, TData>
165
+
166
+ /**
167
+ * Faceted row model factory function type from v8 API
168
+ */
169
+ export type FacetedRowModelFactory<TData extends RowData> = (
170
+ table: Table<StockFeatures, TData>,
171
+ columnId: string,
172
+ ) => () => RowModel<StockFeatures, TData>
173
+
174
+ /**
175
+ * Faceted min/max values factory function type from v8 API
176
+ */
177
+ export type FacetedMinMaxValuesFactory<TData extends RowData> = (
178
+ table: Table<StockFeatures, TData>,
179
+ columnId: string,
180
+ ) => () => undefined | [number, number]
181
+
182
+ /**
183
+ * Faceted unique values factory function type from v8 API
184
+ */
185
+ export type FacetedUniqueValuesFactory<TData extends RowData> = (
186
+ table: Table<StockFeatures, TData>,
187
+ columnId: string,
188
+ ) => () => Map<any, number>
189
+
190
+ /**
191
+ * Legacy v8-style row model options
192
+ */
193
+ export interface LegacyRowModelOptions<TData extends RowData> {
194
+ /**
195
+ * Returns the core row model for the table.
196
+ * @deprecated This option is no longer needed in v9. The core row model is always created automatically.
197
+ */
198
+ getCoreRowModel?: RowModelFactory<TData>
199
+ /**
200
+ * Returns the filtered row model for the table.
201
+ * @deprecated Use `_rowModels.filteredRowModel` with `createFilteredRowModel(filterFns)` instead.
202
+ */
203
+ getFilteredRowModel?: RowModelFactory<TData>
204
+ /**
205
+ * Returns the sorted row model for the table.
206
+ * @deprecated Use `_rowModels.sortedRowModel` with `createSortedRowModel(sortFns)` instead.
207
+ */
208
+ getSortedRowModel?: RowModelFactory<TData>
209
+ /**
210
+ * Returns the paginated row model for the table.
211
+ * @deprecated Use `_rowModels.paginatedRowModel` with `createPaginatedRowModel()` instead.
212
+ */
213
+ getPaginationRowModel?: RowModelFactory<TData>
214
+ /**
215
+ * Returns the expanded row model for the table.
216
+ * @deprecated Use `_rowModels.expandedRowModel` with `createExpandedRowModel()` instead.
217
+ */
218
+ getExpandedRowModel?: RowModelFactory<TData>
219
+ /**
220
+ * Returns the grouped row model for the table.
221
+ * @deprecated Use `_rowModels.groupedRowModel` with `createGroupedRowModel(aggregationFns)` instead.
222
+ */
223
+ getGroupedRowModel?: RowModelFactory<TData>
224
+ /**
225
+ * Returns the faceted row model for a column.
226
+ * @deprecated Use `_rowModels.facetedRowModel` with `createFacetedRowModel()` instead.
227
+ */
228
+ getFacetedRowModel?: FacetedRowModelFactory<TData>
229
+ /**
230
+ * Returns the faceted min/max values for a column.
231
+ * @deprecated Use `_rowModels.facetedMinMaxValues` with `createFacetedMinMaxValues()` instead.
232
+ */
233
+ getFacetedMinMaxValues?: FacetedMinMaxValuesFactory<TData>
234
+ /**
235
+ * Returns the faceted unique values for a column.
236
+ * @deprecated Use `_rowModels.facetedUniqueValues` with `createFacetedUniqueValues()` instead.
237
+ */
238
+ getFacetedUniqueValues?: FacetedUniqueValuesFactory<TData>
239
+ /**
240
+ * Additional filter functions to apply to the table.
241
+ * @deprecated Use `_rowModels.filteredRowModel` with `createFilteredRowModel(filterFns)` instead.
242
+ */
243
+ filterFns?: FilterFns
244
+ /**
245
+ * Additional sort functions to apply to the table.
246
+ * @deprecated Use `_rowModels.sortedRowModel` with `createSortedRowModel(sortFns)` instead.
247
+ */
248
+ sortFns?: SortFns
249
+ /**
250
+ * Additional aggregation functions to apply to the table.
251
+ * @deprecated Use `_rowModels.groupedRowModel` with `createGroupedRowModel(aggregationFns)` instead.
252
+ */
253
+ aggregationFns?: AggregationFns
254
+ }
255
+
256
+ /**
257
+ * Legacy v8-style table options that work with useLegacyTable.
258
+ *
259
+ * This type omits `_features` and `_rowModels` and instead accepts the v8-style
260
+ * `get*RowModel` function options.
261
+ *
262
+ * @deprecated This is a compatibility layer for migrating from v8. Use `useTable` with explicit `_features` and `_rowModels` instead.
263
+ */
264
+ export type LegacyTableOptions<TData extends RowData> = Omit<
265
+ TableOptions<StockFeatures, TData>,
266
+ '_features' | '_rowModels'
267
+ > &
268
+ LegacyRowModelOptions<TData>
269
+
270
+ /**
271
+ * Legacy table instance type that includes the v8-style `getState()` method.
272
+ *
273
+ * @deprecated Use `useTable` with explicit state selection instead.
274
+ */
275
+ export type LegacyReactTable<TData extends RowData> = ReactTable<
276
+ StockFeatures,
277
+ TData,
278
+ TableState<StockFeatures>
279
+ > & {
280
+ /**
281
+ * Returns the current table state.
282
+ * @deprecated In v9, access state directly via `table.state` or use `table.store.state` for the full state.
283
+ */
284
+ getState: () => TableState<StockFeatures>
285
+ /**
286
+ * Sets the current table state.
287
+ * @deprecated In v9, access state directly via `table.baseAtoms`
288
+ */
289
+ setState: (state: TableState<StockFeatures>) => void
290
+ }
291
+
292
+ // =============================================================================
293
+ // Legacy type aliases - StockFeatures hardcoded for simpler prop typing with useLegacyTable
294
+ // =============================================================================
295
+
296
+ /** @deprecated Use Column<TFeatures, TData, TValue> with useTable instead. */
297
+ export type LegacyColumn<TData extends RowData, TValue = unknown> = Column<
298
+ StockFeatures,
299
+ TData,
300
+ TValue
301
+ >
302
+
303
+ /** @deprecated Use Row<TFeatures, TData> with useTable instead. */
304
+ export type LegacyRow<TData extends RowData> = Row<StockFeatures, TData>
305
+
306
+ /** @deprecated Use Cell<TFeatures, TData, TValue> with useTable instead. */
307
+ export type LegacyCell<TData extends RowData, TValue = unknown> = Cell<
308
+ StockFeatures,
309
+ TData,
310
+ TValue
311
+ >
312
+
313
+ /** @deprecated Use Header<TFeatures, TData, TValue> with useTable instead. */
314
+ export type LegacyHeader<TData extends RowData, TValue = unknown> = Header<
315
+ StockFeatures,
316
+ TData,
317
+ TValue
318
+ >
319
+
320
+ /** @deprecated Use HeaderGroup<TFeatures, TData> with useTable instead. */
321
+ export type LegacyHeaderGroup<TData extends RowData> = HeaderGroup<
322
+ StockFeatures,
323
+ TData
324
+ >
325
+
326
+ /** @deprecated Use ColumnDef<TFeatures, TData, TValue> with useTable instead. */
327
+ export type LegacyColumnDef<
328
+ TData extends RowData,
329
+ TValue = unknown,
330
+ > = ColumnDef<StockFeatures, TData, TValue>
331
+
332
+ /** @deprecated Use Table<TFeatures, TData> with useTable instead. */
333
+ export type LegacyTable<TData extends RowData> = Table<StockFeatures, TData>
334
+
335
+ // =============================================================================
336
+ // Legacy column helper - StockFeatures hardcoded
337
+ // =============================================================================
338
+
339
+ /**
340
+ * @deprecated Use `createColumnHelper<TFeatures, TData>()` with useTable instead.
341
+ *
342
+ * A column helper with StockFeatures pre-bound for use with useLegacyTable.
343
+ * Only requires TData—no need to specify TFeatures.
344
+ */
345
+ export function legacyCreateColumnHelper<TData extends RowData>() {
346
+ return createColumnHelper<StockFeatures, TData>()
347
+ }
348
+
349
+ // =============================================================================
350
+ // useLegacyTable hook
351
+ // =============================================================================
352
+
353
+ /**
354
+ * @deprecated This hook is provided as a compatibility layer for migrating from TanStack Table v8.
355
+ *
356
+ * Use the new `useTable` hook instead with explicit `_features` and `_rowModels`:
357
+ *
358
+ * ```tsx
359
+ * // New v9 API
360
+ * const _features = tableFeatures({
361
+ * columnFilteringFeature,
362
+ * rowSortingFeature,
363
+ * rowPaginationFeature,
364
+ * })
365
+ *
366
+ * const table = useTable({
367
+ * _features,
368
+ * _rowModels: {
369
+ * filteredRowModel: createFilteredRowModel(filterFns),
370
+ * sortedRowModel: createSortedRowModel(sortFns),
371
+ * paginatedRowModel: createPaginatedRowModel(),
372
+ * },
373
+ * columns,
374
+ * data,
375
+ * })
376
+ * ```
377
+ *
378
+ * Key differences from v8:
379
+ * - Features are tree-shakeable - only import what you use
380
+ * - Row models are explicitly passed via `_rowModels`
381
+ * - Use `table.Subscribe` for fine-grained re-renders
382
+ * - State is accessed via `table.state` after selecting with the 2nd argument
383
+ *
384
+ * @param options - Legacy v8-style table options
385
+ * @returns A table instance with the full state subscribed and a `getState()` method
386
+ */
387
+ export function useLegacyTable<TData extends RowData>(
388
+ options: LegacyTableOptions<TData>,
389
+ ): LegacyReactTable<TData> {
390
+ const {
391
+ // Extract legacy row model options
392
+ getCoreRowModel: _getCoreRowModel,
393
+ getFilteredRowModel,
394
+ getSortedRowModel,
395
+ getPaginationRowModel,
396
+ getExpandedRowModel,
397
+ getGroupedRowModel,
398
+ getFacetedRowModel,
399
+ getFacetedMinMaxValues,
400
+ getFacetedUniqueValues,
401
+ // Rest of the options
402
+ ...restOptions
403
+ } = options
404
+
405
+ // Build the _rowModels object based on which legacy options were provided
406
+ const _rowModels: CreateRowModels_All<StockFeatures, TData> = {}
407
+
408
+ // Map v8 row model factories to v9 _rowModels
409
+ // Note: getCoreRowModel is handled automatically in v9, so we ignore it
410
+
411
+ if (getFilteredRowModel) {
412
+ _rowModels.filteredRowModel = createFilteredRowModel({
413
+ ...filterFns,
414
+ ...options.filterFns,
415
+ })
416
+ }
417
+
418
+ if (getSortedRowModel) {
419
+ _rowModels.sortedRowModel = createSortedRowModel({
420
+ ...sortFns,
421
+ ...options.sortFns,
422
+ })
423
+ }
424
+
425
+ if (getPaginationRowModel) {
426
+ _rowModels.paginatedRowModel = createPaginatedRowModel()
427
+ }
428
+
429
+ if (getExpandedRowModel) {
430
+ _rowModels.expandedRowModel = createExpandedRowModel()
431
+ }
432
+
433
+ if (getGroupedRowModel) {
434
+ _rowModels.groupedRowModel = createGroupedRowModel({
435
+ ...aggregationFns,
436
+ ...options.aggregationFns,
437
+ })
438
+ }
439
+
440
+ if (getFacetedRowModel) {
441
+ _rowModels.facetedRowModel = createFacetedRowModel()
442
+ }
443
+
444
+ if (getFacetedMinMaxValues) {
445
+ _rowModels.facetedMinMaxValues = createFacetedMinMaxValues()
446
+ }
447
+
448
+ if (getFacetedUniqueValues) {
449
+ _rowModels.facetedUniqueValues = createFacetedUniqueValues()
450
+ }
451
+
452
+ // Call useTable with the v9 API, subscribing to all state changes
453
+ const table = useTable<StockFeatures, TData, TableState<StockFeatures>>(
454
+ {
455
+ ...restOptions,
456
+ _features: stockFeatures,
457
+ _rowModels,
458
+ },
459
+ (state) => state,
460
+ )
461
+
462
+ const getState = useCallback(() => {
463
+ // all state except for columns and data
464
+ return {
465
+ ...table.state,
466
+ columns: undefined,
467
+ data: undefined,
468
+ }
469
+ }, [table])
470
+
471
+ const setState = useCallback(
472
+ (state: TableState<StockFeatures>) => {
473
+ Object.entries(state).forEach(([key, value]) => {
474
+ // @ts-expect-error - baseAtoms is indexed by dynamic string keys
475
+ table.baseAtoms[key].set(value)
476
+ })
477
+ },
478
+ [table],
479
+ )
480
+
481
+ return useMemo(
482
+ () => ({
483
+ ...table,
484
+ getState,
485
+ setState,
486
+ }),
487
+ [table, getState, setState],
488
+ )
489
+ }
@@ -0,0 +1,160 @@
1
+ 'use client'
2
+
3
+ import { useMemo, useState } from 'react'
4
+ import { constructTable } from '@tanstack/table-core'
5
+ import { shallow, useSelector } from '@tanstack/react-store'
6
+ import { FlexRender } from './FlexRender'
7
+ import { Subscribe } from './Subscribe'
8
+ import type {
9
+ CellData,
10
+ RowData,
11
+ Table,
12
+ TableFeatures,
13
+ TableOptions,
14
+ TableState,
15
+ } from '@tanstack/table-core'
16
+ import type { Atom, ReadonlyAtom } from '@tanstack/react-store'
17
+ import type { FunctionComponent, ReactNode } from 'react'
18
+ import type { FlexRenderProps } from './FlexRender'
19
+ import type { SubscribePropsWithStore } from './Subscribe'
20
+
21
+ export type ReactTable<
22
+ TFeatures extends TableFeatures,
23
+ TData extends RowData,
24
+ TSelected = {},
25
+ > = Table<TFeatures, TData> & {
26
+ /**
27
+ * A React HOC (Higher Order Component) that allows you to subscribe to the table state.
28
+ *
29
+ * This is useful for opting into state re-renders for specific parts of the table state.
30
+ *
31
+ * Pass `source` to subscribe to a single atom or store (e.g. `table.atoms.rowSelection`
32
+ * or `table.optionsStore`) instead of the full `table.store`.
33
+ *
34
+ * @example
35
+ * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
36
+ * {({ rowSelection }) => (
37
+ * <tr key={row.id}>...</tr>
38
+ * )}
39
+ * </table.Subscribe>
40
+ *
41
+ * @example
42
+ * <table.Subscribe source={table.atoms.rowSelection}>
43
+ * {(rowSelection) => <div>...</div>}
44
+ * </table.Subscribe>
45
+ *
46
+ * @example
47
+ * <table.Subscribe source={table.atoms.rowSelection} selector={(s) => s?.[row.id]}>
48
+ * {() => <tr key={row.id}>...</tr>}
49
+ * </table.Subscribe>
50
+ */
51
+ /**
52
+ * Overloads (not a single union) so `selector` callbacks get correct contextual
53
+ * types in JSX; a union of two `selector` signatures degrades to implicit `any`.
54
+ *
55
+ * Source **without** `selector` is a separate overload so children receive `TSourceValue`
56
+ * (identity projection). If `selector` were optional on one overload, `TSubSelected`
57
+ * would default to `unknown` instead of inferring from the source.
58
+ *
59
+ * The **source** overloads are listed first so `TSourceValue` is inferred from `source`.
60
+ */
61
+ Subscribe: {
62
+ <TSourceValue>(props: {
63
+ source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>
64
+ selector?: undefined
65
+ children: ((state: TSourceValue) => ReactNode) | ReactNode
66
+ }): ReturnType<FunctionComponent>
67
+ <TSourceValue, TSubSelected>(props: {
68
+ source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>
69
+ selector: (state: TSourceValue) => TSubSelected
70
+ children: ((state: TSubSelected) => ReactNode) | ReactNode
71
+ }): ReturnType<FunctionComponent>
72
+ <TSubSelected>(
73
+ props: Omit<
74
+ SubscribePropsWithStore<TFeatures, TData, TSubSelected>,
75
+ 'table'
76
+ >,
77
+ ): ReturnType<FunctionComponent>
78
+ }
79
+ /**
80
+ * A React component that renders headers, cells, or footers with custom markup.
81
+ * Use this utility component instead of manually calling flexRender.
82
+ *
83
+ * @example
84
+ * ```tsx
85
+ * <table.FlexRender cell={cell} />
86
+ * <table.FlexRender header={header} />
87
+ * <table.FlexRender footer={footer} />
88
+ * ```
89
+ *
90
+ * This replaces calling `flexRender` directly like this:
91
+ * ```tsx
92
+ * flexRender(cell.column.columnDef.cell, cell.getContext())
93
+ * flexRender(header.column.columnDef.header, header.getContext())
94
+ * flexRender(footer.column.columnDef.footer, footer.getContext())
95
+ * ```
96
+ */
97
+ FlexRender: <TValue extends CellData = CellData>(
98
+ props: FlexRenderProps<TFeatures, TData, TValue>,
99
+ ) => ReactNode
100
+ /**
101
+ * 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`.
102
+ *
103
+ * @example
104
+ * const table = useTable(options, (state) => ({ globalFilter: state.globalFilter })) // only globalFilter is part of the selected state
105
+ *
106
+ * console.log(table.state.globalFilter)
107
+ */
108
+ readonly state: Readonly<TSelected> & {
109
+ columns: TableOptions<TFeatures, TData>['columns']
110
+ data: TableOptions<TFeatures, TData>['data']
111
+ }
112
+ }
113
+
114
+ export function useTable<
115
+ TFeatures extends TableFeatures,
116
+ TData extends RowData,
117
+ TSelected = {},
118
+ >(
119
+ tableOptions: TableOptions<TFeatures, TData>,
120
+ selector: (state: TableState<TFeatures>) => TSelected = () =>
121
+ ({}) as TSelected,
122
+ ): ReactTable<TFeatures, TData, TSelected> {
123
+ const [table] = useState(() => {
124
+ const tableInstance = constructTable(tableOptions) as ReactTable<
125
+ TFeatures,
126
+ TData,
127
+ TSelected
128
+ >
129
+
130
+ tableInstance.Subscribe = ((props: any) => {
131
+ return Subscribe({
132
+ ...props,
133
+ table: tableInstance,
134
+ })
135
+ }) as ReactTable<TFeatures, TData, TSelected>['Subscribe']
136
+
137
+ tableInstance.FlexRender = FlexRender
138
+
139
+ return tableInstance
140
+ })
141
+
142
+ // sync table options on every render
143
+ table.setOptions((prev) => ({
144
+ ...prev,
145
+ ...tableOptions,
146
+ }))
147
+
148
+ const state = useSelector(table.store, selector, { compare: shallow })
149
+
150
+ // we know this is not the most efficient way to return the table,
151
+ // but it is required for the react compiler to work
152
+ return useMemo(
153
+ () => ({
154
+ ...table,
155
+ options: tableOptions,
156
+ state,
157
+ }),
158
+ [table, tableOptions, state],
159
+ ) as ReactTable<TFeatures, TData, TSelected>
160
+ }