@tanstack/vue-table 9.0.0-alpha.32 → 9.0.0-alpha.34

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.
@@ -1,29 +1,31 @@
1
1
  import { MaybeRef, VNode } from "vue";
2
2
  import { NoInfer, RowData, Table, TableFeatures, TableOptions, TableState } from "@tanstack/table-core";
3
+ import { Atom, ReadonlyAtom } from "@tanstack/vue-store";
3
4
 
4
5
  //#region src/useTable.d.ts
5
- type TableOptionsWithReactiveData<TFeatures extends TableFeatures, TData extends RowData> = Omit<TableOptions<TFeatures, TData>, 'data'> & {
6
- data: MaybeRef<ReadonlyArray<TData>>;
7
- };
6
+ type TableOptionsWithReactiveData<TFeatures extends TableFeatures, TData extends RowData> = { [K in keyof TableOptions<TFeatures, TData>]: K extends 'data' ? MaybeRef<ReadonlyArray<TData>> : MaybeRef<TableOptions<TFeatures, TData>[K]> };
8
7
  type VueTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}> = Table<TFeatures, TData> & {
9
8
  /**
10
- * A Vue component that allows you to subscribe to the table state.
11
- *
12
- * This is useful for opting into state subscriptions for specific parts of the table state.
13
- *
14
- * @example
15
- * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
16
- * {(state) => (
17
- * <tr>
18
- * // render the row
19
- * </tr>
20
- * )}
21
- * </table.Subscribe>
9
+ * Store mode: `selector` required. Atom mode: pass `atom`; omit `selector` for the
10
+ * whole slice (identity), or pass `selector` to project. Split overloads so atom-only
11
+ * infers `TAtomValue` for `children` (see React `Subscribe`).
22
12
  */
23
- Subscribe: <TSelected>(props: {
24
- selector: (state: NoInfer<TableState<TFeatures>>) => TSelected;
25
- children: ((state: Readonly<TSelected>) => VNode | Array<VNode>) | VNode | Array<VNode>;
26
- }) => VNode | Array<VNode>;
13
+ Subscribe: {
14
+ <TAtomValue>(props: {
15
+ atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>;
16
+ selector?: undefined;
17
+ children: ((state: Readonly<TAtomValue>) => VNode | Array<VNode>) | VNode | Array<VNode>;
18
+ }): VNode | Array<VNode>;
19
+ <TAtomValue, TSubSelected>(props: {
20
+ atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>;
21
+ selector: (state: TAtomValue) => TSubSelected;
22
+ children: ((state: Readonly<TSubSelected>) => VNode | Array<VNode>) | VNode | Array<VNode>;
23
+ }): VNode | Array<VNode>;
24
+ <TSubSelected>(props: {
25
+ selector: (state: NoInfer<TableState<TFeatures>>) => TSubSelected;
26
+ children: ((state: Readonly<TSubSelected>) => VNode | Array<VNode>) | VNode | Array<VNode>;
27
+ }): VNode | Array<VNode>;
28
+ };
27
29
  /**
28
30
  * 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`.
29
31
  *
@@ -34,7 +36,7 @@ type VueTable<TFeatures extends TableFeatures, TData extends RowData, TSelected
34
36
  */
35
37
  readonly state: Readonly<TSelected>;
36
38
  };
37
- declare function useTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptionsWithReactiveData<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): VueTable<TFeatures, TData, TSelected>;
39
+ declare function useTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptions<TFeatures, TData> | TableOptionsWithReactiveData<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): VueTable<TFeatures, TData, TSelected>;
38
40
  //#endregion
39
41
  export { TableOptionsWithReactiveData, VueTable, useTable };
40
42
  //# sourceMappingURL=useTable.d.cts.map
@@ -1,29 +1,31 @@
1
1
  import { MaybeRef, VNode } from "vue";
2
2
  import { NoInfer, RowData, Table, TableFeatures, TableOptions, TableState } from "@tanstack/table-core";
3
+ import { Atom, ReadonlyAtom } from "@tanstack/vue-store";
3
4
 
4
5
  //#region src/useTable.d.ts
5
- type TableOptionsWithReactiveData<TFeatures extends TableFeatures, TData extends RowData> = Omit<TableOptions<TFeatures, TData>, 'data'> & {
6
- data: MaybeRef<ReadonlyArray<TData>>;
7
- };
6
+ type TableOptionsWithReactiveData<TFeatures extends TableFeatures, TData extends RowData> = { [K in keyof TableOptions<TFeatures, TData>]: K extends 'data' ? MaybeRef<ReadonlyArray<TData>> : MaybeRef<TableOptions<TFeatures, TData>[K]> };
8
7
  type VueTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}> = Table<TFeatures, TData> & {
9
8
  /**
10
- * A Vue component that allows you to subscribe to the table state.
11
- *
12
- * This is useful for opting into state subscriptions for specific parts of the table state.
13
- *
14
- * @example
15
- * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
16
- * {(state) => (
17
- * <tr>
18
- * // render the row
19
- * </tr>
20
- * )}
21
- * </table.Subscribe>
9
+ * Store mode: `selector` required. Atom mode: pass `atom`; omit `selector` for the
10
+ * whole slice (identity), or pass `selector` to project. Split overloads so atom-only
11
+ * infers `TAtomValue` for `children` (see React `Subscribe`).
22
12
  */
23
- Subscribe: <TSelected>(props: {
24
- selector: (state: NoInfer<TableState<TFeatures>>) => TSelected;
25
- children: ((state: Readonly<TSelected>) => VNode | Array<VNode>) | VNode | Array<VNode>;
26
- }) => VNode | Array<VNode>;
13
+ Subscribe: {
14
+ <TAtomValue>(props: {
15
+ atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>;
16
+ selector?: undefined;
17
+ children: ((state: Readonly<TAtomValue>) => VNode | Array<VNode>) | VNode | Array<VNode>;
18
+ }): VNode | Array<VNode>;
19
+ <TAtomValue, TSubSelected>(props: {
20
+ atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>;
21
+ selector: (state: TAtomValue) => TSubSelected;
22
+ children: ((state: Readonly<TSubSelected>) => VNode | Array<VNode>) | VNode | Array<VNode>;
23
+ }): VNode | Array<VNode>;
24
+ <TSubSelected>(props: {
25
+ selector: (state: NoInfer<TableState<TFeatures>>) => TSubSelected;
26
+ children: ((state: Readonly<TSubSelected>) => VNode | Array<VNode>) | VNode | Array<VNode>;
27
+ }): VNode | Array<VNode>;
28
+ };
27
29
  /**
28
30
  * 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`.
29
31
  *
@@ -34,7 +36,7 @@ type VueTable<TFeatures extends TableFeatures, TData extends RowData, TSelected
34
36
  */
35
37
  readonly state: Readonly<TSelected>;
36
38
  };
37
- declare function useTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptionsWithReactiveData<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): VueTable<TFeatures, TData, TSelected>;
39
+ declare function useTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptions<TFeatures, TData> | TableOptionsWithReactiveData<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): VueTable<TFeatures, TData, TSelected>;
38
40
  //#endregion
39
41
  export { TableOptionsWithReactiveData, VueTable, useTable };
40
42
  //# sourceMappingURL=useTable.d.ts.map
package/dist/useTable.js CHANGED
@@ -1,19 +1,26 @@
1
1
  import { mergeProxy } from "./merge-proxy.js";
2
- import { isRef, ref, unref, watch, watchEffect } from "vue";
2
+ import { ref, unref, watch, watchEffect } from "vue";
3
3
  import { constructReactivityFeature, constructTable } from "@tanstack/table-core";
4
- import { useStore } from "@tanstack/vue-store";
4
+ import { shallow, useSelector } from "@tanstack/vue-store";
5
5
 
6
6
  //#region src/useTable.ts
7
- function getOptionsWithReactiveData(options) {
8
- return mergeProxy(options, { data: unref(options.data) });
7
+ function getOptionsWithReactiveValues(options) {
8
+ const resolvedOptions = {};
9
+ for (const key of Object.keys(options)) resolvedOptions[key] = unref(options[key]);
10
+ return mergeProxy(options, resolvedOptions);
11
+ }
12
+ function getReactiveOptionDeps(options) {
13
+ return Object.keys(options).map((key) => unref(options[key]));
9
14
  }
10
15
  function useTable(tableOptions, selector = () => ({})) {
11
16
  const notifier = ref(0);
17
+ const syncTableOptions = (table, options) => {
18
+ table.setOptions(() => getOptionsWithReactiveValues(options));
19
+ };
12
20
  const vueReactivityFeature = constructReactivityFeature({
13
21
  stateNotifier: () => notifier.value,
14
22
  optionsNotifier: () => notifier.value
15
23
  });
16
- const IS_REACTIVE = isRef(tableOptions.data);
17
24
  const mergedOptions = {
18
25
  ...tableOptions,
19
26
  _features: {
@@ -21,27 +28,38 @@ function useTable(tableOptions, selector = () => ({})) {
21
28
  vueReactivityFeature
22
29
  }
23
30
  };
24
- const table = constructTable(mergeProxy(IS_REACTIVE ? getOptionsWithReactiveData(mergedOptions) : mergedOptions, { mergeOptions: (defaultOptions, newOptions) => {
31
+ const table = constructTable(mergeProxy(getOptionsWithReactiveValues(mergedOptions), { mergeOptions: (defaultOptions, newOptions) => {
25
32
  return mergeProxy(defaultOptions, newOptions);
26
33
  } }));
27
- const allState = useStore(table.store, (state) => state);
28
- const allOptions = useStore(table.optionsStore, (state) => state);
34
+ const allState = useSelector(table.store, (state) => state);
35
+ const allOptions = useSelector(table.optionsStore, (state) => state);
29
36
  watchEffect(() => {
30
37
  allState.value;
31
38
  allOptions.value;
32
39
  notifier.value++;
33
40
  });
34
- watch(() => [IS_REACTIVE ? unref(tableOptions.data) : tableOptions.data, tableOptions], () => {
35
- table.setOptions((prev) => {
36
- return mergeProxy(prev, IS_REACTIVE ? getOptionsWithReactiveData(tableOptions) : tableOptions);
37
- });
41
+ watch(() => getReactiveOptionDeps(mergedOptions), () => {
42
+ syncTableOptions(table, mergedOptions);
43
+ }, { immediate: true });
44
+ watch(() => {
45
+ const controlledState = unref(tableOptions.state);
46
+ const controlledAtoms = unref(tableOptions.atoms);
47
+ if (!controlledState) return [];
48
+ const controlledValues = [];
49
+ for (const key of Object.keys(table.initialState)) {
50
+ if (!(key in controlledState) || (controlledAtoms === null || controlledAtoms === void 0 ? void 0 : controlledAtoms[key]) !== void 0) continue;
51
+ controlledValues.push(controlledState[key]);
52
+ }
53
+ return controlledValues;
54
+ }, (controlledValues) => {
55
+ if (controlledValues.length > 0) syncTableOptions(table, mergedOptions);
38
56
  }, { immediate: true });
39
- table.Subscribe = function Subscribe(props) {
40
- const selected = useStore(table.store, props.selector);
57
+ table.Subscribe = ((props) => {
58
+ const selected = useSelector(props.atom !== void 0 ? props.atom : table.store, props.atom !== void 0 ? props.selector ?? ((x) => x) : props.selector, { compare: shallow });
41
59
  if (typeof props.children === "function") return props.children(selected.value);
42
60
  return props.children;
43
- };
44
- const stateStore = useStore(table.store, selector);
61
+ });
62
+ const stateStore = useSelector(table.store, selector);
45
63
  return {
46
64
  ...table,
47
65
  get state() {
@@ -1 +1 @@
1
- {"version":3,"file":"useTable.js","names":[],"sources":["../src/useTable.ts"],"sourcesContent":["import { isRef, ref, unref, watch, watchEffect } from 'vue'\nimport {\n constructReactivityFeature,\n constructTable,\n} from '@tanstack/table-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { mergeProxy } from './merge-proxy'\nimport type {\n NoInfer,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { MaybeRef, VNode } from 'vue'\n\nexport type TableOptionsWithReactiveData<\n TFeatures extends TableFeatures,\n TData extends RowData,\n> = Omit<TableOptions<TFeatures, TData>, 'data'> & {\n data: MaybeRef<ReadonlyArray<TData>>\n}\n\nfunction getOptionsWithReactiveData<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(options: TableOptionsWithReactiveData<TFeatures, TData>) {\n return mergeProxy(options, {\n data: unref(options.data),\n })\n}\n\nexport type VueTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A Vue component that allows you to subscribe to the table state.\n *\n * This is useful for opting into state subscriptions for specific parts of the table state.\n *\n * @example\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {(state) => (\n * <tr>\n * // render the row\n * </tr>\n * )}\n * </table.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: NoInfer<TableState<TFeatures>>) => TSelected\n children:\n | ((state: Readonly<TSelected>) => VNode | Array<VNode>)\n | VNode\n | Array<VNode>\n }) => VNode | Array<VNode>\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: TableOptionsWithReactiveData<TFeatures, TData>,\n selector: (state: TableState<TFeatures>) => TSelected = () =>\n ({}) as TSelected,\n): VueTable<TFeatures, TData, TSelected> {\n const notifier = ref<number>(0)\n\n const vueReactivityFeature = constructReactivityFeature({\n stateNotifier: () => notifier.value,\n optionsNotifier: () => notifier.value,\n })\n\n const IS_REACTIVE = isRef(tableOptions.data)\n\n const mergedOptions = {\n ...tableOptions,\n _features: {\n ...tableOptions._features,\n vueReactivityFeature,\n },\n }\n\n const resolvedOptions = mergeProxy(\n IS_REACTIVE\n ? getOptionsWithReactiveData(\n mergedOptions as TableOptions<TFeatures, TData>,\n )\n : mergedOptions,\n {\n // Remove state and onStateChange - store handles it internally\n mergeOptions: (\n defaultOptions: TableOptions<TFeatures, TData>,\n newOptions: Partial<TableOptions<TFeatures, TData>>,\n ) => {\n return mergeProxy(defaultOptions, newOptions)\n },\n },\n ) as TableOptions<TFeatures, TData>\n\n const table = constructTable(resolvedOptions) as VueTable<\n TFeatures,\n TData,\n TSelected\n >\n\n const allState = useStore(table.store, (state) => state)\n const allOptions = useStore(table.optionsStore, (state) => state)\n\n watchEffect(() => {\n allState.value\n allOptions.value\n notifier.value++\n })\n\n watch(\n () =>\n [\n IS_REACTIVE ? unref(tableOptions.data) : tableOptions.data,\n tableOptions,\n ] as const,\n () => {\n table.setOptions((prev) => {\n return mergeProxy(\n prev,\n IS_REACTIVE\n ? getOptionsWithReactiveData(\n tableOptions as TableOptions<TFeatures, TData>,\n )\n : tableOptions,\n ) as TableOptions<TFeatures, TData>\n })\n },\n { immediate: true },\n )\n\n table.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: TableState<TFeatures>) => TSelected\n children:\n | ((state: Readonly<TSelected>) => VNode | Array<VNode>)\n | VNode\n | Array<VNode>\n }): VNode | Array<VNode> {\n const selected = useStore(table.store, props.selector)\n if (typeof props.children === 'function') {\n return props.children(selected.value)\n }\n return props.children\n }\n\n const stateStore = useStore(table.store, selector)\n\n return {\n ...table,\n get state() {\n return stateStore.value\n },\n } as VueTable<TFeatures, TData, TSelected>\n}\n"],"mappings":";;;;;;AAwBA,SAAS,2BAGP,SAAyD;AACzD,QAAO,WAAW,SAAS,EACzB,MAAM,MAAM,QAAQ,KAAK,EAC1B,CAAC;;AAwCJ,SAAgB,SAKd,cACA,kBACG,EAAE,GACkC;CACvC,MAAM,WAAW,IAAY,EAAE;CAE/B,MAAM,uBAAuB,2BAA2B;EACtD,qBAAqB,SAAS;EAC9B,uBAAuB,SAAS;EACjC,CAAC;CAEF,MAAM,cAAc,MAAM,aAAa,KAAK;CAE5C,MAAM,gBAAgB;EACpB,GAAG;EACH,WAAW;GACT,GAAG,aAAa;GAChB;GACD;EACF;CAmBD,MAAM,QAAQ,eAjBU,WACtB,cACI,2BACE,cACD,GACD,eACJ,EAEE,eACE,gBACA,eACG;AACH,SAAO,WAAW,gBAAgB,WAAW;IAEhD,CACF,CAE4C;CAM7C,MAAM,WAAW,SAAS,MAAM,QAAQ,UAAU,MAAM;CACxD,MAAM,aAAa,SAAS,MAAM,eAAe,UAAU,MAAM;AAEjE,mBAAkB;AAChB,WAAS;AACT,aAAW;AACX,WAAS;GACT;AAEF,aAEI,CACE,cAAc,MAAM,aAAa,KAAK,GAAG,aAAa,MACtD,aACD,QACG;AACJ,QAAM,YAAY,SAAS;AACzB,UAAO,WACL,MACA,cACI,2BACE,aACD,GACD,aACL;IACD;IAEJ,EAAE,WAAW,MAAM,CACpB;AAED,OAAM,YAAY,SAAS,UAAqB,OAMvB;EACvB,MAAM,WAAW,SAAS,MAAM,OAAO,MAAM,SAAS;AACtD,MAAI,OAAO,MAAM,aAAa,WAC5B,QAAO,MAAM,SAAS,SAAS,MAAM;AAEvC,SAAO,MAAM;;CAGf,MAAM,aAAa,SAAS,MAAM,OAAO,SAAS;AAElD,QAAO;EACL,GAAG;EACH,IAAI,QAAQ;AACV,UAAO,WAAW;;EAErB"}
1
+ {"version":3,"file":"useTable.js","names":[],"sources":["../src/useTable.ts"],"sourcesContent":["import { isRef, ref, unref, watch, watchEffect } from 'vue'\nimport {\n constructReactivityFeature,\n constructTable,\n} from '@tanstack/table-core'\nimport { shallow, useSelector } from '@tanstack/vue-store'\nimport { mergeProxy } from './merge-proxy'\nimport type { Atom, ReadonlyAtom } from '@tanstack/vue-store'\nimport type {\n NoInfer,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { MaybeRef, VNode } from 'vue'\n\nexport type TableOptionsWithReactiveData<\n TFeatures extends TableFeatures,\n TData extends RowData,\n> = {\n [K in keyof TableOptions<TFeatures, TData>]: K extends 'data'\n ? MaybeRef<ReadonlyArray<TData>>\n : MaybeRef<TableOptions<TFeatures, TData>[K]>\n}\n\nfunction getOptionsWithReactiveValues<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(options: TableOptionsWithReactiveData<TFeatures, TData>) {\n const resolvedOptions: Record<string, unknown> = {}\n\n for (const key of Object.keys(options)) {\n resolvedOptions[key] = unref(\n options[key as keyof TableOptionsWithReactiveData<TFeatures, TData>],\n )\n }\n\n return mergeProxy(options, resolvedOptions)\n}\n\nfunction getReactiveOptionDeps<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(options: TableOptionsWithReactiveData<TFeatures, TData>) {\n return Object.keys(options).map((key) =>\n unref(options[key as keyof TableOptionsWithReactiveData<TFeatures, TData>]),\n )\n}\n\nexport type VueTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * Store mode: `selector` required. Atom mode: pass `atom`; omit `selector` for the\n * whole slice (identity), or pass `selector` to project. Split overloads so atom-only\n * infers `TAtomValue` for `children` (see React `Subscribe`).\n */\n Subscribe: {\n <TAtomValue>(props: {\n atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>\n selector?: undefined\n children:\n | ((state: Readonly<TAtomValue>) => VNode | Array<VNode>)\n | VNode\n | Array<VNode>\n }): VNode | Array<VNode>\n <TAtomValue, TSubSelected>(props: {\n atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>\n selector: (state: TAtomValue) => TSubSelected\n children:\n | ((state: Readonly<TSubSelected>) => VNode | Array<VNode>)\n | VNode\n | Array<VNode>\n }): VNode | Array<VNode>\n <TSubSelected>(props: {\n selector: (state: NoInfer<TableState<TFeatures>>) => TSubSelected\n children:\n | ((state: Readonly<TSubSelected>) => VNode | Array<VNode>)\n | VNode\n | Array<VNode>\n }): VNode | Array<VNode>\n }\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:\n | TableOptions<TFeatures, TData>\n | TableOptionsWithReactiveData<TFeatures, TData>,\n selector: (state: TableState<TFeatures>) => TSelected = () =>\n ({}) as TSelected,\n): VueTable<TFeatures, TData, TSelected> {\n const notifier = ref<number>(0)\n\n const syncTableOptions = (\n table: Table<TFeatures, TData>,\n options: TableOptionsWithReactiveData<TFeatures, TData>,\n ) => {\n table.setOptions(\n () =>\n getOptionsWithReactiveValues(options) as TableOptions<TFeatures, TData>,\n )\n }\n\n const vueReactivityFeature = constructReactivityFeature({\n stateNotifier: () => notifier.value,\n optionsNotifier: () => notifier.value,\n })\n\n const mergedOptions = {\n ...tableOptions,\n _features: {\n ...tableOptions._features,\n vueReactivityFeature,\n },\n }\n\n const resolvedOptions = mergeProxy(\n getOptionsWithReactiveValues(\n mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,\n ),\n {\n // Remove state and onStateChange - store handles it internally\n mergeOptions: (\n defaultOptions: TableOptions<TFeatures, TData>,\n newOptions: Partial<TableOptions<TFeatures, TData>>,\n ) => {\n return mergeProxy(defaultOptions, newOptions)\n },\n },\n ) as TableOptions<TFeatures, TData>\n\n const table = constructTable(resolvedOptions) as VueTable<\n TFeatures,\n TData,\n TSelected\n >\n\n const allState = useSelector(table.store, (state) => state)\n const allOptions = useSelector(table.optionsStore, (state) => state)\n\n watchEffect(() => {\n allState.value\n allOptions.value\n notifier.value++\n })\n\n watch(\n () =>\n getReactiveOptionDeps(\n mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,\n ),\n () => {\n syncTableOptions(\n table,\n mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,\n )\n },\n { immediate: true },\n )\n\n watch(\n () => {\n const controlledState = unref(tableOptions.state) as\n | Record<string, unknown>\n | undefined\n const controlledAtoms = unref(tableOptions.atoms) as\n | Record<string, unknown>\n | undefined\n\n if (!controlledState) {\n return []\n }\n\n const controlledValues: Array<unknown> = []\n\n for (const key of Object.keys(table.initialState)) {\n if (!(key in controlledState) || controlledAtoms?.[key] !== undefined) {\n continue\n }\n\n // Reading only controlled state slices here lets Vue subscribe to\n // getters/computed values that are passed through `options.state`.\n controlledValues.push(controlledState[key])\n }\n\n return controlledValues\n },\n (controlledValues) => {\n if (controlledValues.length > 0) {\n syncTableOptions(\n table,\n mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,\n )\n }\n },\n { immediate: true },\n )\n\n table.Subscribe = ((props: {\n atom?: Atom<unknown> | ReadonlyAtom<unknown>\n selector?: ((state: unknown) => unknown) | undefined\n children:\n | ((state: Readonly<unknown>) => VNode | Array<VNode>)\n | VNode\n | Array<VNode>\n }) => {\n const source = props.atom !== undefined ? props.atom : table.store\n const selectFn =\n props.atom !== undefined\n ? (props.selector ?? ((x: unknown) => x))\n : props.selector\n const selected = useSelector(source as never, selectFn as never, {\n compare: shallow,\n })\n if (typeof props.children === 'function') {\n return props.children(selected.value as Readonly<unknown>)\n }\n return props.children\n }) as VueTable<TFeatures, TData, TSelected>['Subscribe']\n\n const stateStore = useSelector(table.store, selector)\n\n return {\n ...table,\n get state() {\n return stateStore.value\n },\n } as VueTable<TFeatures, TData, TSelected>\n}\n"],"mappings":";;;;;;AA2BA,SAAS,6BAGP,SAAyD;CACzD,MAAM,kBAA2C,EAAE;AAEnD,MAAK,MAAM,OAAO,OAAO,KAAK,QAAQ,CACpC,iBAAgB,OAAO,MACrB,QAAQ,KACT;AAGH,QAAO,WAAW,SAAS,gBAAgB;;AAG7C,SAAS,sBAGP,SAAyD;AACzD,QAAO,OAAO,KAAK,QAAQ,CAAC,KAAK,QAC/B,MAAM,QAAQ,KAA6D,CAC5E;;AAiDH,SAAgB,SAKd,cAGA,kBACG,EAAE,GACkC;CACvC,MAAM,WAAW,IAAY,EAAE;CAE/B,MAAM,oBACJ,OACA,YACG;AACH,QAAM,iBAEF,6BAA6B,QAAQ,CACxC;;CAGH,MAAM,uBAAuB,2BAA2B;EACtD,qBAAqB,SAAS;EAC9B,uBAAuB,SAAS;EACjC,CAAC;CAEF,MAAM,gBAAgB;EACpB,GAAG;EACH,WAAW;GACT,GAAG,aAAa;GAChB;GACD;EACF;CAiBD,MAAM,QAAQ,eAfU,WACtB,6BACE,cACD,EACD,EAEE,eACE,gBACA,eACG;AACH,SAAO,WAAW,gBAAgB,WAAW;IAEhD,CACF,CAE4C;CAM7C,MAAM,WAAW,YAAY,MAAM,QAAQ,UAAU,MAAM;CAC3D,MAAM,aAAa,YAAY,MAAM,eAAe,UAAU,MAAM;AAEpE,mBAAkB;AAChB,WAAS;AACT,aAAW;AACX,WAAS;GACT;AAEF,aAEI,sBACE,cACD,QACG;AACJ,mBACE,OACA,cACD;IAEH,EAAE,WAAW,MAAM,CACpB;AAED,aACQ;EACJ,MAAM,kBAAkB,MAAM,aAAa,MAAM;EAGjD,MAAM,kBAAkB,MAAM,aAAa,MAAM;AAIjD,MAAI,CAAC,gBACH,QAAO,EAAE;EAGX,MAAM,mBAAmC,EAAE;AAE3C,OAAK,MAAM,OAAO,OAAO,KAAK,MAAM,aAAa,EAAE;AACjD,OAAI,EAAE,OAAO,uFAAoB,gBAAkB,UAAS,OAC1D;AAKF,oBAAiB,KAAK,gBAAgB,KAAK;;AAG7C,SAAO;KAER,qBAAqB;AACpB,MAAI,iBAAiB,SAAS,EAC5B,kBACE,OACA,cACD;IAGL,EAAE,WAAW,MAAM,CACpB;AAED,OAAM,cAAc,UAOd;EAMJ,MAAM,WAAW,YALF,MAAM,SAAS,SAAY,MAAM,OAAO,MAAM,OAE3D,MAAM,SAAS,SACV,MAAM,cAAc,MAAe,KACpC,MAAM,UACqD,EAC/D,SAAS,SACV,CAAC;AACF,MAAI,OAAO,MAAM,aAAa,WAC5B,QAAO,MAAM,SAAS,SAAS,MAA2B;AAE5D,SAAO,MAAM;;CAGf,MAAM,aAAa,YAAY,MAAM,OAAO,SAAS;AAErD,QAAO;EACL,GAAG;EACH,IAAI,QAAQ;AACV,UAAO,WAAW;;EAErB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/vue-table",
3
- "version": "9.0.0-alpha.32",
3
+ "version": "9.0.0-alpha.34",
4
4
  "description": "Headless UI for building powerful tables & datagrids for Vue.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -29,6 +29,10 @@
29
29
  "import": "./dist/index.js",
30
30
  "require": "./dist/index.cjs"
31
31
  },
32
+ "./static-functions": {
33
+ "import": "./dist/static-functions.js",
34
+ "require": "./dist/static-functions.cjs"
35
+ },
32
36
  "./package.json": "./package.json"
33
37
  },
34
38
  "sideEffects": false,
@@ -40,11 +44,11 @@
40
44
  "src"
41
45
  ],
42
46
  "dependencies": {
43
- "@tanstack/vue-store": "^0.9.3",
44
- "@tanstack/table-core": "9.0.0-alpha.32"
47
+ "@tanstack/vue-store": "^0.11.0",
48
+ "@tanstack/table-core": "9.0.0-alpha.34"
45
49
  },
46
50
  "devDependencies": {
47
- "@vitejs/plugin-vue": "^6.0.5",
51
+ "@vitejs/plugin-vue": "^6.0.6",
48
52
  "eslint-plugin-vue": "^10.8.0",
49
53
  "vue": "^3.5.32"
50
54
  },
@@ -1,5 +1,5 @@
1
1
  import { createColumnHelper as coreCreateColumnHelper } from '@tanstack/table-core'
2
- import { useStore } from '@tanstack/vue-store'
2
+ import { useSelector } from '@tanstack/vue-store'
3
3
  import { defineComponent, h, inject, provide } from 'vue'
4
4
  import { FlexRender } from './FlexRender'
5
5
  import { mergeProxy } from './merge-proxy'
@@ -424,7 +424,7 @@ export function createTableHook<
424
424
  setup(props, { slots }) {
425
425
  provide(TableContext, table)
426
426
  const selected = props.selector
427
- ? useStore(table.store, props.selector)
427
+ ? useSelector(table.store, props.selector)
428
428
  : undefined
429
429
 
430
430
  return () => {
@@ -455,7 +455,7 @@ export function createTableHook<
455
455
  provide(CellContext, cell)
456
456
 
457
457
  const selected = props.selector
458
- ? useStore(table.store, props.selector)
458
+ ? useSelector(table.store, props.selector)
459
459
  : undefined
460
460
 
461
461
  const extendedCell = Object.assign(cell, {
@@ -492,7 +492,7 @@ export function createTableHook<
492
492
  provide(HeaderContext, header)
493
493
 
494
494
  const selected = props.selector
495
- ? useStore(table.store, props.selector)
495
+ ? useSelector(table.store, props.selector)
496
496
  : undefined
497
497
 
498
498
  const extendedHeader = Object.assign(header, {
@@ -529,7 +529,7 @@ export function createTableHook<
529
529
  provide(HeaderContext, header)
530
530
 
531
531
  const selected = props.selector
532
- ? useStore(table.store, props.selector)
532
+ ? useSelector(table.store, props.selector)
533
533
  : undefined
534
534
 
535
535
  const extendedHeader = Object.assign(header, {
@@ -3,15 +3,18 @@ function trueFn() {
3
3
  }
4
4
 
5
5
  const $PROXY = Symbol('merge-proxy')
6
+ const $SOURCES = Symbol('merge-proxy-sources')
6
7
 
7
8
  // https://github.com/solidjs/solid/blob/c20ca4fd8c36bc0522fedb2c7f38a110b7ee2663/packages/solid/src/render/component.ts#L51-L118
8
9
  const propTraps: ProxyHandler<{
9
10
  get: (k: string | number | symbol) => any
10
11
  has: (k: string | number | symbol) => boolean
11
12
  keys: () => Array<string>
13
+ sources: Array<any>
12
14
  }> = {
13
15
  get(_, property, receiver) {
14
16
  if (property === $PROXY) return receiver
17
+ if (property === $SOURCES) return _.sources
15
18
  return _.get(property)
16
19
  },
17
20
  has(_, property) {
@@ -55,23 +58,37 @@ function resolveSource(s: any) {
55
58
 
56
59
  export function mergeProxy<T extends Array<any>>(...sources: T): MergeProxy<T>
57
60
  export function mergeProxy(...sources: any): any {
61
+ const flattenedSources = sources.flatMap((source: any) => {
62
+ if (
63
+ typeof source === 'object' &&
64
+ source !== null &&
65
+ $SOURCES in source &&
66
+ Array.isArray(source[$SOURCES])
67
+ ) {
68
+ return source[$SOURCES]
69
+ }
70
+
71
+ return [source]
72
+ })
73
+
58
74
  return new Proxy(
59
75
  {
76
+ sources: flattenedSources,
60
77
  get(property: string | number | symbol) {
61
- for (let i = sources.length - 1; i >= 0; i--) {
62
- const v = resolveSource(sources[i])[property]
78
+ for (let i = flattenedSources.length - 1; i >= 0; i--) {
79
+ const v = resolveSource(flattenedSources[i])[property]
63
80
  if (v !== undefined) return v
64
81
  }
65
82
  },
66
83
  has(property: string | number | symbol) {
67
- for (let i = sources.length - 1; i >= 0; i--) {
68
- if (property in resolveSource(sources[i])) return true
84
+ for (let i = flattenedSources.length - 1; i >= 0; i--) {
85
+ if (property in resolveSource(flattenedSources[i])) return true
69
86
  }
70
87
  return false
71
88
  },
72
89
  keys() {
73
90
  const keys = []
74
- for (const source of sources)
91
+ for (const source of flattenedSources)
75
92
  keys.push(...Object.keys(resolveSource(source)))
76
93
  return [...Array.from(new Set(keys))]
77
94
  },
@@ -0,0 +1 @@
1
+ export * from '@tanstack/table-core/static-functions'
package/src/useTable.ts CHANGED
@@ -3,8 +3,9 @@ import {
3
3
  constructReactivityFeature,
4
4
  constructTable,
5
5
  } from '@tanstack/table-core'
6
- import { useStore } from '@tanstack/vue-store'
6
+ import { shallow, useSelector } from '@tanstack/vue-store'
7
7
  import { mergeProxy } from './merge-proxy'
8
+ import type { Atom, ReadonlyAtom } from '@tanstack/vue-store'
8
9
  import type {
9
10
  NoInfer,
10
11
  RowData,
@@ -18,17 +19,34 @@ import type { MaybeRef, VNode } from 'vue'
18
19
  export type TableOptionsWithReactiveData<
19
20
  TFeatures extends TableFeatures,
20
21
  TData extends RowData,
21
- > = Omit<TableOptions<TFeatures, TData>, 'data'> & {
22
- data: MaybeRef<ReadonlyArray<TData>>
22
+ > = {
23
+ [K in keyof TableOptions<TFeatures, TData>]: K extends 'data'
24
+ ? MaybeRef<ReadonlyArray<TData>>
25
+ : MaybeRef<TableOptions<TFeatures, TData>[K]>
23
26
  }
24
27
 
25
- function getOptionsWithReactiveData<
28
+ function getOptionsWithReactiveValues<
26
29
  TFeatures extends TableFeatures,
27
30
  TData extends RowData,
28
31
  >(options: TableOptionsWithReactiveData<TFeatures, TData>) {
29
- return mergeProxy(options, {
30
- data: unref(options.data),
31
- })
32
+ const resolvedOptions: Record<string, unknown> = {}
33
+
34
+ for (const key of Object.keys(options)) {
35
+ resolvedOptions[key] = unref(
36
+ options[key as keyof TableOptionsWithReactiveData<TFeatures, TData>],
37
+ )
38
+ }
39
+
40
+ return mergeProxy(options, resolvedOptions)
41
+ }
42
+
43
+ function getReactiveOptionDeps<
44
+ TFeatures extends TableFeatures,
45
+ TData extends RowData,
46
+ >(options: TableOptionsWithReactiveData<TFeatures, TData>) {
47
+ return Object.keys(options).map((key) =>
48
+ unref(options[key as keyof TableOptionsWithReactiveData<TFeatures, TData>]),
49
+ )
32
50
  }
33
51
 
34
52
  export type VueTable<
@@ -37,26 +55,35 @@ export type VueTable<
37
55
  TSelected = {},
38
56
  > = Table<TFeatures, TData> & {
39
57
  /**
40
- * A Vue component that allows you to subscribe to the table state.
41
- *
42
- * This is useful for opting into state subscriptions for specific parts of the table state.
43
- *
44
- * @example
45
- * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
46
- * {(state) => (
47
- * <tr>
48
- * // render the row
49
- * </tr>
50
- * )}
51
- * </table.Subscribe>
58
+ * Store mode: `selector` required. Atom mode: pass `atom`; omit `selector` for the
59
+ * whole slice (identity), or pass `selector` to project. Split overloads so atom-only
60
+ * infers `TAtomValue` for `children` (see React `Subscribe`).
52
61
  */
53
- Subscribe: <TSelected>(props: {
54
- selector: (state: NoInfer<TableState<TFeatures>>) => TSelected
55
- children:
56
- | ((state: Readonly<TSelected>) => VNode | Array<VNode>)
57
- | VNode
58
- | Array<VNode>
59
- }) => VNode | Array<VNode>
62
+ Subscribe: {
63
+ <TAtomValue>(props: {
64
+ atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>
65
+ selector?: undefined
66
+ children:
67
+ | ((state: Readonly<TAtomValue>) => VNode | Array<VNode>)
68
+ | VNode
69
+ | Array<VNode>
70
+ }): VNode | Array<VNode>
71
+ <TAtomValue, TSubSelected>(props: {
72
+ atom: Atom<TAtomValue> | ReadonlyAtom<TAtomValue>
73
+ selector: (state: TAtomValue) => TSubSelected
74
+ children:
75
+ | ((state: Readonly<TSubSelected>) => VNode | Array<VNode>)
76
+ | VNode
77
+ | Array<VNode>
78
+ }): VNode | Array<VNode>
79
+ <TSubSelected>(props: {
80
+ selector: (state: NoInfer<TableState<TFeatures>>) => TSubSelected
81
+ children:
82
+ | ((state: Readonly<TSubSelected>) => VNode | Array<VNode>)
83
+ | VNode
84
+ | Array<VNode>
85
+ }): VNode | Array<VNode>
86
+ }
60
87
  /**
61
88
  * 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`.
62
89
  *
@@ -73,19 +100,29 @@ export function useTable<
73
100
  TData extends RowData,
74
101
  TSelected = {},
75
102
  >(
76
- tableOptions: TableOptionsWithReactiveData<TFeatures, TData>,
103
+ tableOptions:
104
+ | TableOptions<TFeatures, TData>
105
+ | TableOptionsWithReactiveData<TFeatures, TData>,
77
106
  selector: (state: TableState<TFeatures>) => TSelected = () =>
78
107
  ({}) as TSelected,
79
108
  ): VueTable<TFeatures, TData, TSelected> {
80
109
  const notifier = ref<number>(0)
81
110
 
111
+ const syncTableOptions = (
112
+ table: Table<TFeatures, TData>,
113
+ options: TableOptionsWithReactiveData<TFeatures, TData>,
114
+ ) => {
115
+ table.setOptions(
116
+ () =>
117
+ getOptionsWithReactiveValues(options) as TableOptions<TFeatures, TData>,
118
+ )
119
+ }
120
+
82
121
  const vueReactivityFeature = constructReactivityFeature({
83
122
  stateNotifier: () => notifier.value,
84
123
  optionsNotifier: () => notifier.value,
85
124
  })
86
125
 
87
- const IS_REACTIVE = isRef(tableOptions.data)
88
-
89
126
  const mergedOptions = {
90
127
  ...tableOptions,
91
128
  _features: {
@@ -95,11 +132,9 @@ export function useTable<
95
132
  }
96
133
 
97
134
  const resolvedOptions = mergeProxy(
98
- IS_REACTIVE
99
- ? getOptionsWithReactiveData(
100
- mergedOptions as TableOptions<TFeatures, TData>,
101
- )
102
- : mergedOptions,
135
+ getOptionsWithReactiveValues(
136
+ mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,
137
+ ),
103
138
  {
104
139
  // Remove state and onStateChange - store handles it internally
105
140
  mergeOptions: (
@@ -117,8 +152,8 @@ export function useTable<
117
152
  TSelected
118
153
  >
119
154
 
120
- const allState = useStore(table.store, (state) => state)
121
- const allOptions = useStore(table.optionsStore, (state) => state)
155
+ const allState = useSelector(table.store, (state) => state)
156
+ const allOptions = useSelector(table.optionsStore, (state) => state)
122
157
 
123
158
  watchEffect(() => {
124
159
  allState.value
@@ -128,40 +163,79 @@ export function useTable<
128
163
 
129
164
  watch(
130
165
  () =>
131
- [
132
- IS_REACTIVE ? unref(tableOptions.data) : tableOptions.data,
133
- tableOptions,
134
- ] as const,
166
+ getReactiveOptionDeps(
167
+ mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,
168
+ ),
135
169
  () => {
136
- table.setOptions((prev) => {
137
- return mergeProxy(
138
- prev,
139
- IS_REACTIVE
140
- ? getOptionsWithReactiveData(
141
- tableOptions as TableOptions<TFeatures, TData>,
142
- )
143
- : tableOptions,
144
- ) as TableOptions<TFeatures, TData>
145
- })
170
+ syncTableOptions(
171
+ table,
172
+ mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,
173
+ )
146
174
  },
147
175
  { immediate: true },
148
176
  )
149
177
 
150
- table.Subscribe = function Subscribe<TSelected>(props: {
151
- selector: (state: TableState<TFeatures>) => TSelected
178
+ watch(
179
+ () => {
180
+ const controlledState = unref(tableOptions.state) as
181
+ | Record<string, unknown>
182
+ | undefined
183
+ const controlledAtoms = unref(tableOptions.atoms) as
184
+ | Record<string, unknown>
185
+ | undefined
186
+
187
+ if (!controlledState) {
188
+ return []
189
+ }
190
+
191
+ const controlledValues: Array<unknown> = []
192
+
193
+ for (const key of Object.keys(table.initialState)) {
194
+ if (!(key in controlledState) || controlledAtoms?.[key] !== undefined) {
195
+ continue
196
+ }
197
+
198
+ // Reading only controlled state slices here lets Vue subscribe to
199
+ // getters/computed values that are passed through `options.state`.
200
+ controlledValues.push(controlledState[key])
201
+ }
202
+
203
+ return controlledValues
204
+ },
205
+ (controlledValues) => {
206
+ if (controlledValues.length > 0) {
207
+ syncTableOptions(
208
+ table,
209
+ mergedOptions as TableOptionsWithReactiveData<TFeatures, TData>,
210
+ )
211
+ }
212
+ },
213
+ { immediate: true },
214
+ )
215
+
216
+ table.Subscribe = ((props: {
217
+ atom?: Atom<unknown> | ReadonlyAtom<unknown>
218
+ selector?: ((state: unknown) => unknown) | undefined
152
219
  children:
153
- | ((state: Readonly<TSelected>) => VNode | Array<VNode>)
220
+ | ((state: Readonly<unknown>) => VNode | Array<VNode>)
154
221
  | VNode
155
222
  | Array<VNode>
156
- }): VNode | Array<VNode> {
157
- const selected = useStore(table.store, props.selector)
223
+ }) => {
224
+ const source = props.atom !== undefined ? props.atom : table.store
225
+ const selectFn =
226
+ props.atom !== undefined
227
+ ? (props.selector ?? ((x: unknown) => x))
228
+ : props.selector
229
+ const selected = useSelector(source as never, selectFn as never, {
230
+ compare: shallow,
231
+ })
158
232
  if (typeof props.children === 'function') {
159
- return props.children(selected.value)
233
+ return props.children(selected.value as Readonly<unknown>)
160
234
  }
161
235
  return props.children
162
- }
236
+ }) as VueTable<TFeatures, TData, TSelected>['Subscribe']
163
237
 
164
- const stateStore = useStore(table.store, selector)
238
+ const stateStore = useSelector(table.store, selector)
165
239
 
166
240
  return {
167
241
  ...table,