react-hook-tanstack-table 0.0.1 → 0.0.3
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/README.md +78 -0
- package/dist/TableContext.cjs +1 -6
- package/dist/TableContext.js +1 -7
- package/dist/TableContext.js.map +1 -1
- package/dist/index.cjs +1 -40
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -11
- package/dist/invariant-BzSvgtnX.cjs +1 -0
- package/dist/invariant-DhtaPrYv.js +2 -0
- package/dist/invariant-DhtaPrYv.js.map +1 -0
- package/dist/useCell.cjs +1 -53
- package/dist/useCell.d.cts +9 -9
- package/dist/useCell.d.cts.map +1 -1
- package/dist/useCell.d.ts +9 -9
- package/dist/useCell.d.ts.map +1 -1
- package/dist/useCell.js +1 -52
- package/dist/useCell.js.map +1 -1
- package/dist/useColumn.cjs +1 -54
- package/dist/useColumn.d.cts +8 -8
- package/dist/useColumn.d.cts.map +1 -1
- package/dist/useColumn.d.ts +8 -8
- package/dist/useColumn.d.ts.map +1 -1
- package/dist/useColumn.js +1 -53
- package/dist/useColumn.js.map +1 -1
- package/dist/useHeader.cjs +1 -48
- package/dist/useHeader.d.cts +8 -8
- package/dist/useHeader.d.cts.map +1 -1
- package/dist/useHeader.d.ts +8 -8
- package/dist/useHeader.d.ts.map +1 -1
- package/dist/useHeader.js +1 -47
- package/dist/useHeader.js.map +1 -1
- package/dist/useReactTable-2xEFEdvR.js +2 -0
- package/dist/{useReactTable-Y2z5JrT_.js.map → useReactTable-2xEFEdvR.js.map} +1 -1
- package/dist/useReactTable-BnYobIs-.cjs +1 -0
- package/dist/useReactTable.cjs +1 -3
- package/dist/useReactTable.js +1 -2
- package/dist/useRow.cjs +1 -54
- package/dist/useRow.d.cts +8 -8
- package/dist/useRow.d.cts.map +1 -1
- package/dist/useRow.d.ts +8 -8
- package/dist/useRow.d.ts.map +1 -1
- package/dist/useRow.js +1 -53
- package/dist/useRow.js.map +1 -1
- package/dist/useTable.cjs +1 -44
- package/dist/useTable.d.cts +5 -5
- package/dist/useTable.d.cts.map +1 -1
- package/dist/useTable.d.ts +5 -5
- package/dist/useTable.d.ts.map +1 -1
- package/dist/useTable.js +1 -43
- package/dist/useTable.js.map +1 -1
- package/dist/useTableBase-BLBisaqE.cjs +1 -0
- package/dist/useTableBase-CgF6IzQm.js +2 -0
- package/dist/useTableBase-CgF6IzQm.js.map +1 -0
- package/package.json +1 -5
- package/src/hooks/useCell.ts +41 -30
- package/src/hooks/useColumn.ts +32 -22
- package/src/hooks/useHeader.ts +32 -22
- package/src/hooks/useRow.ts +20 -14
- package/src/hooks/useTable.ts +33 -18
- package/src/index.ts +0 -1
- package/src/lib/hasTableArg.ts +1 -1
- package/src/{hooks/useTableWithSelector.ts → lib/useTableBase.ts} +5 -5
- package/README.org +0 -29
- package/dist/hasTableArg-Bu3FbcSo.js +0 -8
- package/dist/hasTableArg-Bu3FbcSo.js.map +0 -1
- package/dist/hasTableArg-dMtayKWH.cjs +0 -11
- package/dist/invariant-BgWq7zZS.js +0 -12
- package/dist/invariant-BgWq7zZS.js.map +0 -1
- package/dist/invariant-D8b385d_.cjs +0 -21
- package/dist/isShallowEqual-D6PQ041u.cjs +0 -36
- package/dist/isShallowEqual-Kynsoe7a.js +0 -33
- package/dist/isShallowEqual-Kynsoe7a.js.map +0 -1
- package/dist/runGetters-BfRTE7LZ.js +0 -16
- package/dist/runGetters-BfRTE7LZ.js.map +0 -1
- package/dist/runGetters-Q-UsPG2j.cjs +0 -19
- package/dist/useReactTable-Y2z5JrT_.js +0 -70
- package/dist/useReactTable-nxQRPiMU.cjs +0 -95
- package/dist/useTableWithSelector-Bdsf6Zxk.cjs +0 -79
- package/dist/useTableWithSelector-D3f689pF.js +0 -70
- package/dist/useTableWithSelector-D3f689pF.js.map +0 -1
- package/dist/useTableWithSelector.cjs +0 -3
- package/dist/useTableWithSelector.d.cts +0 -8
- package/dist/useTableWithSelector.d.cts.map +0 -1
- package/dist/useTableWithSelector.d.ts +0 -8
- package/dist/useTableWithSelector.d.ts.map +0 -1
- package/dist/useTableWithSelector.js +0 -2
package/dist/useTable.cjs
CHANGED
|
@@ -1,44 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,
|
|
2
|
-
const require_isShallowEqual = require("./isShallowEqual-D6PQ041u.cjs");
|
|
3
|
-
const require_useTableWithSelector = require("./useTableWithSelector-Bdsf6Zxk.cjs");
|
|
4
|
-
const require_runGetters = require("./runGetters-Q-UsPG2j.cjs");
|
|
5
|
-
let react = require("react");
|
|
6
|
-
//#region src/hooks/useTable.ts
|
|
7
|
-
const tableValuesCache = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
const tableHandlersCache = /* @__PURE__ */ new WeakMap();
|
|
9
|
-
const getTableValues = (table) => {
|
|
10
|
-
let cached = tableValuesCache.get(table.options);
|
|
11
|
-
if (!cached) {
|
|
12
|
-
let cachedHandlers = tableHandlersCache.get(table);
|
|
13
|
-
if (!cachedHandlers) {
|
|
14
|
-
cachedHandlers = {
|
|
15
|
-
toggleAllColumnsVisibilityHandler: table.getToggleAllColumnsVisibilityHandler(),
|
|
16
|
-
toggleAllPageRowsSelectedHandler: table.getToggleAllPageRowsSelectedHandler(),
|
|
17
|
-
toggleAllRowsExpandedHandler: table.getToggleAllRowsExpandedHandler(),
|
|
18
|
-
toggleAllRowsSelectedHandler: table.getToggleAllRowsSelectedHandler()
|
|
19
|
-
};
|
|
20
|
-
tableHandlersCache.set(table, cachedHandlers);
|
|
21
|
-
}
|
|
22
|
-
const { toggleAllColumnsVisibilityHandler, toggleAllPageRowsSelectedHandler, toggleAllRowsExpandedHandler, toggleAllRowsSelectedHandler } = cachedHandlers;
|
|
23
|
-
const { getColumn, getRow, ...rest } = table;
|
|
24
|
-
cached = {
|
|
25
|
-
...require_runGetters.runGetters(rest),
|
|
26
|
-
getColumn,
|
|
27
|
-
getRow,
|
|
28
|
-
toggleAllColumnsVisibilityHandler,
|
|
29
|
-
toggleAllPageRowsSelectedHandler,
|
|
30
|
-
toggleAllRowsExpandedHandler,
|
|
31
|
-
toggleAllRowsSelectedHandler
|
|
32
|
-
};
|
|
33
|
-
tableValuesCache.set(table.options, cached);
|
|
34
|
-
}
|
|
35
|
-
return cached;
|
|
36
|
-
};
|
|
37
|
-
const useTable = (table, selector = require_useTableWithSelector.identity, isEqual = require_isShallowEqual.isShallowEqual) => {
|
|
38
|
-
return require_useTableWithSelector.useTableWithSelector(table, (0, react.useCallback)((table) => selector(getTableValues(table)), [selector]), isEqual);
|
|
39
|
-
};
|
|
40
|
-
const tableHook = useTable;
|
|
41
|
-
const tableHookʹ = (table) => (selector = require_useTableWithSelector.identity, isEqual = require_isShallowEqual.isShallowEqual) => tableHook(table, selector, isEqual);
|
|
42
|
-
//#endregion
|
|
43
|
-
exports.useTable = useTable;
|
|
44
|
-
exports.useTableʹ = tableHookʹ;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./invariant-BzSvgtnX.cjs"),t=require("./useTableBase-BLBisaqE.cjs");let n=require("react");const r=new WeakMap,i=new WeakMap,a=e=>{let n=r.get(e.options);if(!n){let a=i.get(e);a||(a={toggleAllColumnsVisibilityHandler:e.getToggleAllColumnsVisibilityHandler(),toggleAllPageRowsSelectedHandler:e.getToggleAllPageRowsSelectedHandler(),toggleAllRowsExpandedHandler:e.getToggleAllRowsExpandedHandler(),toggleAllRowsSelectedHandler:e.getToggleAllRowsSelectedHandler()},i.set(e,a));let{toggleAllColumnsVisibilityHandler:o,toggleAllPageRowsSelectedHandler:s,toggleAllRowsExpandedHandler:c,toggleAllRowsSelectedHandler:l}=a,{getColumn:u,getRow:d,...f}=e;n={...t.r(f),getColumn:u,getRow:d,toggleAllColumnsVisibilityHandler:o,toggleAllPageRowsSelectedHandler:s,toggleAllRowsExpandedHandler:c,toggleAllRowsSelectedHandler:l},r.set(e.options,n)}return n},o=(...r)=>{let[i,o=t.i,s=e.r]=t.n(r)?r:[void 0,...r];return t.t(i,(0,n.useCallback)(e=>o(a(e),e),[o]),s)},s=o,c=e=>(t,n)=>s(e,t,n);exports.useTable=o,exports.useTableʹ=c;
|
package/dist/useTable.d.cts
CHANGED
|
@@ -2,10 +2,10 @@ import { n as RunGetters, t as IsEqual } from "./types-DIWB6wdO.cjs";
|
|
|
2
2
|
import { RowData, Table } from "@tanstack/table-core";
|
|
3
3
|
|
|
4
4
|
//#region src/hooks/useTable.d.ts
|
|
5
|
-
interface
|
|
6
|
-
type Selector<TData extends RowData, Selection> = (
|
|
7
|
-
declare const useTable: <TData extends RowData, Selection =
|
|
8
|
-
declare const tableHookʹ: <TData extends RowData>(table?: Table<TData> | undefined) => <Selection =
|
|
5
|
+
interface TableSnapshot<TData extends RowData> extends RunGetters<Table<TData>> {}
|
|
6
|
+
type Selector<TData extends RowData, Selection> = (tableSnapshot: TableSnapshot<TData>, table: Table<TData>) => Selection;
|
|
7
|
+
declare const useTable: <TData extends RowData, Selection = TableSnapshot<TData>>(...args: [table: Table<TData> | undefined, selector?: Selector<TData, Selection> | undefined, isEqual?: IsEqual<NoInfer<Selection>> | undefined] | [selector?: Selector<TData, Selection> | undefined, isEqual?: IsEqual<NoInfer<Selection>> | undefined]) => Selection;
|
|
8
|
+
declare const tableHookʹ: <TData extends RowData>(table?: Table<TData> | undefined) => <Selection = TableSnapshot<TData>>(selector?: Selector<TData, Selection> | undefined, isEqual?: IsEqual<NoInfer<Selection>> | undefined) => Selection;
|
|
9
9
|
//#endregion
|
|
10
|
-
export {
|
|
10
|
+
export { TableSnapshot, useTable, tableHookʹ as useTableʹ };
|
|
11
11
|
//# sourceMappingURL=useTable.d.cts.map
|
package/dist/useTable.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTable.d.cts","names":[],"sources":["../src/hooks/useTable.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"useTable.d.cts","names":[],"sources":["../src/hooks/useTable.ts"],"mappings":";;;;UAkBiB,aAAA,eAA4B,OAAA,UAAiB,UAAA,CAC5D,KAAA,CAAM,KAAA;AAAA,KAmEH,QAAA,eAAuB,OAAA,gBAC1B,aAAA,EAAe,aAAA,CAAc,KAAA,GAC9B,KAAA,EAAO,KAAA,CAAM,KAAA,MACT,SAAA;AAAA,cAEQ,QAAA,iBAA0B,OAAA,cAAqB,aAAA,CAAc,KAAA,MACrE,IAAA,GAEG,KAAA,EAAO,KAAA,CAAM,KAAA,eACb,QAAA,GAAW,QAAA,CAAS,KAAA,EAAO,SAAA,eAC3B,OAAA,GAAU,OAAA,CAAQ,OAAA,CAAQ,SAAA,mBAG1B,QAAA,GAAW,QAAA,CAAS,KAAA,EAAO,SAAA,eAC3B,OAAA,GAAU,OAAA,CAAQ,OAAA,CAAQ,SAAA,oBAE/B,SAAA;AAAA,cAiBG,UAAA,iBACW,OAAA,EAAS,KAAA,GAAQ,KAAA,CAAM,KAAA,+BACzB,aAAA,CAAc,KAAA,GACzB,QAAA,GAAW,QAAA,CAAS,KAAA,EAAO,SAAA,eAC3B,OAAA,GAAU,OAAA,CAAQ,OAAA,CAAQ,SAAA,mBACzB,SAAA"}
|
package/dist/useTable.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import { n as RunGetters, t as IsEqual } from "./types-DIWB6wdO.js";
|
|
|
2
2
|
import { RowData, Table } from "@tanstack/table-core";
|
|
3
3
|
|
|
4
4
|
//#region src/hooks/useTable.d.ts
|
|
5
|
-
interface
|
|
6
|
-
type Selector<TData extends RowData, Selection> = (
|
|
7
|
-
declare const useTable: <TData extends RowData, Selection =
|
|
8
|
-
declare const tableHookʹ: <TData extends RowData>(table?: Table<TData> | undefined) => <Selection =
|
|
5
|
+
interface TableSnapshot<TData extends RowData> extends RunGetters<Table<TData>> {}
|
|
6
|
+
type Selector<TData extends RowData, Selection> = (tableSnapshot: TableSnapshot<TData>, table: Table<TData>) => Selection;
|
|
7
|
+
declare const useTable: <TData extends RowData, Selection = TableSnapshot<TData>>(...args: [table: Table<TData> | undefined, selector?: Selector<TData, Selection> | undefined, isEqual?: IsEqual<NoInfer<Selection>> | undefined] | [selector?: Selector<TData, Selection> | undefined, isEqual?: IsEqual<NoInfer<Selection>> | undefined]) => Selection;
|
|
8
|
+
declare const tableHookʹ: <TData extends RowData>(table?: Table<TData> | undefined) => <Selection = TableSnapshot<TData>>(selector?: Selector<TData, Selection> | undefined, isEqual?: IsEqual<NoInfer<Selection>> | undefined) => Selection;
|
|
9
9
|
//#endregion
|
|
10
|
-
export {
|
|
10
|
+
export { TableSnapshot, useTable, tableHookʹ as useTableʹ };
|
|
11
11
|
//# sourceMappingURL=useTable.d.ts.map
|
package/dist/useTable.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTable.d.ts","names":[],"sources":["../src/hooks/useTable.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"useTable.d.ts","names":[],"sources":["../src/hooks/useTable.ts"],"mappings":";;;;UAkBiB,aAAA,eAA4B,OAAA,UAAiB,UAAA,CAC5D,KAAA,CAAM,KAAA;AAAA,KAmEH,QAAA,eAAuB,OAAA,gBAC1B,aAAA,EAAe,aAAA,CAAc,KAAA,GAC9B,KAAA,EAAO,KAAA,CAAM,KAAA,MACT,SAAA;AAAA,cAEQ,QAAA,iBAA0B,OAAA,cAAqB,aAAA,CAAc,KAAA,MACrE,IAAA,GAEG,KAAA,EAAO,KAAA,CAAM,KAAA,eACb,QAAA,GAAW,QAAA,CAAS,KAAA,EAAO,SAAA,eAC3B,OAAA,GAAU,OAAA,CAAQ,OAAA,CAAQ,SAAA,mBAG1B,QAAA,GAAW,QAAA,CAAS,KAAA,EAAO,SAAA,eAC3B,OAAA,GAAU,OAAA,CAAQ,OAAA,CAAQ,SAAA,oBAE/B,SAAA;AAAA,cAiBG,UAAA,iBACW,OAAA,EAAS,KAAA,GAAQ,KAAA,CAAM,KAAA,+BACzB,aAAA,CAAc,KAAA,GACzB,QAAA,GAAW,QAAA,CAAS,KAAA,EAAO,SAAA,eAC3B,OAAA,GAAU,OAAA,CAAQ,OAAA,CAAQ,SAAA,mBACzB,SAAA"}
|
package/dist/useTable.js
CHANGED
|
@@ -1,44 +1,2 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import { n as identity, t as useTableWithSelector } from "./useTableWithSelector-D3f689pF.js";
|
|
3
|
-
import { t as runGetters } from "./runGetters-BfRTE7LZ.js";
|
|
4
|
-
import { useCallback } from "react";
|
|
5
|
-
//#region src/hooks/useTable.ts
|
|
6
|
-
const tableValuesCache = /* @__PURE__ */ new WeakMap();
|
|
7
|
-
const tableHandlersCache = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
const getTableValues = (table) => {
|
|
9
|
-
let cached = tableValuesCache.get(table.options);
|
|
10
|
-
if (!cached) {
|
|
11
|
-
let cachedHandlers = tableHandlersCache.get(table);
|
|
12
|
-
if (!cachedHandlers) {
|
|
13
|
-
cachedHandlers = {
|
|
14
|
-
toggleAllColumnsVisibilityHandler: table.getToggleAllColumnsVisibilityHandler(),
|
|
15
|
-
toggleAllPageRowsSelectedHandler: table.getToggleAllPageRowsSelectedHandler(),
|
|
16
|
-
toggleAllRowsExpandedHandler: table.getToggleAllRowsExpandedHandler(),
|
|
17
|
-
toggleAllRowsSelectedHandler: table.getToggleAllRowsSelectedHandler()
|
|
18
|
-
};
|
|
19
|
-
tableHandlersCache.set(table, cachedHandlers);
|
|
20
|
-
}
|
|
21
|
-
const { toggleAllColumnsVisibilityHandler, toggleAllPageRowsSelectedHandler, toggleAllRowsExpandedHandler, toggleAllRowsSelectedHandler } = cachedHandlers;
|
|
22
|
-
const { getColumn, getRow, ...rest } = table;
|
|
23
|
-
cached = {
|
|
24
|
-
...runGetters(rest),
|
|
25
|
-
getColumn,
|
|
26
|
-
getRow,
|
|
27
|
-
toggleAllColumnsVisibilityHandler,
|
|
28
|
-
toggleAllPageRowsSelectedHandler,
|
|
29
|
-
toggleAllRowsExpandedHandler,
|
|
30
|
-
toggleAllRowsSelectedHandler
|
|
31
|
-
};
|
|
32
|
-
tableValuesCache.set(table.options, cached);
|
|
33
|
-
}
|
|
34
|
-
return cached;
|
|
35
|
-
};
|
|
36
|
-
const useTable = (table, selector = identity, isEqual = isShallowEqual) => {
|
|
37
|
-
return useTableWithSelector(table, useCallback((table) => selector(getTableValues(table)), [selector]), isEqual);
|
|
38
|
-
};
|
|
39
|
-
const tableHook = useTable;
|
|
40
|
-
const tableHookʹ = (table) => (selector = identity, isEqual = isShallowEqual) => tableHook(table, selector, isEqual);
|
|
41
|
-
//#endregion
|
|
42
|
-
export { useTable, tableHookʹ as useTableʹ };
|
|
43
|
-
|
|
1
|
+
import{r as e}from"./invariant-DhtaPrYv.js";import{i as t,n,r,t as i}from"./useTableBase-CgF6IzQm.js";import{useCallback as a}from"react";const o=new WeakMap,s=new WeakMap,c=e=>{let t=o.get(e.options);if(!t){let n=s.get(e);n||(n={toggleAllColumnsVisibilityHandler:e.getToggleAllColumnsVisibilityHandler(),toggleAllPageRowsSelectedHandler:e.getToggleAllPageRowsSelectedHandler(),toggleAllRowsExpandedHandler:e.getToggleAllRowsExpandedHandler(),toggleAllRowsSelectedHandler:e.getToggleAllRowsSelectedHandler()},s.set(e,n));let{toggleAllColumnsVisibilityHandler:i,toggleAllPageRowsSelectedHandler:a,toggleAllRowsExpandedHandler:c,toggleAllRowsSelectedHandler:l}=n,{getColumn:u,getRow:d,...f}=e;t={...r(f),getColumn:u,getRow:d,toggleAllColumnsVisibilityHandler:i,toggleAllPageRowsSelectedHandler:a,toggleAllRowsExpandedHandler:c,toggleAllRowsSelectedHandler:l},o.set(e.options,t)}return t},l=(...r)=>{let[o,s=t,l=e]=n(r)?r:[void 0,...r];return i(o,a(e=>s(c(e),e),[s]),l)},u=l,d=e=>(t,n)=>u(e,t,n);export{l as useTable,d as useTableʹ};
|
|
44
2
|
//# sourceMappingURL=useTable.js.map
|
package/dist/useTable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTable.js","names":[],"sources":["../src/hooks/useTable.ts"],"sourcesContent":["import { useCallback } from \"react\"\n\nimport type {\n RequiredKeys,\n RowData,\n Table,\n TableOptionsResolved,\n} from \"@tanstack/table-core\"\n\nimport { identity } from \"../lib/identity\"\nimport { runGetters } from \"../lib/runGetters\"\nimport { isShallowEqual } from \"../lib/isShallowEqual\"\n\nimport type { IsEqual, RunGetters } from \"../types\"\n\nimport {
|
|
1
|
+
{"version":3,"file":"useTable.js","names":[],"sources":["../src/hooks/useTable.ts"],"sourcesContent":["import { useCallback } from \"react\"\n\nimport type {\n RequiredKeys,\n RowData,\n Table,\n TableOptionsResolved,\n} from \"@tanstack/table-core\"\n\nimport { identity } from \"../lib/identity\"\nimport { runGetters } from \"../lib/runGetters\"\nimport { isShallowEqual } from \"../lib/isShallowEqual\"\nimport { hasTableArg } from \"../lib/hasTableArg\"\n\nimport type { IsEqual, RunGetters } from \"../types\"\n\nimport { useTableBase } from \"../lib/useTableBase\"\n\nexport interface TableSnapshot<TData extends RowData> extends RunGetters<\n Table<TData>\n> {}\n\nconst tableSnapshotCache = new WeakMap<\n RequiredKeys<TableOptionsResolved<any>, \"state\">,\n TableSnapshot<any>\n>()\n\nconst tableHandlersCache = new WeakMap<\n Table<any>,\n {\n toggleAllColumnsVisibilityHandler: ReturnType<\n Table<any>[\"getToggleAllColumnsVisibilityHandler\"]\n >\n toggleAllPageRowsSelectedHandler: ReturnType<\n Table<any>[\"getToggleAllPageRowsSelectedHandler\"]\n >\n toggleAllRowsExpandedHandler: ReturnType<\n Table<any>[\"getToggleAllRowsExpandedHandler\"]\n >\n toggleAllRowsSelectedHandler: ReturnType<\n Table<any>[\"getToggleAllRowsSelectedHandler\"]\n >\n }\n>()\n\nconst getTableSnapshot = <TData extends RowData>(\n table: Table<TData>,\n): TableSnapshot<TData> => {\n let cached = tableSnapshotCache.get(table.options)\n if (!cached) {\n let cachedHandlers = tableHandlersCache.get(table)\n if (!cachedHandlers) {\n cachedHandlers = {\n toggleAllColumnsVisibilityHandler:\n table.getToggleAllColumnsVisibilityHandler(),\n toggleAllPageRowsSelectedHandler:\n table.getToggleAllPageRowsSelectedHandler(),\n toggleAllRowsExpandedHandler: table.getToggleAllRowsExpandedHandler(),\n toggleAllRowsSelectedHandler: table.getToggleAllRowsSelectedHandler(),\n }\n tableHandlersCache.set(table, cachedHandlers)\n }\n\n const {\n toggleAllColumnsVisibilityHandler,\n toggleAllPageRowsSelectedHandler,\n toggleAllRowsExpandedHandler,\n toggleAllRowsSelectedHandler,\n } = cachedHandlers\n\n const { getColumn, getRow, ...rest } = table\n\n cached = {\n ...runGetters(rest),\n getColumn,\n getRow,\n toggleAllColumnsVisibilityHandler,\n toggleAllPageRowsSelectedHandler,\n toggleAllRowsExpandedHandler,\n toggleAllRowsSelectedHandler,\n }\n tableSnapshotCache.set(table.options, cached)\n }\n return cached\n}\n\ntype Selector<TData extends RowData, Selection> = (\n tableSnapshot: TableSnapshot<TData>,\n\ttable: Table<TData>\n) => Selection\n\nexport const useTable = <TData extends RowData, Selection = TableSnapshot<TData>>(\n ...args:\n | [\n table: Table<TData> | undefined,\n selector?: Selector<TData, Selection> | undefined,\n isEqual?: IsEqual<NoInfer<Selection>> | undefined,\n ]\n | [\n selector?: Selector<TData, Selection> | undefined,\n isEqual?: IsEqual<NoInfer<Selection>> | undefined,\n ]\n): Selection => {\n const [\n table,\n selector = identity as never,\n isEqual = isShallowEqual,\n ] = hasTableArg(args) ? args : [undefined, ...args]\n\n const getSelection = useCallback(\n (table: Table<TData>) => selector(getTableSnapshot(table), table),\n [selector],\n )\n\n return useTableBase(table, getSelection, isEqual)\n}\n\nconst tableHook = useTable\n\nconst tableHookʹ =\n <TData extends RowData>(table?: Table<TData> | undefined) =>\n <Selection = TableSnapshot<TData>>(\n selector?: Selector<TData, Selection> | undefined,\n isEqual?: IsEqual<NoInfer<Selection>> | undefined\n ): Selection =>\n tableHook(table, selector, isEqual)\n\nexport { tableHookʹ as useTableʹ }\n"],"mappings":"0IAsBA,MAAM,EAAqB,IAAI,QAKzB,EAAqB,IAAI,QAkBzB,EACJ,GACyB,CACzB,IAAI,EAAS,EAAmB,IAAI,EAAM,OAAO,EACjD,GAAI,CAAC,EAAQ,CACX,IAAI,EAAiB,EAAmB,IAAI,CAAK,EAC5C,IACH,EAAiB,CACf,kCACE,EAAM,qCAAqC,EAC7C,iCACE,EAAM,oCAAoC,EAC5C,6BAA8B,EAAM,gCAAgC,EACpE,6BAA8B,EAAM,gCAAgC,CACtE,EACA,EAAmB,IAAI,EAAO,CAAc,GAG9C,GAAM,CACJ,oCACA,mCACA,+BACA,gCACE,EAEE,CAAE,YAAW,SAAQ,GAAG,GAAS,EAEvC,EAAS,CACP,GAAG,EAAW,CAAI,EAClB,YACA,SACA,oCACA,mCACA,+BACA,8BACF,EACA,EAAmB,IAAI,EAAM,QAAS,CAAM,CAC9C,CACA,OAAO,CACT,EAOa,GACX,GAAG,IAUW,CACd,GAAM,CACJ,EACA,EAAW,EACX,EAAU,GACR,EAAY,CAAI,EAAI,EAAO,CAAC,IAAA,GAAW,GAAG,CAAI,EAOlD,OAAO,EAAa,EALC,EAClB,GAAwB,EAAS,EAAiB,CAAK,EAAG,CAAK,EAChE,CAAC,CAAQ,CAG2B,EAAG,CAAO,CAClD,EAEM,EAAY,EAEZ,EACoB,IAEtB,EACA,IAEA,EAAU,EAAO,EAAU,CAAO"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=require("./TableContext.cjs"),t=require("./invariant-BzSvgtnX.cjs");let n=require("react");const r=(e,...t)=>e,i=e=>e.slice(0,1).toLowerCase()+e.slice(1),a=e=>Object.fromEntries(Object.entries(e).map(([e,t])=>{if(typeof t==`function`&&e.startsWith(`get`))try{return[i(e.replace(/^get/,``)),t()]}catch{return[e,t]}return[e,t]})),o=e=>typeof e==`object`&&!!e&&t.n.has(e),s=e=>o(e[0])||e[0]===void 0,c=(i,a=r,o)=>{"use no memo";let s=i??(0,n.use)(e.TableContext);t.t(s,"`useTableBase` must be used as a descendent of `TableContext` or provided with a `Table` as an argument!");let c=(0,n.useMemo)(()=>(t.n.get(s)??{}).listeners,[s]);t.t(c,"The provided/found `Table` instance cannot be subscribed to. Was it created by the correct `useReactTable` hook?");let l=(0,n.useCallback)(e=>(c.add(e),()=>{c.delete(e)}),[c]),u=(0,n.useRef)({hasValue:!1,value:null}),d=(0,n.useSyncExternalStore)(l,(0,n.useMemo)(()=>{let e=!1,t,n;return()=>{let r=s.options;if(!e){e=!0,t=r;let i=a(s);if(o!==void 0&&u.current.hasValue){let e=u.current.value;if(o(e,i))return n=e,e}return n=i,i}let i=t,c=n;if(Object.is(i,r))return c;let l=a(s);return o!==void 0&&o(c,l)?(t=r,c):(t=r,n=l,l)}},[a,s,o]));return(0,n.useInsertionEffect)(()=>{u.current.hasValue=!0,u.current.value=d},[d]),d};Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return r}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return a}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return c}});
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{TableContext as e}from"./TableContext.js";import{n as t,t as n}from"./invariant-DhtaPrYv.js";import{use as r,useCallback as i,useInsertionEffect as a,useMemo as o,useRef as s,useSyncExternalStore as c}from"react";const l=(e,...t)=>e,u=e=>e.slice(0,1).toLowerCase()+e.slice(1),d=e=>Object.fromEntries(Object.entries(e).map(([e,t])=>{if(typeof t==`function`&&e.startsWith(`get`))try{return[u(e.replace(/^get/,``)),t()]}catch{return[e,t]}return[e,t]})),f=e=>typeof e==`object`&&!!e&&t.has(e),p=e=>f(e[0])||e[0]===void 0,m=(u,d=l,f)=>{"use no memo";let p=u??r(e);n(p,"`useTableBase` must be used as a descendent of `TableContext` or provided with a `Table` as an argument!");let m=o(()=>(t.get(p)??{}).listeners,[p]);n(m,"The provided/found `Table` instance cannot be subscribed to. Was it created by the correct `useReactTable` hook?");let h=i(e=>(m.add(e),()=>{m.delete(e)}),[m]),g=s({hasValue:!1,value:null}),_=c(h,o(()=>{let e=!1,t,n;return()=>{let r=p.options;if(!e){e=!0,t=r;let i=d(p);if(f!==void 0&&g.current.hasValue){let e=g.current.value;if(f(e,i))return n=e,e}return n=i,i}let i=t,a=n;if(Object.is(i,r))return a;let o=d(p);return f!==void 0&&f(a,o)?(t=r,a):(t=r,n=o,o)}},[d,p,f]));return a(()=>{g.current.hasValue=!0,g.current.value=_},[_]),_};export{l as i,p as n,d as r,m as t};
|
|
2
|
+
//# sourceMappingURL=useTableBase-CgF6IzQm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTableBase-CgF6IzQm.js","names":[],"sources":["../src/lib/identity.ts","../src/lib/uncapitalize.ts","../src/lib/runGetters.ts","../src/lib/hasTableArg.ts","../src/lib/useTableBase.ts"],"sourcesContent":["export const identity = <T>(x: T, ..._: any[]): T => x\n","export const uncapitalize = (str: string): string =>\n str.slice(0, 1).toLowerCase() + str.slice(1)\n","import type { RunGetters } from \"../types\"\nimport { uncapitalize } from \"./uncapitalize\"\n\nexport const runGetters = <T extends object>(x: T): RunGetters<T> =>\n Object.fromEntries(\n Object.entries(x).map(([key, value]) => {\n if (typeof value === \"function\" && key.startsWith(\"get\")) {\n try {\n return [uncapitalize(key.replace(/^get/, \"\")), value()]\n } catch {\n return [key, value]\n }\n }\n\n return [key, value]\n }),\n ) as RunGetters<T>\n","import type {\n RowData,\n Table,\n} from \"@tanstack/table-core\"\n\nimport { tableRegistry } from \"./tableRegistry\"\n\nconst isTable = <TData extends RowData>(\n x: unknown | Table<TData>,\n): x is Table<TData> =>\n typeof x === \"object\" && x !== null && tableRegistry.has(x as Table<TData>)\n\nexport const hasTableArg = <\n TData extends RowData,\n Rest extends [unknown?, ...unknown[]],\n>(\n args: [Table<TData> | undefined, ...Rest] | Rest,\n): args is [Table<TData> | undefined, ...Rest] =>\n isTable(args[0]) || args[0] === undefined\n","import {\n use,\n useCallback,\n useInsertionEffect,\n useMemo,\n useRef,\n useSyncExternalStore,\n} from \"react\"\n\nimport type {\n RequiredKeys,\n RowData,\n Table,\n TableOptionsResolved,\n} from \"@tanstack/table-core\"\n\nimport { TableContext } from \"../contexts/TableContext\"\n\nimport { identity } from \"./identity\"\nimport { tableRegistry } from \"./tableRegistry\"\nimport { invariant } from \"./invariant\"\n\nimport type { IsEqual } from \"../types\"\n\nexport const useTableBase = <\n TData extends RowData,\n Selection = Table<TData>,\n>(\n table?: Table<TData> | undefined,\n selector: (table: Table<TData>) => Selection = identity as never,\n isEqual?: IsEqual<NoInfer<Selection>> | undefined,\n): Selection => {\n \"use no memo\"\n\n const resolvedTable = table ?? use<Table<TData> | null>(TableContext)\n\n invariant(\n resolvedTable,\n \"`useTableBase` must be used as a descendent of `TableContext` or provided with a `Table` as an argument!\",\n )\n\n const listeners = useMemo(\n () => (tableRegistry.get(resolvedTable) ?? {}).listeners,\n [resolvedTable],\n )\n\n invariant(\n listeners,\n \"The provided/found `Table` instance cannot be subscribed to. Was it created by the correct `useReactTable` hook?\",\n )\n\n const subscribe = useCallback(\n (onTableChange: () => void) => {\n listeners.add(onTableChange)\n return () => {\n listeners.delete(onTableChange)\n }\n },\n [listeners],\n )\n\n // Use this to track the rendered snapshot.\n const instRef = useRef<\n | {\n hasValue: false\n value: null\n }\n | { hasValue: true; value: Selection }\n >({\n hasValue: false,\n value: null,\n })\n\n // eslint-disable-next-line react-hooks/immutability\n const getSnapshot = useMemo(() => {\n let hasMemo = false,\n memoizedTableOptions: RequiredKeys<TableOptionsResolved<TData>, \"state\">,\n memoizedSelection: Selection\n\n return () => {\n const nextTableOptions = resolvedTable.options\n if (!hasMemo) {\n // The first time the hook is called, there is no memoized result.\n // eslint-disable-next-line react-hooks/immutability -- should be fine 😅 this is how it is in the upstream, too\n hasMemo = true\n memoizedTableOptions = nextTableOptions\n const nextSelection = selector(resolvedTable)\n\n if (isEqual !== undefined) {\n // Even if the selector has changed, the currently rendered selection\n // may be equal to the new selection. We should attempt to reuse the\n // current value if possible, to preserve downstream memoizations.\n if (instRef.current.hasValue) {\n const currentSelection = instRef.current.value\n if (isEqual(currentSelection, nextSelection)) {\n memoizedSelection = currentSelection\n return currentSelection\n }\n }\n }\n\n memoizedSelection = nextSelection\n return nextSelection\n }\n\n const prevTableOptions = memoizedTableOptions\n // eslint-disable-next-line react-hooks/memo-dependencies\n const prevSelection = memoizedSelection\n\n if (Object.is(prevTableOptions, nextTableOptions)) {\n // The snapshot is the same as last time. Reuse the previous selection.\n return prevSelection\n }\n\n // The snapshot has changed, so we need to compute a new selection.\n const nextSelection = selector(resolvedTable)\n\n // If a custom isEqual function is provided, use that to check if the data\n // has changed. If it hasn't, return the previous selection. That signals\n // to React that the selections are conceptually equal, and we can bail\n // out of rendering.\n if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {\n // The snapshot still has changed, so make sure to update to not keep\n // old references alive\n memoizedTableOptions = nextTableOptions\n return prevSelection\n }\n\n memoizedTableOptions = nextTableOptions\n memoizedSelection = nextSelection\n return nextSelection\n }\n }, [selector, resolvedTable, isEqual])\n\n const value = useSyncExternalStore(subscribe, getSnapshot)\n\n useInsertionEffect(() => {\n instRef.current.hasValue = true\n instRef.current.value = value\n }, [value])\n\n return value\n}\n"],"mappings":"4NAAA,MAAa,GAAe,EAAM,GAAG,IAAgB,ECAxC,EAAgB,GAC3B,EAAI,MAAM,EAAG,CAAC,EAAE,YAAY,EAAI,EAAI,MAAM,CAAC,ECEhC,EAAgC,GAC3C,OAAO,YACL,OAAO,QAAQ,CAAC,EAAE,KAAK,CAAC,EAAK,KAAW,CACtC,GAAI,OAAO,GAAU,YAAc,EAAI,WAAW,KAAK,EACrD,GAAI,CACF,MAAO,CAAC,EAAa,EAAI,QAAQ,OAAQ,EAAE,CAAC,EAAG,EAAM,CAAC,CACxD,MAAQ,CACN,MAAO,CAAC,EAAK,CAAK,CACpB,CAGF,MAAO,CAAC,EAAK,CAAK,CACpB,CAAC,CACH,ECTI,EACJ,GAEA,OAAO,GAAM,YAAY,GAAc,EAAc,IAAI,CAAiB,EAE/D,EAIX,GAEA,EAAQ,EAAK,EAAE,GAAK,EAAK,KAAO,IAAA,GCMrB,GAIX,EACA,EAA+C,EAC/C,IACc,CACd,cAEA,IAAM,EAAgB,GAAS,EAAyB,CAAY,EAEpE,EACE,EACA,0GACF,EAEA,IAAM,EAAY,OACT,EAAc,IAAI,CAAa,GAAK,CAAC,GAAG,UAC/C,CAAC,CAAa,CAChB,EAEA,EACE,EACA,kHACF,EAEA,IAAM,EAAY,EACf,IACC,EAAU,IAAI,CAAa,MACd,CACX,EAAU,OAAO,CAAa,CAChC,GAEF,CAAC,CAAS,CACZ,EAGM,EAAU,EAMd,CACA,SAAU,GACV,MAAO,IACT,CAAC,EA+DK,EAAQ,EAAqB,EA5Df,MAAc,CAChC,IAAI,EAAU,GACZ,EACA,EAEF,UAAa,CACX,IAAM,EAAmB,EAAc,QACvC,GAAI,CAAC,EAAS,CAGZ,EAAU,GACV,EAAuB,EACvB,IAAM,EAAgB,EAAS,CAAa,EAE5C,GAAI,IAAY,IAAA,IAIV,EAAQ,QAAQ,SAAU,CAC5B,IAAM,EAAmB,EAAQ,QAAQ,MACzC,GAAI,EAAQ,EAAkB,CAAa,EAEzC,MADA,GAAoB,EACb,CAEX,CAIF,MADA,GAAoB,EACb,CACT,CAEA,IAAM,EAAmB,EAEnB,EAAgB,EAEtB,GAAI,OAAO,GAAG,EAAkB,CAAgB,EAE9C,OAAO,EAIT,IAAM,EAAgB,EAAS,CAAa,EAe5C,OATI,IAAY,IAAA,IAAa,EAAQ,EAAe,CAAa,GAG/D,EAAuB,EAChB,IAGT,EAAuB,EACvB,EAAoB,EACb,EACT,CACF,EAAG,CAAC,EAAU,EAAe,CAAO,CAEoB,CAAC,EAOzD,OALA,MAAyB,CACvB,EAAQ,QAAQ,SAAW,GAC3B,EAAQ,QAAQ,MAAQ,CAC1B,EAAG,CAAC,CAAK,CAAC,EAEH,CACT"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "react-hook-tanstack-table",
|
|
3
3
|
"packageManager": "yarn@4.15.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.3",
|
|
6
6
|
"description": "Rules-of-React respecting bindings for TanStack Table.",
|
|
7
7
|
"author": "Xandor Schiefer <me@xandor.co.za>",
|
|
8
8
|
"license": "MIT",
|
|
@@ -47,10 +47,6 @@
|
|
|
47
47
|
"import": "./dist/useTable.js",
|
|
48
48
|
"require": "./dist/useTable.cjs"
|
|
49
49
|
},
|
|
50
|
-
"./useTableWithSelector": {
|
|
51
|
-
"import": "./dist/useTableWithSelector.js",
|
|
52
|
-
"require": "./dist/useTableWithSelector.cjs"
|
|
53
|
-
},
|
|
54
50
|
"./package.json": "./package.json"
|
|
55
51
|
},
|
|
56
52
|
"files": [
|
package/src/hooks/useCell.ts
CHANGED
|
@@ -18,28 +18,41 @@ import { invariant } from "../lib/invariant"
|
|
|
18
18
|
|
|
19
19
|
import type { IsEqual, RunGetters } from "../types"
|
|
20
20
|
|
|
21
|
-
import type {
|
|
22
|
-
import type {
|
|
23
|
-
import {
|
|
21
|
+
import type { ColumnSnapshot } from "./useColumn"
|
|
22
|
+
import type { RowSnapshot } from "./useRow"
|
|
23
|
+
import { useTableBase } from "../lib/useTableBase"
|
|
24
24
|
|
|
25
|
-
export interface
|
|
25
|
+
export interface CellSnapshot<TData extends RowData, TValue> extends RunGetters<
|
|
26
26
|
Cell<TData, TValue>
|
|
27
27
|
> {}
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const cellSnapshotCache = new WeakMap<
|
|
30
30
|
RequiredKeys<TableOptionsResolved<any>, "state">,
|
|
31
|
-
Map<string, Map<string,
|
|
31
|
+
Map<string, Map<string, CellSnapshot<any, any>>>
|
|
32
32
|
>()
|
|
33
33
|
|
|
34
|
-
const
|
|
34
|
+
const getCell = <TData extends RowData, TValue>(
|
|
35
35
|
table: Table<TData>,
|
|
36
36
|
rowId: string,
|
|
37
37
|
columnId: string,
|
|
38
|
-
)
|
|
39
|
-
|
|
38
|
+
) => {
|
|
39
|
+
const cell = table
|
|
40
|
+
.getRow(rowId)
|
|
41
|
+
?.getAllCells()
|
|
42
|
+
.find((c) => c.column.id === columnId) as Cell<TData, TValue> | undefined
|
|
43
|
+
invariant(cell)
|
|
44
|
+
return cell
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const getCellSnapshot = <TData extends RowData, TValue>(
|
|
48
|
+
table: Table<TData>,
|
|
49
|
+
rowId: string,
|
|
50
|
+
columnId: string,
|
|
51
|
+
): CellSnapshot<TData, TValue> => {
|
|
52
|
+
let cellCache = cellSnapshotCache.get(table.options)
|
|
40
53
|
if (!cellCache) {
|
|
41
54
|
cellCache = new Map()
|
|
42
|
-
|
|
55
|
+
cellSnapshotCache.set(table.options, cellCache)
|
|
43
56
|
}
|
|
44
57
|
|
|
45
58
|
let rowCache = cellCache.get(rowId)
|
|
@@ -49,17 +62,9 @@ const getCellValues = <TData extends RowData, TValue>(
|
|
|
49
62
|
}
|
|
50
63
|
|
|
51
64
|
let cached = rowCache.get(columnId)
|
|
52
|
-
|
|
53
65
|
if (!cached) {
|
|
54
|
-
const cell = table
|
|
55
|
-
.getRow(rowId)
|
|
56
|
-
?.getAllCells()
|
|
57
|
-
.find((c) => c.column.id === columnId) as Cell<TData, TValue> | undefined
|
|
58
|
-
|
|
59
|
-
invariant(cell)
|
|
60
|
-
|
|
66
|
+
const cell = getCell(table, rowId, columnId)
|
|
61
67
|
cached = runGetters(cell)
|
|
62
|
-
|
|
63
68
|
rowCache.set(columnId, cached)
|
|
64
69
|
}
|
|
65
70
|
|
|
@@ -67,25 +72,27 @@ const getCellValues = <TData extends RowData, TValue>(
|
|
|
67
72
|
}
|
|
68
73
|
|
|
69
74
|
type Selector<TData extends RowData, TValue, Selection> = (
|
|
70
|
-
|
|
75
|
+
cellSnapshot: CellSnapshot<TData, TValue>,
|
|
76
|
+
cell: Cell<TData, TValue>,
|
|
77
|
+
table: Table<TData>,
|
|
71
78
|
) => Selection
|
|
72
79
|
|
|
73
80
|
interface CellCoords<TData extends RowData, TValue> {
|
|
74
|
-
column: Column<TData, TValue> |
|
|
75
|
-
row: Row<TData> |
|
|
81
|
+
column: Column<TData, TValue> | ColumnSnapshot<TData> | string
|
|
82
|
+
row: Row<TData> | RowSnapshot<TData> | string
|
|
76
83
|
}
|
|
77
84
|
|
|
78
85
|
export const useCell = <
|
|
79
86
|
TData extends RowData,
|
|
80
87
|
TValue,
|
|
81
|
-
Selection =
|
|
88
|
+
Selection = CellSnapshot<TData, TValue>,
|
|
82
89
|
>(
|
|
83
90
|
...args:
|
|
84
91
|
| [
|
|
85
92
|
table: Table<TData> | undefined,
|
|
86
93
|
cell:
|
|
87
94
|
| Cell<TData, TValue>
|
|
88
|
-
|
|
|
95
|
+
| CellSnapshot<TData, TValue>
|
|
89
96
|
| CellCoords<TData, TValue>,
|
|
90
97
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
91
98
|
isEqual?: IsEqual<NoInfer<Selection>> | undefined,
|
|
@@ -93,7 +100,7 @@ export const useCell = <
|
|
|
93
100
|
| [
|
|
94
101
|
cell:
|
|
95
102
|
| Cell<TData, TValue>
|
|
96
|
-
|
|
|
103
|
+
| CellSnapshot<TData, TValue>
|
|
97
104
|
| CellCoords<TData, TValue>,
|
|
98
105
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
99
106
|
isEqual?: IsEqual<NoInfer<Selection>> | undefined,
|
|
@@ -121,11 +128,15 @@ export const useCell = <
|
|
|
121
128
|
}
|
|
122
129
|
|
|
123
130
|
const getSelection = useCallback(
|
|
124
|
-
(table: Table<TData>) => selector(
|
|
131
|
+
(table: Table<TData>) => selector(
|
|
132
|
+
getCellSnapshot<TData, TValue>(table, rowId, columnId),
|
|
133
|
+
getCell<TData, TValue>(table, rowId, columnId),
|
|
134
|
+
table
|
|
135
|
+
),
|
|
125
136
|
[columnId, rowId, selector],
|
|
126
137
|
)
|
|
127
138
|
|
|
128
|
-
return
|
|
139
|
+
return useTableBase(table, getSelection, isEqual)
|
|
129
140
|
}
|
|
130
141
|
|
|
131
142
|
const cellHook = useCell
|
|
@@ -137,17 +148,17 @@ const cellHookʹ =
|
|
|
137
148
|
table: Table<TData> | undefined,
|
|
138
149
|
cell:
|
|
139
150
|
| Cell<TData, TValue>
|
|
140
|
-
|
|
|
151
|
+
| CellSnapshot<TData, TValue>
|
|
141
152
|
| CellCoords<TData, TValue>,
|
|
142
153
|
]
|
|
143
154
|
| [
|
|
144
155
|
cell:
|
|
145
156
|
| Cell<TData, TValue>
|
|
146
|
-
|
|
|
157
|
+
| CellSnapshot<TData, TValue>
|
|
147
158
|
| CellCoords<TData, TValue>,
|
|
148
159
|
]
|
|
149
160
|
) =>
|
|
150
|
-
<Selection =
|
|
161
|
+
<Selection = CellSnapshot<TData, TValue>>(
|
|
151
162
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
152
163
|
isEqual?: IsEqual<NoInfer<Selection>> | undefined,
|
|
153
164
|
): Selection =>
|
package/src/hooks/useColumn.ts
CHANGED
|
@@ -16,16 +16,16 @@ import { invariant } from "../lib/invariant"
|
|
|
16
16
|
|
|
17
17
|
import type { IsEqual, RunGetters } from "../types"
|
|
18
18
|
|
|
19
|
-
import {
|
|
19
|
+
import { useTableBase } from "../lib/useTableBase"
|
|
20
20
|
|
|
21
|
-
export interface
|
|
21
|
+
export interface ColumnSnapshot<
|
|
22
22
|
TData extends RowData,
|
|
23
23
|
TValue = unknown,
|
|
24
24
|
> extends RunGetters<Column<TData, TValue>> {}
|
|
25
25
|
|
|
26
|
-
const
|
|
26
|
+
const columnSnapshotCache = new WeakMap<
|
|
27
27
|
RequiredKeys<TableOptionsResolved<any>, "state">,
|
|
28
|
-
Map<string,
|
|
28
|
+
Map<string, ColumnSnapshot<any, any>>
|
|
29
29
|
>()
|
|
30
30
|
|
|
31
31
|
const columnHandlersCache = new WeakMap<
|
|
@@ -43,24 +43,28 @@ const columnHandlersCache = new WeakMap<
|
|
|
43
43
|
}
|
|
44
44
|
>()
|
|
45
45
|
|
|
46
|
-
const
|
|
46
|
+
const getColumn = <TData extends RowData, TValue = unknown>(table: Table<TData>, columnId: string) => {
|
|
47
|
+
const column = table.getColumn(columnId) as
|
|
48
|
+
| Column<TData, TValue>
|
|
49
|
+
| undefined
|
|
50
|
+
invariant(column)
|
|
51
|
+
return column
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const getColumnSnapshot = <TData extends RowData, TValue = unknown>(
|
|
47
55
|
table: Table<TData>,
|
|
48
56
|
columnId: string,
|
|
49
|
-
):
|
|
50
|
-
let columnCache =
|
|
57
|
+
): ColumnSnapshot<TData, TValue> => {
|
|
58
|
+
let columnCache = columnSnapshotCache.get(table.options)
|
|
51
59
|
if (!columnCache) {
|
|
52
60
|
columnCache = new Map()
|
|
53
|
-
|
|
61
|
+
columnSnapshotCache.set(table.options, columnCache)
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
let cached = columnCache.get(columnId)
|
|
57
65
|
|
|
58
66
|
if (!cached) {
|
|
59
|
-
const column =
|
|
60
|
-
| Column<TData, TValue>
|
|
61
|
-
| undefined
|
|
62
|
-
|
|
63
|
-
invariant(column)
|
|
67
|
+
const column = getColumn<TData, TValue>(table, columnId)
|
|
64
68
|
|
|
65
69
|
let cachedHandlers = columnHandlersCache.get(column)
|
|
66
70
|
if (!cachedHandlers) {
|
|
@@ -92,20 +96,22 @@ const getColumnValues = <TData extends RowData, TValue = unknown>(
|
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
type Selector<TData extends RowData, TValue, Selection> = (
|
|
95
|
-
|
|
99
|
+
columnSnapshot: ColumnSnapshot<TData, TValue>,
|
|
100
|
+
column: Column<TData, TValue>,
|
|
101
|
+
table: Table<TData>
|
|
96
102
|
) => Selection
|
|
97
103
|
|
|
98
104
|
export const useColumn = <
|
|
99
105
|
TData extends RowData,
|
|
100
106
|
TValue = unknown,
|
|
101
|
-
Selection =
|
|
107
|
+
Selection = ColumnSnapshot<TData, TValue>,
|
|
102
108
|
>(
|
|
103
109
|
...args:
|
|
104
110
|
| [
|
|
105
111
|
table: Table<TData> | undefined,
|
|
106
112
|
column:
|
|
107
113
|
| Column<TData, TValue>
|
|
108
|
-
|
|
|
114
|
+
| ColumnSnapshot<TData, TValue>
|
|
109
115
|
| { id: string }
|
|
110
116
|
| string,
|
|
111
117
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
@@ -114,7 +120,7 @@ export const useColumn = <
|
|
|
114
120
|
| [
|
|
115
121
|
column:
|
|
116
122
|
| Column<TData, TValue>
|
|
117
|
-
|
|
|
123
|
+
| ColumnSnapshot<TData, TValue>
|
|
118
124
|
| { id: string }
|
|
119
125
|
| string,
|
|
120
126
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
@@ -131,11 +137,15 @@ export const useColumn = <
|
|
|
131
137
|
const columnId = typeof columnOrId === "string" ? columnOrId : columnOrId.id
|
|
132
138
|
|
|
133
139
|
const getSelection = useCallback(
|
|
134
|
-
(table: Table<TData>) => selector(
|
|
140
|
+
(table: Table<TData>) => selector(
|
|
141
|
+
getColumnSnapshot<TData, TValue>(table, columnId),
|
|
142
|
+
getColumn<TData, TValue>(table, columnId),
|
|
143
|
+
table
|
|
144
|
+
),
|
|
135
145
|
[columnId, selector],
|
|
136
146
|
)
|
|
137
147
|
|
|
138
|
-
return
|
|
148
|
+
return useTableBase(table, getSelection, isEqual)
|
|
139
149
|
}
|
|
140
150
|
|
|
141
151
|
const columnHook = useColumn
|
|
@@ -147,19 +157,19 @@ const columnHookʹ =
|
|
|
147
157
|
table: Table<TData> | undefined,
|
|
148
158
|
column:
|
|
149
159
|
| Column<TData, TValue>
|
|
150
|
-
|
|
|
160
|
+
| ColumnSnapshot<TData, TValue>
|
|
151
161
|
| { id: string }
|
|
152
162
|
| string,
|
|
153
163
|
]
|
|
154
164
|
| [
|
|
155
165
|
column:
|
|
156
166
|
| Column<TData, TValue>
|
|
157
|
-
|
|
|
167
|
+
| ColumnSnapshot<TData, TValue>
|
|
158
168
|
| { id: string }
|
|
159
169
|
| string,
|
|
160
170
|
]
|
|
161
171
|
) =>
|
|
162
|
-
<Selection =
|
|
172
|
+
<Selection = ColumnSnapshot<TData, TValue>>(
|
|
163
173
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
164
174
|
isEqual?: IsEqual<NoInfer<Selection>> | undefined,
|
|
165
175
|
): Selection =>
|
package/src/hooks/useHeader.ts
CHANGED
|
@@ -16,15 +16,15 @@ import { invariant } from "../lib/invariant"
|
|
|
16
16
|
|
|
17
17
|
import type { IsEqual, RunGetters } from "../types"
|
|
18
18
|
|
|
19
|
-
import {
|
|
19
|
+
import { useTableBase } from "../lib/useTableBase"
|
|
20
20
|
|
|
21
|
-
export interface
|
|
21
|
+
export interface HeaderSnapshot<TData extends RowData, TValue> extends RunGetters<
|
|
22
22
|
Header<TData, TValue>
|
|
23
23
|
> {}
|
|
24
24
|
|
|
25
|
-
const
|
|
25
|
+
const headerSnapshotCache = new WeakMap<
|
|
26
26
|
RequiredKeys<TableOptionsResolved<any>, "state">,
|
|
27
|
-
Map<string,
|
|
27
|
+
Map<string, HeaderSnapshot<any, any>>
|
|
28
28
|
>()
|
|
29
29
|
|
|
30
30
|
const headerHandlersCache = new WeakMap<
|
|
@@ -34,24 +34,28 @@ const headerHandlersCache = new WeakMap<
|
|
|
34
34
|
}
|
|
35
35
|
>()
|
|
36
36
|
|
|
37
|
-
const
|
|
37
|
+
const getHeader = <TData extends RowData, TValue>(table: Table<TData>, headerId: string) => {
|
|
38
|
+
const header = table.getFlatHeaders().find((h) => h.id === headerId) as
|
|
39
|
+
| Header<TData, TValue>
|
|
40
|
+
| undefined
|
|
41
|
+
invariant(header)
|
|
42
|
+
return header
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const getHeaderSnapshot = <TData extends RowData, TValue>(
|
|
38
46
|
table: Table<TData>,
|
|
39
47
|
headerId: string,
|
|
40
|
-
):
|
|
41
|
-
let headerCache =
|
|
48
|
+
): HeaderSnapshot<TData, TValue> => {
|
|
49
|
+
let headerCache = headerSnapshotCache.get(table.options)
|
|
42
50
|
if (!headerCache) {
|
|
43
51
|
headerCache = new Map()
|
|
44
|
-
|
|
52
|
+
headerSnapshotCache.set(table.options, headerCache)
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
let cached = headerCache.get(headerId)
|
|
48
56
|
|
|
49
57
|
if (!cached) {
|
|
50
|
-
const header = table
|
|
51
|
-
| Header<TData, TValue>
|
|
52
|
-
| undefined
|
|
53
|
-
|
|
54
|
-
invariant(header)
|
|
58
|
+
const header = getHeader<TData, TValue>(table, headerId)
|
|
55
59
|
|
|
56
60
|
let cachedHandlers = headerHandlersCache.get(header)
|
|
57
61
|
if (!cachedHandlers) {
|
|
@@ -75,20 +79,22 @@ const getHeaderValues = <TData extends RowData, TValue>(
|
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
type Selector<TData extends RowData, TValue, Selection> = (
|
|
78
|
-
|
|
82
|
+
headerSnapshot: HeaderSnapshot<TData, TValue>,
|
|
83
|
+
header: Header<TData, TValue>,
|
|
84
|
+
table: Table<TData>
|
|
79
85
|
) => Selection
|
|
80
86
|
|
|
81
87
|
export const useHeader = <
|
|
82
88
|
TData extends RowData,
|
|
83
89
|
TValue,
|
|
84
|
-
Selection =
|
|
90
|
+
Selection = HeaderSnapshot<TData, TValue>,
|
|
85
91
|
>(
|
|
86
92
|
...args:
|
|
87
93
|
| [
|
|
88
94
|
table: Table<TData> | undefined,
|
|
89
95
|
header:
|
|
90
96
|
| Header<TData, TValue>
|
|
91
|
-
|
|
|
97
|
+
| HeaderSnapshot<TData, TValue>
|
|
92
98
|
| { id: string }
|
|
93
99
|
| string,
|
|
94
100
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
@@ -97,7 +103,7 @@ export const useHeader = <
|
|
|
97
103
|
| [
|
|
98
104
|
header:
|
|
99
105
|
| Header<TData, TValue>
|
|
100
|
-
|
|
|
106
|
+
| HeaderSnapshot<TData, TValue>
|
|
101
107
|
| { id: string }
|
|
102
108
|
| string,
|
|
103
109
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
@@ -114,11 +120,15 @@ export const useHeader = <
|
|
|
114
120
|
const headerId = typeof headerOrId === "string" ? headerOrId : headerOrId.id
|
|
115
121
|
|
|
116
122
|
const getSelection = useCallback(
|
|
117
|
-
(table: Table<TData>) => selector(
|
|
123
|
+
(table: Table<TData>) => selector(
|
|
124
|
+
getHeaderSnapshot<TData, TValue>(table, headerId),
|
|
125
|
+
getHeader<TData, TValue>(table, headerId),
|
|
126
|
+
table
|
|
127
|
+
),
|
|
118
128
|
[headerId, selector],
|
|
119
129
|
)
|
|
120
130
|
|
|
121
|
-
return
|
|
131
|
+
return useTableBase(table, getSelection, isEqual)
|
|
122
132
|
}
|
|
123
133
|
|
|
124
134
|
const headerHook = useHeader
|
|
@@ -130,19 +140,19 @@ const headerHookʹ =
|
|
|
130
140
|
table: Table<TData> | undefined,
|
|
131
141
|
header:
|
|
132
142
|
| Header<TData, TValue>
|
|
133
|
-
|
|
|
143
|
+
| HeaderSnapshot<TData, TValue>
|
|
134
144
|
| { id: string }
|
|
135
145
|
| string,
|
|
136
146
|
]
|
|
137
147
|
| [
|
|
138
148
|
header:
|
|
139
149
|
| Header<TData, TValue>
|
|
140
|
-
|
|
|
150
|
+
| HeaderSnapshot<TData, TValue>
|
|
141
151
|
| { id: string }
|
|
142
152
|
| string,
|
|
143
153
|
]
|
|
144
154
|
) =>
|
|
145
|
-
<Selection =
|
|
155
|
+
<Selection = HeaderSnapshot<TData, TValue>>(
|
|
146
156
|
selector?: Selector<TData, TValue, Selection> | undefined,
|
|
147
157
|
isEqual?: IsEqual<NoInfer<Selection>> | undefined,
|
|
148
158
|
): Selection =>
|