@tanstack/react-table 9.0.0-alpha.33 → 9.0.0-alpha.35

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/src/Subscribe.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  'use client'
2
2
 
3
3
  import { shallow, useSelector } from '@tanstack/react-store'
4
+ import type { Atom, ReadonlyAtom } from '@tanstack/react-store'
4
5
  import type {
5
- NoInfer,
6
6
  RowData,
7
7
  Table,
8
8
  TableFeatures,
@@ -10,35 +10,104 @@ import type {
10
10
  } from '@tanstack/table-core'
11
11
  import type { FunctionComponent, ReactNode } from 'react'
12
12
 
13
- export type SubscribeProps<
13
+ /**
14
+ * Subscribe to `table.store` (full table state). The selector receives the full
15
+ * {@link TableState}.
16
+ */
17
+ export type SubscribePropsWithStore<
14
18
  TFeatures extends TableFeatures,
15
19
  TData extends RowData,
16
- TSelected = {},
20
+ TSelected,
17
21
  > = {
18
- /**
19
- * The table instance to subscribe to. Required when using as a standalone component.
20
- * Not needed when using as `table.Subscribe`.
21
- */
22
22
  table: Table<TFeatures, TData>
23
23
  /**
24
- * A selector function that selects the part of the table state to subscribe to.
25
- * This allows for fine-grained reactivity by only re-rendering when the selected state changes.
26
- */
27
- selector: (state: NoInfer<TableState<TFeatures>>) => TSelected
28
- /**
29
- * The children to render. Can be a function that receives the selected state, or a React node.
24
+ * Select from full table state. Re-renders when the selected value changes
25
+ * (shallow compare).
26
+ *
27
+ * Required in store mode so you never accidentally subscribe to the whole
28
+ * store without an explicit projection.
30
29
  */
30
+ selector: (state: TableState<TFeatures>) => TSelected
31
31
  children: ((state: TSelected) => ReactNode) | ReactNode
32
32
  }
33
33
 
34
+ /**
35
+ * Subscribe to the full value of a source (e.g. `table.atoms.rowSelection` or
36
+ * `table.optionsStore`). Omitting `selector` is equivalent to the identity
37
+ * selector — children receive `TSourceValue`.
38
+ */
39
+ export type SubscribePropsWithSourceIdentity<
40
+ TFeatures extends TableFeatures,
41
+ TData extends RowData,
42
+ TSourceValue,
43
+ > = {
44
+ table: Table<TFeatures, TData>
45
+ source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>
46
+ selector?: undefined
47
+ children: ((state: TSourceValue) => ReactNode) | ReactNode
48
+ }
49
+
50
+ /**
51
+ * Subscribe to a projected value from a source (atom or store). The selector
52
+ * receives the source value; children receive the projected `TSelected`.
53
+ */
54
+ export type SubscribePropsWithSourceWithSelector<
55
+ TFeatures extends TableFeatures,
56
+ TData extends RowData,
57
+ TSourceValue,
58
+ TSelected,
59
+ > = {
60
+ table: Table<TFeatures, TData>
61
+ source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>
62
+ selector: (state: TSourceValue) => TSelected
63
+ children: ((state: TSelected) => ReactNode) | ReactNode
64
+ }
65
+
66
+ /**
67
+ * Subscribe to a single source — atom or store (identity or projected). Prefer
68
+ * {@link SubscribePropsWithSourceIdentity} or {@link SubscribePropsWithSourceWithSelector}
69
+ * for clearer inference when `selector` is omitted.
70
+ */
71
+ export type SubscribePropsWithSource<
72
+ TFeatures extends TableFeatures,
73
+ TData extends RowData,
74
+ TSourceValue,
75
+ TSelected = TSourceValue,
76
+ > =
77
+ | SubscribePropsWithSourceIdentity<TFeatures, TData, TSourceValue>
78
+ | SubscribePropsWithSourceWithSelector<
79
+ TFeatures,
80
+ TData,
81
+ TSourceValue,
82
+ TSelected
83
+ >
84
+
85
+ export type SubscribeProps<
86
+ TFeatures extends TableFeatures,
87
+ TData extends RowData,
88
+ TSelected = unknown,
89
+ TSourceValue = unknown,
90
+ > =
91
+ | SubscribePropsWithStore<TFeatures, TData, TSelected>
92
+ | SubscribePropsWithSourceIdentity<TFeatures, TData, TSourceValue>
93
+ | SubscribePropsWithSourceWithSelector<
94
+ TFeatures,
95
+ TData,
96
+ TSourceValue,
97
+ TSelected
98
+ >
99
+
34
100
  /**
35
101
  * A React component that allows you to subscribe to the table state.
36
102
  *
37
103
  * This is useful for opting into state re-renders for specific parts of the table state.
38
104
  *
105
+ * For `table.Subscribe` from `useTable`, prefer that API — it uses overloads so JSX
106
+ * contextual typing works. This standalone component uses a union `props` type.
107
+ *
39
108
  * @example
40
109
  * ```tsx
41
- * // As a standalone component
110
+ * // As a standalone component — full store
42
111
  * <Subscribe table={table} selector={(state) => ({ rowSelection: state.rowSelection })}>
43
112
  * {({ rowSelection }) => (
44
113
  * <div>Selected rows: {Object.keys(rowSelection).length}</div>
@@ -48,6 +117,26 @@ export type SubscribeProps<
48
117
  *
49
118
  * @example
50
119
  * ```tsx
120
+ * // Entire source (atom or store) — no selector
121
+ * <Subscribe table={table} source={table.atoms.rowSelection}>
122
+ * {(rowSelection) => <div>...</div>}
123
+ * </Subscribe>
124
+ * ```
125
+ *
126
+ * @example
127
+ * ```tsx
128
+ * // Project source value (e.g. one row’s selection)
129
+ * <Subscribe
130
+ * table={table}
131
+ * source={table.atoms.rowSelection}
132
+ * selector={(rowSelection) => rowSelection?.[row.id]}
133
+ * >
134
+ * {(selected) => <tr data-selected={!!selected}>...</tr>}
135
+ * </Subscribe>
136
+ * ```
137
+ *
138
+ * @example
139
+ * ```tsx
51
140
  * // As table.Subscribe (table instance method)
52
141
  * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
53
142
  * {({ rowSelection }) => (
@@ -59,15 +148,52 @@ export type SubscribeProps<
59
148
  export function Subscribe<
60
149
  TFeatures extends TableFeatures,
61
150
  TData extends RowData,
62
- TSelected = {},
151
+ TSourceValue,
152
+ >(
153
+ props: SubscribePropsWithSourceIdentity<TFeatures, TData, TSourceValue>,
154
+ ): ReturnType<FunctionComponent>
155
+ export function Subscribe<
156
+ TFeatures extends TableFeatures,
157
+ TData extends RowData,
158
+ TSourceValue,
159
+ TSelected,
160
+ >(
161
+ props: SubscribePropsWithSourceWithSelector<
162
+ TFeatures,
163
+ TData,
164
+ TSourceValue,
165
+ TSelected
166
+ >,
167
+ ): ReturnType<FunctionComponent>
168
+ export function Subscribe<
169
+ TFeatures extends TableFeatures,
170
+ TData extends RowData,
171
+ TSelected,
63
172
  >(
64
- props: SubscribeProps<TFeatures, TData, TSelected>,
173
+ props: SubscribePropsWithStore<TFeatures, TData, TSelected>,
174
+ ): ReturnType<FunctionComponent>
175
+ export function Subscribe<
176
+ TFeatures extends TableFeatures,
177
+ TData extends RowData,
178
+ TSelected,
179
+ TSourceValue,
180
+ >(
181
+ props: SubscribeProps<TFeatures, TData, TSelected, TSourceValue>,
65
182
  ): ReturnType<FunctionComponent> {
66
- const selected = useSelector(props.table.store, props.selector, {
67
- compare: shallow,
68
- })
183
+ const source = 'source' in props ? props.source : props.table.store
184
+ const selectFn =
185
+ 'source' in props ? (props.selector ?? ((x: unknown) => x)) : props.selector
186
+
187
+ const selected = useSelector(
188
+ // Atom and store share the same selection protocol; union args need a widen for TS.
189
+ source,
190
+ selectFn as Parameters<typeof useSelector>[1],
191
+ {
192
+ compare: shallow,
193
+ },
194
+ ) as TSelected
69
195
 
70
196
  return typeof props.children === 'function'
71
- ? props.children(selected)
197
+ ? (props.children as (state: TSelected) => ReactNode)(selected)
72
198
  : props.children
73
199
  }
@@ -0,0 +1 @@
1
+ export * from '@tanstack/table-core/static-functions'
@@ -15,18 +15,21 @@ import {
15
15
  sortFns,
16
16
  stockFeatures,
17
17
  } from '@tanstack/table-core'
18
- import { useMemo } from 'react'
18
+ import { useCallback, useMemo } from 'react'
19
19
  import { useTable } from './useTable'
20
20
  import type {
21
+ AggregationFns,
21
22
  Cell,
22
23
  Column,
23
24
  ColumnDef,
24
25
  CreateRowModels_All,
26
+ FilterFns,
25
27
  Header,
26
28
  HeaderGroup,
27
29
  Row,
28
30
  RowData,
29
31
  RowModel,
32
+ SortFns,
30
33
  StockFeatures,
31
34
  Table,
32
35
  TableOptions,
@@ -233,6 +236,21 @@ export interface LegacyRowModelOptions<TData extends RowData> {
233
236
  * @deprecated Use `_rowModels.facetedUniqueValues` with `createFacetedUniqueValues()` instead.
234
237
  */
235
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
236
254
  }
237
255
 
238
256
  /**
@@ -264,6 +282,11 @@ export type LegacyReactTable<TData extends RowData> = ReactTable<
264
282
  * @deprecated In v9, access state directly via `table.state` or use `table.store.state` for the full state.
265
283
  */
266
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
267
290
  }
268
291
 
269
292
  // =============================================================================
@@ -386,11 +409,17 @@ export function useLegacyTable<TData extends RowData>(
386
409
  // Note: getCoreRowModel is handled automatically in v9, so we ignore it
387
410
 
388
411
  if (getFilteredRowModel) {
389
- _rowModels.filteredRowModel = createFilteredRowModel(filterFns)
412
+ _rowModels.filteredRowModel = createFilteredRowModel({
413
+ ...filterFns,
414
+ ...options.filterFns,
415
+ })
390
416
  }
391
417
 
392
418
  if (getSortedRowModel) {
393
- _rowModels.sortedRowModel = createSortedRowModel(sortFns)
419
+ _rowModels.sortedRowModel = createSortedRowModel({
420
+ ...sortFns,
421
+ ...options.sortFns,
422
+ })
394
423
  }
395
424
 
396
425
  if (getPaginationRowModel) {
@@ -402,7 +431,10 @@ export function useLegacyTable<TData extends RowData>(
402
431
  }
403
432
 
404
433
  if (getGroupedRowModel) {
405
- _rowModels.groupedRowModel = createGroupedRowModel(aggregationFns)
434
+ _rowModels.groupedRowModel = createGroupedRowModel({
435
+ ...aggregationFns,
436
+ ...options.aggregationFns,
437
+ })
406
438
  }
407
439
 
408
440
  if (getFacetedRowModel) {
@@ -427,12 +459,32 @@ export function useLegacyTable<TData extends RowData>(
427
459
  (state) => state,
428
460
  )
429
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
+
430
481
  return useMemo(
431
482
  () =>
432
483
  ({
433
484
  ...table,
434
- getState: () => table.state,
485
+ getState,
486
+ setState,
435
487
  }) as LegacyReactTable<TData>,
436
- [table],
488
+ [table, getState, setState],
437
489
  )
438
490
  }
package/src/useTable.ts CHANGED
@@ -7,16 +7,16 @@ import { FlexRender } from './FlexRender'
7
7
  import { Subscribe } from './Subscribe'
8
8
  import type {
9
9
  CellData,
10
- NoInfer,
11
10
  RowData,
12
11
  Table,
13
12
  TableFeatures,
14
13
  TableOptions,
15
14
  TableState,
16
15
  } from '@tanstack/table-core'
16
+ import type { Atom, ReadonlyAtom } from '@tanstack/react-store'
17
17
  import type { FunctionComponent, ReactNode } from 'react'
18
18
  import type { FlexRenderProps } from './FlexRender'
19
- import type { SubscribeProps } from './Subscribe'
19
+ import type { SubscribePropsWithStore } from './Subscribe'
20
20
 
21
21
  export type ReactTable<
22
22
  TFeatures extends TableFeatures,
@@ -28,19 +28,54 @@ export type ReactTable<
28
28
  *
29
29
  * This is useful for opting into state re-renders for specific parts of the table state.
30
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
+ *
31
34
  * @example
32
35
  * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
33
- * {({ rowSelection }) => ( // important to include `{() => {()}}` syntax
34
- * <tr key={row.id}>
35
- // render the row
36
- * </tr>
37
- * ))}
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>}
38
49
  * </table.Subscribe>
39
50
  */
40
- Subscribe: <TSelected>(props: {
41
- selector: (state: NoInfer<TableState<TFeatures>>) => TSelected
42
- children: ((state: TSelected) => ReactNode) | ReactNode
43
- }) => ReturnType<FunctionComponent>
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
+ }
44
79
  /**
45
80
  * A React component that renders headers, cells, or footers with custom markup.
46
81
  * Use this utility component instead of manually calling flexRender.
@@ -92,11 +127,12 @@ export function useTable<
92
127
  TSelected
93
128
  >
94
129
 
95
- tableInstance.Subscribe = function SubscribeBound<TSelected>(
96
- props: Omit<SubscribeProps<TFeatures, TData, TSelected>, 'table'>,
97
- ) {
98
- return Subscribe({ ...props, table: tableInstance })
99
- }
130
+ tableInstance.Subscribe = ((props: any) => {
131
+ return Subscribe({
132
+ ...props,
133
+ table: tableInstance,
134
+ })
135
+ }) as ReactTable<TFeatures, TData, TSelected>['Subscribe']
100
136
 
101
137
  tableInstance.FlexRender = FlexRender
102
138