@tanstack/react-table 9.0.0-alpha.43 → 9.0.0-alpha.44
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/dist/Subscribe.cjs +2 -1
- package/dist/Subscribe.cjs.map +1 -1
- package/dist/Subscribe.d.cts +17 -19
- package/dist/Subscribe.d.ts +17 -19
- package/dist/Subscribe.js +2 -1
- package/dist/Subscribe.js.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/useTable.cjs +2 -1
- package/dist/useTable.cjs.map +1 -1
- package/dist/useTable.d.cts +4 -5
- package/dist/useTable.d.ts +4 -5
- package/dist/useTable.js +2 -1
- package/dist/useTable.js.map +1 -1
- package/package.json +1 -1
- package/src/Subscribe.ts +34 -78
- package/src/useTable.ts +7 -9
package/dist/Subscribe.cjs
CHANGED
|
@@ -5,7 +5,8 @@ let _tanstack_react_store = require("@tanstack/react-store");
|
|
|
5
5
|
|
|
6
6
|
//#region src/Subscribe.ts
|
|
7
7
|
function Subscribe(props) {
|
|
8
|
-
const
|
|
8
|
+
const selectFn = props.selector ?? ((x) => x);
|
|
9
|
+
const selected = (0, _tanstack_react_store.useSelector)(props.source, selectFn, { compare: _tanstack_react_store.shallow });
|
|
9
10
|
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
10
11
|
}
|
|
11
12
|
|
package/dist/Subscribe.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Subscribe.cjs","names":["shallow"],"sources":["../src/Subscribe.ts"],"sourcesContent":["'use client'\n\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport type {
|
|
1
|
+
{"version":3,"file":"Subscribe.cjs","names":["shallow"],"sources":["../src/Subscribe.ts"],"sourcesContent":["'use client'\n\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport type {\n Atom,\n ReadonlyAtom,\n ReadonlyStore,\n Store,\n} from '@tanstack/react-store'\nimport type { TableFeatures, TableState } from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\n\nexport type SubscribeSource<TValue> =\n | Atom<TValue>\n | ReadonlyAtom<TValue>\n | Store<TValue>\n | ReadonlyStore<TValue>\n\n/**\n * Subscribe to `table.store` (full table state). The selector receives the full\n * {@link TableState}.\n */\nexport type SubscribePropsWithStore<\n TFeatures extends TableFeatures,\n TSelected,\n> = {\n source: SubscribeSource<TableState<TFeatures>>\n /**\n * Select from full table state. Re-renders when the selected value changes\n * (shallow compare).\n *\n * Required in store mode so you never accidentally subscribe to the whole\n * store without an explicit projection.\n */\n selector: (state: TableState<TFeatures>) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n}\n\n/**\n * Subscribe to the full value of a source (e.g. `table.atoms.rowSelection` or\n * `table.optionsStore`). Omitting `selector` is equivalent to the identity\n * selector — children receive `TSourceValue`.\n */\nexport type SubscribePropsWithSourceIdentity<TSourceValue> = {\n source: SubscribeSource<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ReactNode) | ReactNode\n}\n\n/**\n * Subscribe to a projected value from a source (atom or store). The selector\n * receives the source value; children receive the projected `TSelected`.\n */\nexport type SubscribePropsWithSourceWithSelector<TSourceValue, TSelected> = {\n source: SubscribeSource<TSourceValue>\n selector: (state: TSourceValue) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n}\n\n/**\n * Subscribe to a single source — atom or store (identity or projected). Prefer\n * {@link SubscribePropsWithSourceIdentity} or {@link SubscribePropsWithSourceWithSelector}\n * for clearer inference when `selector` is omitted.\n */\nexport type SubscribePropsWithSource<TSourceValue, TSelected = TSourceValue> =\n | SubscribePropsWithSourceIdentity<TSourceValue>\n | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>\n\nexport type SubscribeProps<\n TFeatures extends TableFeatures,\n TSelected = unknown,\n TSourceValue = unknown,\n> =\n | SubscribePropsWithStore<TFeatures, TSelected>\n | SubscribePropsWithSourceIdentity<TSourceValue>\n | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>\n\n/**\n * A React component that allows you to subscribe to the table state.\n *\n * This is useful for opting into state re-renders for specific parts of the table state.\n *\n * For `table.Subscribe` from `useTable`, prefer that API — it uses overloads so JSX\n * contextual typing works. This standalone component uses a union `props` type.\n *\n * @example\n * ```tsx\n * // As a standalone component — full store\n * <Subscribe source={table.store} selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => (\n * <div>Selected rows: {Object.keys(rowSelection).length}</div>\n * )}\n * </Subscribe>\n * ```\n *\n * @example\n * ```tsx\n * // Entire source (atom or store) — no selector\n * <Subscribe source={table.atoms.rowSelection}>\n * {(rowSelection) => <div>...</div>}\n * </Subscribe>\n * ```\n *\n * @example\n * ```tsx\n * // Project source value (e.g. one row’s selection)\n * <Subscribe\n * source={table.atoms.rowSelection}\n * selector={(rowSelection) => rowSelection?.[row.id]}\n * >\n * {(selected) => <tr data-selected={!!selected}>...</tr>}\n * </Subscribe>\n * ```\n *\n * @example\n * ```tsx\n * // As table.Subscribe (table instance method)\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => (\n * <div>Selected rows: {Object.keys(rowSelection).length}</div>\n * )}\n * </table.Subscribe>\n * ```\n */\nexport function Subscribe<TSourceValue>(\n props: SubscribePropsWithSourceIdentity<TSourceValue>,\n): ReturnType<FunctionComponent>\nexport function Subscribe<TSourceValue, TSelected>(\n props: SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>,\n): ReturnType<FunctionComponent>\nexport function Subscribe<TFeatures extends TableFeatures, TSelected>(\n props: SubscribePropsWithStore<TFeatures, TSelected>,\n): ReturnType<FunctionComponent>\nexport function Subscribe<\n TFeatures extends TableFeatures,\n TSelected,\n TSourceValue,\n>(\n props: SubscribeProps<TFeatures, TSelected, TSourceValue>,\n): ReturnType<FunctionComponent> {\n const selectFn = props.selector ?? ((x: unknown) => x)\n\n const selected = useSelector(\n // Atom and store share the same selection protocol; union args need a widen for TS.\n props.source,\n selectFn as Parameters<typeof useSelector>[1],\n {\n compare: shallow,\n },\n ) as TSelected\n\n return typeof props.children === 'function'\n ? (props.children as (state: TSelected) => ReactNode)(selected)\n : props.children\n}\n"],"mappings":";;;;;;AAqIA,SAAgB,UAKd,OAC+B;CAC/B,MAAM,WAAW,MAAM,cAAc,MAAe;CAEpD,MAAM,kDAEJ,MAAM,QACN,UACA,EACE,SAASA,+BACV,CACF;AAED,QAAO,OAAO,MAAM,aAAa,aAC5B,MAAM,SAA6C,SAAS,GAC7D,MAAM"}
|
package/dist/Subscribe.d.cts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { FunctionComponent, ReactNode } from "react";
|
|
2
|
-
import {
|
|
3
|
-
import { Atom, ReadonlyAtom } from "@tanstack/react-store";
|
|
2
|
+
import { TableFeatures, TableState } from "@tanstack/table-core";
|
|
3
|
+
import { Atom, ReadonlyAtom, ReadonlyStore, Store } from "@tanstack/react-store";
|
|
4
4
|
|
|
5
5
|
//#region src/Subscribe.d.ts
|
|
6
|
+
type SubscribeSource<TValue> = Atom<TValue> | ReadonlyAtom<TValue> | Store<TValue> | ReadonlyStore<TValue>;
|
|
6
7
|
/**
|
|
7
8
|
* Subscribe to `table.store` (full table state). The selector receives the full
|
|
8
9
|
* {@link TableState}.
|
|
9
10
|
*/
|
|
10
|
-
type SubscribePropsWithStore<TFeatures extends TableFeatures,
|
|
11
|
-
|
|
11
|
+
type SubscribePropsWithStore<TFeatures extends TableFeatures, TSelected> = {
|
|
12
|
+
source: SubscribeSource<TableState<TFeatures>>;
|
|
12
13
|
/**
|
|
13
14
|
* Select from full table state. Re-renders when the selected value changes
|
|
14
15
|
* (shallow compare).
|
|
@@ -24,9 +25,8 @@ type SubscribePropsWithStore<TFeatures extends TableFeatures, TData extends RowD
|
|
|
24
25
|
* `table.optionsStore`). Omitting `selector` is equivalent to the identity
|
|
25
26
|
* selector — children receive `TSourceValue`.
|
|
26
27
|
*/
|
|
27
|
-
type SubscribePropsWithSourceIdentity<
|
|
28
|
-
|
|
29
|
-
source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>;
|
|
28
|
+
type SubscribePropsWithSourceIdentity<TSourceValue> = {
|
|
29
|
+
source: SubscribeSource<TSourceValue>;
|
|
30
30
|
selector?: undefined;
|
|
31
31
|
children: ((state: TSourceValue) => ReactNode) | ReactNode;
|
|
32
32
|
};
|
|
@@ -34,9 +34,8 @@ type SubscribePropsWithSourceIdentity<TFeatures extends TableFeatures, TData ext
|
|
|
34
34
|
* Subscribe to a projected value from a source (atom or store). The selector
|
|
35
35
|
* receives the source value; children receive the projected `TSelected`.
|
|
36
36
|
*/
|
|
37
|
-
type SubscribePropsWithSourceWithSelector<
|
|
38
|
-
|
|
39
|
-
source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>;
|
|
37
|
+
type SubscribePropsWithSourceWithSelector<TSourceValue, TSelected> = {
|
|
38
|
+
source: SubscribeSource<TSourceValue>;
|
|
40
39
|
selector: (state: TSourceValue) => TSelected;
|
|
41
40
|
children: ((state: TSelected) => ReactNode) | ReactNode;
|
|
42
41
|
};
|
|
@@ -45,8 +44,8 @@ type SubscribePropsWithSourceWithSelector<TFeatures extends TableFeatures, TData
|
|
|
45
44
|
* {@link SubscribePropsWithSourceIdentity} or {@link SubscribePropsWithSourceWithSelector}
|
|
46
45
|
* for clearer inference when `selector` is omitted.
|
|
47
46
|
*/
|
|
48
|
-
type SubscribePropsWithSource<
|
|
49
|
-
type SubscribeProps<TFeatures extends TableFeatures,
|
|
47
|
+
type SubscribePropsWithSource<TSourceValue, TSelected = TSourceValue> = SubscribePropsWithSourceIdentity<TSourceValue> | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>;
|
|
48
|
+
type SubscribeProps<TFeatures extends TableFeatures, TSelected = unknown, TSourceValue = unknown> = SubscribePropsWithStore<TFeatures, TSelected> | SubscribePropsWithSourceIdentity<TSourceValue> | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>;
|
|
50
49
|
/**
|
|
51
50
|
* A React component that allows you to subscribe to the table state.
|
|
52
51
|
*
|
|
@@ -58,7 +57,7 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
58
57
|
* @example
|
|
59
58
|
* ```tsx
|
|
60
59
|
* // As a standalone component — full store
|
|
61
|
-
* <Subscribe
|
|
60
|
+
* <Subscribe source={table.store} selector={(state) => ({ rowSelection: state.rowSelection })}>
|
|
62
61
|
* {({ rowSelection }) => (
|
|
63
62
|
* <div>Selected rows: {Object.keys(rowSelection).length}</div>
|
|
64
63
|
* )}
|
|
@@ -68,7 +67,7 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
68
67
|
* @example
|
|
69
68
|
* ```tsx
|
|
70
69
|
* // Entire source (atom or store) — no selector
|
|
71
|
-
* <Subscribe
|
|
70
|
+
* <Subscribe source={table.atoms.rowSelection}>
|
|
72
71
|
* {(rowSelection) => <div>...</div>}
|
|
73
72
|
* </Subscribe>
|
|
74
73
|
* ```
|
|
@@ -77,7 +76,6 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
77
76
|
* ```tsx
|
|
78
77
|
* // Project source value (e.g. one row’s selection)
|
|
79
78
|
* <Subscribe
|
|
80
|
-
* table={table}
|
|
81
79
|
* source={table.atoms.rowSelection}
|
|
82
80
|
* selector={(rowSelection) => rowSelection?.[row.id]}
|
|
83
81
|
* >
|
|
@@ -95,9 +93,9 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
95
93
|
* </table.Subscribe>
|
|
96
94
|
* ```
|
|
97
95
|
*/
|
|
98
|
-
declare function Subscribe<
|
|
99
|
-
declare function Subscribe<
|
|
100
|
-
declare function Subscribe<TFeatures extends TableFeatures,
|
|
96
|
+
declare function Subscribe<TSourceValue>(props: SubscribePropsWithSourceIdentity<TSourceValue>): ReturnType<FunctionComponent>;
|
|
97
|
+
declare function Subscribe<TSourceValue, TSelected>(props: SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>): ReturnType<FunctionComponent>;
|
|
98
|
+
declare function Subscribe<TFeatures extends TableFeatures, TSelected>(props: SubscribePropsWithStore<TFeatures, TSelected>): ReturnType<FunctionComponent>;
|
|
101
99
|
//#endregion
|
|
102
|
-
export { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore };
|
|
100
|
+
export { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, SubscribeSource };
|
|
103
101
|
//# sourceMappingURL=Subscribe.d.cts.map
|
package/dist/Subscribe.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TableFeatures, TableState } from "@tanstack/table-core";
|
|
2
2
|
import { FunctionComponent, ReactNode } from "react";
|
|
3
|
-
import { Atom, ReadonlyAtom } from "@tanstack/react-store";
|
|
3
|
+
import { Atom, ReadonlyAtom, ReadonlyStore, Store } from "@tanstack/react-store";
|
|
4
4
|
|
|
5
5
|
//#region src/Subscribe.d.ts
|
|
6
|
+
type SubscribeSource<TValue> = Atom<TValue> | ReadonlyAtom<TValue> | Store<TValue> | ReadonlyStore<TValue>;
|
|
6
7
|
/**
|
|
7
8
|
* Subscribe to `table.store` (full table state). The selector receives the full
|
|
8
9
|
* {@link TableState}.
|
|
9
10
|
*/
|
|
10
|
-
type SubscribePropsWithStore<TFeatures extends TableFeatures,
|
|
11
|
-
|
|
11
|
+
type SubscribePropsWithStore<TFeatures extends TableFeatures, TSelected> = {
|
|
12
|
+
source: SubscribeSource<TableState<TFeatures>>;
|
|
12
13
|
/**
|
|
13
14
|
* Select from full table state. Re-renders when the selected value changes
|
|
14
15
|
* (shallow compare).
|
|
@@ -24,9 +25,8 @@ type SubscribePropsWithStore<TFeatures extends TableFeatures, TData extends RowD
|
|
|
24
25
|
* `table.optionsStore`). Omitting `selector` is equivalent to the identity
|
|
25
26
|
* selector — children receive `TSourceValue`.
|
|
26
27
|
*/
|
|
27
|
-
type SubscribePropsWithSourceIdentity<
|
|
28
|
-
|
|
29
|
-
source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>;
|
|
28
|
+
type SubscribePropsWithSourceIdentity<TSourceValue> = {
|
|
29
|
+
source: SubscribeSource<TSourceValue>;
|
|
30
30
|
selector?: undefined;
|
|
31
31
|
children: ((state: TSourceValue) => ReactNode) | ReactNode;
|
|
32
32
|
};
|
|
@@ -34,9 +34,8 @@ type SubscribePropsWithSourceIdentity<TFeatures extends TableFeatures, TData ext
|
|
|
34
34
|
* Subscribe to a projected value from a source (atom or store). The selector
|
|
35
35
|
* receives the source value; children receive the projected `TSelected`.
|
|
36
36
|
*/
|
|
37
|
-
type SubscribePropsWithSourceWithSelector<
|
|
38
|
-
|
|
39
|
-
source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>;
|
|
37
|
+
type SubscribePropsWithSourceWithSelector<TSourceValue, TSelected> = {
|
|
38
|
+
source: SubscribeSource<TSourceValue>;
|
|
40
39
|
selector: (state: TSourceValue) => TSelected;
|
|
41
40
|
children: ((state: TSelected) => ReactNode) | ReactNode;
|
|
42
41
|
};
|
|
@@ -45,8 +44,8 @@ type SubscribePropsWithSourceWithSelector<TFeatures extends TableFeatures, TData
|
|
|
45
44
|
* {@link SubscribePropsWithSourceIdentity} or {@link SubscribePropsWithSourceWithSelector}
|
|
46
45
|
* for clearer inference when `selector` is omitted.
|
|
47
46
|
*/
|
|
48
|
-
type SubscribePropsWithSource<
|
|
49
|
-
type SubscribeProps<TFeatures extends TableFeatures,
|
|
47
|
+
type SubscribePropsWithSource<TSourceValue, TSelected = TSourceValue> = SubscribePropsWithSourceIdentity<TSourceValue> | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>;
|
|
48
|
+
type SubscribeProps<TFeatures extends TableFeatures, TSelected = unknown, TSourceValue = unknown> = SubscribePropsWithStore<TFeatures, TSelected> | SubscribePropsWithSourceIdentity<TSourceValue> | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>;
|
|
50
49
|
/**
|
|
51
50
|
* A React component that allows you to subscribe to the table state.
|
|
52
51
|
*
|
|
@@ -58,7 +57,7 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
58
57
|
* @example
|
|
59
58
|
* ```tsx
|
|
60
59
|
* // As a standalone component — full store
|
|
61
|
-
* <Subscribe
|
|
60
|
+
* <Subscribe source={table.store} selector={(state) => ({ rowSelection: state.rowSelection })}>
|
|
62
61
|
* {({ rowSelection }) => (
|
|
63
62
|
* <div>Selected rows: {Object.keys(rowSelection).length}</div>
|
|
64
63
|
* )}
|
|
@@ -68,7 +67,7 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
68
67
|
* @example
|
|
69
68
|
* ```tsx
|
|
70
69
|
* // Entire source (atom or store) — no selector
|
|
71
|
-
* <Subscribe
|
|
70
|
+
* <Subscribe source={table.atoms.rowSelection}>
|
|
72
71
|
* {(rowSelection) => <div>...</div>}
|
|
73
72
|
* </Subscribe>
|
|
74
73
|
* ```
|
|
@@ -77,7 +76,6 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
77
76
|
* ```tsx
|
|
78
77
|
* // Project source value (e.g. one row’s selection)
|
|
79
78
|
* <Subscribe
|
|
80
|
-
* table={table}
|
|
81
79
|
* source={table.atoms.rowSelection}
|
|
82
80
|
* selector={(rowSelection) => rowSelection?.[row.id]}
|
|
83
81
|
* >
|
|
@@ -95,9 +93,9 @@ type SubscribeProps<TFeatures extends TableFeatures, TData extends RowData, TSel
|
|
|
95
93
|
* </table.Subscribe>
|
|
96
94
|
* ```
|
|
97
95
|
*/
|
|
98
|
-
declare function Subscribe<
|
|
99
|
-
declare function Subscribe<
|
|
100
|
-
declare function Subscribe<TFeatures extends TableFeatures,
|
|
96
|
+
declare function Subscribe<TSourceValue>(props: SubscribePropsWithSourceIdentity<TSourceValue>): ReturnType<FunctionComponent>;
|
|
97
|
+
declare function Subscribe<TSourceValue, TSelected>(props: SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>): ReturnType<FunctionComponent>;
|
|
98
|
+
declare function Subscribe<TFeatures extends TableFeatures, TSelected>(props: SubscribePropsWithStore<TFeatures, TSelected>): ReturnType<FunctionComponent>;
|
|
101
99
|
//#endregion
|
|
102
|
-
export { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore };
|
|
100
|
+
export { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, SubscribeSource };
|
|
103
101
|
//# sourceMappingURL=Subscribe.d.ts.map
|
package/dist/Subscribe.js
CHANGED
|
@@ -4,7 +4,8 @@ import { shallow, useSelector } from "@tanstack/react-store";
|
|
|
4
4
|
|
|
5
5
|
//#region src/Subscribe.ts
|
|
6
6
|
function Subscribe(props) {
|
|
7
|
-
const
|
|
7
|
+
const selectFn = props.selector ?? ((x) => x);
|
|
8
|
+
const selected = useSelector(props.source, selectFn, { compare: shallow });
|
|
8
9
|
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
9
10
|
}
|
|
10
11
|
|
package/dist/Subscribe.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Subscribe.js","names":[],"sources":["../src/Subscribe.ts"],"sourcesContent":["'use client'\n\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport type {
|
|
1
|
+
{"version":3,"file":"Subscribe.js","names":[],"sources":["../src/Subscribe.ts"],"sourcesContent":["'use client'\n\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport type {\n Atom,\n ReadonlyAtom,\n ReadonlyStore,\n Store,\n} from '@tanstack/react-store'\nimport type { TableFeatures, TableState } from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\n\nexport type SubscribeSource<TValue> =\n | Atom<TValue>\n | ReadonlyAtom<TValue>\n | Store<TValue>\n | ReadonlyStore<TValue>\n\n/**\n * Subscribe to `table.store` (full table state). The selector receives the full\n * {@link TableState}.\n */\nexport type SubscribePropsWithStore<\n TFeatures extends TableFeatures,\n TSelected,\n> = {\n source: SubscribeSource<TableState<TFeatures>>\n /**\n * Select from full table state. Re-renders when the selected value changes\n * (shallow compare).\n *\n * Required in store mode so you never accidentally subscribe to the whole\n * store without an explicit projection.\n */\n selector: (state: TableState<TFeatures>) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n}\n\n/**\n * Subscribe to the full value of a source (e.g. `table.atoms.rowSelection` or\n * `table.optionsStore`). Omitting `selector` is equivalent to the identity\n * selector — children receive `TSourceValue`.\n */\nexport type SubscribePropsWithSourceIdentity<TSourceValue> = {\n source: SubscribeSource<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ReactNode) | ReactNode\n}\n\n/**\n * Subscribe to a projected value from a source (atom or store). The selector\n * receives the source value; children receive the projected `TSelected`.\n */\nexport type SubscribePropsWithSourceWithSelector<TSourceValue, TSelected> = {\n source: SubscribeSource<TSourceValue>\n selector: (state: TSourceValue) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n}\n\n/**\n * Subscribe to a single source — atom or store (identity or projected). Prefer\n * {@link SubscribePropsWithSourceIdentity} or {@link SubscribePropsWithSourceWithSelector}\n * for clearer inference when `selector` is omitted.\n */\nexport type SubscribePropsWithSource<TSourceValue, TSelected = TSourceValue> =\n | SubscribePropsWithSourceIdentity<TSourceValue>\n | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>\n\nexport type SubscribeProps<\n TFeatures extends TableFeatures,\n TSelected = unknown,\n TSourceValue = unknown,\n> =\n | SubscribePropsWithStore<TFeatures, TSelected>\n | SubscribePropsWithSourceIdentity<TSourceValue>\n | SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>\n\n/**\n * A React component that allows you to subscribe to the table state.\n *\n * This is useful for opting into state re-renders for specific parts of the table state.\n *\n * For `table.Subscribe` from `useTable`, prefer that API — it uses overloads so JSX\n * contextual typing works. This standalone component uses a union `props` type.\n *\n * @example\n * ```tsx\n * // As a standalone component — full store\n * <Subscribe source={table.store} selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => (\n * <div>Selected rows: {Object.keys(rowSelection).length}</div>\n * )}\n * </Subscribe>\n * ```\n *\n * @example\n * ```tsx\n * // Entire source (atom or store) — no selector\n * <Subscribe source={table.atoms.rowSelection}>\n * {(rowSelection) => <div>...</div>}\n * </Subscribe>\n * ```\n *\n * @example\n * ```tsx\n * // Project source value (e.g. one row’s selection)\n * <Subscribe\n * source={table.atoms.rowSelection}\n * selector={(rowSelection) => rowSelection?.[row.id]}\n * >\n * {(selected) => <tr data-selected={!!selected}>...</tr>}\n * </Subscribe>\n * ```\n *\n * @example\n * ```tsx\n * // As table.Subscribe (table instance method)\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => (\n * <div>Selected rows: {Object.keys(rowSelection).length}</div>\n * )}\n * </table.Subscribe>\n * ```\n */\nexport function Subscribe<TSourceValue>(\n props: SubscribePropsWithSourceIdentity<TSourceValue>,\n): ReturnType<FunctionComponent>\nexport function Subscribe<TSourceValue, TSelected>(\n props: SubscribePropsWithSourceWithSelector<TSourceValue, TSelected>,\n): ReturnType<FunctionComponent>\nexport function Subscribe<TFeatures extends TableFeatures, TSelected>(\n props: SubscribePropsWithStore<TFeatures, TSelected>,\n): ReturnType<FunctionComponent>\nexport function Subscribe<\n TFeatures extends TableFeatures,\n TSelected,\n TSourceValue,\n>(\n props: SubscribeProps<TFeatures, TSelected, TSourceValue>,\n): ReturnType<FunctionComponent> {\n const selectFn = props.selector ?? ((x: unknown) => x)\n\n const selected = useSelector(\n // Atom and store share the same selection protocol; union args need a widen for TS.\n props.source,\n selectFn as Parameters<typeof useSelector>[1],\n {\n compare: shallow,\n },\n ) as TSelected\n\n return typeof props.children === 'function'\n ? (props.children as (state: TSelected) => ReactNode)(selected)\n : props.children\n}\n"],"mappings":";;;;;AAqIA,SAAgB,UAKd,OAC+B;CAC/B,MAAM,WAAW,MAAM,cAAc,MAAe;CAEpD,MAAM,WAAW,YAEf,MAAM,QACN,UACA,EACE,SAAS,SACV,CACF;AAED,QAAO,OAAO,MAAM,aAAa,aAC5B,MAAM,SAA6C,SAAS,GAC7D,MAAM"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FlexRender, FlexRenderProps, Renderable, flexRender } from "./FlexRender.cjs";
|
|
2
|
-
import { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore } from "./Subscribe.cjs";
|
|
2
|
+
import { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, SubscribeSource } from "./Subscribe.cjs";
|
|
3
3
|
import { ReactTable, useTable } from "./useTable.cjs";
|
|
4
4
|
import { AppCellComponent, AppCellContext, AppCellPropsWithSelector, AppCellPropsWithoutSelector, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderComponent, AppHeaderContext, AppHeaderPropsWithSelector, AppHeaderPropsWithoutSelector, AppReactTable, AppTableComponent, AppTablePropsWithSelector, AppTablePropsWithoutSelector, CreateTableHookOptions, createTableHook } from "./createTableHook.cjs";
|
|
5
5
|
export * from "@tanstack/table-core";
|
|
6
|
-
export { AppCellComponent, AppCellContext, AppCellPropsWithSelector, AppCellPropsWithoutSelector, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderComponent, AppHeaderContext, AppHeaderPropsWithSelector, AppHeaderPropsWithoutSelector, AppReactTable, AppTableComponent, AppTablePropsWithSelector, AppTablePropsWithoutSelector, CreateTableHookOptions, FlexRender, FlexRenderProps, ReactTable, Renderable, Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, createTableHook, flexRender, useTable };
|
|
6
|
+
export { AppCellComponent, AppCellContext, AppCellPropsWithSelector, AppCellPropsWithoutSelector, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderComponent, AppHeaderContext, AppHeaderPropsWithSelector, AppHeaderPropsWithoutSelector, AppReactTable, AppTableComponent, AppTablePropsWithSelector, AppTablePropsWithoutSelector, CreateTableHookOptions, FlexRender, FlexRenderProps, ReactTable, Renderable, Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, SubscribeSource, createTableHook, flexRender, useTable };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FlexRender, FlexRenderProps, Renderable, flexRender } from "./FlexRender.js";
|
|
2
|
-
import { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore } from "./Subscribe.js";
|
|
2
|
+
import { Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, SubscribeSource } from "./Subscribe.js";
|
|
3
3
|
import { ReactTable, useTable } from "./useTable.js";
|
|
4
4
|
import { AppCellComponent, AppCellContext, AppCellPropsWithSelector, AppCellPropsWithoutSelector, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderComponent, AppHeaderContext, AppHeaderPropsWithSelector, AppHeaderPropsWithoutSelector, AppReactTable, AppTableComponent, AppTablePropsWithSelector, AppTablePropsWithoutSelector, CreateTableHookOptions, createTableHook } from "./createTableHook.js";
|
|
5
5
|
export * from "@tanstack/table-core";
|
|
6
|
-
export { AppCellComponent, AppCellContext, AppCellPropsWithSelector, AppCellPropsWithoutSelector, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderComponent, AppHeaderContext, AppHeaderPropsWithSelector, AppHeaderPropsWithoutSelector, AppReactTable, AppTableComponent, AppTablePropsWithSelector, AppTablePropsWithoutSelector, CreateTableHookOptions, FlexRender, FlexRenderProps, ReactTable, Renderable, Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, createTableHook, flexRender, useTable };
|
|
6
|
+
export { AppCellComponent, AppCellContext, AppCellPropsWithSelector, AppCellPropsWithoutSelector, AppColumnDefBase, AppColumnDefTemplate, AppColumnHelper, AppDisplayColumnDef, AppGroupColumnDef, AppHeaderComponent, AppHeaderContext, AppHeaderPropsWithSelector, AppHeaderPropsWithoutSelector, AppReactTable, AppTableComponent, AppTablePropsWithSelector, AppTablePropsWithoutSelector, CreateTableHookOptions, FlexRender, FlexRenderProps, ReactTable, Renderable, Subscribe, SubscribeProps, SubscribePropsWithSource, SubscribePropsWithSourceIdentity, SubscribePropsWithSourceWithSelector, SubscribePropsWithStore, SubscribeSource, createTableHook, flexRender, useTable };
|
package/dist/useTable.cjs
CHANGED
|
@@ -19,9 +19,10 @@ function useTable(tableOptions, selector = () => ({})) {
|
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
tableInstance.Subscribe = ((props) => {
|
|
22
|
+
const source = props.source ?? tableInstance.store;
|
|
22
23
|
return require_Subscribe.Subscribe({
|
|
23
24
|
...props,
|
|
24
|
-
|
|
25
|
+
source
|
|
25
26
|
});
|
|
26
27
|
});
|
|
27
28
|
tableInstance.FlexRender = require_FlexRender.FlexRender;
|
package/dist/useTable.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTable.cjs","names":["reactReactivity","Subscribe","FlexRender","shallow"],"sources":["../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport { reactReactivity } from './reactivity'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {
|
|
1
|
+
{"version":3,"file":"useTable.cjs","names":["reactReactivity","Subscribe","FlexRender","shallow"],"sources":["../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport { reactReactivity } from './reactivity'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribePropsWithStore, SubscribeSource } from './Subscribe'\nimport type {\n CellData,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\n\nexport type ReactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A React HOC (Higher Order Component) that allows you to subscribe to the table state.\n *\n * This is useful for opting into state re-renders for specific parts of the table state.\n *\n * Pass `source` to subscribe to a single atom or store (e.g. `table.atoms.rowSelection`\n * or `table.optionsStore`) instead of the full `table.store`.\n *\n * @example\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => (\n * <tr key={row.id}>...</tr>\n * )}\n * </table.Subscribe>\n *\n * @example\n * <table.Subscribe source={table.atoms.rowSelection}>\n * {(rowSelection) => <div>...</div>}\n * </table.Subscribe>\n *\n * @example\n * <table.Subscribe source={table.atoms.rowSelection} selector={(s) => s?.[row.id]}>\n * {() => <tr key={row.id}>...</tr>}\n * </table.Subscribe>\n */\n /**\n * Overloads (not a single union) so `selector` callbacks get correct contextual\n * types in JSX; a union of two `selector` signatures degrades to implicit `any`.\n *\n * Source **without** `selector` is a separate overload so children receive `TSourceValue`\n * (identity projection). If `selector` were optional on one overload, `TSubSelected`\n * would default to `unknown` instead of inferring from the source.\n *\n * The **source** overloads are listed first so `TSourceValue` is inferred from `source`.\n */\n Subscribe: {\n <TSourceValue>(props: {\n source: SubscribeSource<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ReactNode) | ReactNode\n }): ReturnType<FunctionComponent>\n <TSourceValue, TSubSelected>(props: {\n source: SubscribeSource<TSourceValue>\n selector: (state: TSourceValue) => TSubSelected\n children: ((state: TSubSelected) => ReactNode) | ReactNode\n }): ReturnType<FunctionComponent>\n <TSubSelected>(\n props: Omit<SubscribePropsWithStore<TFeatures, TSubSelected>, 'source'>,\n ): ReturnType<FunctionComponent>\n }\n /**\n * A React component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n *\n * @example\n * ```tsx\n * <table.FlexRender cell={cell} />\n * <table.FlexRender header={header} />\n * <table.FlexRender footer={footer} />\n * ```\n *\n * This replaces calling `flexRender` directly like this:\n * ```tsx\n * flexRender(cell.column.columnDef.cell, cell.getContext())\n * flexRender(header.column.columnDef.header, header.getContext())\n * flexRender(footer.column.columnDef.footer, footer.getContext())\n * ```\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ReactNode\n /**\n * 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`.\n *\n * @example\n * const table = useTable(options, (state) => ({ globalFilter: state.globalFilter })) // only globalFilter is part of the selected state\n *\n * console.log(table.state.globalFilter)\n */\n readonly state: Readonly<TSelected>\n}\n\nexport function useTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n>(\n tableOptions: TableOptions<TFeatures, TData>,\n selector: (state: TableState<TFeatures>) => TSelected = () =>\n ({}) as TSelected,\n): ReactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable({\n ...tableOptions,\n _features: {\n coreReativityFeature: reactReactivity(),\n ...tableOptions._features,\n },\n }) as ReactTable<TFeatures, TData, TSelected>\n\n tableInstance.Subscribe = ((props: any) => {\n const source = props.source ?? tableInstance.store\n\n return Subscribe({\n ...props,\n source,\n })\n }) as ReactTable<TFeatures, TData, TSelected>['Subscribe']\n\n tableInstance.FlexRender = FlexRender\n\n return tableInstance\n })\n\n // sync options on every render\n table.setOptions((prev) => ({\n ...prev,\n ...tableOptions,\n }))\n\n const state = useSelector(table.store, selector, { compare: shallow })\n\n // we know this is not the most efficient way to return the table,\n // but it is required for the react compiler to work\n return useMemo(\n () => ({\n ...table,\n options: tableOptions,\n state,\n }),\n [table, tableOptions, state],\n )\n}\n"],"mappings":";;;;;;;;;;;AA2GA,SAAgB,SAKd,cACA,kBACG,EAAE,GACoC;CACzC,MAAM,CAAC,mCAAwB;EAC7B,MAAM,yDAA+B;GACnC,GAAG;GACH,WAAW;IACT,sBAAsBA,oCAAiB;IACvC,GAAG,aAAa;IACjB;GACF,CAAC;AAEF,gBAAc,cAAc,UAAe;GACzC,MAAM,SAAS,MAAM,UAAU,cAAc;AAE7C,UAAOC,4BAAU;IACf,GAAG;IACH;IACD,CAAC;;AAGJ,gBAAc,aAAaC;AAE3B,SAAO;GACP;AAGF,OAAM,YAAY,UAAU;EAC1B,GAAG;EACH,GAAG;EACJ,EAAE;CAEH,MAAM,+CAAoB,MAAM,OAAO,UAAU,EAAE,SAASC,+BAAS,CAAC;AAItE,kCACS;EACL,GAAG;EACH,SAAS;EACT;EACD,GACD;EAAC;EAAO;EAAc;EAAM,CAC7B"}
|
package/dist/useTable.d.cts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { FlexRenderProps } from "./FlexRender.cjs";
|
|
2
|
-
import { SubscribePropsWithStore } from "./Subscribe.cjs";
|
|
2
|
+
import { SubscribePropsWithStore, SubscribeSource } from "./Subscribe.cjs";
|
|
3
3
|
import { FunctionComponent, ReactNode } from "react";
|
|
4
4
|
import { CellData, RowData, Table, TableFeatures, TableOptions, TableState } from "@tanstack/table-core";
|
|
5
|
-
import { Atom, ReadonlyAtom } from "@tanstack/react-store";
|
|
6
5
|
|
|
7
6
|
//#region src/useTable.d.ts
|
|
8
7
|
type ReactTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}> = Table<TFeatures, TData> & {
|
|
@@ -43,16 +42,16 @@ type ReactTable<TFeatures extends TableFeatures, TData extends RowData, TSelecte
|
|
|
43
42
|
*/
|
|
44
43
|
Subscribe: {
|
|
45
44
|
<TSourceValue>(props: {
|
|
46
|
-
source:
|
|
45
|
+
source: SubscribeSource<TSourceValue>;
|
|
47
46
|
selector?: undefined;
|
|
48
47
|
children: ((state: TSourceValue) => ReactNode) | ReactNode;
|
|
49
48
|
}): ReturnType<FunctionComponent>;
|
|
50
49
|
<TSourceValue, TSubSelected>(props: {
|
|
51
|
-
source:
|
|
50
|
+
source: SubscribeSource<TSourceValue>;
|
|
52
51
|
selector: (state: TSourceValue) => TSubSelected;
|
|
53
52
|
children: ((state: TSubSelected) => ReactNode) | ReactNode;
|
|
54
53
|
}): ReturnType<FunctionComponent>;
|
|
55
|
-
<TSubSelected>(props: Omit<SubscribePropsWithStore<TFeatures,
|
|
54
|
+
<TSubSelected>(props: Omit<SubscribePropsWithStore<TFeatures, TSubSelected>, 'source'>): ReturnType<FunctionComponent>;
|
|
56
55
|
};
|
|
57
56
|
/**
|
|
58
57
|
* A React component that renders headers, cells, or footers with custom markup.
|
package/dist/useTable.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { FlexRenderProps } from "./FlexRender.js";
|
|
2
|
-
import { SubscribePropsWithStore } from "./Subscribe.js";
|
|
2
|
+
import { SubscribePropsWithStore, SubscribeSource } from "./Subscribe.js";
|
|
3
3
|
import { CellData, RowData, Table, TableFeatures, TableOptions, TableState } from "@tanstack/table-core";
|
|
4
4
|
import { FunctionComponent, ReactNode } from "react";
|
|
5
|
-
import { Atom, ReadonlyAtom } from "@tanstack/react-store";
|
|
6
5
|
|
|
7
6
|
//#region src/useTable.d.ts
|
|
8
7
|
type ReactTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}> = Table<TFeatures, TData> & {
|
|
@@ -43,16 +42,16 @@ type ReactTable<TFeatures extends TableFeatures, TData extends RowData, TSelecte
|
|
|
43
42
|
*/
|
|
44
43
|
Subscribe: {
|
|
45
44
|
<TSourceValue>(props: {
|
|
46
|
-
source:
|
|
45
|
+
source: SubscribeSource<TSourceValue>;
|
|
47
46
|
selector?: undefined;
|
|
48
47
|
children: ((state: TSourceValue) => ReactNode) | ReactNode;
|
|
49
48
|
}): ReturnType<FunctionComponent>;
|
|
50
49
|
<TSourceValue, TSubSelected>(props: {
|
|
51
|
-
source:
|
|
50
|
+
source: SubscribeSource<TSourceValue>;
|
|
52
51
|
selector: (state: TSourceValue) => TSubSelected;
|
|
53
52
|
children: ((state: TSubSelected) => ReactNode) | ReactNode;
|
|
54
53
|
}): ReturnType<FunctionComponent>;
|
|
55
|
-
<TSubSelected>(props: Omit<SubscribePropsWithStore<TFeatures,
|
|
54
|
+
<TSubSelected>(props: Omit<SubscribePropsWithStore<TFeatures, TSubSelected>, 'source'>): ReturnType<FunctionComponent>;
|
|
56
55
|
};
|
|
57
56
|
/**
|
|
58
57
|
* A React component that renders headers, cells, or footers with custom markup.
|
package/dist/useTable.js
CHANGED
|
@@ -18,9 +18,10 @@ function useTable(tableOptions, selector = () => ({})) {
|
|
|
18
18
|
}
|
|
19
19
|
});
|
|
20
20
|
tableInstance.Subscribe = ((props) => {
|
|
21
|
+
const source = props.source ?? tableInstance.store;
|
|
21
22
|
return Subscribe({
|
|
22
23
|
...props,
|
|
23
|
-
|
|
24
|
+
source
|
|
24
25
|
});
|
|
25
26
|
});
|
|
26
27
|
tableInstance.FlexRender = FlexRender;
|
package/dist/useTable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTable.js","names":[],"sources":["../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport { reactReactivity } from './reactivity'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {
|
|
1
|
+
{"version":3,"file":"useTable.js","names":[],"sources":["../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/react-store'\nimport { reactReactivity } from './reactivity'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribePropsWithStore, SubscribeSource } from './Subscribe'\nimport type {\n CellData,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\n\nexport type ReactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A React HOC (Higher Order Component) that allows you to subscribe to the table state.\n *\n * This is useful for opting into state re-renders for specific parts of the table state.\n *\n * Pass `source` to subscribe to a single atom or store (e.g. `table.atoms.rowSelection`\n * or `table.optionsStore`) instead of the full `table.store`.\n *\n * @example\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => (\n * <tr key={row.id}>...</tr>\n * )}\n * </table.Subscribe>\n *\n * @example\n * <table.Subscribe source={table.atoms.rowSelection}>\n * {(rowSelection) => <div>...</div>}\n * </table.Subscribe>\n *\n * @example\n * <table.Subscribe source={table.atoms.rowSelection} selector={(s) => s?.[row.id]}>\n * {() => <tr key={row.id}>...</tr>}\n * </table.Subscribe>\n */\n /**\n * Overloads (not a single union) so `selector` callbacks get correct contextual\n * types in JSX; a union of two `selector` signatures degrades to implicit `any`.\n *\n * Source **without** `selector` is a separate overload so children receive `TSourceValue`\n * (identity projection). If `selector` were optional on one overload, `TSubSelected`\n * would default to `unknown` instead of inferring from the source.\n *\n * The **source** overloads are listed first so `TSourceValue` is inferred from `source`.\n */\n Subscribe: {\n <TSourceValue>(props: {\n source: SubscribeSource<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ReactNode) | ReactNode\n }): ReturnType<FunctionComponent>\n <TSourceValue, TSubSelected>(props: {\n source: SubscribeSource<TSourceValue>\n selector: (state: TSourceValue) => TSubSelected\n children: ((state: TSubSelected) => ReactNode) | ReactNode\n }): ReturnType<FunctionComponent>\n <TSubSelected>(\n props: Omit<SubscribePropsWithStore<TFeatures, TSubSelected>, 'source'>,\n ): ReturnType<FunctionComponent>\n }\n /**\n * A React component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n *\n * @example\n * ```tsx\n * <table.FlexRender cell={cell} />\n * <table.FlexRender header={header} />\n * <table.FlexRender footer={footer} />\n * ```\n *\n * This replaces calling `flexRender` directly like this:\n * ```tsx\n * flexRender(cell.column.columnDef.cell, cell.getContext())\n * flexRender(header.column.columnDef.header, header.getContext())\n * flexRender(footer.column.columnDef.footer, footer.getContext())\n * ```\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ReactNode\n /**\n * 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`.\n *\n * @example\n * const table = useTable(options, (state) => ({ globalFilter: state.globalFilter })) // only globalFilter is part of the selected state\n *\n * console.log(table.state.globalFilter)\n */\n readonly state: Readonly<TSelected>\n}\n\nexport function useTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n>(\n tableOptions: TableOptions<TFeatures, TData>,\n selector: (state: TableState<TFeatures>) => TSelected = () =>\n ({}) as TSelected,\n): ReactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable({\n ...tableOptions,\n _features: {\n coreReativityFeature: reactReactivity(),\n ...tableOptions._features,\n },\n }) as ReactTable<TFeatures, TData, TSelected>\n\n tableInstance.Subscribe = ((props: any) => {\n const source = props.source ?? tableInstance.store\n\n return Subscribe({\n ...props,\n source,\n })\n }) as ReactTable<TFeatures, TData, TSelected>['Subscribe']\n\n tableInstance.FlexRender = FlexRender\n\n return tableInstance\n })\n\n // sync options on every render\n table.setOptions((prev) => ({\n ...prev,\n ...tableOptions,\n }))\n\n const state = useSelector(table.store, selector, { compare: shallow })\n\n // we know this is not the most efficient way to return the table,\n // but it is required for the react compiler to work\n return useMemo(\n () => ({\n ...table,\n options: tableOptions,\n state,\n }),\n [table, tableOptions, state],\n )\n}\n"],"mappings":";;;;;;;;;;AA2GA,SAAgB,SAKd,cACA,kBACG,EAAE,GACoC;CACzC,MAAM,CAAC,SAAS,eAAe;EAC7B,MAAM,gBAAgB,eAAe;GACnC,GAAG;GACH,WAAW;IACT,sBAAsB,iBAAiB;IACvC,GAAG,aAAa;IACjB;GACF,CAAC;AAEF,gBAAc,cAAc,UAAe;GACzC,MAAM,SAAS,MAAM,UAAU,cAAc;AAE7C,UAAO,UAAU;IACf,GAAG;IACH;IACD,CAAC;;AAGJ,gBAAc,aAAa;AAE3B,SAAO;GACP;AAGF,OAAM,YAAY,UAAU;EAC1B,GAAG;EACH,GAAG;EACJ,EAAE;CAEH,MAAM,QAAQ,YAAY,MAAM,OAAO,UAAU,EAAE,SAAS,SAAS,CAAC;AAItE,QAAO,eACE;EACL,GAAG;EACH,SAAS;EACT;EACD,GACD;EAAC;EAAO;EAAc;EAAM,CAC7B"}
|
package/package.json
CHANGED
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from '@tanstack/
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
73
|
-
|
|
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,
|
|
92
|
-
| SubscribePropsWithSourceIdentity<
|
|
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
|
|
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
|
|
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,48 +122,27 @@ export type SubscribeProps<
|
|
|
145
122
|
* </table.Subscribe>
|
|
146
123
|
* ```
|
|
147
124
|
*/
|
|
148
|
-
export function Subscribe<
|
|
149
|
-
|
|
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
|
-
|
|
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
|
|
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,
|
|
139
|
+
props: SubscribeProps<TFeatures, TSelected, TSourceValue>,
|
|
182
140
|
): ReturnType<FunctionComponent> {
|
|
183
|
-
const
|
|
184
|
-
const selectFn =
|
|
185
|
-
'source' in props ? (props.selector ?? ((x: unknown) => x)) : props.selector
|
|
141
|
+
const selectFn = props.selector ?? ((x: unknown) => x)
|
|
186
142
|
|
|
187
143
|
const selected = useSelector(
|
|
188
144
|
// Atom and store share the same selection protocol; union args need a widen for TS.
|
|
189
|
-
source,
|
|
145
|
+
props.source,
|
|
190
146
|
selectFn as Parameters<typeof useSelector>[1],
|
|
191
147
|
{
|
|
192
148
|
compare: shallow,
|
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,
|
|
@@ -61,20 +60,17 @@ export type ReactTable<
|
|
|
61
60
|
*/
|
|
62
61
|
Subscribe: {
|
|
63
62
|
<TSourceValue>(props: {
|
|
64
|
-
source:
|
|
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:
|
|
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
|
/**
|
|
@@ -128,9 +124,11 @@ export function useTable<
|
|
|
128
124
|
}) as ReactTable<TFeatures, TData, TSelected>
|
|
129
125
|
|
|
130
126
|
tableInstance.Subscribe = ((props: any) => {
|
|
127
|
+
const source = props.source ?? tableInstance.store
|
|
128
|
+
|
|
131
129
|
return Subscribe({
|
|
132
130
|
...props,
|
|
133
|
-
|
|
131
|
+
source,
|
|
134
132
|
})
|
|
135
133
|
}) as ReactTable<TFeatures, TData, TSelected>['Subscribe']
|
|
136
134
|
|