@tanstack/react-table 9.0.0-alpha.20 → 9.0.0-alpha.22

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.
@@ -47,6 +47,9 @@ export type ReactTable<TFeatures extends TableFeatures, TData extends RowData, T
47
47
  *
48
48
  * console.log(table.state.globalFilter)
49
49
  */
50
- readonly state: Readonly<TSelected>;
50
+ readonly state: Readonly<TSelected> & {
51
+ columns: TableOptions<TFeatures, TData>['columns'];
52
+ data: TableOptions<TFeatures, TData>['data'];
53
+ };
51
54
  };
52
55
  export declare function useTable<TFeatures extends TableFeatures, TData extends RowData, TSelected = {}>(tableOptions: TableOptions<TFeatures, TData>, selector?: (state: TableState<TFeatures>) => TSelected): ReactTable<TFeatures, TData, TSelected>;
@@ -1,10 +1,9 @@
1
1
  "use client";
2
- import { useState, useLayoutEffect, useEffect, useMemo } from "react";
2
+ import { useState, useMemo } from "react";
3
3
  import { constructTable } from "@tanstack/table-core";
4
4
  import { useStore, shallow } from "@tanstack/react-store";
5
5
  import { FlexRender } from "./FlexRender.js";
6
6
  import { Subscribe } from "./Subscribe.js";
7
- const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
8
7
  function useTable(tableOptions, selector = () => ({})) {
9
8
  const [table] = useState(() => {
10
9
  const tableInstance = constructTable(tableOptions);
@@ -18,21 +17,12 @@ function useTable(tableOptions, selector = () => ({})) {
18
17
  ...prev,
19
18
  ...tableOptions
20
19
  }));
21
- useIsomorphicLayoutEffect(() => {
22
- queueMicrotask(() => {
23
- table.baseStore.setState((prev) => ({
24
- ...prev
25
- }));
26
- });
27
- }, [
28
- table.options.columns,
29
- // re-render when columns change
30
- table.options.data,
31
- // re-render when data changes
32
- table.options.state
33
- // sync react state to the table store
34
- ]);
35
- const state = useStore(table.store, selector, shallow);
20
+ const selectorWithDataAndColumns = (state2) => ({
21
+ columns: tableOptions.columns,
22
+ data: tableOptions.data,
23
+ ...selector(state2)
24
+ });
25
+ const state = useStore(table.store, selectorWithDataAndColumns, shallow);
36
26
  return useMemo(
37
27
  () => ({
38
28
  ...table,
@@ -1 +1 @@
1
- {"version":3,"file":"useTable.js","sources":["../../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useEffect, useLayoutEffect, useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useStore } from '@tanstack/react-store'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n NoInfer,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribeProps } from './Subscribe'\n\nconst useIsomorphicLayoutEffect =\n typeof window !== 'undefined' ? useLayoutEffect : useEffect\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 * @example\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => ( // important to include `{() => {()}}` syntax\n * <tr key={row.id}>\n // render the row\n * </tr>\n * ))}\n * </table.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: NoInfer<TableState<TFeatures>>) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n }) => ReturnType<FunctionComponent>\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(tableOptions) as ReactTable<\n TFeatures,\n TData,\n TSelected\n >\n\n tableInstance.Subscribe = function SubscribeBound<TSelected>(\n props: Omit<SubscribeProps<TFeatures, TData, TSelected>, 'table'>,\n ) {\n return Subscribe({ ...props, table: tableInstance })\n }\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 useIsomorphicLayoutEffect(() => {\n // prevent race condition between table.setOptions and table.baseStore.setState\n queueMicrotask(() => {\n table.baseStore.setState((prev) => ({\n ...prev,\n }))\n })\n }, [\n table.options.columns, // re-render when columns change\n table.options.data, // re-render when data changes\n table.options.state, // sync react state to the table store\n ])\n\n const state = useStore(table.store, selector, shallow)\n\n return useMemo(\n () => ({\n ...table,\n state,\n }),\n [state, table],\n )\n}\n"],"names":[],"mappings":";;;;;;AAoBA;AA0DO;AASL;AACE;AAMA;AAGE;AAAmD;AAGrD;AAEA;AAAO;AAIT;AAA4B;AACvB;AACA;AAGL;AAEE;AACE;AAAoC;AAC/B;AACH;AACH;AACA;AACa;AAAA;AACA;AAAA;AACA;AAAA;AAGhB;AAEA;AAAO;AACE;AACF;AACH;AAAA;AAEW;AAEjB;;;;"}
1
+ {"version":3,"file":"useTable.js","sources":["../../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useStore } from '@tanstack/react-store'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n NoInfer,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribeProps } from './Subscribe'\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 * @example\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => ( // important to include `{() => {()}}` syntax\n * <tr key={row.id}>\n // render the row\n * </tr>\n * ))}\n * </table.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: NoInfer<TableState<TFeatures>>) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n }) => ReturnType<FunctionComponent>\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 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): ReactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable(tableOptions) as ReactTable<\n TFeatures,\n TData,\n TSelected\n >\n\n tableInstance.Subscribe = function SubscribeBound<TSelected>(\n props: Omit<SubscribeProps<TFeatures, TData, TSelected>, 'table'>,\n ) {\n return Subscribe({ ...props, table: tableInstance })\n }\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 = useStore(table.store, selectorWithDataAndColumns, shallow)\n\n return useMemo(\n () => ({\n ...table,\n state,\n }),\n [state, table],\n ) as ReactTable<TFeatures, TData, TSelected>\n}\n"],"names":[],"mappings":";;;;;;AA8EO;AASL;AACE;AAMA;AAGE;AAAmD;AAGrD;AAEA;AAAO;AAIT;AAA4B;AACvB;AACA;AAGL;AAAsE;AAC9C;AACH;AACF;AAGnB;AAEA;AAAO;AACE;AACF;AACH;AAAA;AAEW;AAEjB;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-table",
3
- "version": "9.0.0-alpha.20",
3
+ "version": "9.0.0-alpha.22",
4
4
  "description": "Headless UI for building powerful tables & datagrids for React.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  ],
50
50
  "dependencies": {
51
51
  "@tanstack/react-store": "^0.9.2",
52
- "@tanstack/table-core": "9.0.0-alpha.16"
52
+ "@tanstack/table-core": "9.0.0-alpha.21"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@eslint-react/eslint-plugin": "^2.13.0",
package/src/useTable.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  'use client'
2
2
 
3
- import { useEffect, useLayoutEffect, useMemo, useState } from 'react'
3
+ import { useMemo, useState } from 'react'
4
4
  import { constructTable } from '@tanstack/table-core'
5
5
  import { shallow, useStore } from '@tanstack/react-store'
6
6
  import { FlexRender } from './FlexRender'
@@ -18,9 +18,6 @@ import type { FunctionComponent, ReactNode } from 'react'
18
18
  import type { FlexRenderProps } from './FlexRender'
19
19
  import type { SubscribeProps } from './Subscribe'
20
20
 
21
- const useIsomorphicLayoutEffect =
22
- typeof window !== 'undefined' ? useLayoutEffect : useEffect
23
-
24
21
  export type ReactTable<
25
22
  TFeatures extends TableFeatures,
26
23
  TData extends RowData,
@@ -73,7 +70,10 @@ export type ReactTable<
73
70
  *
74
71
  * console.log(table.state.globalFilter)
75
72
  */
76
- readonly state: Readonly<TSelected>
73
+ readonly state: Readonly<TSelected> & {
74
+ columns: TableOptions<TFeatures, TData>['columns']
75
+ data: TableOptions<TFeatures, TData>['data']
76
+ }
77
77
  }
78
78
 
79
79
  export function useTable<
@@ -109,20 +109,13 @@ export function useTable<
109
109
  ...tableOptions,
110
110
  }))
111
111
 
112
- useIsomorphicLayoutEffect(() => {
113
- // prevent race condition between table.setOptions and table.baseStore.setState
114
- queueMicrotask(() => {
115
- table.baseStore.setState((prev) => ({
116
- ...prev,
117
- }))
118
- })
119
- }, [
120
- table.options.columns, // re-render when columns change
121
- table.options.data, // re-render when data changes
122
- table.options.state, // sync react state to the table store
123
- ])
112
+ const selectorWithDataAndColumns = (state: TableState<TFeatures>) => ({
113
+ columns: tableOptions.columns,
114
+ data: tableOptions.data,
115
+ ...selector(state),
116
+ })
124
117
 
125
- const state = useStore(table.store, selector, shallow)
118
+ const state = useStore(table.store, selectorWithDataAndColumns, shallow)
126
119
 
127
120
  return useMemo(
128
121
  () => ({
@@ -130,5 +123,5 @@ export function useTable<
130
123
  state,
131
124
  }),
132
125
  [state, table],
133
- )
126
+ ) as ReactTable<TFeatures, TData, TSelected>
134
127
  }