@tanstack/table-core 9.0.0-alpha.12 → 9.0.0-alpha.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/core/columns/coreColumnsFeature.types.d.ts +1 -1
- package/dist/esm/core/columns/coreColumnsFeature.utils.js.map +1 -1
- package/dist/esm/core/row-models/createCoreRowModel.js.map +1 -1
- package/dist/esm/core/rows/coreRowsFeature.types.d.ts +2 -2
- package/dist/esm/core/table/constructTable.js +10 -4
- package/dist/esm/core/table/constructTable.js.map +1 -1
- package/dist/esm/core/table/coreTablesFeature.types.d.ts +6 -2
- package/dist/esm/core/table/coreTablesFeature.utils.js +2 -1
- package/dist/esm/core/table/coreTablesFeature.utils.js.map +1 -1
- package/dist/esm/features/table-reactivity/tableReactivityFeature.d.ts +9 -0
- package/dist/esm/features/table-reactivity/tableReactivityFeature.js +30 -0
- package/dist/esm/features/table-reactivity/tableReactivityFeature.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/ColumnDef.d.ts +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/core/columns/coreColumnsFeature.types.ts +1 -1
- package/src/core/columns/coreColumnsFeature.utils.ts +1 -1
- package/src/core/row-models/createCoreRowModel.ts +2 -2
- package/src/core/rows/coreRowsFeature.types.ts +5 -2
- package/src/core/table/constructTable.ts +9 -3
- package/src/core/table/coreTablesFeature.types.ts +6 -2
- package/src/core/table/coreTablesFeature.utils.ts +2 -1
- package/src/features/table-reactivity/tableReactivityFeature.ts +47 -0
- package/src/index.ts +3 -0
- package/src/types/ColumnDef.ts +1 -1
- package/src/utils.ts +1 -1
|
@@ -50,7 +50,7 @@ export interface TableOptions_Columns<TFeatures extends TableFeatures, TData ext
|
|
|
50
50
|
/**
|
|
51
51
|
* The array of column defs to use for the table.
|
|
52
52
|
*/
|
|
53
|
-
columns:
|
|
53
|
+
columns: ReadonlyArray<ColumnDef<TFeatures, TData, TValue>>;
|
|
54
54
|
/**
|
|
55
55
|
* Default column options to use for all column defs supplied to the table.
|
|
56
56
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coreColumnsFeature.utils.js","sources":["../../../../src/core/columns/coreColumnsFeature.utils.ts"],"sourcesContent":["import { callMemoOrStaticFn } from '../../utils'\nimport { table_getOrderColumnsFn } from '../../features/column-ordering/columnOrderingFeature.utils'\nimport { constructColumn } from './constructColumn'\nimport type { Table_Internal } from '../../types/Table'\nimport type { CellData, RowData } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type {\n ColumnDef,\n ColumnDefResolved,\n GroupColumnDef,\n} from '../../types/ColumnDef'\nimport type { Column } from '../../types/Column'\n\nexport function column_getFlatColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column<TFeatures, TData, TValue>,\n): Array<Column<TFeatures, TData, TValue>> {\n return [column, ...column.columns.flatMap((col) => col.getFlatColumns())]\n}\n\nexport function column_getLeafColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column<TFeatures, TData, TValue>,\n): Array<Column<TFeatures, TData, TValue>> {\n if (column.columns.length) {\n const leafColumns = column.columns.flatMap(\n (col) => col.getLeafColumns(), // recursive\n )\n\n return callMemoOrStaticFn(\n column.table,\n 'getOrderColumns',\n table_getOrderColumnsFn,\n )(leafColumns as any) as any\n }\n\n return [column]\n}\n\nexport function table_getDefaultColumnDef<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): Partial<ColumnDef<TFeatures, TData, unknown>> {\n return {\n header: (props) => {\n const resolvedColumnDef = props.header.column\n .columnDef as ColumnDefResolved<{}, TData>\n\n if (resolvedColumnDef.accessorKey) {\n return resolvedColumnDef.accessorKey\n }\n\n if (resolvedColumnDef.accessorFn) {\n return resolvedColumnDef.id\n }\n\n return null\n },\n cell: (props) => props.renderValue<any>()?.toString?.() ?? null,\n ...Object.values(table._features).reduce((obj, feature) => {\n return Object.assign(obj, feature.getDefaultColumnDef?.())\n }, {}),\n ...table.options.defaultColumn,\n } as Partial<ColumnDef<TFeatures, TData, unknown>>\n}\n\nexport function table_getAllColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): Array<Column<TFeatures, TData, unknown>> {\n const recurseColumns = (\n colDefs:
|
|
1
|
+
{"version":3,"file":"coreColumnsFeature.utils.js","sources":["../../../../src/core/columns/coreColumnsFeature.utils.ts"],"sourcesContent":["import { callMemoOrStaticFn } from '../../utils'\nimport { table_getOrderColumnsFn } from '../../features/column-ordering/columnOrderingFeature.utils'\nimport { constructColumn } from './constructColumn'\nimport type { Table_Internal } from '../../types/Table'\nimport type { CellData, RowData } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type {\n ColumnDef,\n ColumnDefResolved,\n GroupColumnDef,\n} from '../../types/ColumnDef'\nimport type { Column } from '../../types/Column'\n\nexport function column_getFlatColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column<TFeatures, TData, TValue>,\n): Array<Column<TFeatures, TData, TValue>> {\n return [column, ...column.columns.flatMap((col) => col.getFlatColumns())]\n}\n\nexport function column_getLeafColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column<TFeatures, TData, TValue>,\n): Array<Column<TFeatures, TData, TValue>> {\n if (column.columns.length) {\n const leafColumns = column.columns.flatMap(\n (col) => col.getLeafColumns(), // recursive\n )\n\n return callMemoOrStaticFn(\n column.table,\n 'getOrderColumns',\n table_getOrderColumnsFn,\n )(leafColumns as any) as any\n }\n\n return [column]\n}\n\nexport function table_getDefaultColumnDef<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): Partial<ColumnDef<TFeatures, TData, unknown>> {\n return {\n header: (props) => {\n const resolvedColumnDef = props.header.column\n .columnDef as ColumnDefResolved<{}, TData>\n\n if (resolvedColumnDef.accessorKey) {\n return resolvedColumnDef.accessorKey\n }\n\n if (resolvedColumnDef.accessorFn) {\n return resolvedColumnDef.id\n }\n\n return null\n },\n cell: (props) => props.renderValue<any>()?.toString?.() ?? null,\n ...Object.values(table._features).reduce((obj, feature) => {\n return Object.assign(obj, feature.getDefaultColumnDef?.())\n }, {}),\n ...table.options.defaultColumn,\n } as Partial<ColumnDef<TFeatures, TData, unknown>>\n}\n\nexport function table_getAllColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): Array<Column<TFeatures, TData, unknown>> {\n const recurseColumns = (\n colDefs: ReadonlyArray<ColumnDef<TFeatures, TData, unknown>>,\n parent?: Column<TFeatures, TData, unknown>,\n depth = 0,\n ): Array<Column<TFeatures, TData, unknown>> => {\n return colDefs.map((columnDef) => {\n const column = constructColumn(table, columnDef, depth, parent)\n\n const groupingColumnDef = columnDef as GroupColumnDef<\n TFeatures,\n TData,\n unknown\n >\n\n column.columns = groupingColumnDef.columns\n ? recurseColumns(groupingColumnDef.columns, column, depth + 1)\n : []\n\n return column\n })\n }\n\n return recurseColumns(table.options.columns as any)\n}\n\nexport function table_getAllFlatColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): Array<Column<TFeatures, TData, unknown>> {\n return table.getAllColumns().flatMap((column) => column.getFlatColumns())\n}\n\nexport function table_getAllFlatColumnsById<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): Record<string, Column<TFeatures, TData, unknown>> {\n return table.getAllFlatColumns().reduce(\n (acc, column) => {\n acc[column.id] = column\n return acc\n },\n {} as Record<string, Column<TFeatures, TData, unknown>>,\n )\n}\n\nexport function table_getAllLeafColumns<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): Array<Column<TFeatures, TData, unknown>> {\n const leafColumns = table.getAllColumns().flatMap(\n (c) => c.getLeafColumns(), // recursive\n )\n return callMemoOrStaticFn(\n table,\n 'getOrderColumns',\n table_getOrderColumnsFn,\n )(leafColumns as any) as any\n}\n\nexport function table_getColumn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n columnId: string,\n): Column<TFeatures, TData, unknown> | undefined {\n const column = table.getAllFlatColumnsById()[columnId]\n\n if (process.env.NODE_ENV === 'development' && !column) {\n console.warn(`[Table] Column with id '${columnId}' does not exist.`)\n }\n\n return column\n}\n"],"names":[],"mappings":";;;AAaO,SAAS,sBAKd,QACyC;AACzC,SAAO,CAAC,QAAQ,GAAG,OAAO,QAAQ,QAAQ,CAAC,QAAQ,IAAI,eAAA,CAAgB,CAAC;AAC1E;AAEO,SAAS,sBAKd,QACyC;AACzC,MAAI,OAAO,QAAQ,QAAQ;AACzB,UAAM,cAAc,OAAO,QAAQ;AAAA,MACjC,CAAC,QAAQ,IAAI,eAAA;AAAA;AAAA,IAAe;AAG9B,WAAO;AAAA,MACL,OAAO;AAAA,MACP;AAAA,MACA;AAAA,IAAA,EACA,WAAkB;AAAA,EACtB;AAEA,SAAO,CAAC,MAAM;AAChB;AAEO,SAAS,0BAId,OAC+C;AAC/C,SAAO;AAAA,IACL,QAAQ,CAAC,UAAU;AACjB,YAAM,oBAAoB,MAAM,OAAO,OACpC;AAEH,UAAI,kBAAkB,aAAa;AACjC,eAAO,kBAAkB;AAAA,MAC3B;AAEA,UAAI,kBAAkB,YAAY;AAChC,eAAO,kBAAkB;AAAA,MAC3B;AAEA,aAAO;AAAA,IACT;AAAA,IACA,MAAM,CAAC,UAAU,MAAM,YAAA,GAAoB,gBAAgB;AAAA,IAC3D,GAAG,OAAO,OAAO,MAAM,SAAS,EAAE,OAAO,CAAC,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row-models/createCoreRowModel.ts"],"sourcesContent":["import { constructRow } from '../rows/constructRow'\nimport { tableMemo } from '../../utils'\nimport { table_autoResetPageIndex } from '../../features/row-pagination/rowPaginationFeature.utils'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { RowModel } from './coreRowModelsFeature.types'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Row } from '../../types/Row'\nimport type { RowData } from '../../types/type-utils'\n\nexport function createCoreRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData> {\n return (_table) => {\n const table = _table as Table_Internal<TFeatures, TData>\n return tableMemo({\n feature: 'coreRowModelsFeature',\n table,\n fnName: 'table.getCoreRowModel',\n memoDeps: () => [table.options.data],\n fn: () => _createCoreRowModel(table, table.options.data),\n onAfterUpdate: () => table_autoResetPageIndex(table),\n })\n }\n}\n\nfunction _createCoreRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n data:
|
|
1
|
+
{"version":3,"file":"createCoreRowModel.js","sources":["../../../../src/core/row-models/createCoreRowModel.ts"],"sourcesContent":["import { constructRow } from '../rows/constructRow'\nimport { tableMemo } from '../../utils'\nimport { table_autoResetPageIndex } from '../../features/row-pagination/rowPaginationFeature.utils'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { RowModel } from './coreRowModelsFeature.types'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Row } from '../../types/Row'\nimport type { RowData } from '../../types/type-utils'\n\nexport function createCoreRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData> {\n return (_table) => {\n const table = _table as Table_Internal<TFeatures, TData>\n return tableMemo({\n feature: 'coreRowModelsFeature',\n table,\n fnName: 'table.getCoreRowModel',\n memoDeps: () => [table.options.data],\n fn: () => _createCoreRowModel(table, table.options.data),\n onAfterUpdate: () => table_autoResetPageIndex(table),\n })\n }\n}\n\nfunction _createCoreRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n data: ReadonlyArray<TData>,\n): {\n rows: Array<Row<TFeatures, TData>>\n flatRows: Array<Row<TFeatures, TData>>\n rowsById: Record<string, Row<TFeatures, TData>>\n} {\n const rowModel: RowModel<TFeatures, TData> = {\n rows: [],\n flatRows: [],\n rowsById: {},\n }\n\n const accessRows = (\n originalRows: ReadonlyArray<TData>,\n depth = 0,\n parentRow?: Row<TFeatures, TData>,\n ): Array<Row<TFeatures, TData>> => {\n const rows = [] as Array<Row<TFeatures, TData>>\n\n for (let i = 0; i < originalRows.length; i++) {\n const originalRow = originalRows[i]!\n // Make the row\n const row = constructRow(\n table,\n table.getRowId(originalRow, i, parentRow),\n originalRow,\n i,\n depth,\n undefined,\n parentRow?.id,\n )\n\n // Keep track of every row in a flat array\n rowModel.flatRows.push(row)\n // Also keep track of every row by its ID\n rowModel.rowsById[row.id] = row\n // Push table row into parent\n rows.push(row)\n\n // Get the original subrows\n if (table.options.getSubRows) {\n row.originalSubRows = table.options.getSubRows(originalRow, i)\n\n // Then recursively access them\n if (row.originalSubRows?.length) {\n row.subRows = accessRows(row.originalSubRows, depth + 1, row)\n }\n }\n }\n\n return rows\n }\n\n rowModel.rows = accessRows(data)\n\n return rowModel\n}\n"],"names":[],"mappings":";;;AASO,SAAS,qBAG0D;AACxE,SAAO,CAAC,WAAW;AACjB,UAAM,QAAQ;AACd,WAAO,UAAU;AAAA,MACf,SAAS;AAAA,MACT;AAAA,MACA,QAAQ;AAAA,MACR,UAAU,MAAM,CAAC,MAAM,QAAQ,IAAI;AAAA,MACnC,IAAI,MAAM,oBAAoB,OAAO,MAAM,QAAQ,IAAI;AAAA,MACvD,eAAe,MAAM,yBAAyB,KAAK;AAAA,IAAA,CACpD;AAAA,EACH;AACF;AAEA,SAAS,oBAIP,OACA,MAKA;AACA,QAAM,WAAuC;AAAA,IAC3C,MAAM,CAAA;AAAA,IACN,UAAU,CAAA;AAAA,IACV,UAAU,CAAA;AAAA,EAAC;AAGb,QAAM,aAAa,CACjB,cACA,QAAQ,GACR,cACiC;AACjC,UAAM,OAAO,CAAA;AAEb,aAAS,IAAI,GAAG,IAAI,aAAa,QAAQ,KAAK;AAC5C,YAAM,cAAc,aAAa,CAAC;AAElC,YAAM,MAAM;AAAA,QACV;AAAA,QACA,MAAM,SAAS,aAAa,GAAG,SAAS;AAAA,QACxC;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,WAAW;AAAA,MAAA;AAIb,eAAS,SAAS,KAAK,GAAG;AAE1B,eAAS,SAAS,IAAI,EAAE,IAAI;AAE5B,WAAK,KAAK,GAAG;AAGb,UAAI,MAAM,QAAQ,YAAY;AAC5B,YAAI,kBAAkB,MAAM,QAAQ,WAAW,aAAa,CAAC;AAG7D,YAAI,IAAI,iBAAiB,QAAQ;AAC/B,cAAI,UAAU,WAAW,IAAI,iBAAiB,QAAQ,GAAG,GAAG;AAAA,QAC9D;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAEA,WAAS,OAAO,WAAW,IAAI;AAE/B,SAAO;AACT;"}
|
|
@@ -25,7 +25,7 @@ export interface Row_CoreProperties<TFeatures extends TableFeatures, TData exten
|
|
|
25
25
|
/**
|
|
26
26
|
* An array of the original subRows as returned by the `options.getSubRows` option.
|
|
27
27
|
*/
|
|
28
|
-
originalSubRows?:
|
|
28
|
+
originalSubRows?: ReadonlyArray<TData>;
|
|
29
29
|
/**
|
|
30
30
|
* If nested, this row's parent row id.
|
|
31
31
|
*/
|
|
@@ -80,7 +80,7 @@ export interface TableOptions_Rows<TFeatures extends TableFeatures, TData extend
|
|
|
80
80
|
* This optional function is used to access the sub rows for any given row. If you are using nested rows, you will need to use this function to return the sub rows object (or undefined) from the row.
|
|
81
81
|
* @example getSubRows: row => row.subRows
|
|
82
82
|
*/
|
|
83
|
-
getSubRows?: (originalRow: TData, index: number) => undefined |
|
|
83
|
+
getSubRows?: (originalRow: TData, index: number) => undefined | ReadonlyArray<TData>;
|
|
84
84
|
}
|
|
85
85
|
export interface Table_Rows<TFeatures extends TableFeatures, TData extends RowData> {
|
|
86
86
|
getRowId: (_: TData, index: number, parent?: Row<TFeatures, TData>) => string;
|
|
@@ -13,16 +13,22 @@ function constructTable(tableOptions) {
|
|
|
13
13
|
const table = {
|
|
14
14
|
_features: { ...coreFeatures, ...tableOptions._features },
|
|
15
15
|
_rowModels: {},
|
|
16
|
-
_rowModelFns: {}
|
|
16
|
+
_rowModelFns: {},
|
|
17
|
+
get options() {
|
|
18
|
+
return this.optionsStore.state;
|
|
19
|
+
},
|
|
20
|
+
set options(value) {
|
|
21
|
+
this.optionsStore.setState(() => value);
|
|
22
|
+
}
|
|
17
23
|
};
|
|
18
24
|
const featuresList = Object.values(table._features);
|
|
19
25
|
const defaultOptions = featuresList.reduce((obj, feature) => {
|
|
20
26
|
return Object.assign(obj, feature.getDefaultTableOptions?.(table));
|
|
21
27
|
}, {});
|
|
22
|
-
table.
|
|
28
|
+
table.optionsStore = createStore({
|
|
23
29
|
...defaultOptions,
|
|
24
30
|
...tableOptions
|
|
25
|
-
};
|
|
31
|
+
});
|
|
26
32
|
table.initialState = getInitialTableState(
|
|
27
33
|
table._features,
|
|
28
34
|
table.options.initialState
|
|
@@ -32,7 +38,7 @@ function constructTable(tableOptions) {
|
|
|
32
38
|
const state = table.baseStore.state;
|
|
33
39
|
return {
|
|
34
40
|
...state,
|
|
35
|
-
...table.
|
|
41
|
+
...table.optionsStore.state.state ?? {}
|
|
36
42
|
};
|
|
37
43
|
});
|
|
38
44
|
if (process.env.NODE_ENV === "development" && (tableOptions.debugAll || tableOptions.debugTable)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constructTable.js","sources":["../../../../src/core/table/constructTable.ts"],"sourcesContent":["import { createStore } from '@tanstack/store'\nimport { coreFeatures } from '../coreFeatures'\nimport type { Store } from '@tanstack/store'\nimport type { RowData } from '../../types/type-utils'\nimport type { TableFeature, TableFeatures } from '../../types/TableFeatures'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { TableOptions } from '../../types/TableOptions'\nimport type { TableState } from '../../types/TableState'\n\nexport function getInitialTableState<TFeatures extends TableFeatures>(\n features: TFeatures,\n initialState: Partial<TableState<TFeatures>> | undefined = {},\n): TableState<TFeatures> {\n Object.values(features).forEach((feature) => {\n initialState =\n feature.getInitialState?.(initialState as TableState<TFeatures>) ??\n initialState\n })\n return structuredClone(initialState) as TableState<TFeatures>\n}\n\nexport function createTableStore<TFeatures extends TableFeatures>(\n features: TFeatures,\n initialState: Partial<TableState<TFeatures>> | undefined = {},\n): Store<TableState<TFeatures>> {\n return createStore(getInitialTableState(features, initialState))\n}\n\nexport function constructTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(tableOptions: TableOptions<TFeatures, TData>): Table<TFeatures, TData> {\n const table = {\n _features: { ...coreFeatures, ...tableOptions._features },\n _rowModels: {},\n _rowModelFns: {},\n } as Table_Internal<TFeatures, TData>\n\n const featuresList: Array<TableFeature<{}>> = Object.values(table._features)\n\n const defaultOptions = featuresList.reduce((obj, feature) => {\n return Object.assign(obj, feature.getDefaultTableOptions?.(table))\n }, {}) as TableOptions<TFeatures, TData>\n\n table.
|
|
1
|
+
{"version":3,"file":"constructTable.js","sources":["../../../../src/core/table/constructTable.ts"],"sourcesContent":["import { createStore } from '@tanstack/store'\nimport { coreFeatures } from '../coreFeatures'\nimport type { Store } from '@tanstack/store'\nimport type { RowData } from '../../types/type-utils'\nimport type { TableFeature, TableFeatures } from '../../types/TableFeatures'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { TableOptions } from '../../types/TableOptions'\nimport type { TableState } from '../../types/TableState'\n\nexport function getInitialTableState<TFeatures extends TableFeatures>(\n features: TFeatures,\n initialState: Partial<TableState<TFeatures>> | undefined = {},\n): TableState<TFeatures> {\n Object.values(features).forEach((feature) => {\n initialState =\n feature.getInitialState?.(initialState as TableState<TFeatures>) ??\n initialState\n })\n return structuredClone(initialState) as TableState<TFeatures>\n}\n\nexport function createTableStore<TFeatures extends TableFeatures>(\n features: TFeatures,\n initialState: Partial<TableState<TFeatures>> | undefined = {},\n): Store<TableState<TFeatures>> {\n return createStore(getInitialTableState(features, initialState))\n}\n\nexport function constructTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(tableOptions: TableOptions<TFeatures, TData>): Table<TFeatures, TData> {\n const table = {\n _features: { ...coreFeatures, ...tableOptions._features },\n _rowModels: {},\n _rowModelFns: {},\n get options() {\n return this.optionsStore.state\n },\n set options(value) {\n this.optionsStore.setState(() => value)\n },\n } as Table_Internal<TFeatures, TData>\n\n const featuresList: Array<TableFeature<{}>> = Object.values(table._features)\n\n const defaultOptions = featuresList.reduce((obj, feature) => {\n return Object.assign(obj, feature.getDefaultTableOptions?.(table))\n }, {}) as TableOptions<TFeatures, TData>\n\n table.optionsStore = createStore({\n ...defaultOptions,\n ...tableOptions,\n })\n\n table.initialState = getInitialTableState(\n table._features,\n table.options.initialState,\n )\n\n table.baseStore = table.options.store ?? createStore(table.initialState)\n\n table.store = createStore(() => {\n const state = table.baseStore.state\n return {\n ...state,\n ...(table.optionsStore.state.state ?? {}),\n }\n })\n\n if (\n process.env.NODE_ENV === 'development' &&\n (tableOptions.debugAll || tableOptions.debugTable)\n ) {\n const features = Object.keys(table._features)\n const rowModels = Object.keys(table.options._rowModels || {})\n const states = Object.keys(table.initialState)\n\n console.log(\n `Constructing Table Instance\n\n Features: ${features.join('\\n ')}\n\n Row Models: ${rowModels.length ? rowModels.join('\\n ') : '(none)'}\n\n States: ${states.join('\\n ')}`,\n )\n }\n\n for (const feature of featuresList) {\n feature.constructTableAPIs?.(table)\n }\n\n return table\n}\n"],"names":[],"mappings":";;AASO,SAAS,qBACd,UACA,eAA2D,IACpC;AACvB,SAAO,OAAO,QAAQ,EAAE,QAAQ,CAAC,YAAY;AAC3C,mBACE,QAAQ,k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AQ;AAAA,MACN;AAAA;AAAA,gBAEU,SAAS,KAAK,kBAAkB,CAAC;AAAA;AAAA,gBAEjC,UAAU,SAAS,UAAU,KAAK,kBAAkB,IAAI,QAAQ;AAAA;AAAA,gBAEhE,OAAO,KAAK,kBAAkB,CAAC;AAAA,IAAA;AAAA,EAE7C;AAEA,aAAW,WAAW,cAAc;AAClC,YAAQ,qBAAqB,KAAK;AAAA,EACpC;AAEA,SAAO;AACT;"}
|
|
@@ -24,7 +24,7 @@ export interface TableOptions_Table<TFeatures extends TableFeatures, TData exten
|
|
|
24
24
|
/**
|
|
25
25
|
* The data for the table to display. When the `data` option changes reference, the table will reprocess the data.
|
|
26
26
|
*/
|
|
27
|
-
data:
|
|
27
|
+
data: ReadonlyArray<TData>;
|
|
28
28
|
/**
|
|
29
29
|
* Use this option to optionally pass initial state to the table. This state will be used when resetting various table states either automatically by the table (eg. `options.autoResetPageIndex`) or via functions like `table.resetRowSelection()`. Most reset function allow you optionally pass a flag to reset to a blank/default state instead of the initial state.
|
|
30
30
|
* Table state will not be reset when this object changes, which also means that the initial state object does not need to be stable.
|
|
@@ -80,6 +80,10 @@ export interface Table_CoreProperties<TFeatures extends TableFeatures, TData ext
|
|
|
80
80
|
* The base store for the table. This can be used to write to the table state.
|
|
81
81
|
*/
|
|
82
82
|
baseStore: Store<TableState<TFeatures>>;
|
|
83
|
+
/**
|
|
84
|
+
* The base store for the table options.
|
|
85
|
+
*/
|
|
86
|
+
optionsStore: Store<TableOptions<TFeatures, TData>>;
|
|
83
87
|
/**
|
|
84
88
|
* This is the resolved initial state of the table.
|
|
85
89
|
*/
|
|
@@ -87,7 +91,7 @@ export interface Table_CoreProperties<TFeatures extends TableFeatures, TData ext
|
|
|
87
91
|
/**
|
|
88
92
|
* A read-only reference to the table's current options.
|
|
89
93
|
*/
|
|
90
|
-
options: TableOptions<TFeatures, TData>;
|
|
94
|
+
readonly options: TableOptions<TFeatures, TData>;
|
|
91
95
|
/**
|
|
92
96
|
* Where the table state is stored.
|
|
93
97
|
*/
|
|
@@ -13,7 +13,8 @@ function table_mergeOptions(table, newOptions) {
|
|
|
13
13
|
}
|
|
14
14
|
function table_setOptions(table, updater) {
|
|
15
15
|
const newOptions = functionalUpdate(updater, table.options);
|
|
16
|
-
|
|
16
|
+
const mergedOptions = table_mergeOptions(table, newOptions);
|
|
17
|
+
table.optionsStore.setState(() => mergedOptions);
|
|
17
18
|
}
|
|
18
19
|
export {
|
|
19
20
|
table_mergeOptions,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coreTablesFeature.utils.js","sources":["../../../../src/core/table/coreTablesFeature.utils.ts"],"sourcesContent":["import { functionalUpdate } from '../../utils'\nimport type { RowData, Updater } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\nimport type { TableOptions } from '../../types/TableOptions'\n\nexport function table_reset<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>): void {\n table.baseStore.setState(() => structuredClone(table.initialState))\n}\n\nexport function table_mergeOptions<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n newOptions: TableOptions<TFeatures, TData>,\n) {\n if (table.options.mergeOptions) {\n return table.options.mergeOptions(table.options, newOptions)\n }\n\n return {\n ...table.options,\n ...newOptions,\n }\n}\n\nexport function table_setOptions<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n updater: Updater<TableOptions<TFeatures, TData>>,\n): void {\n const newOptions = functionalUpdate(updater, table.options)\n
|
|
1
|
+
{"version":3,"file":"coreTablesFeature.utils.js","sources":["../../../../src/core/table/coreTablesFeature.utils.ts"],"sourcesContent":["import { functionalUpdate } from '../../utils'\nimport type { RowData, Updater } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\nimport type { TableOptions } from '../../types/TableOptions'\n\nexport function table_reset<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>): void {\n table.baseStore.setState(() => structuredClone(table.initialState))\n}\n\nexport function table_mergeOptions<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n newOptions: TableOptions<TFeatures, TData>,\n) {\n if (table.options.mergeOptions) {\n return table.options.mergeOptions(table.options, newOptions)\n }\n\n return {\n ...table.options,\n ...newOptions,\n }\n}\n\nexport function table_setOptions<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n updater: Updater<TableOptions<TFeatures, TData>>,\n): void {\n const newOptions = functionalUpdate(updater, table.options)\n const mergedOptions = table_mergeOptions(table, newOptions)\n table.optionsStore.setState(() => mergedOptions)\n}\n"],"names":[],"mappings":";AAMO,SAAS,YAGd,OAA+C;AAC/C,QAAM,UAAU,SAAS,MAAM,gBAAgB,MAAM,YAAY,CAAC;AACpE;AAEO,SAAS,mBAId,OACA,YACA;AACA,MAAI,MAAM,QAAQ,cAAc;AAC9B,WAAO,MAAM,QAAQ,aAAa,MAAM,SAAS,UAAU;AAAA,EAC7D;AAEA,SAAO;AAAA,IACL,GAAG,MAAM;AAAA,IACT,GAAG;AAAA,EAAA;AAEP;AAEO,SAAS,iBAId,OACA,SACM;AACN,QAAM,aAAa,iBAAiB,SAAS,MAAM,OAAO;AAC1D,QAAM,gBAAgB,mBAAmB,OAAO,UAAU;AAC1D,QAAM,aAAa,SAAS,MAAM,aAAa;AACjD;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { TableFeature, TableFeatures } from '../../types/TableFeatures.js';
|
|
2
|
+
import { RowData } from '../../types/type-utils.js';
|
|
3
|
+
interface TableReactivityFeatureConstructors<TFeatures extends TableFeatures, TData extends RowData> {
|
|
4
|
+
}
|
|
5
|
+
export declare function constructReactivityFeature<TFeatures extends TableFeatures, TData extends RowData>(bindings: {
|
|
6
|
+
stateNotifier?: () => unknown;
|
|
7
|
+
optionsNotifier?: () => unknown;
|
|
8
|
+
}): TableFeature<TableReactivityFeatureConstructors<TFeatures, TData>>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
function constructReactivityFeature(bindings) {
|
|
2
|
+
return {
|
|
3
|
+
constructTableAPIs: (table) => {
|
|
4
|
+
table.store = bindStore(table.store, bindings.stateNotifier);
|
|
5
|
+
table.optionsStore = bindStore(
|
|
6
|
+
table.optionsStore,
|
|
7
|
+
bindings.optionsNotifier
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
const bindStore = (store, notifier) => {
|
|
13
|
+
const stateDescriptor = Object.getOwnPropertyDescriptor(
|
|
14
|
+
Object.getPrototypeOf(store),
|
|
15
|
+
"state"
|
|
16
|
+
);
|
|
17
|
+
Object.defineProperty(store, "state", {
|
|
18
|
+
configurable: true,
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get() {
|
|
21
|
+
notifier?.();
|
|
22
|
+
return stateDescriptor.get.call(store);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return store;
|
|
26
|
+
};
|
|
27
|
+
export {
|
|
28
|
+
constructReactivityFeature
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=tableReactivityFeature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tableReactivityFeature.js","sources":["../../../../src/features/table-reactivity/tableReactivityFeature.ts"],"sourcesContent":["import type { ReadonlyStore, Store } from '@tanstack/store'\nimport type { TableFeature, TableFeatures } from '../../types/TableFeatures'\nimport type { RowData } from '../../types/type-utils'\n\ninterface TableReactivityFeatureConstructors<\n TFeatures extends TableFeatures,\n TData extends RowData,\n> {}\n\nexport function constructReactivityFeature<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(bindings: {\n stateNotifier?: () => unknown\n optionsNotifier?: () => unknown\n}): TableFeature<TableReactivityFeatureConstructors<TFeatures, TData>> {\n return {\n constructTableAPIs: (table) => {\n table.store = bindStore(table.store, bindings.stateNotifier)\n table.optionsStore = bindStore(\n table.optionsStore,\n bindings.optionsNotifier,\n )\n },\n }\n}\n\nconst bindStore = <T extends Store<any> | ReadonlyStore<any>>(\n store: T,\n notifier?: () => unknown,\n): T => {\n const stateDescriptor = Object.getOwnPropertyDescriptor(\n Object.getPrototypeOf(store),\n 'state',\n )!\n\n Object.defineProperty(store, 'state', {\n configurable: true,\n enumerable: true,\n get() {\n notifier?.()\n return stateDescriptor.get!.call(store)\n },\n })\n\n return store\n}\n"],"names":[],"mappings":"AASO,SAAS,2BAGd,UAGqE;AACrE,SAAO;AAAA,IACL,oBAAoB,CAAC,UAAU;AAC7B,YAAM,QAAQ,UAAU,MAAM,OAAO,SAAS,aAAa;AAC3D,YAAM,eAAe;AAAA,QACnB,MAAM;AAAA,QACN,SAAS;AAAA,MAAA;AAAA,IAEb;AAAA,EAAA;AAEJ;AAEA,MAAM,YAAY,CAChB,OACA,aACM;AACN,QAAM,kBAAkB,OAAO;AAAA,IAC7B,OAAO,eAAe,KAAK;AAAA,IAC3B;AAAA,EAAA;AAGF,SAAO,eAAe,OAAO,SAAS;AAAA,IACpC,cAAc;AAAA,IACd,YAAY;AAAA,IACZ,MAAM;AACJ,iBAAA;AACA,aAAO,gBAAgB,IAAK,KAAK,KAAK;AAAA,IACxC;AAAA,EAAA,CACD;AAED,SAAO;AACT;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -58,6 +58,7 @@ export * from './fns/sortFns.js';
|
|
|
58
58
|
* Features
|
|
59
59
|
*/
|
|
60
60
|
export * from './features/stockFeatures.js';
|
|
61
|
+
export * from './features/table-reactivity/tableReactivityFeature.js';
|
|
61
62
|
export * from './features/column-faceting/columnFacetingFeature.js';
|
|
62
63
|
export * from './features/column-faceting/columnFacetingFeature.types';
|
|
63
64
|
export * from './features/column-faceting/columnFacetingFeature.utils';
|
package/dist/esm/index.js
CHANGED
|
@@ -27,6 +27,7 @@ import { aggregationFn_count, aggregationFn_extent, aggregationFn_max, aggregati
|
|
|
27
27
|
import { filterFn_arrHas, filterFn_arrIncludes, filterFn_arrIncludesAll, filterFn_arrIncludesSome, filterFn_equals, filterFn_equalsString, filterFn_equalsStringSensitive, filterFn_greaterThan, filterFn_greaterThanOrEqualTo, filterFn_inNumberRange, filterFn_includesString, filterFn_includesStringSensitive, filterFn_lessThan, filterFn_lessThanOrEqualTo, filterFn_weakEquals, filterFns } from "./fns/filterFns.js";
|
|
28
28
|
import { reSplitAlphaNumeric, sortFn_alphanumeric, sortFn_alphanumericCaseSensitive, sortFn_basic, sortFn_datetime, sortFn_text, sortFn_textCaseSensitive, sortFns } from "./fns/sortFns.js";
|
|
29
29
|
import { stockFeatures } from "./features/stockFeatures.js";
|
|
30
|
+
import { constructReactivityFeature } from "./features/table-reactivity/tableReactivityFeature.js";
|
|
30
31
|
import { columnFacetingFeature, constructColumnFacetingFeature } from "./features/column-faceting/columnFacetingFeature.js";
|
|
31
32
|
import { column_getFacetedMinMaxValues, column_getFacetedRowModel, column_getFacetedUniqueValues, table_getGlobalFacetedMinMaxValues, table_getGlobalFacetedRowModel, table_getGlobalFacetedUniqueValues } from "./features/column-faceting/columnFacetingFeature.utils.js";
|
|
32
33
|
import { createFacetedMinMaxValues } from "./features/column-faceting/createFacetedMinMaxValues.js";
|
|
@@ -160,6 +161,7 @@ export {
|
|
|
160
161
|
constructCoreTablesFeature,
|
|
161
162
|
constructGlobalFilteringFeature,
|
|
162
163
|
constructHeader,
|
|
164
|
+
constructReactivityFeature,
|
|
163
165
|
constructRow,
|
|
164
166
|
constructRowExpandingFeature,
|
|
165
167
|
constructRowPaginationFeature,
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -44,7 +44,7 @@ export type IdentifiedColumnDef<TFeatures extends TableFeatures, TData extends R
|
|
|
44
44
|
};
|
|
45
45
|
export type DisplayColumnDef<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData> = ColumnDefBase<TFeatures, TData, TValue> & ColumnIdentifiers<TFeatures, TData, TValue>;
|
|
46
46
|
type GroupColumnDefBase<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData> = ColumnDefBase<TFeatures, TData, TValue> & {
|
|
47
|
-
columns?:
|
|
47
|
+
columns?: ReadonlyArray<ColumnDef<TFeatures, TData, unknown>>;
|
|
48
48
|
};
|
|
49
49
|
export type GroupColumnDef<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData> = GroupColumnDefBase<TFeatures, TData, TValue> & ColumnIdentifiers<TFeatures, TData, TValue>;
|
|
50
50
|
export type AccessorFnColumnDefBase<TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData> = ColumnDefBase<TFeatures, TData, TValue> & {
|
package/dist/esm/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import type { Table, Table_Internal } from './types/Table'\nimport type { NoInfer, RowData, Updater } from './types/type-utils'\nimport type { TableFeatures } from './types/TableFeatures'\nimport type { TableState, TableState_All } from './types/TableState'\n\nexport function functionalUpdate<T>(updater: Updater<T>, input: T): T {\n return typeof updater === 'function'\n ? (updater as (i: T) => T)(input)\n : updater\n}\n\nexport function noop() {}\n\nexport function makeStateUpdater<\n TFeatures extends TableFeatures,\n K extends (string & {}) | keyof TableState_All | keyof TableState<TFeatures>,\n>(key: K, instance: Table<TFeatures, any>) {\n return (updater: Updater<TableState<any>[K & keyof TableState<any>]>) => {\n ;(instance as Table_Internal<any, any>).baseStore.setState(\n <TTableState extends TableState_All>(old: TTableState) => {\n return {\n ...old,\n [key]: functionalUpdate(updater, (old as any)[key]),\n }\n },\n )\n }\n}\n\ntype AnyFunction = (...args: any) => any\n\nexport function isFunction<T extends AnyFunction>(d: any): d is T {\n return d instanceof Function\n}\n\nexport function isNumberArray(d: any): d is Array<number> {\n return Array.isArray(d) && d.every((val) => typeof val === 'number')\n}\n\nexport function flattenBy<TNode>(\n arr: Array<TNode>,\n getChildren: (item: TNode) => Array<TNode>,\n) {\n const flat: Array<TNode> = []\n\n const recurse = (subArr: Array<TNode>) => {\n subArr.forEach((item) => {\n flat.push(item)\n const children = getChildren(item)\n if (children.length) {\n recurse(children)\n }\n })\n }\n\n recurse(arr)\n\n return flat\n}\n\nexport const $internalMemoFnMeta = Symbol('memoFnMeta')\nexport type MemoFnMeta = { originalArgsLength?: number }\n\n/**\n * @internal\n */\nfunction setMemoFnMeta(fn: Function, meta: MemoFnMeta) {\n Object.defineProperty(fn, $internalMemoFnMeta, { value: meta })\n}\n\n/**\n * @internal\n */\nexport function getMemoFnMeta(fn: any): MemoFnMeta | null {\n return (typeof fn === 'function' && fn[$internalMemoFnMeta]) ?? null\n}\n\ninterface MemoOptions<TDeps extends ReadonlyArray<any>, TDepArgs, TResult> {\n fn: (...args: NoInfer<TDeps>) => TResult\n memoDeps?: (depArgs?: TDepArgs) => [...TDeps] | undefined\n onAfterCompare?: (depsChanged: boolean) => void\n onAfterUpdate?: (result: TResult) => void\n onBeforeCompare?: () => void\n onBeforeUpdate?: () => void\n}\n\nexport const memo = <TDeps extends ReadonlyArray<any>, TDepArgs, TResult>({\n fn,\n memoDeps,\n onAfterCompare,\n onAfterUpdate,\n onBeforeCompare,\n onBeforeUpdate,\n}: MemoOptions<TDeps, TDepArgs, TResult>): ((\n depArgs?: TDepArgs,\n) => TResult) => {\n let deps: Array<any> | undefined = []\n let result: TResult | undefined\n\n const memoizedFn = (depArgs?: TDepArgs): TResult => {\n onBeforeCompare?.()\n const newDeps = memoDeps?.(depArgs)\n const depsChanged =\n !newDeps ||\n newDeps.length !== deps?.length ||\n newDeps.some((dep: any, index: number) => deps?.[index] !== dep)\n onAfterCompare?.(depsChanged)\n\n if (!depsChanged) {\n return result!\n }\n\n deps = newDeps\n\n onBeforeUpdate?.()\n result = fn(...(newDeps ?? ([] as any)))\n onAfterUpdate?.(result)\n\n return result\n }\n\n setMemoFnMeta(memoizedFn, { originalArgsLength: fn.length })\n\n return memoizedFn\n}\n\ninterface TableMemoOptions<\n TFeatures extends TableFeatures,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n TResult,\n> extends MemoOptions<TDeps, TDepArgs, TResult> {\n feature?: keyof TFeatures & string\n fnName: string\n objectId?: string\n onAfterUpdate?: () => void\n table: Table_Internal<TFeatures, any>\n}\n\nconst pad = (str: number | string, num: number) => {\n str = String(str)\n while (str.length < num) {\n str = ' ' + str\n }\n return str\n}\n\nexport function tableMemo<\n TFeatures extends TableFeatures,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n TResult,\n>({\n feature,\n fnName,\n objectId,\n onAfterUpdate,\n table,\n ...memoOptions\n}: TableMemoOptions<TFeatures, TDeps, TDepArgs, TResult>) {\n let beforeCompareTime: number\n let afterCompareTime: number\n let startCalcTime: number\n let endCalcTime: number\n let runCount = 0\n let debug: boolean | undefined\n let debugCache: boolean | undefined\n\n if (process.env.NODE_ENV === 'development') {\n const { debugCache: _debugCache, debugAll } = table.options\n debugCache = _debugCache\n const { parentName } = getFunctionNameInfo(fnName, '.')\n\n const debugByParent =\n // @ts-expect-error\n table.options[\n `debug${(parentName != 'table' ? parentName + 's' : parentName).replace(\n parentName,\n parentName.charAt(0).toUpperCase() + parentName.slice(1),\n )}`\n ]\n const debugByFeature = feature\n ? // @ts-expect-error\n table.options[\n `debug${feature.charAt(0).toUpperCase() + feature.slice(1)}`\n ]\n : false\n\n debug = debugAll || debugByParent || debugByFeature\n }\n\n function logTime(time: number, depsChanged: boolean) {\n const runType =\n runCount === 0\n ? '(1st run)'\n : depsChanged\n ? '(rerun #' + runCount + ')'\n : '(cache)'\n runCount++\n\n console.groupCollapsed(\n `%c⏱ ${pad(`${time.toFixed(1)} ms`, 12)} %c${runType}%c ${fnName}%c ${objectId ? `(${fnName.split('.')[0]}Id: ${objectId})` : ''}`,\n `font-size: .6rem; font-weight: bold; ${\n depsChanged\n ? `color: hsl(\n ${Math.max(0, Math.min(120 - Math.log10(time) * 60, 120))}deg 100% 31%);`\n : ''\n } `,\n `color: ${runCount < 2 ? '#FF00FF' : '#FF1493'}`,\n 'color: #666',\n 'color: #87CEEB',\n )\n console.info({\n feature,\n state: table.store.state,\n deps: memoOptions.memoDeps?.toString(),\n })\n console.trace()\n console.groupEnd()\n }\n\n const debugOptions =\n process.env.NODE_ENV === 'development'\n ? {\n onBeforeCompare: () => {\n if (debugCache) {\n beforeCompareTime = performance.now()\n }\n },\n onAfterCompare: (depsChanged: boolean) => {\n if (debugCache) {\n afterCompareTime = performance.now()\n const compareTime =\n Math.round((afterCompareTime - beforeCompareTime) * 100) / 100\n if (!depsChanged) {\n logTime(compareTime, depsChanged)\n }\n }\n },\n onBeforeUpdate: () => {\n if (debug) {\n startCalcTime = performance.now()\n }\n },\n onAfterUpdate: () => {\n if (debug) {\n endCalcTime = performance.now()\n const executionTime =\n Math.round((endCalcTime - startCalcTime) * 100) / 100\n logTime(executionTime, true)\n }\n queueMicrotask(() => onAfterUpdate?.())\n },\n }\n : {\n onAfterUpdate: () => {\n queueMicrotask(() => onAfterUpdate?.())\n },\n }\n\n return memo({\n ...memoOptions,\n ...debugOptions,\n })\n}\n\nexport interface API<TDeps extends ReadonlyArray<any>, TDepArgs> {\n fn: (...args: any) => any\n memoDeps?: (depArgs?: any) => [...any] | undefined\n}\n\nexport type APIObject<TDeps extends ReadonlyArray<any>, TDepArgs> = Record<\n string,\n API<TDeps, TDepArgs>\n>\n\n/**\n * Assumes that a function name is in the format of `parentName_fnKey` and returns the `fnKey` and `fnName` in the format of `parentName.fnKey`.\n */\nexport function getFunctionNameInfo(\n staticFnName: string,\n splitBy: '_' | '.' = '_',\n) {\n const [parentName, fnKey] = staticFnName.split(splitBy)\n const fnName = `${parentName}.${fnKey}`\n return { fnKey, fnName, parentName } as {\n fnKey: string\n fnName: string\n parentName: string\n }\n}\n\n/**\n * Assigns Table API methods directly to the table instance.\n * Unlike row/cell/column/header, the table is a singleton so methods are assigned directly.\n */\nexport function assignTableAPIs<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n>(\n feature: keyof TFeatures & string,\n table: Table_Internal<TFeatures, TData>,\n apis: APIObject<TDeps, NoInfer<TDepArgs>>,\n): void {\n for (const [staticFnName, { fn, memoDeps }] of Object.entries(apis)) {\n const { fnKey, fnName } = getFunctionNameInfo(staticFnName)\n\n ;(table as Record<string, any>)[fnKey] = memoDeps\n ? tableMemo({\n memoDeps,\n fn,\n fnName,\n table,\n feature,\n })\n : fn\n }\n}\n\nexport interface PrototypeAPI<TDeps extends ReadonlyArray<any>, TDepArgs> {\n fn: (self: any, ...args: any) => any\n memoDeps?: (self: any, depArgs?: any) => [...any] | undefined\n}\n\nexport type PrototypeAPIObject<\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n> = Record<string, PrototypeAPI<TDeps, TDepArgs>>\n\n/**\n * Assigns API methods to a prototype object for memory-efficient method sharing.\n * All instances created with this prototype will share the same method references.\n *\n * For memoized methods, the memo state is lazily created and stored on each instance.\n * This provides the best of both worlds: shared method code + per-instance caching.\n */\nexport function assignPrototypeAPIs<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n>(\n feature: keyof TFeatures & string,\n prototype: Record<string, any>,\n table: Table_Internal<TFeatures, TData>,\n apis: PrototypeAPIObject<TDeps, NoInfer<TDepArgs>>,\n): void {\n for (const [staticFnName, { fn, memoDeps }] of Object.entries(apis)) {\n const { fnKey, fnName } = getFunctionNameInfo(staticFnName)\n\n if (memoDeps) {\n // For memoized methods, create a function that lazily initializes\n // the memo on first access and stores it on the instance\n const memoKey = `_memo_${fnKey}`\n\n prototype[fnKey] = function (this: any, ...args: Array<any>) {\n // Lazily create memo on first access for this instance\n if (!this[memoKey]) {\n const self = this\n this[memoKey] = tableMemo({\n memoDeps: () => memoDeps(self),\n fn: (...deps) => fn(self, ...deps),\n fnName,\n objectId: self.id,\n table,\n feature,\n })\n }\n return this[memoKey](...args)\n }\n } else {\n // Non-memoized methods just call the static function with `this`\n prototype[fnKey] = function (this: any, ...args: Array<any>) {\n return fn(this, ...args)\n }\n }\n\n setMemoFnMeta(prototype[fnKey], { originalArgsLength: fn.length })\n }\n}\n\n/**\n * Looks to run the memoized function with the builder pattern on the object if it exists, otherwise fallback to the static method passed in.\n */\nexport function callMemoOrStaticFn<\n TObject extends Record<string, any>,\n TStaticFn extends AnyFunction,\n>(\n obj: TObject,\n fnKey: string,\n staticFn: TStaticFn,\n ...args: Parameters<TStaticFn> extends [any, ...infer Rest] ? Rest : never\n): ReturnType<TStaticFn> {\n return (\n (obj[fnKey] as Function | undefined)?.(...args) ?? staticFn(obj, ...args)\n )\n}\n"],"names":[],"mappings":"AAKO,SAAS,iBAAoB,SAAqB,OAAa;AACpE,SAAO,OAAO,YAAY,aACrB,QAAwB,KAAK,IAC9B;AACN;AAEO,SAAS,OAAO;AAAC;AAEjB,SAAS,iBAGd,KAAQ,UAAiC;AACzC,SAAO,CAAC,YAAiE;AACrE,aAAsC,UAAU;AAAA,MAChD,CAAqC,QAAqB;AACxD,eAAO;AAAA,UACL,GAAG;AAAA,UACH,CAAC,GAAG,GAAG,iBAAiB,SAAU,IAAY,GAAG,CAAC;AAAA,QAAA;AAAA,MAEtD;AAAA,IAAA;AAAA,EAEJ;AACF;AAIO,SAAS,WAAkC,GAAgB;AAChE,SAAO,aAAa;AACtB;AAEO,SAAS,cAAc,GAA4B;AACxD,SAAO,MAAM,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,OAAO,Q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WAAW,oBAAoB,YAAY;AAExD,UAA8B,KAAK,IAAI,WACrC,UAAU;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA,CACD,IACD;AAAA,EACN;AACF;AAmBO,SAAS,oBAMd,SACA,WACA,OACA,MACM;AACN,aAAW,CAAC,cAAc,EAAE,IAAI,SAAA,CAAU,KAAK,OAAO,QAAQ,IAAI,GAAG;AACnE,UAAM,EAAE,OAAO,WAAW,oBAAoB,YAAY;AAE1D,QAAI,UAAU;AAGZ,YAAM,UAAU,SAAS,KAAK;AAE9B,gBAAU,KAAK,IAAI,YAAwB,MAAkB;AAE3D,YAAI,CAAC,KAAK,OAAO,GAAG;AAClB,gBAAM,OAAO;AACb,eAAK,OAAO,IAAI,UAAU;AAAA,YACxB,UAAU,MAAM,SAAS,IAAI;AAAA,YAC7B,IAAI,IAAI,SAAS,GAAG,MAAM,GAAG,IAAI;AAAA,YACjC;AAAA,YACA,UAAU,KAAK;AAAA,YACf;AAAA,YACA;AAAA,UAAA,CACD;AAAA,QACH;AACA,eAAO,KAAK,OAAO,EAAE,GAAG,IAAI;AAAA,MAC9B;AAAA,IACF,OAAO;AAEL,gBAAU,KAAK,IAAI,YAAwB,MAAkB;AAC3D,eAAO,GAAG,MAAM,GAAG,IAAI;AAAA,MACzB;AAAA,IACF;AAEA,kBAAc,UAAU,KAAK,GAAG,EAAE,oBAAoB,GAAG,QAAQ;AAAA,EACnE;AACF;AAKO,SAAS,mBAId,KACA,OACA,aACG,MACoB;AACvB,SACG,IAAI,KAAK,IAA6B,GAAG,IAAI,KAAK,SAAS,KAAK,GAAG,IAAI;AAE5E;"}
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import type { Table, Table_Internal } from './types/Table'\nimport type { NoInfer, RowData, Updater } from './types/type-utils'\nimport type { TableFeatures } from './types/TableFeatures'\nimport type { TableState, TableState_All } from './types/TableState'\n\nexport function functionalUpdate<T>(updater: Updater<T>, input: T): T {\n return typeof updater === 'function'\n ? (updater as (i: T) => T)(input)\n : updater\n}\n\nexport function noop() {}\n\nexport function makeStateUpdater<\n TFeatures extends TableFeatures,\n K extends (string & {}) | keyof TableState_All | keyof TableState<TFeatures>,\n>(key: K, instance: Table<TFeatures, any>) {\n return (updater: Updater<TableState<any>[K & keyof TableState<any>]>) => {\n instance.baseStore.setState(\n <TTableState extends TableState_All>(old: TTableState) => {\n return {\n ...old,\n [key]: functionalUpdate(updater, (old as any)[key]),\n }\n },\n )\n }\n}\n\ntype AnyFunction = (...args: any) => any\n\nexport function isFunction<T extends AnyFunction>(d: any): d is T {\n return d instanceof Function\n}\n\nexport function isNumberArray(d: any): d is Array<number> {\n return Array.isArray(d) && d.every((val) => typeof val === 'number')\n}\n\nexport function flattenBy<TNode>(\n arr: Array<TNode>,\n getChildren: (item: TNode) => Array<TNode>,\n) {\n const flat: Array<TNode> = []\n\n const recurse = (subArr: Array<TNode>) => {\n subArr.forEach((item) => {\n flat.push(item)\n const children = getChildren(item)\n if (children.length) {\n recurse(children)\n }\n })\n }\n\n recurse(arr)\n\n return flat\n}\n\nexport const $internalMemoFnMeta = Symbol('memoFnMeta')\nexport type MemoFnMeta = { originalArgsLength?: number }\n\n/**\n * @internal\n */\nfunction setMemoFnMeta(fn: Function, meta: MemoFnMeta) {\n Object.defineProperty(fn, $internalMemoFnMeta, { value: meta })\n}\n\n/**\n * @internal\n */\nexport function getMemoFnMeta(fn: any): MemoFnMeta | null {\n return (typeof fn === 'function' && fn[$internalMemoFnMeta]) ?? null\n}\n\ninterface MemoOptions<TDeps extends ReadonlyArray<any>, TDepArgs, TResult> {\n fn: (...args: NoInfer<TDeps>) => TResult\n memoDeps?: (depArgs?: TDepArgs) => [...TDeps] | undefined\n onAfterCompare?: (depsChanged: boolean) => void\n onAfterUpdate?: (result: TResult) => void\n onBeforeCompare?: () => void\n onBeforeUpdate?: () => void\n}\n\nexport const memo = <TDeps extends ReadonlyArray<any>, TDepArgs, TResult>({\n fn,\n memoDeps,\n onAfterCompare,\n onAfterUpdate,\n onBeforeCompare,\n onBeforeUpdate,\n}: MemoOptions<TDeps, TDepArgs, TResult>): ((\n depArgs?: TDepArgs,\n) => TResult) => {\n let deps: Array<any> | undefined = []\n let result: TResult | undefined\n\n const memoizedFn = (depArgs?: TDepArgs): TResult => {\n onBeforeCompare?.()\n const newDeps = memoDeps?.(depArgs)\n const depsChanged =\n !newDeps ||\n newDeps.length !== deps?.length ||\n newDeps.some((dep: any, index: number) => deps?.[index] !== dep)\n onAfterCompare?.(depsChanged)\n\n if (!depsChanged) {\n return result!\n }\n\n deps = newDeps\n\n onBeforeUpdate?.()\n result = fn(...(newDeps ?? ([] as any)))\n onAfterUpdate?.(result)\n\n return result\n }\n\n setMemoFnMeta(memoizedFn, { originalArgsLength: fn.length })\n\n return memoizedFn\n}\n\ninterface TableMemoOptions<\n TFeatures extends TableFeatures,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n TResult,\n> extends MemoOptions<TDeps, TDepArgs, TResult> {\n feature?: keyof TFeatures & string\n fnName: string\n objectId?: string\n onAfterUpdate?: () => void\n table: Table_Internal<TFeatures, any>\n}\n\nconst pad = (str: number | string, num: number) => {\n str = String(str)\n while (str.length < num) {\n str = ' ' + str\n }\n return str\n}\n\nexport function tableMemo<\n TFeatures extends TableFeatures,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n TResult,\n>({\n feature,\n fnName,\n objectId,\n onAfterUpdate,\n table,\n ...memoOptions\n}: TableMemoOptions<TFeatures, TDeps, TDepArgs, TResult>) {\n let beforeCompareTime: number\n let afterCompareTime: number\n let startCalcTime: number\n let endCalcTime: number\n let runCount = 0\n let debug: boolean | undefined\n let debugCache: boolean | undefined\n\n if (process.env.NODE_ENV === 'development') {\n const { debugCache: _debugCache, debugAll } = table.options\n debugCache = _debugCache\n const { parentName } = getFunctionNameInfo(fnName, '.')\n\n const debugByParent =\n // @ts-expect-error\n table.options[\n `debug${(parentName != 'table' ? parentName + 's' : parentName).replace(\n parentName,\n parentName.charAt(0).toUpperCase() + parentName.slice(1),\n )}`\n ]\n const debugByFeature = feature\n ? // @ts-expect-error\n table.options[\n `debug${feature.charAt(0).toUpperCase() + feature.slice(1)}`\n ]\n : false\n\n debug = debugAll || debugByParent || debugByFeature\n }\n\n function logTime(time: number, depsChanged: boolean) {\n const runType =\n runCount === 0\n ? '(1st run)'\n : depsChanged\n ? '(rerun #' + runCount + ')'\n : '(cache)'\n runCount++\n\n console.groupCollapsed(\n `%c⏱ ${pad(`${time.toFixed(1)} ms`, 12)} %c${runType}%c ${fnName}%c ${objectId ? `(${fnName.split('.')[0]}Id: ${objectId})` : ''}`,\n `font-size: .6rem; font-weight: bold; ${\n depsChanged\n ? `color: hsl(\n ${Math.max(0, Math.min(120 - Math.log10(time) * 60, 120))}deg 100% 31%);`\n : ''\n } `,\n `color: ${runCount < 2 ? '#FF00FF' : '#FF1493'}`,\n 'color: #666',\n 'color: #87CEEB',\n )\n console.info({\n feature,\n state: table.store.state,\n deps: memoOptions.memoDeps?.toString(),\n })\n console.trace()\n console.groupEnd()\n }\n\n const debugOptions =\n process.env.NODE_ENV === 'development'\n ? {\n onBeforeCompare: () => {\n if (debugCache) {\n beforeCompareTime = performance.now()\n }\n },\n onAfterCompare: (depsChanged: boolean) => {\n if (debugCache) {\n afterCompareTime = performance.now()\n const compareTime =\n Math.round((afterCompareTime - beforeCompareTime) * 100) / 100\n if (!depsChanged) {\n logTime(compareTime, depsChanged)\n }\n }\n },\n onBeforeUpdate: () => {\n if (debug) {\n startCalcTime = performance.now()\n }\n },\n onAfterUpdate: () => {\n if (debug) {\n endCalcTime = performance.now()\n const executionTime =\n Math.round((endCalcTime - startCalcTime) * 100) / 100\n logTime(executionTime, true)\n }\n queueMicrotask(() => onAfterUpdate?.())\n },\n }\n : {\n onAfterUpdate: () => {\n queueMicrotask(() => onAfterUpdate?.())\n },\n }\n\n return memo({\n ...memoOptions,\n ...debugOptions,\n })\n}\n\nexport interface API<TDeps extends ReadonlyArray<any>, TDepArgs> {\n fn: (...args: any) => any\n memoDeps?: (depArgs?: any) => [...any] | undefined\n}\n\nexport type APIObject<TDeps extends ReadonlyArray<any>, TDepArgs> = Record<\n string,\n API<TDeps, TDepArgs>\n>\n\n/**\n * Assumes that a function name is in the format of `parentName_fnKey` and returns the `fnKey` and `fnName` in the format of `parentName.fnKey`.\n */\nexport function getFunctionNameInfo(\n staticFnName: string,\n splitBy: '_' | '.' = '_',\n) {\n const [parentName, fnKey] = staticFnName.split(splitBy)\n const fnName = `${parentName}.${fnKey}`\n return { fnKey, fnName, parentName } as {\n fnKey: string\n fnName: string\n parentName: string\n }\n}\n\n/**\n * Assigns Table API methods directly to the table instance.\n * Unlike row/cell/column/header, the table is a singleton so methods are assigned directly.\n */\nexport function assignTableAPIs<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n>(\n feature: keyof TFeatures & string,\n table: Table_Internal<TFeatures, TData>,\n apis: APIObject<TDeps, NoInfer<TDepArgs>>,\n): void {\n for (const [staticFnName, { fn, memoDeps }] of Object.entries(apis)) {\n const { fnKey, fnName } = getFunctionNameInfo(staticFnName)\n\n ;(table as Record<string, any>)[fnKey] = memoDeps\n ? tableMemo({\n memoDeps,\n fn,\n fnName,\n table,\n feature,\n })\n : fn\n }\n}\n\nexport interface PrototypeAPI<TDeps extends ReadonlyArray<any>, TDepArgs> {\n fn: (self: any, ...args: any) => any\n memoDeps?: (self: any, depArgs?: any) => [...any] | undefined\n}\n\nexport type PrototypeAPIObject<\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n> = Record<string, PrototypeAPI<TDeps, TDepArgs>>\n\n/**\n * Assigns API methods to a prototype object for memory-efficient method sharing.\n * All instances created with this prototype will share the same method references.\n *\n * For memoized methods, the memo state is lazily created and stored on each instance.\n * This provides the best of both worlds: shared method code + per-instance caching.\n */\nexport function assignPrototypeAPIs<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TDeps extends ReadonlyArray<any>,\n TDepArgs,\n>(\n feature: keyof TFeatures & string,\n prototype: Record<string, any>,\n table: Table_Internal<TFeatures, TData>,\n apis: PrototypeAPIObject<TDeps, NoInfer<TDepArgs>>,\n): void {\n for (const [staticFnName, { fn, memoDeps }] of Object.entries(apis)) {\n const { fnKey, fnName } = getFunctionNameInfo(staticFnName)\n\n if (memoDeps) {\n // For memoized methods, create a function that lazily initializes\n // the memo on first access and stores it on the instance\n const memoKey = `_memo_${fnKey}`\n\n prototype[fnKey] = function (this: any, ...args: Array<any>) {\n // Lazily create memo on first access for this instance\n if (!this[memoKey]) {\n const self = this\n this[memoKey] = tableMemo({\n memoDeps: () => memoDeps(self),\n fn: (...deps) => fn(self, ...deps),\n fnName,\n objectId: self.id,\n table,\n feature,\n })\n }\n return this[memoKey](...args)\n }\n } else {\n // Non-memoized methods just call the static function with `this`\n prototype[fnKey] = function (this: any, ...args: Array<any>) {\n return fn(this, ...args)\n }\n }\n\n setMemoFnMeta(prototype[fnKey], { originalArgsLength: fn.length })\n }\n}\n\n/**\n * Looks to run the memoized function with the builder pattern on the object if it exists, otherwise fallback to the static method passed in.\n */\nexport function callMemoOrStaticFn<\n TObject extends Record<string, any>,\n TStaticFn extends AnyFunction,\n>(\n obj: TObject,\n fnKey: string,\n staticFn: TStaticFn,\n ...args: Parameters<TStaticFn> extends [any, ...infer Rest] ? Rest : never\n): ReturnType<TStaticFn> {\n return (\n (obj[fnKey] as Function | undefined)?.(...args) ?? staticFn(obj, ...args)\n )\n}\n"],"names":[],"mappings":"AAKO,SAAS,iBAAoB,SAAqB,OAAa;AACpE,SAAO,OAAO,YAAY,aACrB,QAAwB,KAAK,IAC9B;AACN;AAEO,SAAS,OAAO;AAAC;AAEjB,SAAS,iBAGd,KAAQ,UAAiC;AACzC,SAAO,CAAC,YAAiE;AACvE,aAAS,UAAU;AAAA,MACjB,CAAqC,QAAqB;AACxD,eAAO;AAAA,UACL,GAAG;AAAA,UAC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,GAAG,IAAI,KAAK,SAAS,KAAK,GAAG,IAAI;AAE5E;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/table-core",
|
|
3
|
-
"version": "9.0.0-alpha.
|
|
3
|
+
"version": "9.0.0-alpha.16",
|
|
4
4
|
"description": "Headless UI for building powerful tables & datagrids for TS/JS.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"src"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@tanstack/store": "^0.9.
|
|
48
|
+
"@tanstack/store": "^0.9.2"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"clean": "rimraf ./build && rimraf ./dist",
|
|
@@ -65,7 +65,7 @@ export interface TableOptions_Columns<
|
|
|
65
65
|
/**
|
|
66
66
|
* The array of column defs to use for the table.
|
|
67
67
|
*/
|
|
68
|
-
columns:
|
|
68
|
+
columns: ReadonlyArray<ColumnDef<TFeatures, TData, TValue>>
|
|
69
69
|
/**
|
|
70
70
|
* Default column options to use for all column defs supplied to the table.
|
|
71
71
|
*/
|
|
@@ -79,7 +79,7 @@ export function table_getAllColumns<
|
|
|
79
79
|
table: Table_Internal<TFeatures, TData>,
|
|
80
80
|
): Array<Column<TFeatures, TData, unknown>> {
|
|
81
81
|
const recurseColumns = (
|
|
82
|
-
colDefs:
|
|
82
|
+
colDefs: ReadonlyArray<ColumnDef<TFeatures, TData, unknown>>,
|
|
83
83
|
parent?: Column<TFeatures, TData, unknown>,
|
|
84
84
|
depth = 0,
|
|
85
85
|
): Array<Column<TFeatures, TData, unknown>> => {
|
|
@@ -29,7 +29,7 @@ function _createCoreRowModel<
|
|
|
29
29
|
TData extends RowData,
|
|
30
30
|
>(
|
|
31
31
|
table: Table_Internal<TFeatures, TData>,
|
|
32
|
-
data:
|
|
32
|
+
data: ReadonlyArray<TData>,
|
|
33
33
|
): {
|
|
34
34
|
rows: Array<Row<TFeatures, TData>>
|
|
35
35
|
flatRows: Array<Row<TFeatures, TData>>
|
|
@@ -42,7 +42,7 @@ function _createCoreRowModel<
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
const accessRows = (
|
|
45
|
-
originalRows:
|
|
45
|
+
originalRows: ReadonlyArray<TData>,
|
|
46
46
|
depth = 0,
|
|
47
47
|
parentRow?: Row<TFeatures, TData>,
|
|
48
48
|
): Array<Row<TFeatures, TData>> => {
|
|
@@ -29,7 +29,7 @@ export interface Row_CoreProperties<
|
|
|
29
29
|
/**
|
|
30
30
|
* An array of the original subRows as returned by the `options.getSubRows` option.
|
|
31
31
|
*/
|
|
32
|
-
originalSubRows?:
|
|
32
|
+
originalSubRows?: ReadonlyArray<TData>
|
|
33
33
|
/**
|
|
34
34
|
* If nested, this row's parent row id.
|
|
35
35
|
*/
|
|
@@ -96,7 +96,10 @@ export interface TableOptions_Rows<
|
|
|
96
96
|
* This optional function is used to access the sub rows for any given row. If you are using nested rows, you will need to use this function to return the sub rows object (or undefined) from the row.
|
|
97
97
|
* @example getSubRows: row => row.subRows
|
|
98
98
|
*/
|
|
99
|
-
getSubRows?: (
|
|
99
|
+
getSubRows?: (
|
|
100
|
+
originalRow: TData,
|
|
101
|
+
index: number,
|
|
102
|
+
) => undefined | ReadonlyArray<TData>
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
export interface Table_Rows<
|
|
@@ -34,6 +34,12 @@ export function constructTable<
|
|
|
34
34
|
_features: { ...coreFeatures, ...tableOptions._features },
|
|
35
35
|
_rowModels: {},
|
|
36
36
|
_rowModelFns: {},
|
|
37
|
+
get options() {
|
|
38
|
+
return this.optionsStore.state
|
|
39
|
+
},
|
|
40
|
+
set options(value) {
|
|
41
|
+
this.optionsStore.setState(() => value)
|
|
42
|
+
},
|
|
37
43
|
} as Table_Internal<TFeatures, TData>
|
|
38
44
|
|
|
39
45
|
const featuresList: Array<TableFeature<{}>> = Object.values(table._features)
|
|
@@ -42,10 +48,10 @@ export function constructTable<
|
|
|
42
48
|
return Object.assign(obj, feature.getDefaultTableOptions?.(table))
|
|
43
49
|
}, {}) as TableOptions<TFeatures, TData>
|
|
44
50
|
|
|
45
|
-
table.
|
|
51
|
+
table.optionsStore = createStore({
|
|
46
52
|
...defaultOptions,
|
|
47
53
|
...tableOptions,
|
|
48
|
-
}
|
|
54
|
+
})
|
|
49
55
|
|
|
50
56
|
table.initialState = getInitialTableState(
|
|
51
57
|
table._features,
|
|
@@ -58,7 +64,7 @@ export function constructTable<
|
|
|
58
64
|
const state = table.baseStore.state
|
|
59
65
|
return {
|
|
60
66
|
...state,
|
|
61
|
-
...(table.
|
|
67
|
+
...(table.optionsStore.state.state ?? {}),
|
|
62
68
|
}
|
|
63
69
|
})
|
|
64
70
|
|
|
@@ -31,7 +31,7 @@ export interface TableOptions_Table<
|
|
|
31
31
|
/**
|
|
32
32
|
* The data for the table to display. When the `data` option changes reference, the table will reprocess the data.
|
|
33
33
|
*/
|
|
34
|
-
data:
|
|
34
|
+
data: ReadonlyArray<TData>
|
|
35
35
|
/**
|
|
36
36
|
* Use this option to optionally pass initial state to the table. This state will be used when resetting various table states either automatically by the table (eg. `options.autoResetPageIndex`) or via functions like `table.resetRowSelection()`. Most reset function allow you optionally pass a flag to reset to a blank/default state instead of the initial state.
|
|
37
37
|
* Table state will not be reset when this object changes, which also means that the initial state object does not need to be stable.
|
|
@@ -94,6 +94,10 @@ export interface Table_CoreProperties<
|
|
|
94
94
|
* The base store for the table. This can be used to write to the table state.
|
|
95
95
|
*/
|
|
96
96
|
baseStore: Store<TableState<TFeatures>>
|
|
97
|
+
/**
|
|
98
|
+
* The base store for the table options.
|
|
99
|
+
*/
|
|
100
|
+
optionsStore: Store<TableOptions<TFeatures, TData>>
|
|
97
101
|
/**
|
|
98
102
|
* This is the resolved initial state of the table.
|
|
99
103
|
*/
|
|
@@ -101,7 +105,7 @@ export interface Table_CoreProperties<
|
|
|
101
105
|
/**
|
|
102
106
|
* A read-only reference to the table's current options.
|
|
103
107
|
*/
|
|
104
|
-
options: TableOptions<TFeatures, TData>
|
|
108
|
+
readonly options: TableOptions<TFeatures, TData>
|
|
105
109
|
/**
|
|
106
110
|
* Where the table state is stored.
|
|
107
111
|
*/
|
|
@@ -36,5 +36,6 @@ export function table_setOptions<
|
|
|
36
36
|
updater: Updater<TableOptions<TFeatures, TData>>,
|
|
37
37
|
): void {
|
|
38
38
|
const newOptions = functionalUpdate(updater, table.options)
|
|
39
|
-
|
|
39
|
+
const mergedOptions = table_mergeOptions(table, newOptions)
|
|
40
|
+
table.optionsStore.setState(() => mergedOptions)
|
|
40
41
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ReadonlyStore, Store } from '@tanstack/store'
|
|
2
|
+
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
|
|
3
|
+
import type { RowData } from '../../types/type-utils'
|
|
4
|
+
|
|
5
|
+
interface TableReactivityFeatureConstructors<
|
|
6
|
+
TFeatures extends TableFeatures,
|
|
7
|
+
TData extends RowData,
|
|
8
|
+
> {}
|
|
9
|
+
|
|
10
|
+
export function constructReactivityFeature<
|
|
11
|
+
TFeatures extends TableFeatures,
|
|
12
|
+
TData extends RowData,
|
|
13
|
+
>(bindings: {
|
|
14
|
+
stateNotifier?: () => unknown
|
|
15
|
+
optionsNotifier?: () => unknown
|
|
16
|
+
}): TableFeature<TableReactivityFeatureConstructors<TFeatures, TData>> {
|
|
17
|
+
return {
|
|
18
|
+
constructTableAPIs: (table) => {
|
|
19
|
+
table.store = bindStore(table.store, bindings.stateNotifier)
|
|
20
|
+
table.optionsStore = bindStore(
|
|
21
|
+
table.optionsStore,
|
|
22
|
+
bindings.optionsNotifier,
|
|
23
|
+
)
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const bindStore = <T extends Store<any> | ReadonlyStore<any>>(
|
|
29
|
+
store: T,
|
|
30
|
+
notifier?: () => unknown,
|
|
31
|
+
): T => {
|
|
32
|
+
const stateDescriptor = Object.getOwnPropertyDescriptor(
|
|
33
|
+
Object.getPrototypeOf(store),
|
|
34
|
+
'state',
|
|
35
|
+
)!
|
|
36
|
+
|
|
37
|
+
Object.defineProperty(store, 'state', {
|
|
38
|
+
configurable: true,
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get() {
|
|
41
|
+
notifier?.()
|
|
42
|
+
return stateDescriptor.get!.call(store)
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
return store
|
|
47
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -78,6 +78,9 @@ export * from './fns/sortFns'
|
|
|
78
78
|
|
|
79
79
|
export * from './features/stockFeatures'
|
|
80
80
|
|
|
81
|
+
// tableReactivityFeature
|
|
82
|
+
export * from './features/table-reactivity/tableReactivityFeature'
|
|
83
|
+
|
|
81
84
|
// columnFacetingFeature
|
|
82
85
|
export * from './features/column-faceting/columnFacetingFeature'
|
|
83
86
|
export * from './features/column-faceting/columnFacetingFeature.types'
|
package/src/types/ColumnDef.ts
CHANGED
|
@@ -150,7 +150,7 @@ type GroupColumnDefBase<
|
|
|
150
150
|
TData extends RowData,
|
|
151
151
|
TValue extends CellData = CellData,
|
|
152
152
|
> = ColumnDefBase<TFeatures, TData, TValue> & {
|
|
153
|
-
columns?:
|
|
153
|
+
columns?: ReadonlyArray<ColumnDef<TFeatures, TData, unknown>>
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
export type GroupColumnDef<
|
package/src/utils.ts
CHANGED
|
@@ -16,7 +16,7 @@ export function makeStateUpdater<
|
|
|
16
16
|
K extends (string & {}) | keyof TableState_All | keyof TableState<TFeatures>,
|
|
17
17
|
>(key: K, instance: Table<TFeatures, any>) {
|
|
18
18
|
return (updater: Updater<TableState<any>[K & keyof TableState<any>]>) => {
|
|
19
|
-
|
|
19
|
+
instance.baseStore.setState(
|
|
20
20
|
<TTableState extends TableState_All>(old: TTableState) => {
|
|
21
21
|
return {
|
|
22
22
|
...old,
|