@tanstack/preact-table 9.0.0-alpha.41 → 9.0.0-alpha.43

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.
@@ -0,0 +1,20 @@
1
+ let _tanstack_preact_store = require("@tanstack/preact-store");
2
+
3
+ //#region src/reactivity.ts
4
+ function preactReactivity() {
5
+ return {
6
+ createOptionsStore: false,
7
+ batch: _tanstack_preact_store.batch,
8
+ untrack: (fn) => fn(),
9
+ createReadonlyAtom: (fn, options) => {
10
+ return (0, _tanstack_preact_store.createAtom)(() => fn(), { compare: options === null || options === void 0 ? void 0 : options.compare });
11
+ },
12
+ createWritableAtom: (value, options) => {
13
+ return (0, _tanstack_preact_store.createAtom)(value, { compare: options === null || options === void 0 ? void 0 : options.compare });
14
+ }
15
+ };
16
+ }
17
+
18
+ //#endregion
19
+ exports.preactReactivity = preactReactivity;
20
+ //# sourceMappingURL=reactivity.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reactivity.cjs","names":[],"sources":["../src/reactivity.ts"],"sourcesContent":["import { batch, createAtom } from '@tanstack/preact-store'\nimport type {\n TableAtomOptions,\n TableReactivityBindings,\n} from '@tanstack/table-core/reactivity'\n\nexport function preactReactivity(): TableReactivityBindings {\n return {\n createOptionsStore: false,\n batch,\n untrack: (fn) => fn(),\n createReadonlyAtom: <T>(fn: () => T, options?: TableAtomOptions<T>) => {\n return createAtom(() => fn(), {\n compare: options?.compare,\n })\n },\n createWritableAtom: <T>(value: T, options?: TableAtomOptions<T>) => {\n return createAtom(value, {\n compare: options?.compare,\n })\n },\n }\n}\n\n// // TOTO - re-explore preact signals for reactivity\n// import { batch, computed, signal, untracked } from '@preact/signals'\n// import type {\n// TableAtomOptions,\n// TableReactivityBindings,\n// } from '@tanstack/table-core/reactivity'\n// import type { Atom, Observer, ReadonlyAtom } from '@tanstack/preact-store'\n\n// function observerToCallback<T>(\n// observerOrNext: Observer<T> | ((value: T) => void),\n// ): (value: T) => void {\n// return typeof observerOrNext === 'function'\n// ? observerOrNext\n// : (value) => observerOrNext.next?.(value)\n// }\n\n// function signalToReadonlyAtom<T>(source: {\n// value: T\n// subscribe: (observer: (value: T) => void) => () => void\n// }): ReadonlyAtom<T> {\n// return Object.assign(source, {\n// get: () => source.value,\n// subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {\n// const unsubscribe = source.subscribe(observerToCallback(observerOrNext))\n// return { unsubscribe }\n// }) as ReadonlyAtom<T>['subscribe'],\n// })\n// }\n\n// function signalToWritableAtom<T>(source: {\n// value: T\n// subscribe: (observer: (value: T) => void) => () => void\n// }): Atom<T> {\n// return Object.assign(source, {\n// set: (updater: T | ((prevVal: T) => T)) => {\n// source.value =\n// typeof updater === 'function'\n// ? (updater as (prevVal: T) => T)(source.value)\n// : updater\n// },\n// get: () => source.value,\n// subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {\n// const unsubscribe = source.subscribe(observerToCallback(observerOrNext))\n// return { unsubscribe }\n// }) as Atom<T>['subscribe'],\n// })\n// }\n\n// export function preactReactivity(): TableReactivityBindings {\n// return {\n// createReadonlyAtom: <T>(fn: () => T, _options?: TableAtomOptions<T>) => {\n// return signalToReadonlyAtom(computed(fn))\n// },\n// createWritableAtom: <T>(\n// value: T,\n// _options?: TableAtomOptions<T>,\n// ): Atom<T> => {\n// return signalToWritableAtom(signal(value))\n// },\n// untrack: untracked,\n// batch: batch,\n// }\n// }\n"],"mappings":";;;AAMA,SAAgB,mBAA4C;AAC1D,QAAO;EACL,oBAAoB;EACpB;EACA,UAAU,OAAO,IAAI;EACrB,qBAAwB,IAAa,YAAkC;AACrE,uDAAwB,IAAI,EAAE,EAC5B,2DAAS,QAAS,SACnB,CAAC;;EAEJ,qBAAwB,OAAU,YAAkC;AAClE,iDAAkB,OAAO,EACvB,2DAAS,QAAS,SACnB,CAAC;;EAEL"}
@@ -0,0 +1,20 @@
1
+ import { batch, createAtom } from "@tanstack/preact-store";
2
+
3
+ //#region src/reactivity.ts
4
+ function preactReactivity() {
5
+ return {
6
+ createOptionsStore: false,
7
+ batch,
8
+ untrack: (fn) => fn(),
9
+ createReadonlyAtom: (fn, options) => {
10
+ return createAtom(() => fn(), { compare: options === null || options === void 0 ? void 0 : options.compare });
11
+ },
12
+ createWritableAtom: (value, options) => {
13
+ return createAtom(value, { compare: options === null || options === void 0 ? void 0 : options.compare });
14
+ }
15
+ };
16
+ }
17
+
18
+ //#endregion
19
+ export { preactReactivity };
20
+ //# sourceMappingURL=reactivity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reactivity.js","names":[],"sources":["../src/reactivity.ts"],"sourcesContent":["import { batch, createAtom } from '@tanstack/preact-store'\nimport type {\n TableAtomOptions,\n TableReactivityBindings,\n} from '@tanstack/table-core/reactivity'\n\nexport function preactReactivity(): TableReactivityBindings {\n return {\n createOptionsStore: false,\n batch,\n untrack: (fn) => fn(),\n createReadonlyAtom: <T>(fn: () => T, options?: TableAtomOptions<T>) => {\n return createAtom(() => fn(), {\n compare: options?.compare,\n })\n },\n createWritableAtom: <T>(value: T, options?: TableAtomOptions<T>) => {\n return createAtom(value, {\n compare: options?.compare,\n })\n },\n }\n}\n\n// // TOTO - re-explore preact signals for reactivity\n// import { batch, computed, signal, untracked } from '@preact/signals'\n// import type {\n// TableAtomOptions,\n// TableReactivityBindings,\n// } from '@tanstack/table-core/reactivity'\n// import type { Atom, Observer, ReadonlyAtom } from '@tanstack/preact-store'\n\n// function observerToCallback<T>(\n// observerOrNext: Observer<T> | ((value: T) => void),\n// ): (value: T) => void {\n// return typeof observerOrNext === 'function'\n// ? observerOrNext\n// : (value) => observerOrNext.next?.(value)\n// }\n\n// function signalToReadonlyAtom<T>(source: {\n// value: T\n// subscribe: (observer: (value: T) => void) => () => void\n// }): ReadonlyAtom<T> {\n// return Object.assign(source, {\n// get: () => source.value,\n// subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {\n// const unsubscribe = source.subscribe(observerToCallback(observerOrNext))\n// return { unsubscribe }\n// }) as ReadonlyAtom<T>['subscribe'],\n// })\n// }\n\n// function signalToWritableAtom<T>(source: {\n// value: T\n// subscribe: (observer: (value: T) => void) => () => void\n// }): Atom<T> {\n// return Object.assign(source, {\n// set: (updater: T | ((prevVal: T) => T)) => {\n// source.value =\n// typeof updater === 'function'\n// ? (updater as (prevVal: T) => T)(source.value)\n// : updater\n// },\n// get: () => source.value,\n// subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {\n// const unsubscribe = source.subscribe(observerToCallback(observerOrNext))\n// return { unsubscribe }\n// }) as Atom<T>['subscribe'],\n// })\n// }\n\n// export function preactReactivity(): TableReactivityBindings {\n// return {\n// createReadonlyAtom: <T>(fn: () => T, _options?: TableAtomOptions<T>) => {\n// return signalToReadonlyAtom(computed(fn))\n// },\n// createWritableAtom: <T>(\n// value: T,\n// _options?: TableAtomOptions<T>,\n// ): Atom<T> => {\n// return signalToWritableAtom(signal(value))\n// },\n// untrack: untracked,\n// batch: batch,\n// }\n// }\n"],"mappings":";;;AAMA,SAAgB,mBAA4C;AAC1D,QAAO;EACL,oBAAoB;EACpB;EACA,UAAU,OAAO,IAAI;EACrB,qBAAwB,IAAa,YAAkC;AACrE,UAAO,iBAAiB,IAAI,EAAE,EAC5B,2DAAS,QAAS,SACnB,CAAC;;EAEJ,qBAAwB,OAAU,YAAkC;AAClE,UAAO,WAAW,OAAO,EACvB,2DAAS,QAAS,SACnB,CAAC;;EAEL"}
package/dist/useTable.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  const require_FlexRender = require('./FlexRender.cjs');
2
2
  const require_Subscribe = require('./Subscribe.cjs');
3
+ const require_reactivity = require('./reactivity.cjs');
3
4
  let _tanstack_table_core = require("@tanstack/table-core");
4
5
  let _tanstack_preact_store = require("@tanstack/preact-store");
5
6
  let preact_hooks = require("preact/hooks");
@@ -7,7 +8,13 @@ let preact_hooks = require("preact/hooks");
7
8
  //#region src/useTable.ts
8
9
  function useTable(tableOptions, selector = () => ({})) {
9
10
  const [table] = (0, preact_hooks.useState)(() => {
10
- const tableInstance = (0, _tanstack_table_core.constructTable)(tableOptions);
11
+ const tableInstance = (0, _tanstack_table_core.constructTable)({
12
+ ...tableOptions,
13
+ _features: {
14
+ coreReativityFeature: require_reactivity.preactReactivity(),
15
+ ...tableOptions._features
16
+ }
17
+ });
11
18
  tableInstance.Subscribe = ((props) => {
12
19
  return require_Subscribe.Subscribe({
13
20
  ...props,
@@ -21,16 +28,16 @@ function useTable(tableOptions, selector = () => ({})) {
21
28
  ...prev,
22
29
  ...tableOptions
23
30
  }));
24
- const selectorWithDataAndColumns = (state) => ({
25
- columns: tableOptions.columns,
26
- data: tableOptions.data,
27
- ...selector(state)
28
- });
29
- const state = (0, _tanstack_preact_store.useSelector)(table.store, selectorWithDataAndColumns, { compare: _tanstack_preact_store.shallow });
31
+ const state = (0, _tanstack_preact_store.useSelector)(table.store, selector, { compare: _tanstack_preact_store.shallow });
30
32
  return (0, preact_hooks.useMemo)(() => ({
31
33
  ...table,
34
+ options: tableOptions,
35
+ state
36
+ }), [
37
+ table,
38
+ tableOptions,
32
39
  state
33
- }), [state, table]);
40
+ ]);
34
41
  }
35
42
 
36
43
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useTable.cjs","names":["Subscribe","FlexRender","shallow"],"sources":["../src/useTable.ts"],"sourcesContent":["import { useMemo, useState } from 'preact/hooks'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/preact-store'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { Atom, ReadonlyAtom } from '@tanstack/preact-store'\nimport type { ComponentChildren } from 'preact'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribePropsWithStore } from './Subscribe'\n\nexport type PreactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to 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 (source first, then store) so JSX contextual typing works for both modes.\n * Source without `selector` is separate so children infer `TSourceValue` (identity projection).\n */\n Subscribe: {\n <TSourceValue>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSourceValue, TSubSelected>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector: (state: TSourceValue) => TSubSelected\n children: ((state: TSubSelected) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSubSelected>(\n props: Omit<\n SubscribePropsWithStore<TFeatures, TData, TSubSelected>,\n 'table'\n >,\n ): ComponentChildren\n }\n /**\n * A Preact component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ComponentChildren\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 readonly state: Readonly<TSelected> & {\n columns: TableOptions<TFeatures, TData>['columns']\n data: TableOptions<TFeatures, TData>['data']\n }\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): PreactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable(tableOptions) as PreactTable<\n TFeatures,\n TData,\n TSelected\n >\n\n tableInstance.Subscribe = ((props: any) => {\n return Subscribe({\n ...props,\n table: tableInstance,\n })\n }) as PreactTable<TFeatures, TData, TSelected>['Subscribe']\n\n tableInstance.FlexRender = FlexRender\n\n return tableInstance\n })\n\n // sync table options on every render\n table.setOptions((prev) => ({\n ...prev,\n ...tableOptions,\n }))\n\n const selectorWithDataAndColumns = (state: TableState<TFeatures>) => ({\n columns: tableOptions.columns,\n data: tableOptions.data,\n ...selector(state),\n })\n\n const state = useSelector(table.store, selectorWithDataAndColumns, {\n compare: shallow,\n })\n\n return useMemo(\n () => ({\n ...table,\n state,\n }),\n [state, table],\n ) as PreactTable<TFeatures, TData, TSelected>\n}\n"],"mappings":";;;;;;;AAoFA,SAAgB,SAKd,cACA,kBACG,EAAE,GACqC;CAC1C,MAAM,CAAC,0CAAwB;EAC7B,MAAM,yDAA+B,aAAa;AAMlD,gBAAc,cAAc,UAAe;AACzC,UAAOA,4BAAU;IACf,GAAG;IACH,OAAO;IACR,CAAC;;AAGJ,gBAAc,aAAaC;AAE3B,SAAO;GACP;AAGF,OAAM,YAAY,UAAU;EAC1B,GAAG;EACH,GAAG;EACJ,EAAE;CAEH,MAAM,8BAA8B,WAAkC;EACpE,SAAS,aAAa;EACtB,MAAM,aAAa;EACnB,GAAG,SAAS,MAAM;EACnB;CAED,MAAM,gDAAoB,MAAM,OAAO,4BAA4B,EACjE,SAASC,gCACV,CAAC;AAEF,yCACS;EACL,GAAG;EACH;EACD,GACD,CAAC,OAAO,MAAM,CACf"}
1
+ {"version":3,"file":"useTable.cjs","names":["preactReactivity","Subscribe","FlexRender","shallow"],"sources":["../src/useTable.ts"],"sourcesContent":["import { useMemo, useState } from 'preact/hooks'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/preact-store'\nimport { preactReactivity } from './reactivity'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { Atom, ReadonlyAtom } from '@tanstack/preact-store'\nimport type { ComponentChildren } from 'preact'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribePropsWithStore } from './Subscribe'\n\nexport type PreactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to 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 (source first, then store) so JSX contextual typing works for both modes.\n * Source without `selector` is separate so children infer `TSourceValue` (identity projection).\n */\n Subscribe: {\n <TSourceValue>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSourceValue, TSubSelected>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector: (state: TSourceValue) => TSubSelected\n children: ((state: TSubSelected) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSubSelected>(\n props: Omit<\n SubscribePropsWithStore<TFeatures, TData, TSubSelected>,\n 'table'\n >,\n ): ComponentChildren\n }\n /**\n * A Preact component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ComponentChildren\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 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): PreactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable({\n ...tableOptions,\n _features: {\n coreReativityFeature: preactReactivity(),\n ...tableOptions._features,\n },\n }) as PreactTable<TFeatures, TData, TSelected>\n\n tableInstance.Subscribe = ((props: any) => {\n return Subscribe({\n ...props,\n table: tableInstance,\n })\n }) as PreactTable<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 return useMemo(\n () => ({\n ...table,\n options: tableOptions,\n state,\n }),\n [table, tableOptions, state],\n )\n}\n"],"mappings":";;;;;;;;AAkFA,SAAgB,SAKd,cACA,kBACG,EAAE,GACqC;CAC1C,MAAM,CAAC,0CAAwB;EAC7B,MAAM,yDAA+B;GACnC,GAAG;GACH,WAAW;IACT,sBAAsBA,qCAAkB;IACxC,GAAG,aAAa;IACjB;GACF,CAAC;AAEF,gBAAc,cAAc,UAAe;AACzC,UAAOC,4BAAU;IACf,GAAG;IACH,OAAO;IACR,CAAC;;AAGJ,gBAAc,aAAaC;AAE3B,SAAO;GACP;AAGF,OAAM,YAAY,UAAU;EAC1B,GAAG;EACH,GAAG;EACJ,EAAE;CAEH,MAAM,gDAAoB,MAAM,OAAO,UAAU,EAAE,SAASC,gCAAS,CAAC;AAEtE,yCACS;EACL,GAAG;EACH,SAAS;EACT;EACD,GACD;EAAC;EAAO;EAAc;EAAM,CAC7B"}
@@ -54,10 +54,7 @@ type PreactTable<TFeatures extends TableFeatures, TData extends RowData, TSelect
54
54
  /**
55
55
  * 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`.
56
56
  */
57
- readonly state: Readonly<TSelected> & {
58
- columns: TableOptions<TFeatures, TData>['columns'];
59
- data: TableOptions<TFeatures, TData>['data'];
60
- };
57
+ readonly state: Readonly<TSelected>;
61
58
  };
62
59
  declare function useTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptions<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): PreactTable<TFeatures, TData, TSelected>;
63
60
  //#endregion
@@ -54,10 +54,7 @@ type PreactTable<TFeatures extends TableFeatures, TData extends RowData, TSelect
54
54
  /**
55
55
  * 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`.
56
56
  */
57
- readonly state: Readonly<TSelected> & {
58
- columns: TableOptions<TFeatures, TData>['columns'];
59
- data: TableOptions<TFeatures, TData>['data'];
60
- };
57
+ readonly state: Readonly<TSelected>;
61
58
  };
62
59
  declare function useTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptions<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): PreactTable<TFeatures, TData, TSelected>;
63
60
  //#endregion
package/dist/useTable.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { FlexRender } from "./FlexRender.js";
2
2
  import { Subscribe } from "./Subscribe.js";
3
+ import { preactReactivity } from "./reactivity.js";
3
4
  import { constructTable } from "@tanstack/table-core";
4
5
  import { shallow, useSelector } from "@tanstack/preact-store";
5
6
  import { useMemo, useState } from "preact/hooks";
@@ -7,7 +8,13 @@ import { useMemo, useState } from "preact/hooks";
7
8
  //#region src/useTable.ts
8
9
  function useTable(tableOptions, selector = () => ({})) {
9
10
  const [table] = useState(() => {
10
- const tableInstance = constructTable(tableOptions);
11
+ const tableInstance = constructTable({
12
+ ...tableOptions,
13
+ _features: {
14
+ coreReativityFeature: preactReactivity(),
15
+ ...tableOptions._features
16
+ }
17
+ });
11
18
  tableInstance.Subscribe = ((props) => {
12
19
  return Subscribe({
13
20
  ...props,
@@ -21,16 +28,16 @@ function useTable(tableOptions, selector = () => ({})) {
21
28
  ...prev,
22
29
  ...tableOptions
23
30
  }));
24
- const selectorWithDataAndColumns = (state) => ({
25
- columns: tableOptions.columns,
26
- data: tableOptions.data,
27
- ...selector(state)
28
- });
29
- const state = useSelector(table.store, selectorWithDataAndColumns, { compare: shallow });
31
+ const state = useSelector(table.store, selector, { compare: shallow });
30
32
  return useMemo(() => ({
31
33
  ...table,
34
+ options: tableOptions,
35
+ state
36
+ }), [
37
+ table,
38
+ tableOptions,
32
39
  state
33
- }), [state, table]);
40
+ ]);
34
41
  }
35
42
 
36
43
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useTable.js","names":[],"sources":["../src/useTable.ts"],"sourcesContent":["import { useMemo, useState } from 'preact/hooks'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/preact-store'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { Atom, ReadonlyAtom } from '@tanstack/preact-store'\nimport type { ComponentChildren } from 'preact'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribePropsWithStore } from './Subscribe'\n\nexport type PreactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to 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 (source first, then store) so JSX contextual typing works for both modes.\n * Source without `selector` is separate so children infer `TSourceValue` (identity projection).\n */\n Subscribe: {\n <TSourceValue>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSourceValue, TSubSelected>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector: (state: TSourceValue) => TSubSelected\n children: ((state: TSubSelected) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSubSelected>(\n props: Omit<\n SubscribePropsWithStore<TFeatures, TData, TSubSelected>,\n 'table'\n >,\n ): ComponentChildren\n }\n /**\n * A Preact component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ComponentChildren\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 readonly state: Readonly<TSelected> & {\n columns: TableOptions<TFeatures, TData>['columns']\n data: TableOptions<TFeatures, TData>['data']\n }\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): PreactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable(tableOptions) as PreactTable<\n TFeatures,\n TData,\n TSelected\n >\n\n tableInstance.Subscribe = ((props: any) => {\n return Subscribe({\n ...props,\n table: tableInstance,\n })\n }) as PreactTable<TFeatures, TData, TSelected>['Subscribe']\n\n tableInstance.FlexRender = FlexRender\n\n return tableInstance\n })\n\n // sync table options on every render\n table.setOptions((prev) => ({\n ...prev,\n ...tableOptions,\n }))\n\n const selectorWithDataAndColumns = (state: TableState<TFeatures>) => ({\n columns: tableOptions.columns,\n data: tableOptions.data,\n ...selector(state),\n })\n\n const state = useSelector(table.store, selectorWithDataAndColumns, {\n compare: shallow,\n })\n\n return useMemo(\n () => ({\n ...table,\n state,\n }),\n [state, table],\n ) as PreactTable<TFeatures, TData, TSelected>\n}\n"],"mappings":";;;;;;;AAoFA,SAAgB,SAKd,cACA,kBACG,EAAE,GACqC;CAC1C,MAAM,CAAC,SAAS,eAAe;EAC7B,MAAM,gBAAgB,eAAe,aAAa;AAMlD,gBAAc,cAAc,UAAe;AACzC,UAAO,UAAU;IACf,GAAG;IACH,OAAO;IACR,CAAC;;AAGJ,gBAAc,aAAa;AAE3B,SAAO;GACP;AAGF,OAAM,YAAY,UAAU;EAC1B,GAAG;EACH,GAAG;EACJ,EAAE;CAEH,MAAM,8BAA8B,WAAkC;EACpE,SAAS,aAAa;EACtB,MAAM,aAAa;EACnB,GAAG,SAAS,MAAM;EACnB;CAED,MAAM,QAAQ,YAAY,MAAM,OAAO,4BAA4B,EACjE,SAAS,SACV,CAAC;AAEF,QAAO,eACE;EACL,GAAG;EACH;EACD,GACD,CAAC,OAAO,MAAM,CACf"}
1
+ {"version":3,"file":"useTable.js","names":[],"sources":["../src/useTable.ts"],"sourcesContent":["import { useMemo, useState } from 'preact/hooks'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/preact-store'\nimport { preactReactivity } from './reactivity'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { Atom, ReadonlyAtom } from '@tanstack/preact-store'\nimport type { ComponentChildren } from 'preact'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribePropsWithStore } from './Subscribe'\n\nexport type PreactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to 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 (source first, then store) so JSX contextual typing works for both modes.\n * Source without `selector` is separate so children infer `TSourceValue` (identity projection).\n */\n Subscribe: {\n <TSourceValue>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector?: undefined\n children: ((state: TSourceValue) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSourceValue, TSubSelected>(props: {\n source: Atom<TSourceValue> | ReadonlyAtom<TSourceValue>\n selector: (state: TSourceValue) => TSubSelected\n children: ((state: TSubSelected) => ComponentChildren) | ComponentChildren\n }): ComponentChildren\n <TSubSelected>(\n props: Omit<\n SubscribePropsWithStore<TFeatures, TData, TSubSelected>,\n 'table'\n >,\n ): ComponentChildren\n }\n /**\n * A Preact component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ComponentChildren\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 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): PreactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable({\n ...tableOptions,\n _features: {\n coreReativityFeature: preactReactivity(),\n ...tableOptions._features,\n },\n }) as PreactTable<TFeatures, TData, TSelected>\n\n tableInstance.Subscribe = ((props: any) => {\n return Subscribe({\n ...props,\n table: tableInstance,\n })\n }) as PreactTable<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 return useMemo(\n () => ({\n ...table,\n options: tableOptions,\n state,\n }),\n [table, tableOptions, state],\n )\n}\n"],"mappings":";;;;;;;;AAkFA,SAAgB,SAKd,cACA,kBACG,EAAE,GACqC;CAC1C,MAAM,CAAC,SAAS,eAAe;EAC7B,MAAM,gBAAgB,eAAe;GACnC,GAAG;GACH,WAAW;IACT,sBAAsB,kBAAkB;IACxC,GAAG,aAAa;IACjB;GACF,CAAC;AAEF,gBAAc,cAAc,UAAe;AACzC,UAAO,UAAU;IACf,GAAG;IACH,OAAO;IACR,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;AAEtE,QAAO,eACE;EACL,GAAG;EACH,SAAS;EACT;EACD,GACD;EAAC;EAAO;EAAc;EAAM,CAC7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-table",
3
- "version": "9.0.0-alpha.41",
3
+ "version": "9.0.0-alpha.43",
4
4
  "description": "Headless UI for building powerful tables & datagrids for Preact.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  ],
50
50
  "dependencies": {
51
51
  "@tanstack/preact-store": "^0.13.0",
52
- "@tanstack/table-core": "9.0.0-alpha.41"
52
+ "@tanstack/table-core": "9.0.0-alpha.43"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@preact/preset-vite": "^2.10.5",
@@ -0,0 +1,87 @@
1
+ import { batch, createAtom } from '@tanstack/preact-store'
2
+ import type {
3
+ TableAtomOptions,
4
+ TableReactivityBindings,
5
+ } from '@tanstack/table-core/reactivity'
6
+
7
+ export function preactReactivity(): TableReactivityBindings {
8
+ return {
9
+ createOptionsStore: false,
10
+ batch,
11
+ untrack: (fn) => fn(),
12
+ createReadonlyAtom: <T>(fn: () => T, options?: TableAtomOptions<T>) => {
13
+ return createAtom(() => fn(), {
14
+ compare: options?.compare,
15
+ })
16
+ },
17
+ createWritableAtom: <T>(value: T, options?: TableAtomOptions<T>) => {
18
+ return createAtom(value, {
19
+ compare: options?.compare,
20
+ })
21
+ },
22
+ }
23
+ }
24
+
25
+ // // TOTO - re-explore preact signals for reactivity
26
+ // import { batch, computed, signal, untracked } from '@preact/signals'
27
+ // import type {
28
+ // TableAtomOptions,
29
+ // TableReactivityBindings,
30
+ // } from '@tanstack/table-core/reactivity'
31
+ // import type { Atom, Observer, ReadonlyAtom } from '@tanstack/preact-store'
32
+
33
+ // function observerToCallback<T>(
34
+ // observerOrNext: Observer<T> | ((value: T) => void),
35
+ // ): (value: T) => void {
36
+ // return typeof observerOrNext === 'function'
37
+ // ? observerOrNext
38
+ // : (value) => observerOrNext.next?.(value)
39
+ // }
40
+
41
+ // function signalToReadonlyAtom<T>(source: {
42
+ // value: T
43
+ // subscribe: (observer: (value: T) => void) => () => void
44
+ // }): ReadonlyAtom<T> {
45
+ // return Object.assign(source, {
46
+ // get: () => source.value,
47
+ // subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {
48
+ // const unsubscribe = source.subscribe(observerToCallback(observerOrNext))
49
+ // return { unsubscribe }
50
+ // }) as ReadonlyAtom<T>['subscribe'],
51
+ // })
52
+ // }
53
+
54
+ // function signalToWritableAtom<T>(source: {
55
+ // value: T
56
+ // subscribe: (observer: (value: T) => void) => () => void
57
+ // }): Atom<T> {
58
+ // return Object.assign(source, {
59
+ // set: (updater: T | ((prevVal: T) => T)) => {
60
+ // source.value =
61
+ // typeof updater === 'function'
62
+ // ? (updater as (prevVal: T) => T)(source.value)
63
+ // : updater
64
+ // },
65
+ // get: () => source.value,
66
+ // subscribe: ((observerOrNext: Observer<T> | ((value: T) => void)) => {
67
+ // const unsubscribe = source.subscribe(observerToCallback(observerOrNext))
68
+ // return { unsubscribe }
69
+ // }) as Atom<T>['subscribe'],
70
+ // })
71
+ // }
72
+
73
+ // export function preactReactivity(): TableReactivityBindings {
74
+ // return {
75
+ // createReadonlyAtom: <T>(fn: () => T, _options?: TableAtomOptions<T>) => {
76
+ // return signalToReadonlyAtom(computed(fn))
77
+ // },
78
+ // createWritableAtom: <T>(
79
+ // value: T,
80
+ // _options?: TableAtomOptions<T>,
81
+ // ): Atom<T> => {
82
+ // return signalToWritableAtom(signal(value))
83
+ // },
84
+ // untrack: untracked,
85
+ // batch: batch,
86
+ // }
87
+ // }
package/src/useTable.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { useMemo, useState } from 'preact/hooks'
2
2
  import { constructTable } from '@tanstack/table-core'
3
3
  import { shallow, useSelector } from '@tanstack/preact-store'
4
+ import { preactReactivity } from './reactivity'
4
5
  import { FlexRender } from './FlexRender'
5
6
  import { Subscribe } from './Subscribe'
6
7
  import type {
@@ -76,10 +77,7 @@ export type PreactTable<
76
77
  /**
77
78
  * 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`.
78
79
  */
79
- readonly state: Readonly<TSelected> & {
80
- columns: TableOptions<TFeatures, TData>['columns']
81
- data: TableOptions<TFeatures, TData>['data']
82
- }
80
+ readonly state: Readonly<TSelected>
83
81
  }
84
82
 
85
83
  export function useTable<
@@ -92,11 +90,13 @@ export function useTable<
92
90
  ({}) as TSelected,
93
91
  ): PreactTable<TFeatures, TData, TSelected> {
94
92
  const [table] = useState(() => {
95
- const tableInstance = constructTable(tableOptions) as PreactTable<
96
- TFeatures,
97
- TData,
98
- TSelected
99
- >
93
+ const tableInstance = constructTable({
94
+ ...tableOptions,
95
+ _features: {
96
+ coreReativityFeature: preactReactivity(),
97
+ ...tableOptions._features,
98
+ },
99
+ }) as PreactTable<TFeatures, TData, TSelected>
100
100
 
101
101
  tableInstance.Subscribe = ((props: any) => {
102
102
  return Subscribe({
@@ -110,27 +110,20 @@ export function useTable<
110
110
  return tableInstance
111
111
  })
112
112
 
113
- // sync table options on every render
113
+ // sync options on every render
114
114
  table.setOptions((prev) => ({
115
115
  ...prev,
116
116
  ...tableOptions,
117
117
  }))
118
118
 
119
- const selectorWithDataAndColumns = (state: TableState<TFeatures>) => ({
120
- columns: tableOptions.columns,
121
- data: tableOptions.data,
122
- ...selector(state),
123
- })
124
-
125
- const state = useSelector(table.store, selectorWithDataAndColumns, {
126
- compare: shallow,
127
- })
119
+ const state = useSelector(table.store, selector, { compare: shallow })
128
120
 
129
121
  return useMemo(
130
122
  () => ({
131
123
  ...table,
124
+ options: tableOptions,
132
125
  state,
133
126
  }),
134
- [state, table],
135
- ) as PreactTable<TFeatures, TData, TSelected>
127
+ [table, tableOptions, state],
128
+ )
136
129
  }