From 57602b3e2d0c5fd5408b0b510dca67ac2a162828 Mon Sep 17 00:00:00 2001 From: Kevin Van Cott Date: Wed, 23 Oct 2024 07:52:05 -0500 Subject: [PATCH] simplify api assignment --- packages/table-core/src/core/cells/Cells.ts | 12 ++-- .../table-core/src/core/cells/Cells.types.ts | 6 +- .../table-core/src/core/cells/Cells.utils.ts | 13 ++-- .../src/core/cells/constructCell.ts | 7 +- .../table-core/src/core/columns/Columns.ts | 10 +-- .../src/core/columns/Columns.types.ts | 5 ++ .../src/core/columns/Columns.utils.ts | 4 +- .../src/core/columns/constructColumn.ts | 13 ++-- .../table-core/src/core/headers/Headers.ts | 10 +-- .../src/core/headers/Headers.types.ts | 6 +- .../src/core/headers/Headers.utils.ts | 4 +- .../src/core/headers/constructHeader.ts | 7 +- .../src/core/row-models/RowModels.ts | 4 +- .../src/core/row-models/createCoreRowModel.ts | 2 - packages/table-core/src/core/rows/Rows.ts | 22 +++--- .../table-core/src/core/rows/Rows.types.ts | 5 ++ .../table-core/src/core/rows/Rows.utils.ts | 22 +++--- .../table-core/src/core/rows/constructRow.ts | 7 +- packages/table-core/src/core/table/Tables.ts | 4 +- .../src/core/table/constructTable.ts | 2 +- .../column-faceting/ColumnFaceting.ts | 4 +- .../column-filtering/ColumnFiltering.ts | 10 +-- .../column-grouping/ColumnGrouping.ts | 16 ++--- .../column-ordering/ColumnOrdering.ts | 8 +-- .../features/column-pinning/ColumnPinning.ts | 12 ++-- .../column-resizing/ColumnResizing.ts | 12 ++-- .../features/column-sizing/ColumnSizing.ts | 11 ++- .../column-visibility/ColumnVisibility.ts | 12 ++-- .../global-faceting/GlobalFaceting.ts | 4 +- .../global-filtering/GlobalFiltering.ts | 8 +-- .../features/row-expanding/RowExpanding.ts | 8 +-- .../features/row-pagination/RowPagination.ts | 4 +- .../src/features/row-pinning/RowPinning.ts | 8 +-- .../features/row-selection/RowSelection.ts | 8 +-- .../src/features/row-sorting/RowSorting.ts | 8 +-- .../table-core/src/types/TableFeatures.ts | 10 +-- packages/table-core/src/utils.test.ts | 34 ++++++++++ packages/table-core/src/utils.ts | 68 +++++++++++++++---- 38 files changed, 250 insertions(+), 160 deletions(-) create mode 100644 packages/table-core/src/utils.test.ts diff --git a/packages/table-core/src/core/cells/Cells.ts b/packages/table-core/src/core/cells/Cells.ts index 06368f0c74..e91661b5c0 100644 --- a/packages/table-core/src/core/cells/Cells.ts +++ b/packages/table-core/src/core/cells/Cells.ts @@ -1,29 +1,27 @@ import { assignAPIs } from '../../utils' import { cell_getContext, cell_getValue, cell_renderValue } from './Cells.utils' -import type { Table_Internal } from '../../types/Table' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Cell } from '../../types/Cell' export const Cells: TableFeature = { - constructCell: < + constructCellAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, >( cell: Cell, - table: Table_Internal, ) => { - assignAPIs(cell, table, [ + assignAPIs(cell, [ { fn: () => cell_getValue(cell), }, { - fn: () => cell_renderValue(cell, table), + fn: () => cell_renderValue(cell), }, { - fn: () => cell_getContext(cell, table), - memoDeps: () => [cell, table], + fn: () => cell_getContext(cell), + memoDeps: () => [cell], }, ]) }, diff --git a/packages/table-core/src/core/cells/Cells.types.ts b/packages/table-core/src/core/cells/Cells.types.ts index fa9f1684c5..665010f2d3 100644 --- a/packages/table-core/src/core/cells/Cells.types.ts +++ b/packages/table-core/src/core/cells/Cells.types.ts @@ -1,6 +1,6 @@ import type { CellData, Getter, RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' -import type { Table } from '../../types/Table' +import type { Table, Table_Internal } from '../../types/Table' import type { Row } from '../../types/Row' import type { Cell } from '../../types/Cell' import type { Column } from '../../types/Column' @@ -41,6 +41,10 @@ export interface Cell_CoreProperties< * @link [Guide](https://tanstack.com/table/v8/docs/guide/cells) */ row: Row + /** + * @deprecated Reference to the table instance. + */ + table: Table_Internal } export interface Cell_Cell< diff --git a/packages/table-core/src/core/cells/Cells.utils.ts b/packages/table-core/src/core/cells/Cells.utils.ts index 98a2137411..fa359edcfc 100644 --- a/packages/table-core/src/core/cells/Cells.utils.ts +++ b/packages/table-core/src/core/cells/Cells.utils.ts @@ -1,6 +1,7 @@ +import { callMemoOrStaticFn } from '../../utils' +import { row_getValue } from '../rows/Rows.utils' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' -import type { Table } from '../../types/Table' import type { Cell } from '../../types/Cell' export function cell_getValue< @@ -8,24 +9,24 @@ export function cell_getValue< TData extends RowData, TValue extends CellData = CellData, >(cell: Cell): TValue { - return cell.row.getValue(cell.column.id) + return callMemoOrStaticFn(cell.row, row_getValue, [cell.column.id]) } export function cell_renderValue< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(cell: Cell, table: Table) { - return cell.getValue() ?? table.options.renderFallbackValue +>(cell: Cell) { + return cell.getValue() ?? cell.table.options.renderFallbackValue } export function cell_getContext< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(cell: Cell, table: Table) { +>(cell: Cell) { return { - table, + table: cell.table, column: cell.column, row: cell.row, cell: cell, diff --git a/packages/table-core/src/core/cells/constructCell.ts b/packages/table-core/src/core/cells/constructCell.ts index ba652f2555..ae92e8da86 100644 --- a/packages/table-core/src/core/cells/constructCell.ts +++ b/packages/table-core/src/core/cells/constructCell.ts @@ -1,5 +1,5 @@ import type { CellData, RowData } from '../../types/type-utils' -import type { TableFeatures } from '../../types/TableFeatures' +import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' import type { Row } from '../../types/Row' import type { Cell } from '../../types/Cell' @@ -19,10 +19,11 @@ export function constructCell< column, id: `${row.id}_${column.id}`, row, + table, } - for (const feature of Object.values(table._features)) { - feature?.constructCell?.(cell as Cell, table) + for (const feature of Object.values(table._features) as Array) { + feature.constructCellAPIs?.(cell as Cell, table) } return cell as Cell diff --git a/packages/table-core/src/core/columns/Columns.ts b/packages/table-core/src/core/columns/Columns.ts index acfa564743..68f36d12e7 100644 --- a/packages/table-core/src/core/columns/Columns.ts +++ b/packages/table-core/src/core/columns/Columns.ts @@ -15,7 +15,7 @@ import type { Table_Internal } from '../../types/Table' import type { Column } from '../../types/Column' export const Columns: TableFeature = { - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -23,13 +23,13 @@ export const Columns: TableFeature = { column: Column, table: Table_Internal, ) => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getFlatColumns(column), memoDeps: () => [table.options.columns], }, { - fn: () => column_getLeafColumns(column, table), + fn: () => column_getLeafColumns(column), memoDeps: () => [ table.getState().columnOrder, table.getState().grouping, @@ -40,10 +40,10 @@ export const Columns: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table_Internal, ) => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_getDefaultColumnDef(table), memoDeps: () => [table.options.defaultColumn], diff --git a/packages/table-core/src/core/columns/Columns.types.ts b/packages/table-core/src/core/columns/Columns.types.ts index fea17d6735..7c1054811d 100644 --- a/packages/table-core/src/core/columns/Columns.types.ts +++ b/packages/table-core/src/core/columns/Columns.types.ts @@ -1,3 +1,4 @@ +import type { Table_Internal } from '../../types/Table' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { AccessorFn, ColumnDef } from '../../types/ColumnDef' @@ -47,6 +48,10 @@ export interface Column_CoreProperties< * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-defs) */ parent?: Column + /** + * @deprecated Reference to the table instance. + */ + table: Table_Internal } export interface Column_Column< diff --git a/packages/table-core/src/core/columns/Columns.utils.ts b/packages/table-core/src/core/columns/Columns.utils.ts index 12e81fdb82..9f3e06b986 100644 --- a/packages/table-core/src/core/columns/Columns.utils.ts +++ b/packages/table-core/src/core/columns/Columns.utils.ts @@ -26,14 +26,13 @@ export function column_getLeafColumns< TValue extends CellData = CellData, >( column: Column, - table: Table, ): Array> { if (column.columns.length) { const leafColumns = column.columns.flatMap( (col) => col.getLeafColumns(), // recursive ) - return table_getOrderColumnsFn(table)(leafColumns) as any + return table_getOrderColumnsFn(column.table)(leafColumns as any) as any } return [column] @@ -144,3 +143,4 @@ export function table_getColumn< return column } + diff --git a/packages/table-core/src/core/columns/constructColumn.ts b/packages/table-core/src/core/columns/constructColumn.ts index ae42dbc221..e888beb2a5 100644 --- a/packages/table-core/src/core/columns/constructColumn.ts +++ b/packages/table-core/src/core/columns/constructColumn.ts @@ -1,5 +1,5 @@ import type { CellData, RowData } from '../../types/type-utils' -import type { TableFeatures } from '../../types/TableFeatures' +import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' import type { AccessorFn, @@ -74,16 +74,17 @@ export function constructColumn< } const column: Column_CoreProperties = { - id: `${String(id)}`, accessorFn, - parent: parent, - depth, columnDef: resolvedColumnDef as ColumnDef, columns: [], + depth, + id: `${String(id)}`, + parent: parent, + table, } - for (const feature of Object.values(table._features)) { - feature?.constructColumn?.( + for (const feature of Object.values(table._features) as Array) { + feature.constructColumnAPIs?.( column as Column, table, ) diff --git a/packages/table-core/src/core/headers/Headers.ts b/packages/table-core/src/core/headers/Headers.ts index 0537346a01..c70eef0afb 100644 --- a/packages/table-core/src/core/headers/Headers.ts +++ b/packages/table-core/src/core/headers/Headers.ts @@ -18,7 +18,7 @@ import type { Table, Table_Internal } from '../../types/Table' import type { Header } from '../../types/Header' export const Headers: TableFeature = { - constructHeader: < + constructHeaderAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -26,22 +26,22 @@ export const Headers: TableFeature = { header: Header, table: Table, ): void => { - assignAPIs(header, table, [ + assignAPIs(header, [ { fn: () => header_getLeafHeaders(header), memoDeps: () => [table.options.columns], }, { - fn: () => header_getContext(header, table), + fn: () => header_getContext(header), memoDeps: () => [table.options.columns], }, ]) }, - constructTable: ( + constructTableAPIs: ( table: Table_Internal, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_getHeaderGroups(table), memoDeps: () => [ diff --git a/packages/table-core/src/core/headers/Headers.types.ts b/packages/table-core/src/core/headers/Headers.types.ts index 183795344d..125fd93fbf 100644 --- a/packages/table-core/src/core/headers/Headers.types.ts +++ b/packages/table-core/src/core/headers/Headers.types.ts @@ -1,6 +1,6 @@ import type { CellData, RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' -import type { Table } from '../../types/Table' +import type { Table, Table_Internal } from '../../types/Table' import type { Header } from '../../types/Header' import type { HeaderGroup } from '../../types/HeaderGroup' import type { Column } from '../../types/Column' @@ -128,6 +128,10 @@ export interface Header_CoreProperties< * @link [Guide](https://tanstack.com/table/v8/docs/guide/headers) */ subHeaders: Array> + /** + * @deprecated Reference to the table instance. + */ + table: Table_Internal } export interface Header_Header< diff --git a/packages/table-core/src/core/headers/Headers.utils.ts b/packages/table-core/src/core/headers/Headers.utils.ts index e830c31ddd..c751b43905 100644 --- a/packages/table-core/src/core/headers/Headers.utils.ts +++ b/packages/table-core/src/core/headers/Headers.utils.ts @@ -35,11 +35,11 @@ export function header_getContext< TFeatures extends TableFeatures, TData extends RowData, TValue, ->(header: Header, table: Table) { +>(header: Header) { return { column: header.column, header, - table, + table: header.table, } } diff --git a/packages/table-core/src/core/headers/constructHeader.ts b/packages/table-core/src/core/headers/constructHeader.ts index b9319561e5..418b3ba3fe 100644 --- a/packages/table-core/src/core/headers/constructHeader.ts +++ b/packages/table-core/src/core/headers/constructHeader.ts @@ -1,5 +1,5 @@ import type { CellData, RowData } from '../../types/type-utils' -import type { TableFeatures } from '../../types/TableFeatures' +import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' import type { Header } from '../../types/Header' import type { Column } from '../../types/Column' @@ -31,10 +31,11 @@ export function constructHeader< placeholderId: options.placeholderId, rowSpan: 0, subHeaders: [], + table, } - for (const feature of Object.values(table._features)) { - feature?.constructHeader?.( + for (const feature of Object.values(table._features) as Array) { + feature.constructHeaderAPIs?.( header as Header, table, ) diff --git a/packages/table-core/src/core/row-models/RowModels.ts b/packages/table-core/src/core/row-models/RowModels.ts index cf785b6da6..f283601d48 100644 --- a/packages/table-core/src/core/row-models/RowModels.ts +++ b/packages/table-core/src/core/row-models/RowModels.ts @@ -18,10 +18,10 @@ import type { RowData } from '../../types/type-utils' import type { TableFeature, TableFeatures } from '../../types/TableFeatures' export const RowModels: TableFeature = { - constructTable: ( + constructTableAPIs: ( table: Table, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_getCoreRowModel(table), }, diff --git a/packages/table-core/src/core/row-models/createCoreRowModel.ts b/packages/table-core/src/core/row-models/createCoreRowModel.ts index a11141b371..e9f1a84bad 100644 --- a/packages/table-core/src/core/row-models/createCoreRowModel.ts +++ b/packages/table-core/src/core/row-models/createCoreRowModel.ts @@ -45,7 +45,6 @@ function _createCoreRowModel< ): Array> => { const rows = [] as Array> - console.time('constructing rows') for (let i = 0; i < originalRows.length; i++) { const originalRow = originalRows[i]! // Make the row @@ -76,7 +75,6 @@ function _createCoreRowModel< } } } - console.timeEnd('constructing rows') return rows } diff --git a/packages/table-core/src/core/rows/Rows.ts b/packages/table-core/src/core/rows/Rows.ts index 7e4a96f672..efbb61d7bb 100644 --- a/packages/table-core/src/core/rows/Rows.ts +++ b/packages/table-core/src/core/rows/Rows.ts @@ -17,44 +17,44 @@ import type { Table } from '../../types/Table' import type { Row } from '../../types/Row' export const Rows: TableFeature = { - constructRow: ( + constructRowAPIs: ( row: Row, table: Table, ): void => { - assignAPIs(row, table, [ + assignAPIs(row, [ { - fn: () => row_getAllCellsByColumnId(row, table), + fn: () => row_getAllCellsByColumnId(row), memoDeps: () => [row.getAllCells()], }, { - fn: () => row_getAllCells(row, table), + fn: () => row_getAllCells(row), memoDeps: () => [table.getAllLeafColumns()], }, { fn: () => row_getLeafRows(row), }, { - fn: () => row_getParentRow(row, table), + fn: () => row_getParentRow(row), }, { - fn: () => row_getParentRows(row, table), + fn: () => row_getParentRows(row), }, { - fn: (columnId) => row_getUniqueValues(row, table, columnId), + fn: (columnId) => row_getUniqueValues(row, columnId), }, { - fn: (columnId) => row_getValue(row, table, columnId), + fn: (columnId) => row_getValue(row, columnId), }, { - fn: (columnId) => row_renderValue(row, table, columnId), + fn: (columnId) => row_renderValue(row, columnId), }, ]) }, - constructTable: ( + constructTableAPIs: ( table: Table, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (row, index, parent) => table_getRowId(row, table, index, parent), }, diff --git a/packages/table-core/src/core/rows/Rows.types.ts b/packages/table-core/src/core/rows/Rows.types.ts index 212f4ed515..1dc2ade8d0 100644 --- a/packages/table-core/src/core/rows/Rows.types.ts +++ b/packages/table-core/src/core/rows/Rows.types.ts @@ -1,3 +1,4 @@ +import type { Table_Internal } from '../../types/Table' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Row } from '../../types/Row' @@ -51,6 +52,10 @@ export interface Row_CoreProperties< * @link [Guide](https://tanstack.com/table/v8/docs/guide/rows) */ subRows: Array> + /** + * @deprecated Reference to the table instance. + */ + table: Table_Internal } export interface Row_Row diff --git a/packages/table-core/src/core/rows/Rows.utils.ts b/packages/table-core/src/core/rows/Rows.utils.ts index 0b7fb557c4..d21ec5e245 100644 --- a/packages/table-core/src/core/rows/Rows.utils.ts +++ b/packages/table-core/src/core/rows/Rows.utils.ts @@ -11,14 +11,13 @@ export function row_getValue< TData extends RowData, >( row: Row, - table: Table, columnId: string, ) { if (row._valuesCache.hasOwnProperty(columnId)) { return row._valuesCache[columnId] } - const column = table.getColumn(columnId) + const column = row.table.getColumn(columnId) if (!column?.accessorFn) { return undefined @@ -34,14 +33,13 @@ export function row_getUniqueValues< TData extends RowData, >( row: Row, - table: Table, columnId: string, ) { if (row._uniqueValuesCache.hasOwnProperty(columnId)) { return row._uniqueValuesCache[columnId] } - const column = table.getColumn(columnId) + const column = row.table.getColumn(columnId) if (!column?.accessorFn) { return undefined @@ -65,10 +63,9 @@ export function row_renderValue< TData extends RowData, >( row: Row, - table: Table, columnId: string, ) { - return row.getValue(columnId) ?? table.options.renderFallbackValue + return row.getValue(columnId) ?? row.table.options.renderFallbackValue } export function row_getLeafRows< @@ -81,14 +78,14 @@ export function row_getLeafRows< export function row_getParentRow< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { - return row.parentId ? table.getRow(row.parentId, true) : undefined +>(row: Row) { + return row.parentId ? row.table.getRow(row.parentId, true) : undefined } export function row_getParentRows< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>(row: Row) { const parentRows: Array> = [] let currentRow = row // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition @@ -106,17 +103,16 @@ export function row_getAllCells< TData extends RowData, >( row: Row, - table: Table, ): Array> { - return table.getAllLeafColumns().map((column) => { - return constructCell(column, row, table) + return row.table.getAllLeafColumns().map((column) => { + return constructCell(column, row, row.table) }) } export function row_getAllCellsByColumnId< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>(row: Row) { return row.getAllCells().reduce( (acc, cell) => { acc[cell.column.id] = cell diff --git a/packages/table-core/src/core/rows/constructRow.ts b/packages/table-core/src/core/rows/constructRow.ts index e3ad9ab1ee..f3f040d1a0 100644 --- a/packages/table-core/src/core/rows/constructRow.ts +++ b/packages/table-core/src/core/rows/constructRow.ts @@ -1,5 +1,5 @@ import type { RowData } from '../../types/type-utils' -import type { TableFeatures } from '../../types/TableFeatures' +import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' import type { Row } from '../../types/Row' import type { Row_CoreProperties } from './Rows.types' @@ -25,10 +25,11 @@ export const constructRow = < original, parentId, subRows: subRows ?? [], + table, } - for (const feature of Object.values(table._features)) { - feature?.constructRow?.(row as Row, table) + for (const feature of Object.values(table._features) as Array) { + feature.constructRowAPIs?.(row as Row, table) } return row as Row diff --git a/packages/table-core/src/core/table/Tables.ts b/packages/table-core/src/core/table/Tables.ts index b34d184146..913a4843c8 100644 --- a/packages/table-core/src/core/table/Tables.ts +++ b/packages/table-core/src/core/table/Tables.ts @@ -10,10 +10,10 @@ import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' export const Tables: TableFeature = { - constructTable: ( + constructTableAPIs: ( table: Table, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_getState(table), }, diff --git a/packages/table-core/src/core/table/constructTable.ts b/packages/table-core/src/core/table/constructTable.ts index f368f171e1..1a5a85b3d2 100644 --- a/packages/table-core/src/core/table/constructTable.ts +++ b/packages/table-core/src/core/table/constructTable.ts @@ -51,7 +51,7 @@ export function constructTable< Object.assign(table, coreInstance) for (const feature of featuresList) { - feature.constructTable?.(table) + feature.constructTableAPIs?.(table) } return table diff --git a/packages/table-core/src/features/column-faceting/ColumnFaceting.ts b/packages/table-core/src/features/column-faceting/ColumnFaceting.ts index 5f16f071a6..0e32b6e91e 100644 --- a/packages/table-core/src/features/column-faceting/ColumnFaceting.ts +++ b/packages/table-core/src/features/column-faceting/ColumnFaceting.ts @@ -14,7 +14,7 @@ import type { Column } from '../../types/Column' * The Column Faceting feature adds column faceting APIs to the column objects. */ export const ColumnFaceting: TableFeature = { - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -23,7 +23,7 @@ export const ColumnFaceting: TableFeature = { Partial>, table: Table, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getFacetedMinMaxValues(column, table), }, diff --git a/packages/table-core/src/features/column-filtering/ColumnFiltering.ts b/packages/table-core/src/features/column-filtering/ColumnFiltering.ts index f154f97bda..f3050829fe 100644 --- a/packages/table-core/src/features/column-filtering/ColumnFiltering.ts +++ b/packages/table-core/src/features/column-filtering/ColumnFiltering.ts @@ -67,7 +67,7 @@ export const ColumnFiltering: TableFeature = { } as TableOptions_ColumnFiltering }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -77,7 +77,7 @@ export const ColumnFiltering: TableFeature = { table: Table & Partial>, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getAutoFilterFn(column, table), }, @@ -102,18 +102,18 @@ export const ColumnFiltering: TableFeature = { ]) }, - constructRow: ( + constructRowAPIs: ( row: Row & Partial>, ): void => { row.columnFilters = {} row.columnFiltersMeta = {} }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater: Updater) => table_setColumnFilters(table, updater), diff --git a/packages/table-core/src/features/column-grouping/ColumnGrouping.ts b/packages/table-core/src/features/column-grouping/ColumnGrouping.ts index 801a4af009..3dfbcb20b2 100644 --- a/packages/table-core/src/features/column-grouping/ColumnGrouping.ts +++ b/packages/table-core/src/features/column-grouping/ColumnGrouping.ts @@ -71,7 +71,7 @@ export const ColumnGrouping: TableFeature = { } }, - constructCell: < + constructCellAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue, @@ -80,7 +80,7 @@ export const ColumnGrouping: TableFeature = { table: Table & Partial>, ): void => { - assignAPIs(cell, table, [ + assignAPIs(cell, [ { fn: () => cell_getIsGrouped(cell, table), }, @@ -93,7 +93,7 @@ export const ColumnGrouping: TableFeature = { ]) }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -103,7 +103,7 @@ export const ColumnGrouping: TableFeature = { table: Table & Partial>, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_toggleGrouping(column, table), }, @@ -128,13 +128,13 @@ export const ColumnGrouping: TableFeature = { ]) }, - constructRow: ( + constructRowAPIs: ( row: Row_Internal, table: Table, ): void => { row._groupingValuesCache = {} - assignAPIs(row, table, [ + assignAPIs(row, [ { fn: () => row_getIsGrouped(row), }, @@ -144,11 +144,11 @@ export const ColumnGrouping: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setGrouping(table, updater), }, diff --git a/packages/table-core/src/features/column-ordering/ColumnOrdering.ts b/packages/table-core/src/features/column-ordering/ColumnOrdering.ts index 1b950630fa..69675b08ef 100644 --- a/packages/table-core/src/features/column-ordering/ColumnOrdering.ts +++ b/packages/table-core/src/features/column-ordering/ColumnOrdering.ts @@ -46,7 +46,7 @@ export const ColumnOrdering: TableFeature = { } }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -55,7 +55,7 @@ export const ColumnOrdering: TableFeature = { table: Table & Partial>, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: (position) => column_getIndex(column, table, position), memoDeps: (position) => [ @@ -74,11 +74,11 @@ export const ColumnOrdering: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setColumnOrder(table, updater), }, diff --git a/packages/table-core/src/features/column-pinning/ColumnPinning.ts b/packages/table-core/src/features/column-pinning/ColumnPinning.ts index c48f94a9eb..b9fe70bdff 100644 --- a/packages/table-core/src/features/column-pinning/ColumnPinning.ts +++ b/packages/table-core/src/features/column-pinning/ColumnPinning.ts @@ -72,7 +72,7 @@ export const ColumnPinning: TableFeature = { } }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -81,7 +81,7 @@ export const ColumnPinning: TableFeature = { table: Table & Partial>, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: (position) => column_pin(column, table, position), }, @@ -97,12 +97,12 @@ export const ColumnPinning: TableFeature = { ]) }, - constructRow: ( + constructRowAPIs: ( row: Row & Partial>, table: Table & Partial>, ): void => { - assignAPIs(row, table, [ + assignAPIs(row, [ { fn: () => row_getCenterVisibleCells(row, table), memoDeps: () => [ @@ -130,11 +130,11 @@ export const ColumnPinning: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setColumnPinning(table, updater), }, diff --git a/packages/table-core/src/features/column-resizing/ColumnResizing.ts b/packages/table-core/src/features/column-resizing/ColumnResizing.ts index da718d0b83..81bedab8dc 100644 --- a/packages/table-core/src/features/column-resizing/ColumnResizing.ts +++ b/packages/table-core/src/features/column-resizing/ColumnResizing.ts @@ -50,7 +50,7 @@ export const ColumnResizing: TableFeature = { } }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -58,7 +58,7 @@ export const ColumnResizing: TableFeature = { column: Column & Partial, table: Table & Partial, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getCanResize(column, table), }, @@ -68,7 +68,7 @@ export const ColumnResizing: TableFeature = { ]) }, - constructHeader: < + constructHeaderAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -76,7 +76,7 @@ export const ColumnResizing: TableFeature = { header: Header & Partial, table: Table & Partial, ): void => { - assignAPIs(header, table, [ + assignAPIs(header, [ { fn: (_contextDocument) => header_getResizeHandler(header, table, _contextDocument), @@ -84,10 +84,10 @@ export const ColumnResizing: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setColumnResizing(table, updater), }, diff --git a/packages/table-core/src/features/column-sizing/ColumnSizing.ts b/packages/table-core/src/features/column-sizing/ColumnSizing.ts index a52971efe0..eb3591fb9b 100644 --- a/packages/table-core/src/features/column-sizing/ColumnSizing.ts +++ b/packages/table-core/src/features/column-sizing/ColumnSizing.ts @@ -1,6 +1,5 @@ import { assignAPIs, makeStateUpdater } from '../../utils' import { column_getVisibleLeafColumns } from '../column-visibility/ColumnVisibility.utils' - import { column_getAfter, column_getSize, @@ -63,7 +62,7 @@ export const ColumnSizing: TableFeature = { } }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -71,7 +70,7 @@ export const ColumnSizing: TableFeature = { column: Column & Partial, table: Table & Partial, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getSize(column, table), }, @@ -97,7 +96,7 @@ export const ColumnSizing: TableFeature = { ]) }, - constructHeader: < + constructHeaderAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -110,10 +109,10 @@ export const ColumnSizing: TableFeature = { header.getStart = () => header_getStart(header, table) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setColumnSizing(table, updater), }, diff --git a/packages/table-core/src/features/column-visibility/ColumnVisibility.ts b/packages/table-core/src/features/column-visibility/ColumnVisibility.ts index 949d302aee..f523539991 100644 --- a/packages/table-core/src/features/column-visibility/ColumnVisibility.ts +++ b/packages/table-core/src/features/column-visibility/ColumnVisibility.ts @@ -60,7 +60,7 @@ export const ColumnVisibility: TableFeature = { } }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -69,7 +69,7 @@ export const ColumnVisibility: TableFeature = { table: Table & Partial>, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getIsVisible(column, table), }, @@ -85,13 +85,13 @@ export const ColumnVisibility: TableFeature = { ]) }, - constructRow: ( + constructRowAPIs: ( row: Row & Partial>, table: Table & Partial>, ): void => { - assignAPIs(row, table, [ + assignAPIs(row, [ { fn: () => row_getAllVisibleCells(row, table), memoDeps: () => [row.getAllCells(), table.getState().columnVisibility], @@ -107,11 +107,11 @@ export const ColumnVisibility: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_getVisibleFlatColumns(table), memoDeps: () => [ diff --git a/packages/table-core/src/features/global-faceting/GlobalFaceting.ts b/packages/table-core/src/features/global-faceting/GlobalFaceting.ts index 2c57007307..2c32702695 100644 --- a/packages/table-core/src/features/global-faceting/GlobalFaceting.ts +++ b/packages/table-core/src/features/global-faceting/GlobalFaceting.ts @@ -15,11 +15,11 @@ import type { Table } from '../../types/Table' * @link [Guide](https://tanstack.com/table/v8/docs/guide/global-faceting) */ export const GlobalFaceting: TableFeature = { - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_getGlobalFacetedMinMaxValues(table), }, diff --git a/packages/table-core/src/features/global-filtering/GlobalFiltering.ts b/packages/table-core/src/features/global-filtering/GlobalFiltering.ts index 2baef48ef8..da58b9557f 100644 --- a/packages/table-core/src/features/global-filtering/GlobalFiltering.ts +++ b/packages/table-core/src/features/global-filtering/GlobalFiltering.ts @@ -56,7 +56,7 @@ export const GlobalFiltering: TableFeature = { } }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -65,18 +65,18 @@ export const GlobalFiltering: TableFeature = { table: Table & Partial>, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getCanGlobalFilter(column, table), }, ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_getGlobalAutoFilterFn(), }, diff --git a/packages/table-core/src/features/row-expanding/RowExpanding.ts b/packages/table-core/src/features/row-expanding/RowExpanding.ts index 9d825f43ea..d6a56fda41 100644 --- a/packages/table-core/src/features/row-expanding/RowExpanding.ts +++ b/packages/table-core/src/features/row-expanding/RowExpanding.ts @@ -55,12 +55,12 @@ export const RowExpanding: TableFeature = { } }, - constructRow: ( + constructRowAPIs: ( row: Row & Partial, table: Table & Partial>, ): void => { - assignAPIs(row, table, [ + assignAPIs(row, [ { fn: (expanded) => row_toggleExpanded(row, table, expanded), }, @@ -79,11 +79,11 @@ export const RowExpanding: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_autoResetExpanded(table), }, diff --git a/packages/table-core/src/features/row-pagination/RowPagination.ts b/packages/table-core/src/features/row-pagination/RowPagination.ts index ade6f0cc2e..637c112187 100644 --- a/packages/table-core/src/features/row-pagination/RowPagination.ts +++ b/packages/table-core/src/features/row-pagination/RowPagination.ts @@ -58,11 +58,11 @@ export const RowPagination: TableFeature = { } }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: () => table_autoResetPageIndex(table), }, diff --git a/packages/table-core/src/features/row-pinning/RowPinning.ts b/packages/table-core/src/features/row-pinning/RowPinning.ts index 51a58cf3da..c40921018d 100644 --- a/packages/table-core/src/features/row-pinning/RowPinning.ts +++ b/packages/table-core/src/features/row-pinning/RowPinning.ts @@ -51,12 +51,12 @@ export const RowPinning: TableFeature = { } }, - constructRow: ( + constructRowAPIs: ( row: Row & Partial, table: Table & Partial>, ): void => { - assignAPIs(row, table, [ + assignAPIs(row, [ { fn: () => row_getCanPin(row, table), }, @@ -74,11 +74,11 @@ export const RowPinning: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setRowPinning(table, updater), }, diff --git a/packages/table-core/src/features/row-selection/RowSelection.ts b/packages/table-core/src/features/row-selection/RowSelection.ts index 8a5c4c9868..f4264ada12 100644 --- a/packages/table-core/src/features/row-selection/RowSelection.ts +++ b/packages/table-core/src/features/row-selection/RowSelection.ts @@ -70,12 +70,12 @@ export const RowSelection: TableFeature = { } }, - constructRow: ( + constructRowAPIs: ( row: Row & Partial, table: Table & Partial>, ): void => { - assignAPIs(row, table, [ + assignAPIs(row, [ { fn: (value, opts) => row_toggleSelected(row, table, value, opts), }, @@ -103,11 +103,11 @@ export const RowSelection: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table & Partial>, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setRowSelection(table, updater), }, diff --git a/packages/table-core/src/features/row-sorting/RowSorting.ts b/packages/table-core/src/features/row-sorting/RowSorting.ts index ded7ee0514..165b9465e7 100644 --- a/packages/table-core/src/features/row-sorting/RowSorting.ts +++ b/packages/table-core/src/features/row-sorting/RowSorting.ts @@ -65,7 +65,7 @@ export const RowSorting: TableFeature = { } }, - constructColumn: < + constructColumnAPIs: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -73,7 +73,7 @@ export const RowSorting: TableFeature = { column: Column, table: Table, ): void => { - assignAPIs(column, table, [ + assignAPIs(column, [ { fn: () => column_getAutoSortingFn(column, table), }, @@ -113,10 +113,10 @@ export const RowSorting: TableFeature = { ]) }, - constructTable: ( + constructTableAPIs: ( table: Table, ): void => { - assignAPIs(table, table, [ + assignAPIs(table, [ { fn: (updater) => table_setSorting(table, updater), }, diff --git a/packages/table-core/src/types/TableFeatures.ts b/packages/table-core/src/types/TableFeatures.ts index 85fc5ab270..858b1a9b73 100644 --- a/packages/table-core/src/types/TableFeatures.ts +++ b/packages/table-core/src/types/TableFeatures.ts @@ -9,7 +9,7 @@ import type { TableOptions_All } from './TableOptions' import type { TableState } from './TableState' export interface TableFeature { - constructCell?: < + constructCellAPIs?: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -17,7 +17,7 @@ export interface TableFeature { cell: Cell, table: Table, ) => void - constructColumn?: < + constructColumnAPIs?: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -25,7 +25,7 @@ export interface TableFeature { column: Column, table: Table, ) => void - constructHeader?: < + constructHeaderAPIs?: < TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, @@ -33,11 +33,11 @@ export interface TableFeature { header: Header, table: Table, ) => void - constructRow?: ( + constructRowAPIs?: ( row: Row, table: Table, ) => void - constructTable?: ( + constructTableAPIs?: ( table: Table, ) => void getDefaultColumnDef?: < diff --git a/packages/table-core/src/utils.test.ts b/packages/table-core/src/utils.test.ts new file mode 100644 index 0000000000..4caac807fc --- /dev/null +++ b/packages/table-core/src/utils.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'vitest' +import { getFunctionNameInfo } from './utils' + +describe('getFunctionNameInfo', () => { + it('should correctly parse a function with a standard name', () => { + function table_getRowModel() {} + const result = getFunctionNameInfo(table_getRowModel) + expect(result).toEqual({ + fnKey: 'getRowModel', + fnName: 'table.getRowModel', + parentName: 'table', + }) + }) + + it('should handle anonymous functions with a name assigned', () => { + const row_getCells = function () {} + const result = getFunctionNameInfo(row_getCells) + expect(result).toEqual({ + fnKey: 'getCells', + fnName: 'row.getCells', + parentName: 'row', + }) + }) + + it('should parse arrow functions with a name', () => { + const column_getIsVisible = () => {} + const result = getFunctionNameInfo(column_getIsVisible) + expect(result).toEqual({ + fnKey: 'getIsVisible', + fnName: 'column.getIsVisible', + parentName: 'column', + }) + }) +}) diff --git a/packages/table-core/src/utils.ts b/packages/table-core/src/utils.ts index 26f0da2317..9b88d8bcec 100755 --- a/packages/table-core/src/utils.ts +++ b/packages/table-core/src/utils.ts @@ -1,7 +1,11 @@ -import type { Table } from './types/Table' +import type { Row } from './types/Row' +import type { Table, Table_Internal } from './types/Table' import type { NoInfer, RowData, Updater } from './types/type-utils' import type { TableFeatures } from './types/TableFeatures' import type { TableState } from './types/TableState' +import type { Column } from './types/Column' +import type { Header } from './types/Header' +import type { Cell } from './types/Cell' export const isDev = process.env.NODE_ENV === 'development' @@ -59,12 +63,12 @@ export function flattenBy( } interface MemoOptions, TDepArgs, TResult> { - memoDeps?: (depArgs?: TDepArgs) => [...TDeps] | undefined fn: (...args: NoInfer) => TResult + memoDeps?: (depArgs?: TDepArgs) => [...TDeps] | undefined + onAfterCompare?: () => void onAfterUpdate?: (result: TResult) => void - onBeforeUpdate?: () => void onBeforeCompare?: () => void - onAfterCompare?: () => void + onBeforeUpdate?: () => void } export const memo = , TDepArgs, TResult>( @@ -172,6 +176,27 @@ interface API, TDepArgs> { memoDeps?: (depArgs?: any) => [...any] | undefined } +/** + * Assumes that a function name is in the format of `parentName_fnKey` and returns the `fnKey` and `fnName` in the format of `parentName.fnKey`. + */ +export function getFunctionNameInfo(fn: AnyFunction) { + const rawName = fn.name + const name = + rawName != 'fn' + ? rawName + : (fn.toString().match(/\s*(\w+)\s*\(/)?.[1] as `${string}_${string}`) + const [parentName, fnKey] = name.split('_') + const fnName = `${parentName}.${fnKey}` + return { fnKey, fnName, parentName } as { + fnKey: string + fnName: string + parentName: string + } +} + +/** + * Takes a static function, looks at its name and assigns it to an object with optional memoization and debugging. + */ export function assignAPIs< TFeatures extends TableFeatures, TData extends RowData, @@ -180,18 +205,15 @@ export function assignAPIs< TDepArgs, >( obj: TObject extends Record ? U : never, // table, row, cell, column, header - table: Table, apis: Array>>, ): void { + const table = (obj.table ?? obj) as Table_Internal apis.forEach(({ fn, memoDeps }) => { - const name = fn.toString().match(/\s*(\w+)\s*\(/)?.[1] as string - const fnName = name.replace('_', '.') - const fnKey = name.split('_')[1]! - - const debugLevel = (name.split('_')[0]! + 's').replace( - name.split('_')[0]!, - name.split('_')[0]!.charAt(0).toUpperCase() + - name.split('_')[0]!.slice(1), + const { fnKey, fnName, parentName } = getFunctionNameInfo(fn) + + const debugLevel = (parentName + 's').replace( + parentName, + parentName.charAt(0).toUpperCase() + parentName.slice(1), ) as 'Table' | 'Rows' | 'Columns' | 'Headers' | 'Cells' obj[fnKey] = memoDeps @@ -204,3 +226,23 @@ export function assignAPIs< : fn }) } + +/** + * Looks to run the memoized function with the builder pattern on the object if it exists, otherwise fallback to the static method passed in. + */ +export function callMemoOrStaticFn< + TFeatures extends TableFeatures, + TData extends RowData, +>( + obj: + | Table + | Row + | Column + | Header + | Cell, + staticFn: AnyFunction, + args: Array, +) { + const { fnKey } = getFunctionNameInfo(staticFn) + return (obj as any)?.[fnKey](...args) ?? staticFn(obj, ...args) +}