@tanstack/react-table 9.0.0-alpha.43 → 9.0.0-alpha.45

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,25 +1,30 @@
1
1
  'use client'
2
2
 
3
3
  import { shallow, useSelector } from '@tanstack/react-store'
4
- import type { Atom, ReadonlyAtom } from '@tanstack/react-store'
5
4
  import type {
6
- RowData,
7
- Table,
8
- TableFeatures,
9
- TableState,
10
- } from '@tanstack/table-core'
5
+ Atom,
6
+ ReadonlyAtom,
7
+ ReadonlyStore,
8
+ Store,
9
+ } from '@tanstack/react-store'
10
+ import type { TableFeatures, TableState } from '@tanstack/table-core'
11
11
  import type { FunctionComponent, ReactNode } from 'react'
12
12
 
13
+ export type SubscribeSource<TValue> =
14
+ | Atom<TValue>
15
+ | ReadonlyAtom<TValue>
16
+ | Store<TValue>
17
+ | ReadonlyStore<TValue>
18
+
13
19
  /**
14
20
  * Subscribe to `table.store` (full table state). The selector receives the full
15
21
  * {@link TableState}.
16
22
  */
17
23
  export type SubscribePropsWithStore<
18
24
  TFeatures extends TableFeatures,
19
- TData extends RowData,
20
25
  TSelected,
21
26
  > = {
22
- table: Table<TFeatures, TData>
27
+ source: SubscribeSource<TableState<TFeatures>>
23
28
  /**
24
29
  * Select from full table state. Re-renders when the selected value changes
25
30
  * (shallow compare).
@@ -36,13 +41,8 @@ export type SubscribePropsWithStore<
36
41
  * `table.optionsStore`). Omitting `selector` is equivalent to the identity
37
42
  * selector — children receive `TSourceValue`.
38
43
  */
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>
44
+ export type SubscribePropsWithSourceIdentity<TSourceValue> = {
45
+ source: SubscribeSource<TSourceValue>
46
46
  selector?: undefined
47
47
  children: ((state: TSourceValue) => ReactNode) | ReactNode
48
48
  }
@@ -51,14 +51,8 @@ export type SubscribePropsWithSourceIdentity<
51
51
  * Subscribe to a projected value from a source (atom or store). The selector
52
52
  * receives the source value; children receive the projected `TSelected`.
53
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>
54
+ export type SubscribePropsWithSourceWithSelector<TSourceValue, TSelected> = {
55
+ source: SubscribeSource<TSourceValue>
62
56
  selector: (state: TSourceValue) => TSelected
63
57
  children: ((state: TSelected) => ReactNode) | ReactNode
64
58
  }
@@ -68,34 +62,18 @@ export type SubscribePropsWithSourceWithSelector<
68
62
  * {@link SubscribePropsWithSourceIdentity} or {@link SubscribePropsWithSourceWithSelector}
69
63
  * for clearer inference when `selector` is omitted.
70
64
  */
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
- >
65
+ export type SubscribePropsWithSource<TSourceValue, TSelected = TSourceValue> =
66
+ | SubscribePropsWithSourceIdentity<TSourceValue>
67
+ | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>
84
68
 
85
69
  export type SubscribeProps<
86
70
  TFeatures extends TableFeatures,
87
- TData extends RowData,
88
71
  TSelected = unknown,
89
72
  TSourceValue = unknown,
90
73
  > =
91
- | SubscribePropsWithStore<TFeatures, TData, TSelected>
92
- | SubscribePropsWithSourceIdentity<TFeatures, TData, TSourceValue>
93
- | SubscribePropsWithSourceWithSelector<
94
- TFeatures,
95
- TData,
96
- TSourceValue,
97
- TSelected
98
- >
74
+ | SubscribePropsWithStore<TFeatures, TSelected>
75
+ | SubscribePropsWithSourceIdentity<TSourceValue>
76
+ | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>
99
77
 
100
78
  /**
101
79
  * A React component that allows you to subscribe to the table state.
@@ -108,7 +86,7 @@ export type SubscribeProps<
108
86
  * @example
109
87
  * ```tsx
110
88
  * // As a standalone component — full store
111
- * <Subscribe table={table} selector={(state) => ({ rowSelection: state.rowSelection })}>
89
+ * <Subscribe source={table.store} selector={(state) => ({ rowSelection: state.rowSelection })}>
112
90
  * {({ rowSelection }) => (
113
91
  * <div>Selected rows: {Object.keys(rowSelection).length}</div>
114
92
  * )}
@@ -118,7 +96,7 @@ export type SubscribeProps<
118
96
  * @example
119
97
  * ```tsx
120
98
  * // Entire source (atom or store) — no selector
121
- * <Subscribe table={table} source={table.atoms.rowSelection}>
99
+ * <Subscribe source={table.atoms.rowSelection}>
122
100
  * {(rowSelection) => <div>...</div>}
123
101
  * </Subscribe>
124
102
  * ```
@@ -127,7 +105,6 @@ export type SubscribeProps<
127
105
  * ```tsx
128
106
  * // Project source value (e.g. one row’s selection)
129
107
  * <Subscribe
130
- * table={table}
131
108
  * source={table.atoms.rowSelection}
132
109
  * selector={(rowSelection) => rowSelection?.[row.id]}
133
110
  * >
@@ -145,49 +122,26 @@ export type SubscribeProps<
145
122
  * </table.Subscribe>
146
123
  * ```
147
124
  */
148
- export function Subscribe<
149
- TFeatures extends TableFeatures,
150
- TData extends RowData,
151
- TSourceValue,
152
- >(
153
- props: SubscribePropsWithSourceIdentity<TFeatures, TData, TSourceValue>,
125
+ export function Subscribe<TSourceValue>(
126
+ props: SubscribePropsWithSourceIdentity<TSourceValue>,
154
127
  ): 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
- >,
128
+ export function Subscribe<TSourceValue, TSelected>(
129
+ props: SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>,
167
130
  ): ReturnType<FunctionComponent>
168
- export function Subscribe<
169
- TFeatures extends TableFeatures,
170
- TData extends RowData,
171
- TSelected,
172
- >(
173
- props: SubscribePropsWithStore<TFeatures, TData, TSelected>,
131
+ export function Subscribe<TFeatures extends TableFeatures, TSelected>(
132
+ props: SubscribePropsWithStore<TFeatures, TSelected>,
174
133
  ): ReturnType<FunctionComponent>
175
134
  export function Subscribe<
176
135
  TFeatures extends TableFeatures,
177
- TData extends RowData,
178
136
  TSelected,
179
137
  TSourceValue,
180
138
  >(
181
- props: SubscribeProps<TFeatures, TData, TSelected, TSourceValue>,
139
+ props: SubscribeProps<TFeatures, TSelected, TSourceValue>,
182
140
  ): 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
141
  const selected = useSelector(
188
142
  // Atom and store share the same selection protocol; union args need a widen for TS.
189
- source,
190
- selectFn as Parameters<typeof useSelector>[1],
143
+ props.source,
144
+ props.selector as Parameters<typeof useSelector>[1],
191
145
  {
192
146
  compare: shallow,
193
147
  },
@@ -800,7 +800,10 @@ export function createTableHook<
800
800
  *
801
801
  * TFeatures is already known from the createTableHook call; TData is inferred from the data prop.
802
802
  */
803
- function useAppTable<TData extends RowData, TSelected = {}>(
803
+ function useAppTable<
804
+ TData extends RowData,
805
+ TSelected = TableState<TFeatures>,
806
+ >(
804
807
  tableOptions: Omit<
805
808
  TableOptions<TFeatures, TData>,
806
809
  '_features' | '_rowModels'
package/src/useTable.ts CHANGED
@@ -6,9 +6,8 @@ import { shallow, useSelector } from '@tanstack/react-store'
6
6
  import { reactReactivity } from './reactivity'
7
7
  import { FlexRender } from './FlexRender'
8
8
  import { Subscribe } from './Subscribe'
9
- import type { Atom, ReadonlyAtom } from '@tanstack/react-store'
10
9
  import type { FlexRenderProps } from './FlexRender'
11
- import type { SubscribePropsWithStore } from './Subscribe'
10
+ import type { SubscribePropsWithStore, SubscribeSource } from './Subscribe'
12
11
  import type {
13
12
  CellData,
14
13
  RowData,
@@ -22,7 +21,7 @@ import type { FunctionComponent, ReactNode } from 'react'
22
21
  export type ReactTable<
23
22
  TFeatures extends TableFeatures,
24
23
  TData extends RowData,
25
- TSelected = {},
24
+ TSelected = TableState<TFeatures>,
26
25
  > = Table<TFeatures, TData> & {
27
26
  /**
28
27
  * A React HOC (Higher Order Component) that allows you to subscribe to the table state.
@@ -61,20 +60,17 @@ export type ReactTable<
61
60
  */
62
61
  Subscribe: {
63
62
  <TSourceValue>(props: {
64
- source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>
63
+ source: SubscribeSource<TSourceValue>
65
64
  selector?: undefined
66
65
  children: ((state: TSourceValue) => ReactNode) | ReactNode
67
66
  }): ReturnType<FunctionComponent>
68
67
  <TSourceValue, TSubSelected>(props: {
69
- source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>
68
+ source: SubscribeSource<TSourceValue>
70
69
  selector: (state: TSourceValue) => TSubSelected
71
70
  children: ((state: TSubSelected) => ReactNode) | ReactNode
72
71
  }): ReturnType<FunctionComponent>
73
72
  <TSubSelected>(
74
- props: Omit<
75
- SubscribePropsWithStore<TFeatures, TData, TSubSelected>,
76
- 'table'
77
- >,
73
+ props: Omit<SubscribePropsWithStore<TFeatures, TSubSelected>, 'source'>,
78
74
  ): ReturnType<FunctionComponent>
79
75
  }
80
76
  /**
@@ -112,11 +108,10 @@ export type ReactTable<
112
108
  export function useTable<
113
109
  TFeatures extends TableFeatures,
114
110
  TData extends RowData,
115
- TSelected = {},
111
+ TSelected = TableState<TFeatures>,
116
112
  >(
117
113
  tableOptions: TableOptions<TFeatures, TData>,
118
- selector: (state: TableState<TFeatures>) => TSelected = () =>
119
- ({}) as TSelected,
114
+ selector?: (state: TableState<TFeatures>) => TSelected,
120
115
  ): ReactTable<TFeatures, TData, TSelected> {
121
116
  const [table] = useState(() => {
122
117
  const tableInstance = constructTable({
@@ -128,9 +123,11 @@ export function useTable<
128
123
  }) as ReactTable<TFeatures, TData, TSelected>
129
124
 
130
125
  tableInstance.Subscribe = ((props: any) => {
126
+ const source = props.source ?? tableInstance.store
127
+
131
128
  return Subscribe({
132
129
  ...props,
133
- table: tableInstance,
130
+ source,
134
131
  })
135
132
  }) as ReactTable<TFeatures, TData, TSelected>['Subscribe']
136
133