11import { shallow , useSelector } from '@tanstack/preact-store'
2- import type { Atom , ReadonlyAtom } from '@tanstack/preact-store'
32import type {
4- RowData ,
5- Table ,
6- TableFeatures ,
7- TableState ,
8- } from '@tanstack/table-core'
3+ Atom ,
4+ ReadonlyAtom ,
5+ ReadonlyStore ,
6+ Store ,
7+ } from '@tanstack/preact-store'
8+ import type { TableFeatures , TableState } from '@tanstack/table-core'
99import type { ComponentChildren } from 'preact'
1010
11+ export type SubscribeSource < TValue > =
12+ | Atom < TValue >
13+ | ReadonlyAtom < TValue >
14+ | Store < TValue >
15+ | ReadonlyStore < TValue >
16+
1117/**
1218 * Subscribe to `table.store` (full table state). The selector receives the full
1319 * {@link TableState}.
1420 */
1521export type SubscribePropsWithStore <
1622 TFeatures extends TableFeatures ,
17- TData extends RowData ,
1823 TSelected ,
1924> = {
20- table : Table < TFeatures , TData >
25+ source : SubscribeSource < TableState < TFeatures > >
2126 /**
2227 * Select from full table state. Re-renders when the selected value changes
2328 * (shallow compare).
@@ -34,111 +39,63 @@ export type SubscribePropsWithStore<
3439 * `table.optionsStore`). Omitting `selector` is equivalent to the identity
3540 * selector — children receive `TSourceValue`.
3641 */
37- export type SubscribePropsWithSourceIdentity <
38- TFeatures extends TableFeatures ,
39- TData extends RowData ,
40- TSourceValue ,
41- > = {
42- table : Table < TFeatures , TData >
43- source : Atom < TSourceValue > | ReadonlyAtom < TSourceValue >
42+ export type SubscribePropsWithSourceIdentity < TSourceValue > = {
43+ source : SubscribeSource < TSourceValue >
4444 selector ?: undefined
4545 children : ( ( state : TSourceValue ) => ComponentChildren ) | ComponentChildren
4646}
4747
4848/**
4949 * Subscribe to a projected value from a source (atom or store).
5050 */
51- export type SubscribePropsWithSourceWithSelector <
52- TFeatures extends TableFeatures ,
53- TData extends RowData ,
54- TSourceValue ,
55- TSelected ,
56- > = {
57- table : Table < TFeatures , TData >
58- source : Atom < TSourceValue > | ReadonlyAtom < TSourceValue >
51+ export type SubscribePropsWithSourceWithSelector < TSourceValue , TSelected > = {
52+ source : SubscribeSource < TSourceValue >
5953 selector : ( state : TSourceValue ) => TSelected
6054 children : ( ( state : TSelected ) => ComponentChildren ) | ComponentChildren
6155}
6256
6357/**
6458 * Subscribe to a single source — atom or store (identity or projected).
6559 */
66- export type SubscribePropsWithSource <
67- TFeatures extends TableFeatures ,
68- TData extends RowData ,
69- TSourceValue ,
70- TSelected = TSourceValue ,
71- > =
72- | SubscribePropsWithSourceIdentity < TFeatures , TData , TSourceValue >
73- | SubscribePropsWithSourceWithSelector <
74- TFeatures ,
75- TData ,
76- TSourceValue ,
77- TSelected
78- >
60+ export type SubscribePropsWithSource < TSourceValue , TSelected = TSourceValue > =
61+ | SubscribePropsWithSourceIdentity < TSourceValue >
62+ | SubscribePropsWithSourceWithSelector < TSourceValue , TSelected >
7963
8064export type SubscribeProps <
8165 TFeatures extends TableFeatures ,
82- TData extends RowData ,
8366 TSelected = unknown ,
8467 TSourceValue = unknown ,
8568> =
86- | SubscribePropsWithStore < TFeatures , TData , TSelected >
87- | SubscribePropsWithSourceIdentity < TFeatures , TData , TSourceValue >
88- | SubscribePropsWithSourceWithSelector <
89- TFeatures ,
90- TData ,
91- TSourceValue ,
92- TSelected
93- >
69+ | SubscribePropsWithStore < TFeatures , TSelected >
70+ | SubscribePropsWithSourceIdentity < TSourceValue >
71+ | SubscribePropsWithSourceWithSelector < TSourceValue , TSelected >
9472
9573/**
9674 * A Preact component that allows you to subscribe to the table state.
9775 *
9876 * For `table.Subscribe` from `useTable`, prefer that API — it uses overloads so
9977 * JSX contextual typing works. This standalone component uses a union `props` type.
10078 */
101- export function Subscribe <
102- TFeatures extends TableFeatures ,
103- TData extends RowData ,
104- TSourceValue ,
105- > (
106- props : SubscribePropsWithSourceIdentity < TFeatures , TData , TSourceValue > ,
79+ export function Subscribe < TSourceValue > (
80+ props : SubscribePropsWithSourceIdentity < TSourceValue > ,
10781) : ComponentChildren
108- export function Subscribe <
109- TFeatures extends TableFeatures ,
110- TData extends RowData ,
111- TSourceValue ,
112- TSelected ,
113- > (
114- props : SubscribePropsWithSourceWithSelector <
115- TFeatures ,
116- TData ,
117- TSourceValue ,
118- TSelected
119- > ,
82+ export function Subscribe < TSourceValue , TSelected > (
83+ props : SubscribePropsWithSourceWithSelector < TSourceValue , TSelected > ,
12084) : ComponentChildren
121- export function Subscribe <
122- TFeatures extends TableFeatures ,
123- TData extends RowData ,
124- TSelected ,
125- > (
126- props : SubscribePropsWithStore < TFeatures , TData , TSelected > ,
85+ export function Subscribe < TFeatures extends TableFeatures , TSelected > (
86+ props : SubscribePropsWithStore < TFeatures , TSelected > ,
12787) : ComponentChildren
12888export function Subscribe <
12989 TFeatures extends TableFeatures ,
130- TData extends RowData ,
13190 TSelected ,
13291 TSourceValue ,
13392> (
134- props : SubscribeProps < TFeatures , TData , TSelected , TSourceValue > ,
93+ props : SubscribeProps < TFeatures , TSelected , TSourceValue > ,
13594) : ComponentChildren {
136- const source = 'source' in props ? props . source : props . table . store
137- const selectFn =
138- 'source' in props ? ( props . selector ?? ( ( x : unknown ) => x ) ) : props . selector
95+ const selectFn = props . selector ?? ( ( x : unknown ) => x )
13996
14097 const selected = useSelector (
141- source as never ,
98+ props . source as never ,
14299 selectFn as Parameters < typeof useSelector > [ 1 ] ,
143100 {
144101 compare : shallow ,
0 commit comments