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

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,199 @@
1
+ 'use client'
2
+
3
+ import { shallow, useSelector } from '@tanstack/react-store'
4
+ import type { Atom, ReadonlyAtom } from '@tanstack/react-store'
5
+ import type {
6
+ RowData,
7
+ Table,
8
+ TableFeatures,
9
+ TableState,
10
+ } from '@tanstack/table-core'
11
+ import type { FunctionComponent, ReactNode } from 'react'
12
+
13
+ /**
14
+ * Subscribe to `table.store` (full table state). The selector receives the full
15
+ * {@link TableState}.
16
+ */
17
+ export type SubscribePropsWithStore<
18
+ TFeatures extends TableFeatures,
19
+ TData extends RowData,
20
+ TSelected,
21
+ > = {
22
+ table: Table<TFeatures, TData>
23
+ /**
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.
29
+ */
30
+ selector: (state: TableState<TFeatures>) => TSelected
31
+ children: ((state: TSelected) => ReactNode) | ReactNode
32
+ }
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
+
100
+ /**
101
+ * A React component that allows you to subscribe to the table state.
102
+ *
103
+ * This is useful for opting into state re-renders for specific parts of the table state.
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
+ *
108
+ * @example
109
+ * ```tsx
110
+ * // As a standalone component — full store
111
+ * <Subscribe table={table} selector={(state) => ({ rowSelection: state.rowSelection })}>
112
+ * {({ rowSelection }) => (
113
+ * <div>Selected rows: {Object.keys(rowSelection).length}</div>
114
+ * )}
115
+ * </Subscribe>
116
+ * ```
117
+ *
118
+ * @example
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
140
+ * // As table.Subscribe (table instance method)
141
+ * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
142
+ * {({ rowSelection }) => (
143
+ * <div>Selected rows: {Object.keys(rowSelection).length}</div>
144
+ * )}
145
+ * </table.Subscribe>
146
+ * ```
147
+ */
148
+ export function Subscribe<
149
+ TFeatures extends TableFeatures,
150
+ TData extends RowData,
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,
172
+ >(
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>,
182
+ ): ReturnType<FunctionComponent> {
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
195
+
196
+ return typeof props.children === 'function'
197
+ ? (props.children as (state: TSelected) => ReactNode)(selected)
198
+ : props.children
199
+ }