From 4f502cac1c4af26eefed38466725cdf531b07c48 Mon Sep 17 00:00:00 2001 From: Kevin Van Cott Date: Sat, 26 Oct 2024 20:42:24 -0500 Subject: [PATCH] simplify functions by including table ref on rows, cols, etc. --- examples/react/filters/src/main.tsx | 8 +- packages/table-core/package.json | 2 +- .../table-core/src/core/cells/Cells.utils.ts | 4 +- .../src/core/cells/constructCell.ts | 2 +- .../table-core/src/core/columns/Columns.ts | 2 +- .../src/core/columns/Columns.utils.ts | 2 +- .../src/core/columns/constructColumn.ts | 7 +- .../table-core/src/core/headers/Headers.ts | 7 +- .../src/core/headers/Headers.types.ts | 4 +- .../src/core/headers/Headers.utils.ts | 2 +- .../src/core/headers/buildHeaderGroups.ts | 4 +- .../src/core/headers/constructHeader.ts | 5 +- packages/table-core/src/core/rows/Rows.ts | 3 +- .../table-core/src/core/rows/constructRow.ts | 2 +- .../column-faceting/ColumnFaceting.ts | 8 +- .../column-faceting/createFacetedRowModel.ts | 10 +- .../createFacetedUniqueValues.ts | 6 +- .../column-filtering/ColumnFiltering.ts | 16 +- .../column-filtering/ColumnFiltering.utils.ts | 38 ++- .../createFilteredRowModel.ts | 20 +- .../column-filtering/filterRowsUtils.ts | 58 ++--- .../column-grouping/ColumnGrouping.ts | 32 ++- .../column-grouping/ColumnGrouping.utils.ts | 78 +++--- .../column-grouping/createGroupedRowModel.ts | 22 +- .../column-ordering/ColumnOrdering.ts | 14 +- .../column-ordering/ColumnOrdering.utils.ts | 9 +- .../features/column-pinning/ColumnPinning.ts | 30 +-- .../column-pinning/ColumnPinning.utils.ts | 58 ++--- .../column-resizing/ColumnResizing.ts | 8 +- .../column-resizing/ColumnResizing.utils.ts | 32 +-- .../features/column-sizing/ColumnSizing.ts | 22 +- .../column-sizing/ColumnSizing.utils.ts | 49 ++-- .../column-visibility/ColumnVisibility.ts | 25 +- .../ColumnVisibility.utils.ts | 35 +-- .../global-faceting/GlobalFaceting.utils.ts | 15 -- .../global-filtering/GlobalFiltering.ts | 4 +- .../global-filtering/GlobalFiltering.utils.ts | 34 +-- .../features/row-expanding/RowExpanding.ts | 12 +- .../row-expanding/RowExpanding.utils.ts | 117 ++------- .../row-expanding/createExpandedRowModel.ts | 8 +- .../row-pagination/RowPagination.utils.ts | 87 ------- .../src/features/row-pinning/RowPinning.ts | 15 +- .../features/row-pinning/RowPinning.utils.ts | 94 +------ .../features/row-selection/RowSelection.ts | 18 +- .../row-selection/RowSelection.utils.ts | 235 +++--------------- .../src/features/row-sorting/RowSorting.ts | 25 +- .../features/row-sorting/RowSorting.utils.ts | 102 +++----- .../row-sorting/createSortedRowModel.ts | 4 +- .../table-core/src/helpers/tableHelper.ts | 3 - packages/table-core/src/index.ts | 18 +- .../table-core/src/types/TableFeatures.ts | 4 - packages/table-core/src/utils.ts | 22 +- 52 files changed, 455 insertions(+), 986 deletions(-) diff --git a/examples/react/filters/src/main.tsx b/examples/react/filters/src/main.tsx index bceddcfe43..2777ae1d6b 100644 --- a/examples/react/filters/src/main.tsx +++ b/examples/react/filters/src/main.tsx @@ -100,15 +100,15 @@ function App() { const table = useTable({ _features, - _processingFns: { - filterFns, // client side filtering - sortingFns, - }, _rowModels: { filteredRowModel: createFilteredRowModel(), // client side filtering sortedRowModel: createSortedRowModel(), paginatedRowModel: createPaginatedRowModel(), }, + _processingFns: { + filterFns, // client side filtering + sortingFns, + }, columns, data, state: { diff --git a/packages/table-core/package.json b/packages/table-core/package.json index 6b46706be6..c056fcc1ef 100644 --- a/packages/table-core/package.json +++ b/packages/table-core/package.json @@ -41,7 +41,7 @@ }, "sideEffects": false, "engines": { - "node": ">=12" + "node": ">=16" }, "files": [ "dist/", diff --git a/packages/table-core/src/core/cells/Cells.utils.ts b/packages/table-core/src/core/cells/Cells.utils.ts index fa359edcfc..f5f7cf351f 100644 --- a/packages/table-core/src/core/cells/Cells.utils.ts +++ b/packages/table-core/src/core/cells/Cells.utils.ts @@ -1,5 +1,3 @@ -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 { Cell } from '../../types/Cell' @@ -9,7 +7,7 @@ export function cell_getValue< TData extends RowData, TValue extends CellData = CellData, >(cell: Cell): TValue { - return callMemoOrStaticFn(cell.row, row_getValue, [cell.column.id]) + return cell.row.getValue(cell.column.id) } export function cell_renderValue< diff --git a/packages/table-core/src/core/cells/constructCell.ts b/packages/table-core/src/core/cells/constructCell.ts index ae92e8da86..a82ca1558f 100644 --- a/packages/table-core/src/core/cells/constructCell.ts +++ b/packages/table-core/src/core/cells/constructCell.ts @@ -23,7 +23,7 @@ export function constructCell< } for (const feature of Object.values(table._features) as Array) { - feature.constructCellAPIs?.(cell as Cell, table) + feature.constructCellAPIs?.(cell as Cell) } 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 68f36d12e7..328783c39e 100644 --- a/packages/table-core/src/core/columns/Columns.ts +++ b/packages/table-core/src/core/columns/Columns.ts @@ -21,8 +21,8 @@ export const Columns: TableFeature = { TValue extends CellData = CellData, >( column: Column, - table: Table_Internal, ) => { + const { table } = column assignAPIs(column, [ { fn: () => column_getFlatColumns(column), diff --git a/packages/table-core/src/core/columns/Columns.utils.ts b/packages/table-core/src/core/columns/Columns.utils.ts index 6caaed54f3..f3e374726b 100644 --- a/packages/table-core/src/core/columns/Columns.utils.ts +++ b/packages/table-core/src/core/columns/Columns.utils.ts @@ -47,7 +47,7 @@ export function table_getDefaultColumnDef< return { header: (props) => { const resolvedColumnDef = props.header.column - .columnDef as ColumnDefResolved + .columnDef as ColumnDefResolved<{}, TData> if (resolvedColumnDef.accessorKey) { return resolvedColumnDef.accessorKey diff --git a/packages/table-core/src/core/columns/constructColumn.ts b/packages/table-core/src/core/columns/constructColumn.ts index e888beb2a5..1f71274261 100644 --- a/packages/table-core/src/core/columns/constructColumn.ts +++ b/packages/table-core/src/core/columns/constructColumn.ts @@ -24,7 +24,7 @@ export function constructColumn< const resolvedColumnDef = { ...defaultColumn, ...columnDef, - } as ColumnDefResolved + } as ColumnDefResolved<{}, TData, TValue> const accessorKey = resolvedColumnDef.accessorKey @@ -84,10 +84,7 @@ export function constructColumn< } for (const feature of Object.values(table._features) as Array) { - feature.constructColumnAPIs?.( - column as Column, - table, - ) + feature.constructColumnAPIs?.(column as Column) } return column as Column diff --git a/packages/table-core/src/core/headers/Headers.ts b/packages/table-core/src/core/headers/Headers.ts index c70eef0afb..a51ff00ba0 100644 --- a/packages/table-core/src/core/headers/Headers.ts +++ b/packages/table-core/src/core/headers/Headers.ts @@ -14,7 +14,7 @@ import { } from './Headers.utils' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeature, TableFeatures } from '../../types/TableFeatures' -import type { Table, Table_Internal } from '../../types/Table' +import type { Table_Internal } from '../../types/Table' import type { Header } from '../../types/Header' export const Headers: TableFeature = { @@ -24,16 +24,15 @@ export const Headers: TableFeature = { TValue extends CellData = CellData, >( header: Header, - table: Table, ): void => { assignAPIs(header, [ { fn: () => header_getLeafHeaders(header), - memoDeps: () => [table.options.columns], + memoDeps: () => [header.column.table.options.columns], }, { fn: () => header_getContext(header), - memoDeps: () => [table.options.columns], + memoDeps: () => [header.column.table.options.columns], }, ]) }, diff --git a/packages/table-core/src/core/headers/Headers.types.ts b/packages/table-core/src/core/headers/Headers.types.ts index 125fd93fbf..fde72b31c2 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, Table_Internal } from '../../types/Table' +import type { Table } from '../../types/Table' import type { Header } from '../../types/Header' import type { HeaderGroup } from '../../types/HeaderGroup' import type { Column } from '../../types/Column' @@ -131,7 +131,7 @@ export interface Header_CoreProperties< /** * @deprecated Reference to the table instance. */ - table: Table_Internal + table: Table } 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 c751b43905..ac08a3bd10 100644 --- a/packages/table-core/src/core/headers/Headers.utils.ts +++ b/packages/table-core/src/core/headers/Headers.utils.ts @@ -39,7 +39,7 @@ export function header_getContext< return { column: header.column, header, - table: header.table, + table: header.column.table, } } diff --git a/packages/table-core/src/core/headers/buildHeaderGroups.ts b/packages/table-core/src/core/headers/buildHeaderGroups.ts index b4609ad3d9..386786baac 100644 --- a/packages/table-core/src/core/headers/buildHeaderGroups.ts +++ b/packages/table-core/src/core/headers/buildHeaderGroups.ts @@ -32,7 +32,7 @@ export function buildHeaderGroups< maxDepth = Math.max(maxDepth, depth) columns - .filter((column) => column_getIsVisible(column, table)) + .filter((column) => column_getIsVisible(column)) .forEach((column) => { if (column.columns.length) { findMaxDepth(column.columns, depth + 1) @@ -135,7 +135,7 @@ export function buildHeaderGroups< headers: Array>, ): Array<{ colSpan: number; rowSpan: number }> => { const filteredHeaders = headers.filter((header) => - column_getIsVisible(header.column, table), + column_getIsVisible(header.column), ) return filteredHeaders.map((header) => { diff --git a/packages/table-core/src/core/headers/constructHeader.ts b/packages/table-core/src/core/headers/constructHeader.ts index 418b3ba3fe..bf243624e9 100644 --- a/packages/table-core/src/core/headers/constructHeader.ts +++ b/packages/table-core/src/core/headers/constructHeader.ts @@ -35,10 +35,7 @@ export function constructHeader< } for (const feature of Object.values(table._features) as Array) { - feature.constructHeaderAPIs?.( - header as Header, - table, - ) + feature.constructHeaderAPIs?.(header as Header) } return header as Header diff --git a/packages/table-core/src/core/rows/Rows.ts b/packages/table-core/src/core/rows/Rows.ts index efbb61d7bb..048addffd4 100644 --- a/packages/table-core/src/core/rows/Rows.ts +++ b/packages/table-core/src/core/rows/Rows.ts @@ -19,7 +19,6 @@ import type { Row } from '../../types/Row' export const Rows: TableFeature = { constructRowAPIs: ( row: Row, - table: Table, ): void => { assignAPIs(row, [ { @@ -28,7 +27,7 @@ export const Rows: TableFeature = { }, { fn: () => row_getAllCells(row), - memoDeps: () => [table.getAllLeafColumns()], + memoDeps: () => [row.table.getAllLeafColumns()], }, { fn: () => row_getLeafRows(row), diff --git a/packages/table-core/src/core/rows/constructRow.ts b/packages/table-core/src/core/rows/constructRow.ts index f3f040d1a0..6f53737377 100644 --- a/packages/table-core/src/core/rows/constructRow.ts +++ b/packages/table-core/src/core/rows/constructRow.ts @@ -29,7 +29,7 @@ export const constructRow = < } for (const feature of Object.values(table._features) as Array) { - feature.constructRowAPIs?.(row as Row, table) + feature.constructRowAPIs?.(row as Row) } return row as Row diff --git a/packages/table-core/src/features/column-faceting/ColumnFaceting.ts b/packages/table-core/src/features/column-faceting/ColumnFaceting.ts index 0e32b6e91e..40d632ee65 100644 --- a/packages/table-core/src/features/column-faceting/ColumnFaceting.ts +++ b/packages/table-core/src/features/column-faceting/ColumnFaceting.ts @@ -7,7 +7,6 @@ import { import type { Column_ColumnFaceting } from './ColumnFaceting.types' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeature, TableFeatures } from '../../types/TableFeatures' -import type { Table } from '../../types/Table' import type { Column } from '../../types/Column' /** @@ -21,17 +20,16 @@ export const ColumnFaceting: TableFeature = { >( column: Column & Partial>, - table: Table, ): void => { assignAPIs(column, [ { - fn: () => column_getFacetedMinMaxValues(column, table), + fn: () => column_getFacetedMinMaxValues(column, column.table), }, { - fn: () => column_getFacetedRowModel(column, table), + fn: () => column_getFacetedRowModel(column, column.table), }, { - fn: () => column_getFacetedUniqueValues(column, table), + fn: () => column_getFacetedUniqueValues(column, column.table), }, ]) }, diff --git a/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts b/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts index b1a2c87b87..efc748d5c4 100644 --- a/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts +++ b/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts @@ -1,7 +1,9 @@ import { isDev, tableMemo } from '../../utils' import { filterRows } from '../column-filtering/filterRowsUtils' - -import type { ColumnFiltersState } from '../column-filtering/ColumnFiltering.types' +import type { + ColumnFiltersState, + Row_ColumnFiltering, +} from '../column-filtering/ColumnFiltering.types' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../core/row-models/RowModels.types' @@ -55,7 +57,9 @@ function _createFacetedRowModel< globalFilter ? '__global__' : undefined, ].filter(Boolean) as Array - const filterRowsImpl = (row: Row) => { + const filterRowsImpl = ( + row: Row & Partial>, + ) => { // Horizontally filter rows through each column for (const colId of filterableIds) { if (row.columnFilters?.[colId] === false) { diff --git a/packages/table-core/src/features/column-faceting/createFacetedUniqueValues.ts b/packages/table-core/src/features/column-faceting/createFacetedUniqueValues.ts index afc61984e1..ed140b6a45 100644 --- a/packages/table-core/src/features/column-faceting/createFacetedUniqueValues.ts +++ b/packages/table-core/src/features/column-faceting/createFacetedUniqueValues.ts @@ -1,5 +1,6 @@ import { isDev, tableMemo } from '../../utils' import { column_getFacetedRowModel } from './ColumnFaceting.utils' +import type { RowModel } from '../../core/row-models/RowModels.types' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' @@ -19,7 +20,7 @@ export function createFacetedUniqueValues< column_getFacetedRowModel(table.getColumn(columnId), table)(), ], fn: (facetedRowModel) => - _createFacetedUniqueValues(table, columnId, facetedRowModel), + _createFacetedUniqueValues(columnId, facetedRowModel), }) } @@ -27,9 +28,8 @@ function _createFacetedUniqueValues< TFeatures extends TableFeatures, TData extends RowData, >( - table: Table, columnId: string, - facetedRowModel?: ReturnType>, + facetedRowModel: RowModel | undefined, ): Map { if (!facetedRowModel) return new Map() diff --git a/packages/table-core/src/features/column-filtering/ColumnFiltering.ts b/packages/table-core/src/features/column-filtering/ColumnFiltering.ts index f3050829fe..574954d419 100644 --- a/packages/table-core/src/features/column-filtering/ColumnFiltering.ts +++ b/packages/table-core/src/features/column-filtering/ColumnFiltering.ts @@ -74,30 +74,28 @@ export const ColumnFiltering: TableFeature = { >( column: Column & Partial>, - table: Table & - Partial>, ): void => { assignAPIs(column, [ { - fn: () => column_getAutoFilterFn(column, table), + fn: () => column_getAutoFilterFn(column), }, { - fn: () => column_getFilterFn(column, table), + fn: () => column_getFilterFn(column), }, { - fn: () => column_getCanFilter(column, table), + fn: () => column_getCanFilter(column), }, { - fn: () => column_getIsFiltered(column, table), + fn: () => column_getIsFiltered(column), }, { - fn: () => column_getFilterValue(column, table), + fn: () => column_getFilterValue(column), }, { - fn: () => column_getFilterIndex(column, table), + fn: () => column_getFilterIndex(column), }, { - fn: (value) => column_setFilterValue(column, table, value), + fn: (value) => column_setFilterValue(column, value), }, ]) }, diff --git a/packages/table-core/src/features/column-filtering/ColumnFiltering.utils.ts b/packages/table-core/src/features/column-filtering/ColumnFiltering.utils.ts index 142fec7649..a296aa1720 100644 --- a/packages/table-core/src/features/column-filtering/ColumnFiltering.utils.ts +++ b/packages/table-core/src/features/column-filtering/ColumnFiltering.utils.ts @@ -17,15 +17,12 @@ export function column_getAutoFilterFn< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column, - table: Table_Internal, -) { - const filterFns = table._processingFns.filterFns as +>(column: Column) { + const filterFns = column.table._processingFns.filterFns as | Record> | undefined - const firstRow = table.getCoreRowModel().flatRows[0] + const firstRow = column.table.getCoreRowModel().flatRows[0] const value = firstRow ? firstRow.getValue(column.id) : undefined @@ -60,15 +57,14 @@ export function column_getFilterFn< column: Column & { columnDef: ColumnDef_ColumnFiltering }, - table: Table_Internal, ): FilterFn | undefined { - const filterFns = table._processingFns.filterFns as + const filterFns = column.table._processingFns.filterFns as | Record> | undefined return isFunction(column.columnDef.filterFn) ? column.columnDef.filterFn : column.columnDef.filterFn === 'auto' - ? column_getAutoFilterFn(column, table) + ? column_getAutoFilterFn(column) : filterFns?.[column.columnDef.filterFn as string] } @@ -80,12 +76,11 @@ export function column_getCanFilter< column: Column & { columnDef: ColumnDef_ColumnFiltering }, - table: Table_Internal, ) { return ( (column.columnDef.enableColumnFilter ?? true) && - (table.options.enableColumnFilters ?? true) && - (table.options.enableFilters ?? true) && + (column.table.options.enableColumnFilters ?? true) && + (column.table.options.enableFilters ?? true) && !!column.accessorFn ) } @@ -98,9 +93,8 @@ export function column_getIsFiltered< column: Column & { columnDef: ColumnDef_ColumnFiltering }, - table: Table_Internal, ) { - return column_getFilterIndex(column, table) > -1 + return column_getFilterIndex(column) > -1 } export function column_getFilterValue< @@ -111,9 +105,9 @@ export function column_getFilterValue< column: Column & { columnDef: ColumnDef_ColumnFiltering }, - table: Table_Internal, ) { - return table.getState().columnFilters?.find((d) => d.id === column.id)?.value + return column.table.getState().columnFilters?.find((d) => d.id === column.id) + ?.value } export function column_getFilterIndex< @@ -124,10 +118,11 @@ export function column_getFilterIndex< column: Column & { columnDef: ColumnDef_ColumnFiltering }, - table: Table_Internal, ): number { return ( - table.getState().columnFilters?.findIndex((d) => d.id === column.id) ?? -1 + column.table + .getState() + .columnFilters?.findIndex((d) => d.id === column.id) ?? -1 ) } @@ -139,11 +134,10 @@ export function column_setFilterValue< column: Column & { columnDef: ColumnDef_ColumnFiltering }, - table: Table_Internal, value: any, ) { - table_setColumnFilters(table, (old) => { - const filterFn = column_getFilterFn(column, table) + table_setColumnFilters(column.table, (old) => { + const filterFn = column_getFilterFn(column) const previousFilter = old.find((d) => d.id === column.id) const newFilter = functionalUpdate( @@ -188,7 +182,7 @@ export function table_setColumnFilters< const column = leafColumns.find((d) => d.id === filter.id) if (column) { - const filterFn = column_getFilterFn(column, table) + const filterFn = column_getFilterFn(column) if (shouldAutoRemoveFilter(filterFn, filter.value, column)) { return false diff --git a/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts b/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts index 78a6a9eb64..607b037152 100644 --- a/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts +++ b/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts @@ -43,7 +43,9 @@ function _createFilteredRowModel< const { columnFilters, globalFilter } = table.getState() if (!rowModel.rows.length || (!columnFilters?.length && !globalFilter)) { - for (const row of rowModel.flatRows) { + for (const row of rowModel.flatRows as Array< + Row & Partial> + >) { row.columnFilters = {} row.columnFiltersMeta = {} } @@ -62,7 +64,7 @@ function _createFilteredRowModel< return } - const filterFn = column_getFilterFn(column, table) + const filterFn = column_getFilterFn(column) if (!filterFn) { if (process.env.NODE_ENV !== 'production') { @@ -86,7 +88,7 @@ function _createFilteredRowModel< const globallyFilterableColumns = table .getAllLeafColumns() - .filter((column) => column_getCanGlobalFilter(column, table)) + .filter((column) => column_getCanGlobalFilter(column)) if (globalFilter && globalFilterFn && globallyFilterableColumns.length) { filterableIds.push('__global__') @@ -102,7 +104,9 @@ function _createFilteredRowModel< } // Flag the prefiltered row model with each filter state - for (const row of rowModel.flatRows) { + for (const row of rowModel.flatRows as Array< + Row & Partial> + >) { row.columnFilters = {} if (resolvedColumnFilters.length) { @@ -115,7 +119,9 @@ function _createFilteredRowModel< id, currentColumnFilter.resolvedValue, (filterMeta) => { - row.columnFiltersMeta[id] = filterMeta + !row.columnFiltersMeta + ? (row.columnFiltersMeta = {}) + : (row.columnFiltersMeta[id] = filterMeta) }, ) } @@ -131,7 +137,9 @@ function _createFilteredRowModel< id, currentGlobalFilter.resolvedValue, (filterMeta) => { - row.columnFiltersMeta[id] = filterMeta + !row.columnFiltersMeta + ? (row.columnFiltersMeta = {}) + : (row.columnFiltersMeta[id] = filterMeta) }, ) ) { diff --git a/packages/table-core/src/features/column-filtering/filterRowsUtils.ts b/packages/table-core/src/features/column-filtering/filterRowsUtils.ts index 78c25b9b19..60b3126253 100644 --- a/packages/table-core/src/features/column-filtering/filterRowsUtils.ts +++ b/packages/table-core/src/features/column-filtering/filterRowsUtils.ts @@ -1,23 +1,18 @@ import { constructRow } from '../../core/rows/constructRow' -import type { - Row_ColumnFiltering, - TableOptions_ColumnFiltering, -} from './ColumnFiltering.types' -import type { RowData } from '../../types/type-utils' -import type { TableFeatures } from '../../types/TableFeatures' +import type { Row_ColumnFiltering } from './ColumnFiltering.types' import type { RowModel } from '../../core/row-models/RowModels.types' -import type { Table } from '../../types/Table' import type { Row } from '../../types/Row' +import type { Table_Internal } from '../../types/Table' +import type { TableFeatures } from '../../types/TableFeatures' +import type { RowData } from '../../types/type-utils' export function filterRows< TFeatures extends TableFeatures, TData extends RowData, >( - rows: Array & Row_ColumnFiltering>, + rows: Array>, filterRowImpl: (row: Row) => any, - table: Table & { - options: TableOptions_ColumnFiltering - }, + table: Table_Internal, ) { if (table.options.filterFromLeafRows) { return filterRowModelFromLeafs(rows, filterRowImpl, table) @@ -32,22 +27,23 @@ function filterRowModelFromLeafs< >( rowsToFilter: Array>, filterRow: (row: Row) => Array>, - table: Table & { - options: TableOptions_ColumnFiltering - }, + table: Table_Internal, ): RowModel { const newFilteredFlatRows: Array> = [] const newFilteredRowsById: Record> = {} const maxDepth = table.options.maxLeafRowFilterDepth ?? 100 const recurseFilterRows = ( - rows: Array & Row_ColumnFiltering>, + rowsToFilter: Array< + Row & Partial> + >, depth = 0, ) => { - const filteredRows: Array> = [] + const filteredRows: Array> & + Partial> = [] // Filter from children up first - for (let row of rows) { + for (let row of rowsToFilter) { const newRow = constructRow( table, row.id, @@ -56,21 +52,22 @@ function filterRowModelFromLeafs< row.depth, undefined, row.parentId, - ) + ) as Row & + Partial> newRow.columnFilters = row.columnFilters if (row.subRows.length && depth < maxDepth) { newRow.subRows = recurseFilterRows(row.subRows, depth + 1) row = newRow - if (!newRow.subRows.length) { + if (filterRow(row) && !newRow.subRows.length) { filteredRows.push(row) newFilteredRowsById[row.id] = row newFilteredFlatRows.push(row) continue } - if (newRow.subRows.length) { + if (filterRow(row) || newRow.subRows.length) { filteredRows.push(row) newFilteredRowsById[row.id] = row newFilteredFlatRows.push(row) @@ -78,9 +75,11 @@ function filterRowModelFromLeafs< } } else { row = newRow - filteredRows.push(row) - newFilteredRowsById[row.id] = row - newFilteredFlatRows.push(row) + if (filterRow(row)) { + filteredRows.push(row) + newFilteredRowsById[row.id] = row + newFilteredFlatRows.push(row) + } } } @@ -88,7 +87,7 @@ function filterRowModelFromLeafs< } return { - rows: recurseFilterRows(rowsToFilter as any), + rows: recurseFilterRows(rowsToFilter), flatRows: newFilteredFlatRows, rowsById: newFilteredRowsById, } @@ -100,22 +99,23 @@ function filterRowModelFromRoot< >( rowsToFilter: Array>, filterRow: (row: Row) => any, - table: Table & { - options: TableOptions_ColumnFiltering - }, + table: Table_Internal, ): RowModel { const newFilteredFlatRows: Array> = [] const newFilteredRowsById: Record> = {} const maxDepth = table.options.maxLeafRowFilterDepth ?? 100 // Filters top level and nested rows - const recurseFilterRows = (rows: Array>, depth = 0) => { + const recurseFilterRows = ( + rowsToFilter: Array>, + depth = 0, + ) => { // Filter from parents downward first const filteredRows: Array> = [] // Apply the filter to any subRows - for (let row of rows) { + for (let row of rowsToFilter) { const pass = filterRow(row) if (pass) { diff --git a/packages/table-core/src/features/column-grouping/ColumnGrouping.ts b/packages/table-core/src/features/column-grouping/ColumnGrouping.ts index 3dfbcb20b2..49b9918dc0 100644 --- a/packages/table-core/src/features/column-grouping/ColumnGrouping.ts +++ b/packages/table-core/src/features/column-grouping/ColumnGrouping.ts @@ -16,17 +16,18 @@ import { table_resetGrouping, table_setGrouping, } from './ColumnGrouping.utils' +import type { Row } from '../../types/Row' import type { TableState } from '../../types/TableState' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' -import type { Row_Internal } from '../../types/Row' import type { Cell } from '../../types/Cell' import type { Column } from '../../types/Column' import type { Cell_ColumnGrouping, ColumnDef_ColumnGrouping, Column_ColumnGrouping, + Row_ColumnGrouping, TableOptions_ColumnGrouping, TableState_ColumnGrouping, Table_ColumnGrouping, @@ -77,18 +78,16 @@ export const ColumnGrouping: TableFeature = { TValue, >( cell: Cell & Partial, - table: Table & - Partial>, ): void => { assignAPIs(cell, [ { - fn: () => cell_getIsGrouped(cell, table), + fn: () => cell_getIsGrouped(cell), }, { - fn: () => cell_getIsPlaceholder(cell, table), + fn: () => cell_getIsPlaceholder(cell), }, { - fn: () => cell_getIsAggregated(cell, table), + fn: () => cell_getIsAggregated(cell), }, ]) }, @@ -100,37 +99,34 @@ export const ColumnGrouping: TableFeature = { >( column: Column & Partial>, - table: Table & - Partial>, ): void => { assignAPIs(column, [ { - fn: () => column_toggleGrouping(column, table), + fn: () => column_toggleGrouping(column), }, { - fn: () => column_getCanGroup(column, table), + fn: () => column_getCanGroup(column), }, { - fn: () => column_getIsGrouped(column, table), + fn: () => column_getIsGrouped(column), }, { - fn: () => column_getGroupedIndex(column, table), + fn: () => column_getGroupedIndex(column), }, { - fn: () => column_getToggleGroupingHandler(column, table), + fn: () => column_getToggleGroupingHandler(column), }, { - fn: () => column_getAutoAggregationFn(column, table), + fn: () => column_getAutoAggregationFn(column), }, { - fn: () => column_getAggregationFn(column, table), + fn: () => column_getAggregationFn(column), }, ]) }, constructRowAPIs: ( - row: Row_Internal, - table: Table, + row: Row & Partial, ): void => { row._groupingValuesCache = {} @@ -139,7 +135,7 @@ export const ColumnGrouping: TableFeature = { fn: () => row_getIsGrouped(row), }, { - fn: (columnId) => row_getGroupingValue(row, table, columnId), + fn: (columnId) => row_getGroupingValue(row, columnId), }, ]) }, diff --git a/packages/table-core/src/features/column-grouping/ColumnGrouping.utils.ts b/packages/table-core/src/features/column-grouping/ColumnGrouping.utils.ts index 54c61d0847..760ff91548 100644 --- a/packages/table-core/src/features/column-grouping/ColumnGrouping.utils.ts +++ b/packages/table-core/src/features/column-grouping/ColumnGrouping.utils.ts @@ -21,11 +21,8 @@ export function column_toggleGrouping< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column, - table: Table_Internal, -) { - table_setGrouping(table, (old) => { +>(column: Column) { + table_setGrouping(column.table, (old) => { // Find any existing grouping for this column if (old.includes(column.id)) { return old.filter((d) => d !== column.id) @@ -43,11 +40,10 @@ export function column_getCanGroup< column: Column & { columnDef: Partial> }, - table: Table_Internal, ) { return ( (column.columnDef.enableGrouping ?? true) && - (table.options.enableGrouping ?? true) && + (column.table.options.enableGrouping ?? true) && (!!column.accessorFn || !!column.columnDef.getGroupingValue) ) } @@ -60,9 +56,8 @@ export function column_getIsGrouped< column: Column & { columnDef: Partial> }, - table: Table_Internal, ): boolean { - return !!table.getState().grouping?.includes(column.id) + return !!column.table.getState().grouping?.includes(column.id) } export function column_getGroupedIndex< @@ -73,9 +68,8 @@ export function column_getGroupedIndex< column: Column & { columnDef: Partial> }, - table: Table_Internal, ): number { - return table.getState().grouping?.indexOf(column.id) ?? -1 + return column.table.getState().grouping?.indexOf(column.id) ?? -1 } export function column_getToggleGroupingHandler< @@ -86,13 +80,12 @@ export function column_getToggleGroupingHandler< column: Column & { columnDef: Partial> }, - table: Table_Internal, ) { - const canGroup = column_getCanGroup(column, table) + const canGroup = column_getCanGroup(column) return () => { if (!canGroup) return - column_toggleGrouping(column, table) + column_toggleGrouping(column) } } @@ -104,13 +97,12 @@ export function column_getAutoAggregationFn< column: Column & { columnDef: Partial> }, - table: Table_Internal, ) { - const aggregationFns = table._processingFns.aggregationFns as + const aggregationFns = column.table._processingFns.aggregationFns as | Record> | undefined - const firstRow = table.getCoreRowModel().flatRows[0] + const firstRow = column.table.getCoreRowModel().flatRows[0] const value = firstRow?.getValue(column.id) @@ -131,16 +123,15 @@ export function column_getAggregationFn< column: Column & { columnDef: Partial> }, - table: Table_Internal, ) { - const aggregationFns = table._processingFns.aggregationFns as + const aggregationFns = column.table._processingFns.aggregationFns as | Record> | undefined return isFunction(column.columnDef.aggregationFn) ? column.columnDef.aggregationFn : column.columnDef.aggregationFn === 'auto' - ? column_getAutoAggregationFn(column, table) + ? column_getAutoAggregationFn(column) : aggregationFns?.[column.columnDef.aggregationFn as string] } @@ -157,32 +148,33 @@ export function table_resetGrouping< >(table: Table_Internal, defaultState?: boolean) { table_setGrouping( table, - defaultState ? [] : (table.options.initialState.grouping ?? []), + defaultState ? [] : (table.options.initialState?.grouping ?? []), ) } export function row_getIsGrouped< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row & Row_ColumnGrouping) { +>(row: Row & Partial) { return !!row.groupingColumnId } export function row_getGroupingValue< TFeatures extends TableFeatures, TData extends RowData, ->( - row: Row & Partial, - table: Table_Internal, - columnId: string, -) { +>(row: Row & Partial, columnId: string) { if (row._groupingValuesCache?.hasOwnProperty(columnId)) { return row._groupingValuesCache[columnId] } - const column = table_getColumn(table, columnId) + const column = table_getColumn(row.table, columnId) as Column< + TFeatures, + TData + > & { + columnDef: Partial> + } - if (!column?.columnDef.getGroupingValue) { + if (!column.columnDef.getGroupingValue) { return row.getValue(columnId) } @@ -199,14 +191,10 @@ export function cell_getIsGrouped< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - cell: Cell, - table: Table_Internal, -) { - const row = cell.row +>(cell: Cell) { + const row = cell.row as Row & Partial return ( - column_getIsGrouped(cell.column, table) && - cell.column.id === row.groupingColumnId + column_getIsGrouped(cell.column) && cell.column.id === row.groupingColumnId ) } @@ -214,26 +202,18 @@ export function cell_getIsPlaceholder< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - cell: Cell, - table: Table_Internal, -) { - return ( - !cell_getIsGrouped(cell, table) && column_getIsGrouped(cell.column, table) - ) +>(cell: Cell) { + return !cell_getIsGrouped(cell) && column_getIsGrouped(cell.column) } export function cell_getIsAggregated< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - cell: Cell, - table: Table_Internal, -) { +>(cell: Cell) { return ( - !cell_getIsGrouped(cell, table) && - !cell_getIsPlaceholder(cell, table) && + !cell_getIsGrouped(cell) && + !cell_getIsPlaceholder(cell) && !!cell.row.subRows.length ) } diff --git a/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts b/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts index 26a7874e5f..66fddbfb61 100644 --- a/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts +++ b/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts @@ -3,7 +3,12 @@ import { constructRow } from '../../core/rows/constructRow' import { table_getColumn } from '../../core/columns/Columns.utils' import { table_autoResetExpanded } from '../row-expanding/RowExpanding.utils' import { table_autoResetPageIndex } from '../row-pagination/RowPagination.utils' -import { row_getGroupingValue } from './ColumnGrouping.utils' +import { + column_getAggregationFn, + row_getGroupingValue, +} from './ColumnGrouping.utils' +import type { Column } from '../../types/Column' +import type { Row_ColumnGrouping } from './ColumnGrouping.types' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../core/row-models/RowModels.types' @@ -50,7 +55,8 @@ function _createGroupedRowModel< table_getColumn(table, columnId), ) - const groupedFlatRows: Array> = [] + const groupedFlatRows: Array> & + Partial = [] const groupedRowsById: Record> = {} // Recursively group the data @@ -107,7 +113,7 @@ function _createGroupedRowModel< depth, undefined, parentId, - ) + ) as Row & Partial Object.assign(row, { groupingColumnId: columnId, @@ -129,13 +135,17 @@ function _createGroupedRowModel< return row._valuesCache[colId] } - if (row._groupingValuesCache.hasOwnProperty(colId)) { + if (row._groupingValuesCache?.hasOwnProperty(colId)) { return row._groupingValuesCache[colId] } // Aggregate the values const column = table.getColumn(colId) - const aggregateFn = column.getAggregationFn() + const aggregateFn = column_getAggregationFn( + column as Column, + ) + + if (!row._groupingValuesCache) row._groupingValuesCache = {} if (aggregateFn) { row._groupingValuesCache[colId] = aggregateFn( @@ -183,7 +193,7 @@ function groupBy( const groupMap = new Map>>() return rows.reduce((map, row) => { - const resKey = `${row_getGroupingValue(row, table, columnId)}` + const resKey = `${row_getGroupingValue(row, columnId)}` const previous = map.get(resKey) if (!previous) { map.set(resKey, [row]) diff --git a/packages/table-core/src/features/column-ordering/ColumnOrdering.ts b/packages/table-core/src/features/column-ordering/ColumnOrdering.ts index 69675b08ef..b79a7d10ed 100644 --- a/packages/table-core/src/features/column-ordering/ColumnOrdering.ts +++ b/packages/table-core/src/features/column-ordering/ColumnOrdering.ts @@ -52,24 +52,22 @@ export const ColumnOrdering: TableFeature = { TValue extends CellData = CellData, >( column: Column & Partial, - table: Table & - Partial>, ): void => { assignAPIs(column, [ { - fn: (position) => column_getIndex(column, table, position), + fn: (position) => column_getIndex(column, position), memoDeps: (position) => [ position, - table.getState().columnOrder, - table.getState().columnPinning, - table.getState().grouping, + column.table.getState().columnOrder, + column.table.getState().columnPinning, + column.table.getState().grouping, ], }, { - fn: (position) => column_getIsFirstColumn(column, table, position), + fn: (position) => column_getIsFirstColumn(column, position), }, { - fn: (position) => column_getIsLastColumn(column, table, position), + fn: (position) => column_getIsLastColumn(column, position), }, ]) }, diff --git a/packages/table-core/src/features/column-ordering/ColumnOrdering.utils.ts b/packages/table-core/src/features/column-ordering/ColumnOrdering.utils.ts index de1ccbe509..3cc4796838 100644 --- a/packages/table-core/src/features/column-ordering/ColumnOrdering.utils.ts +++ b/packages/table-core/src/features/column-ordering/ColumnOrdering.utils.ts @@ -16,10 +16,9 @@ export function column_getIndex< TValue extends CellData = CellData, >( column: Column, - table: Table_Internal, position?: ColumnPinningPosition | 'center', ) { - const columns = column_getVisibleLeafColumns(table, position) + const columns = column_getVisibleLeafColumns(column.table, position) return columns.findIndex((d) => d.id === column.id) } @@ -29,10 +28,9 @@ export function column_getIsFirstColumn< TValue extends CellData = CellData, >( column: Column, - table: Table_Internal, position?: ColumnPinningPosition | 'center', ) { - const columns = column_getVisibleLeafColumns(table, position) + const columns = column_getVisibleLeafColumns(column.table, position) return columns[0]?.id === column.id } @@ -42,10 +40,9 @@ export function column_getIsLastColumn< TValue extends CellData = CellData, >( column: Column, - table: Table_Internal, position?: ColumnPinningPosition | 'center', ) { - const columns = column_getVisibleLeafColumns(table, position) + const columns = column_getVisibleLeafColumns(column.table, position) return columns[columns.length - 1]?.id === column.id } diff --git a/packages/table-core/src/features/column-pinning/ColumnPinning.ts b/packages/table-core/src/features/column-pinning/ColumnPinning.ts index b9fe70bdff..1e6dd39222 100644 --- a/packages/table-core/src/features/column-pinning/ColumnPinning.ts +++ b/packages/table-core/src/features/column-pinning/ColumnPinning.ts @@ -78,53 +78,49 @@ export const ColumnPinning: TableFeature = { TValue extends CellData = CellData, >( column: Column & Partial, - table: Table & - Partial>, ): void => { assignAPIs(column, [ { - fn: (position) => column_pin(column, table, position), + fn: (position) => column_pin(column, position), }, { - fn: () => column_getCanPin(column, table), + fn: () => column_getCanPin(column), }, { - fn: () => column_getPinnedIndex(column, table), + fn: () => column_getPinnedIndex(column), }, { - fn: () => column_getIsPinned(column, table), + fn: () => column_getIsPinned(column), }, ]) }, constructRowAPIs: ( row: Row & Partial>, - table: Table & - Partial>, ): void => { assignAPIs(row, [ { - fn: () => row_getCenterVisibleCells(row, table), + fn: () => row_getCenterVisibleCells(row), memoDeps: () => [ row.getAllCells(), - table.getState().columnPinning, - table.getState().columnVisibility, + row.table.getState().columnPinning, + row.table.getState().columnVisibility, ], }, { - fn: () => row_getLeftVisibleCells(row, table), + fn: () => row_getLeftVisibleCells(row), memoDeps: () => [ row.getAllCells(), - table.getState().columnPinning?.left, - table.getState().columnVisibility, + row.table.getState().columnPinning?.left, + row.table.getState().columnVisibility, ], }, { - fn: () => row_getRightVisibleCells(row, table), + fn: () => row_getRightVisibleCells(row), memoDeps: () => [ row.getAllCells(), - table.getState().columnPinning?.right, - table.getState().columnVisibility, + row.table.getState().columnPinning?.right, + row.table.getState().columnVisibility, ], }, ]) diff --git a/packages/table-core/src/features/column-pinning/ColumnPinning.utils.ts b/packages/table-core/src/features/column-pinning/ColumnPinning.utils.ts index 7bfb564bcf..0c0a1d7b78 100644 --- a/packages/table-core/src/features/column-pinning/ColumnPinning.utils.ts +++ b/packages/table-core/src/features/column-pinning/ColumnPinning.utils.ts @@ -24,21 +24,19 @@ export function getDefaultColumnPinningState(): ColumnPinningState { }) } +// Column APIs + export function column_pin< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column, - table: Table_Internal, - position: ColumnPinningPosition, -) { +>(column: Column, position: ColumnPinningPosition) { const columnIds = column .getLeafColumns() .map((d) => d.id) .filter(Boolean) - table_setColumnPinning(table, (old) => { + table_setColumnPinning(column.table, (old) => { if (position === 'right') { return { left: old.left.filter((d) => !columnIds.includes(d)), @@ -71,7 +69,6 @@ export function column_getCanPin< column: Column & { columnDef: ColumnDef_ColumnPinning }, - table: Table_Internal, ) { const leafColumns = column.getLeafColumns() as Array< Column & { @@ -82,7 +79,7 @@ export function column_getCanPin< return leafColumns.some( (leafColumn) => (leafColumn.columnDef.enablePinning ?? true) && - (table.options.enableColumnPinning ?? true), + (column.table.options.enableColumnPinning ?? true), ) } @@ -90,14 +87,11 @@ export function column_getIsPinned< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column, - table: Table_Internal, -): ColumnPinningPosition | false { +>(column: Column): ColumnPinningPosition | false { const leafColumnIds = column.getLeafColumns().map((d) => d.id) const { left, right } = - table.getState().columnPinning ?? getDefaultColumnPinningState() + column.table.getState().columnPinning ?? getDefaultColumnPinningState() const isLeft = leafColumnIds.some((d) => left.includes(d)) const isRight = leafColumnIds.some((d) => right.includes(d)) @@ -109,24 +103,24 @@ export function column_getPinnedIndex< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column, - table: Table_Internal, -) { - const position = column_getIsPinned(column, table) +>(column: Column) { + const position = column_getIsPinned(column) return position - ? (table.getState().columnPinning?.[position].indexOf(column.id) ?? -1) + ? (column.table.getState().columnPinning?.[position].indexOf(column.id) ?? + -1) : 0 } +// Row APIs + export function row_getCenterVisibleCells< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const allCells = row_getAllVisibleCells(row, table) +>(row: Row) { + const allCells = row_getAllVisibleCells(row) const { left, right } = - table.getState().columnPinning ?? getDefaultColumnPinningState() + row.table.getState().columnPinning ?? getDefaultColumnPinningState() const leftAndRight: Array = [...left, ...right] return allCells.filter((d) => !leftAndRight.includes(d.column.id)) } @@ -134,10 +128,10 @@ export function row_getCenterVisibleCells< export function row_getLeftVisibleCells< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const allCells = row_getAllVisibleCells(row, table) +>(row: Row) { + const allCells = row_getAllVisibleCells(row) const { left } = - table.getState().columnPinning ?? getDefaultColumnPinningState() + row.table.getState().columnPinning ?? getDefaultColumnPinningState() const cells = left .map((columnId) => allCells.find((cell) => cell.column.id === columnId)!) .filter(Boolean) @@ -149,10 +143,10 @@ export function row_getLeftVisibleCells< export function row_getRightVisibleCells< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const allCells = row_getAllVisibleCells(row, table) +>(row: Row) { + const allCells = row_getAllVisibleCells(row) const { right } = - table.getState().columnPinning ?? getDefaultColumnPinningState() + row.table.getState().columnPinning ?? getDefaultColumnPinningState() const cells = right .map((columnId) => allCells.find((cell) => cell.column.id === columnId)!) .filter(Boolean) @@ -161,6 +155,8 @@ export function row_getRightVisibleCells< return cells } +// Table APIs + export function table_setColumnPinning< TFeatures extends TableFeatures, TData extends RowData, @@ -386,7 +382,7 @@ export function table_getLeftVisibleLeafColumns< TData extends RowData, >(table: Table_Internal) { return table_getLeftLeafColumns(table).filter((column) => - column_getIsVisible(column, table), + column_getIsVisible(column), ) } @@ -395,7 +391,7 @@ export function table_getRightVisibleLeafColumns< TData extends RowData, >(table: Table_Internal) { return table_getRightLeafColumns(table).filter((column) => - column_getIsVisible(column, table), + column_getIsVisible(column), ) } @@ -404,6 +400,6 @@ export function table_getCenterVisibleLeafColumns< TData extends RowData, >(table: Table_Internal) { return table_getCenterLeafColumns(table).filter((column) => - column_getIsVisible(column, table), + column_getIsVisible(column), ) } diff --git a/packages/table-core/src/features/column-resizing/ColumnResizing.ts b/packages/table-core/src/features/column-resizing/ColumnResizing.ts index 81bedab8dc..802b09564c 100644 --- a/packages/table-core/src/features/column-resizing/ColumnResizing.ts +++ b/packages/table-core/src/features/column-resizing/ColumnResizing.ts @@ -56,14 +56,13 @@ export const ColumnResizing: TableFeature = { TValue extends CellData = CellData, >( column: Column & Partial, - table: Table & Partial, ): void => { assignAPIs(column, [ { - fn: () => column_getCanResize(column, table), + fn: () => column_getCanResize(column), }, { - fn: () => column_getIsResizing(column, table), + fn: () => column_getIsResizing(column), }, ]) }, @@ -74,12 +73,11 @@ export const ColumnResizing: TableFeature = { TValue extends CellData = CellData, >( header: Header & Partial, - table: Table & Partial, ): void => { assignAPIs(header, [ { fn: (_contextDocument) => - header_getResizeHandler(header, table, _contextDocument), + header_getResizeHandler(header, _contextDocument), }, ]) }, diff --git a/packages/table-core/src/features/column-resizing/ColumnResizing.utils.ts b/packages/table-core/src/features/column-resizing/ColumnResizing.utils.ts index fe6653aef4..90abae8a40 100644 --- a/packages/table-core/src/features/column-resizing/ColumnResizing.utils.ts +++ b/packages/table-core/src/features/column-resizing/ColumnResizing.utils.ts @@ -34,11 +34,10 @@ export function column_getCanResize< column: Column & { columnDef: Partial }, - table: Table_Internal, ) { return ( (column.columnDef.enableResizing ?? true) && - (table.options.enableColumnResizing ?? true) + (column.table.options.enableColumnResizing ?? true) ) } @@ -50,22 +49,17 @@ export function column_getIsResizing< column: Column & { columnDef: Partial }, - table: Table_Internal, ) { - return table.getState().columnResizing?.isResizingColumn === column.id + return column.table.getState().columnResizing?.isResizingColumn === column.id } export function header_getResizeHandler< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - header: Header, - table: Table_Internal, - _contextDocument?: Document, -) { - const column = table_getColumn(table, header.column.id)! - const canResize = column_getCanResize(column, table) +>(header: Header, _contextDocument?: Document) { + const column = table_getColumn(header.column.table, header.column.id)! + const canResize = column_getCanResize(column) return (event: unknown) => { if (!canResize) { @@ -81,13 +75,13 @@ export function header_getResizeHandler< } } - const startSize = header_getSize(header, table) + const startSize = header_getSize(header) const columnSizingStart: Array<[string, number]> = header .getLeafHeaders() .map((leafHeader) => [ leafHeader.column.id, - column_getSize(leafHeader.column, table), + column_getSize(leafHeader.column), ]) const clientX = isTouchStartEvent(event) @@ -101,9 +95,9 @@ export function header_getResizeHandler< return } - table_setColumnResizing(table, (old) => { + table_setColumnResizing(column.table, (old) => { const deltaDirection = - table.options.columnResizeDirection === 'rtl' ? -1 : 1 + column.table.options.columnResizeDirection === 'rtl' ? -1 : 1 const deltaOffset = (clientXPos - (old.startOffset ?? 0)) * deltaDirection const deltaPercentage = Math.max( @@ -126,10 +120,10 @@ export function header_getResizeHandler< }) if ( - table.options.columnResizeMode === 'onChange' || + column.table.options.columnResizeMode === 'onChange' || eventType === 'end' ) { - table_setColumnSizing(table, (old) => ({ + table_setColumnSizing(column.table, (old) => ({ ...old, ...newColumnSizing, })) @@ -141,7 +135,7 @@ export function header_getResizeHandler< const onEnd = (clientXPos?: number) => { updateOffset('end', clientXPos) - table_setColumnResizing(table, (old) => ({ + table_setColumnResizing(column.table, (old) => ({ ...old, isResizingColumn: false, startOffset: null, @@ -218,7 +212,7 @@ export function header_getResizeHandler< ) } - table_setColumnResizing(table, (old) => ({ + table_setColumnResizing(column.table, (old) => ({ ...old, startOffset: clientX, startSize, diff --git a/packages/table-core/src/features/column-sizing/ColumnSizing.ts b/packages/table-core/src/features/column-sizing/ColumnSizing.ts index eb3591fb9b..aceaa30e96 100644 --- a/packages/table-core/src/features/column-sizing/ColumnSizing.ts +++ b/packages/table-core/src/features/column-sizing/ColumnSizing.ts @@ -68,30 +68,29 @@ export const ColumnSizing: TableFeature = { TValue extends CellData = CellData, >( column: Column & Partial, - table: Table & Partial, ): void => { assignAPIs(column, [ { - fn: () => column_getSize(column, table), + fn: () => column_getSize(column), }, { - fn: (position) => column_getStart(column, table, position), + fn: (position) => column_getStart(column, position), memoDeps: (position) => [ position, - column_getVisibleLeafColumns(table, position), - table.getState().columnSizing, + column_getVisibleLeafColumns(column.table, position), + column.table.getState().columnSizing, ], }, { - fn: (position) => column_getAfter(column, table, position), + fn: (position) => column_getAfter(column, position), memoDeps: (position) => [ position, - column_getVisibleLeafColumns(table, position), - table.getState().columnSizing, + column_getVisibleLeafColumns(column.table, position), + column.table.getState().columnSizing, ], }, { - fn: () => column_resetSize(table, column), + fn: () => column_resetSize(column), }, ]) }, @@ -102,11 +101,10 @@ export const ColumnSizing: TableFeature = { TValue extends CellData = CellData, >( header: Header & Partial, - table: Table & Partial, ): void => { - header.getSize = () => header_getSize(header, table) + header.getSize = () => header_getSize(header) - header.getStart = () => header_getStart(header, table) + header.getStart = () => header_getStart(header) }, constructTableAPIs: ( diff --git a/packages/table-core/src/features/column-sizing/ColumnSizing.utils.ts b/packages/table-core/src/features/column-sizing/ColumnSizing.utils.ts index 40819e42c5..adc37d29f7 100644 --- a/packages/table-core/src/features/column-sizing/ColumnSizing.utils.ts +++ b/packages/table-core/src/features/column-sizing/ColumnSizing.utils.ts @@ -35,10 +35,9 @@ export function column_getSize< column: Column & { columnDef: ColumnDef_ColumnSizing }, - table: Table_Internal, ): number { const defaultSizes = getDefaultColumnSizingColumnDef() - const columnSize = table.getState().columnSizing?.[column.id] + const columnSize = column.table.getState().columnSizing?.[column.id] return Math.min( Math.max( @@ -57,12 +56,11 @@ export function column_getStart< column: Column & { columnDef: ColumnDef_ColumnSizing }, - table: Table_Internal, position?: false | 'left' | 'right' | 'center', ): number { - return column_getVisibleLeafColumns(table, position) - .slice(0, column_getIndex(column, table, position)) - .reduce((sum, c) => sum + column_getSize(c, table), 0) + return column_getVisibleLeafColumns(column.table, position) + .slice(0, column_getIndex(column, position)) + .reduce((sum, c) => sum + column_getSize(c), 0) } export function column_getAfter< @@ -71,23 +69,19 @@ export function column_getAfter< TValue extends CellData = CellData, >( column: Column, - table: Table_Internal, position?: false | 'left' | 'right' | 'center', ): number { - return column_getVisibleLeafColumns(table, position) - .slice(column_getIndex(column, table, position) + 1) - .reduce((sum, c) => sum + column_getSize(c, table), 0) + return column_getVisibleLeafColumns(column.table, position) + .slice(column_getIndex(column, position) + 1) + .reduce((sum, c) => sum + column_getSize(c), 0) } export function column_resetSize< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - table: Table_Internal, - column: Column, -) { - table_setColumnSizing(table, ({ [column.id]: _, ...rest }) => { +>(column: Column) { + table_setColumnSizing(column.table, ({ [column.id]: _, ...rest }) => { return rest }) } @@ -96,17 +90,14 @@ export function header_getSize< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - header: Header, - table: Table_Internal, -) { +>(header: Header) { let sum = 0 const recurse = (h: Header) => { if (h.subHeaders.length) { h.subHeaders.forEach(recurse) } else { - sum += column_getSize(h.column, table) + sum += column_getSize(h.column) } } @@ -119,16 +110,12 @@ export function header_getStart< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - header: Header, - table: Table_Internal, -): number { +>(header: Header): number { if (header.index > 0) { const prevSiblingHeader = header.headerGroup?.headers[header.index - 1] if (prevSiblingHeader) { return ( - header_getStart(prevSiblingHeader, table) + - header_getSize(prevSiblingHeader, table) + header_getStart(prevSiblingHeader) + header_getSize(prevSiblingHeader) ) } } @@ -136,6 +123,8 @@ export function header_getStart< return 0 } +// Table APIs + export function table_setColumnSizing< TFeatures extends TableFeatures, TData extends RowData, @@ -162,7 +151,7 @@ export function table_getTotalSize< >(table: Table_Internal) { return ( table.getHeaderGroups()[0]?.headers.reduce((sum, header) => { - return sum + header_getSize(header, table) + return sum + header_getSize(header) }, 0) ?? 0 ) } @@ -173,7 +162,7 @@ export function table_getLeftTotalSize< >(table: Table_Internal) { return ( table_getLeftHeaderGroups(table)[0]?.headers.reduce((sum, header) => { - return sum + header_getSize(header, table) + return sum + header_getSize(header) }, 0) ?? 0 ) } @@ -184,7 +173,7 @@ export function table_getCenterTotalSize< >(table: Table_Internal) { return ( table_getCenterHeaderGroups(table)[0]?.headers.reduce((sum, header) => { - return sum + header_getSize(header, table) + return sum + header_getSize(header) }, 0) ?? 0 ) } @@ -195,7 +184,7 @@ export function table_getRightTotalSize< >(table: Table_Internal) { return ( table_getRightHeaderGroups(table)[0]?.headers.reduce((sum, header) => { - return sum + header_getSize(header, table) + return sum + header_getSize(header) }, 0) ?? 0 ) } diff --git a/packages/table-core/src/features/column-visibility/ColumnVisibility.ts b/packages/table-core/src/features/column-visibility/ColumnVisibility.ts index f523539991..1b699a9838 100644 --- a/packages/table-core/src/features/column-visibility/ColumnVisibility.ts +++ b/packages/table-core/src/features/column-visibility/ColumnVisibility.ts @@ -66,21 +66,19 @@ export const ColumnVisibility: TableFeature = { TValue extends CellData = CellData, >( column: Column & Partial, - table: Table & - Partial>, ): void => { assignAPIs(column, [ { - fn: () => column_getIsVisible(column, table), + fn: () => column_getIsVisible(column), }, { - fn: () => column_getCanHide(column, table), + fn: () => column_getCanHide(column), }, { - fn: () => column_getToggleVisibilityHandler(column, table), + fn: () => column_getToggleVisibilityHandler(column), }, { - fn: (visible) => column_toggleVisibility(column, table, visible), + fn: (visible) => column_toggleVisibility(column, visible), }, ]) }, @@ -88,20 +86,21 @@ export const ColumnVisibility: TableFeature = { constructRowAPIs: ( row: Row & Partial>, - table: Table & - Partial>, ): void => { assignAPIs(row, [ { - fn: () => row_getAllVisibleCells(row, table), - memoDeps: () => [row.getAllCells(), table.getState().columnVisibility], + fn: () => row_getAllVisibleCells(row), + memoDeps: () => [ + row.getAllCells(), + row.table.getState().columnVisibility, + ], }, { fn: (left, center, right) => row_getVisibleCells(left, center, right), memoDeps: () => [ - row_getLeftVisibleCells(row, table), - row_getCenterVisibleCells(row, table), - row_getRightVisibleCells(row, table), + row_getLeftVisibleCells(row), + row_getCenterVisibleCells(row), + row_getRightVisibleCells(row), ], }, ]) diff --git a/packages/table-core/src/features/column-visibility/ColumnVisibility.utils.ts b/packages/table-core/src/features/column-visibility/ColumnVisibility.utils.ts index 6c11dd4f77..cba6cce2b3 100644 --- a/packages/table-core/src/features/column-visibility/ColumnVisibility.utils.ts +++ b/packages/table-core/src/features/column-visibility/ColumnVisibility.utils.ts @@ -27,13 +27,12 @@ export function column_toggleVisibility< column: Column & { columnDef: Partial }, - table: Table_Internal, visible?: boolean, ): void { - if (column_getCanHide(column, table)) { - table_setColumnVisibility(table, (old) => ({ + if (column_getCanHide(column)) { + table_setColumnVisibility(column.table, (old) => ({ ...old, - [column.id]: visible ?? !column_getIsVisible(column, table), + [column.id]: visible ?? !column_getIsVisible(column), })) } } @@ -46,13 +45,12 @@ export function column_getIsVisible< column: Column & { columnDef: Partial }, - table: Table_Internal, ): boolean { const childColumns = column.columns return ( (childColumns.length - ? childColumns.some((c) => column_getIsVisible(c, table)) - : table.getState().columnVisibility?.[column.id]) ?? true + ? childColumns.some((c) => column_getIsVisible(c)) + : column.table.getState().columnVisibility?.[column.id]) ?? true ) } @@ -64,11 +62,10 @@ export function column_getCanHide< column: Column & { columnDef: Partial }, - table: Table_Internal, ) { return ( (column.columnDef.enableHiding ?? true) && - (table.options.enableHiding ?? true) + (column.table.options.enableHiding ?? true) ) } @@ -80,12 +77,10 @@ export function column_getToggleVisibilityHandler< column: Column & { columnDef: Partial }, - table: Table_Internal, ) { return (e: unknown) => { column_toggleVisibility( column, - table, ((e as MouseEvent).target as HTMLInputElement).checked, ) } @@ -110,10 +105,8 @@ export function column_getVisibleLeafColumns< export function row_getAllVisibleCells< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - return row - .getAllCells() - .filter((cell) => column_getIsVisible(cell.column, table)) +>(row: Row) { + return row.getAllCells().filter((cell) => column_getIsVisible(cell.column)) } export function row_getVisibleCells< @@ -133,7 +126,7 @@ export function table_getVisibleFlatColumns< >(table: Table_Internal) { return table .getAllFlatColumns() - .filter((column) => column_getIsVisible(column, table)) + .filter((column) => column_getIsVisible(column)) } export function table_getVisibleLeafColumns< @@ -142,7 +135,7 @@ export function table_getVisibleLeafColumns< >(table: Table_Internal) { return table .getAllLeafColumns() - .filter((column) => column_getIsVisible(column, table)) + .filter((column) => column_getIsVisible(column)) } export function table_setColumnVisibility< @@ -176,7 +169,7 @@ export function table_toggleAllColumnsVisible< table.getAllLeafColumns().reduce( (obj, column) => ({ ...obj, - [column.id]: !value ? !column_getCanHide(column, table) : value, + [column.id]: !value ? !column_getCanHide(column) : value, }), {}, ), @@ -189,16 +182,14 @@ export function table_getIsAllColumnsVisible< >(table: Table_Internal) { return !table .getAllLeafColumns() - .some((column) => !column_getIsVisible(column, table)) + .some((column) => !column_getIsVisible(column)) } export function table_getIsSomeColumnsVisible< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table - .getAllLeafColumns() - .some((column) => column_getIsVisible(column, table)) + return table.getAllLeafColumns().some((column) => column_getIsVisible(column)) } export function table_getToggleAllColumnsVisibilityHandler< diff --git a/packages/table-core/src/features/global-faceting/GlobalFaceting.utils.ts b/packages/table-core/src/features/global-faceting/GlobalFaceting.utils.ts index dc1a676f7b..7b19c7dd3c 100644 --- a/packages/table-core/src/features/global-faceting/GlobalFaceting.utils.ts +++ b/packages/table-core/src/features/global-faceting/GlobalFaceting.utils.ts @@ -3,11 +3,6 @@ import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../core/row-models/RowModels.types' import type { Table_Internal } from '../../types/Table' -/** - * - * @param table - * @returns - */ export function table_getGlobalFacetedMinMaxValues< TFeatures extends TableFeatures, TData extends RowData, @@ -18,11 +13,6 @@ export function table_getGlobalFacetedMinMaxValues< ) } -/** - * - * @param table - * @returns - */ export function table_getGlobalFacetedRowModel< TFeatures extends TableFeatures, TData extends RowData, @@ -33,11 +23,6 @@ export function table_getGlobalFacetedRowModel< ) } -/** - * - * @param table - * @returns - */ export function table_getGlobalFacetedUniqueValues< TFeatures extends TableFeatures, TData extends RowData, diff --git a/packages/table-core/src/features/global-filtering/GlobalFiltering.ts b/packages/table-core/src/features/global-filtering/GlobalFiltering.ts index da58b9557f..58f14a9072 100644 --- a/packages/table-core/src/features/global-filtering/GlobalFiltering.ts +++ b/packages/table-core/src/features/global-filtering/GlobalFiltering.ts @@ -62,12 +62,10 @@ export const GlobalFiltering: TableFeature = { TValue extends CellData = CellData, >( column: Column & Partial, - table: Table & - Partial>, ): void => { assignAPIs(column, [ { - fn: () => column_getCanGlobalFilter(column, table), + fn: () => column_getCanGlobalFilter(column), }, ]) }, diff --git a/packages/table-core/src/features/global-filtering/GlobalFiltering.utils.ts b/packages/table-core/src/features/global-filtering/GlobalFiltering.utils.ts index 8b1726331b..d4f0b1134e 100644 --- a/packages/table-core/src/features/global-filtering/GlobalFiltering.utils.ts +++ b/packages/table-core/src/features/global-filtering/GlobalFiltering.utils.ts @@ -1,4 +1,4 @@ -import { filterFns } from '../../fns/filterFns' +import { filterFn_includesString } from '../../fns/filterFns' import { isFunction } from '../../utils' import type { ColumnDefBase_All } from '../../types/ColumnDef' import type { FilterFn } from '../column-filtering/ColumnFiltering.types' @@ -6,14 +6,7 @@ import type { CellData, RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table_Internal } from '../../types/Table' import type { Column } from '../../types/Column' -import type { BuiltInFilterFn } from '../../fns/filterFns' -/** - * - * @param column - * @param table - * @returns - */ export function column_getCanGlobalFilter< TFeatures extends TableFeatures, TData extends RowData, @@ -22,8 +15,8 @@ export function column_getCanGlobalFilter< column: Column & { columnDef: ColumnDefBase_All }, - table: Table_Internal, ): boolean { + const { table } = column return ( (column.columnDef.enableGlobalFilter ?? true) && (table.options.enableGlobalFilter ?? true) && @@ -34,14 +27,9 @@ export function column_getCanGlobalFilter< } export function table_getGlobalAutoFilterFn() { - return filterFns.includesString + return filterFn_includesString } -/** - * - * @param table - * @returns - */ export function table_getGlobalFilterFn< TFeatures extends TableFeatures, TData extends RowData, @@ -49,20 +37,17 @@ export function table_getGlobalFilterFn< table: Table_Internal, ): FilterFn | FilterFn | undefined { const { globalFilterFn: globalFilterFn } = table.options + const filterFns = table._processingFns.filterFns as + | Record> + | undefined return isFunction(globalFilterFn) ? globalFilterFn : globalFilterFn === 'auto' ? table_getGlobalAutoFilterFn() - : (table._processingFns.filterFns?.[globalFilterFn as string] ?? - filterFns[globalFilterFn as BuiltInFilterFn]) + : filterFns?.[globalFilterFn as string] } -/** - * - * @param table - * @param updater - */ export function table_setGlobalFilter< TFeatures extends TableFeatures, TData extends RowData, @@ -70,11 +55,6 @@ export function table_setGlobalFilter< table.options.onGlobalFilterChange?.(updater) } -/** - * - * @param table - * @param defaultState - */ export function table_resetGlobalFilter< TFeatures extends TableFeatures, TData extends RowData, diff --git a/packages/table-core/src/features/row-expanding/RowExpanding.ts b/packages/table-core/src/features/row-expanding/RowExpanding.ts index d6a56fda41..f662e38c76 100644 --- a/packages/table-core/src/features/row-expanding/RowExpanding.ts +++ b/packages/table-core/src/features/row-expanding/RowExpanding.ts @@ -57,24 +57,22 @@ export const RowExpanding: TableFeature = { constructRowAPIs: ( row: Row & Partial, - table: Table & - Partial>, ): void => { assignAPIs(row, [ { - fn: (expanded) => row_toggleExpanded(row, table, expanded), + fn: (expanded) => row_toggleExpanded(row, expanded), }, { - fn: () => row_getIsExpanded(row, table), + fn: () => row_getIsExpanded(row), }, { - fn: () => row_getCanExpand(row, table), + fn: () => row_getCanExpand(row), }, { - fn: () => row_getIsAllParentsExpanded(row, table), + fn: () => row_getIsAllParentsExpanded(row), }, { - fn: () => row_getToggleExpandedHandler(row, table), + fn: () => row_getToggleExpandedHandler(row), }, ]) }, diff --git a/packages/table-core/src/features/row-expanding/RowExpanding.utils.ts b/packages/table-core/src/features/row-expanding/RowExpanding.utils.ts index a2e4db46ec..80ca6cec58 100644 --- a/packages/table-core/src/features/row-expanding/RowExpanding.utils.ts +++ b/packages/table-core/src/features/row-expanding/RowExpanding.utils.ts @@ -8,13 +8,6 @@ export function getDefaultExpandedState(): ExpandedState { return structuredClone({}) } -/** - * - * @param table - * @param registered - * @param queued - * @returns - */ export function table_autoResetExpanded< TFeatures extends TableFeatures, TData extends RowData, @@ -28,11 +21,6 @@ export function table_autoResetExpanded< } } -/** - * - * @param table - * @param updater - */ export function table_setExpanded< TFeatures extends TableFeatures, TData extends RowData, @@ -40,11 +28,6 @@ export function table_setExpanded< table.options.onExpandedChange?.(updater) } -/** - * - * @param table - * @param expanded - */ export function table_toggleAllRowsExpanded< TFeatures extends TableFeatures, TData extends RowData, @@ -56,11 +39,6 @@ export function table_toggleAllRowsExpanded< } } -/** - * - * @param table - * @param defaultState - */ export function table_resetExpanded< TFeatures extends TableFeatures, TData extends RowData, @@ -71,25 +49,15 @@ export function table_resetExpanded< ) } -/** - * - * @param table - * @returns - */ export function table_getCanSomeRowsExpand< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { return table .getPrePaginatedRowModel() - .flatRows.some((row) => row_getCanExpand(row, table)) + .flatRows.some((row) => row_getCanExpand(row)) } -/** - * - * @param table - * @returns - */ export function table_getToggleAllRowsExpandedHandler< TFeatures extends TableFeatures, TData extends RowData, @@ -100,11 +68,6 @@ export function table_getToggleAllRowsExpandedHandler< } } -/** - * - * @param table - * @returns - */ export function table_getIsSomeRowsExpanded< TFeatures extends TableFeatures, TData extends RowData, @@ -113,11 +76,6 @@ export function table_getIsSomeRowsExpanded< return expanded === true || Object.values(expanded).some(Boolean) } -/** - * - * @param table - * @returns - */ export function table_getIsAllRowsExpanded< TFeatures extends TableFeatures, TData extends RowData, @@ -134,9 +92,7 @@ export function table_getIsAllRowsExpanded< } // If any row is not expanded, return false - if ( - table.getRowModel().flatRows.some((row) => !row_getIsExpanded(row, table)) - ) { + if (table.getRowModel().flatRows.some((row) => !row_getIsExpanded(row))) { return false } @@ -144,11 +100,6 @@ export function table_getIsAllRowsExpanded< return true } -/** - * - * @param table - * @returns - */ export function table_getExpandedDepth< TFeatures extends TableFeatures, TData extends RowData, @@ -168,27 +119,17 @@ export function table_getExpandedDepth< return maxDepth } -/** - * - * @param row - * @param table - * @param expanded - */ export function row_toggleExpanded< TFeatures extends TableFeatures, TData extends RowData, ->( - row: Row, - table: Table_Internal, - expanded?: boolean, -) { - table_setExpanded(table, (old) => { +>(row: Row, expanded?: boolean) { + table_setExpanded(row.table, (old) => { const exists = old === true ? true : !!old[row.id] let oldExpanded: ExpandedStateList = {} if (old === true) { - Object.keys(table.getRowModel().rowsById).forEach((rowId) => { + Object.keys(row.table.getRowModel().rowsById).forEach((rowId) => { oldExpanded[rowId] = true }) } else { @@ -213,75 +154,51 @@ export function row_toggleExpanded< }) } -/** - * - * @param row - * @param table - * @returns - */ export function row_getIsExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const expanded = table.getState().expanded ?? {} +>(row: Row) { + const expanded = row.table.getState().expanded ?? {} return !!( - table.options.getIsRowExpanded?.(row) ?? + row.table.options.getIsRowExpanded?.(row) ?? (expanded === true || expanded[row.id]) ) } -/** - * - * @param row - * @param table - * @returns - */ export function row_getCanExpand< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { +>(row: Row) { return ( - table.options.getRowCanExpand?.(row) ?? - ((table.options.enableExpanding ?? true) && !!row.subRows.length) + row.table.options.getRowCanExpand?.(row) ?? + ((row.table.options.enableExpanding ?? true) && !!row.subRows.length) ) } -/** - * - * @param row - * @param table - * @returns - */ export function row_getIsAllParentsExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { +>(row: Row) { let isFullyExpanded = true let currentRow = row while (isFullyExpanded && currentRow.parentId) { - currentRow = table.getRow(currentRow.parentId, true) - isFullyExpanded = row_getIsExpanded(row, table) + currentRow = row.table.getRow(currentRow.parentId, true) + isFullyExpanded = row_getIsExpanded(row) } return isFullyExpanded } -/** - * - * @param row - * @param table - * @returns - */ export function row_getToggleExpandedHandler< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const canExpand = row_getCanExpand(row, table) +>(row: Row) { + const canExpand = row_getCanExpand(row) return () => { if (!canExpand) return - row_toggleExpanded(row, table) + row_toggleExpanded(row) } } diff --git a/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts b/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts index 2b10ba11e5..b788a5032a 100644 --- a/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts +++ b/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts @@ -3,7 +3,7 @@ import { row_getIsExpanded } from './RowExpanding.utils' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../core/row-models/RowModels.types' -import type { Table, Table_Internal } from '../../types/Table' +import type { Table_Internal } from '../../types/Table' import type { Row } from '../../types/Row' export function createExpandedRowModel< @@ -44,19 +44,19 @@ export function _createExpandedRowModel< return rowModel } - return expandRows(rowModel, table) + return expandRows(rowModel) } export function expandRows< TFeatures extends TableFeatures, TData extends RowData, ->(rowModel: RowModel, table: Table) { +>(rowModel: RowModel) { const expandedRows: Array> = [] const handleRow = (row: Row) => { expandedRows.push(row) - if (row.subRows.length && row_getIsExpanded(row, table)) { + if (row.subRows.length && row_getIsExpanded(row)) { row.subRows.forEach(handleRow) } } diff --git a/packages/table-core/src/features/row-pagination/RowPagination.utils.ts b/packages/table-core/src/features/row-pagination/RowPagination.utils.ts index 070eb355dc..658698ee30 100644 --- a/packages/table-core/src/features/row-pagination/RowPagination.utils.ts +++ b/packages/table-core/src/features/row-pagination/RowPagination.utils.ts @@ -7,10 +7,6 @@ import type { PaginationState } from './RowPagination.types' const defaultPageIndex = 0 const defaultPageSize = 10 -/** - * - * @returns - */ export function getDefaultPaginationState(): PaginationState { return structuredClone({ pageIndex: defaultPageIndex, @@ -18,13 +14,6 @@ export function getDefaultPaginationState(): PaginationState { }) } -/** - * - * @param table - * @param registered - * @param queued - * @returns - */ export function table_autoResetPageIndex< TFeatures extends TableFeatures, TData extends RowData, @@ -38,12 +27,6 @@ export function table_autoResetPageIndex< } } -/** - * - * @param table - * @param updater - * @returns - */ export function table_setPagination< TFeatures extends TableFeatures, TData extends RowData, @@ -57,11 +40,6 @@ export function table_setPagination< return table.options.onPaginationChange?.(safeUpdater) } -/** - * - * @param table - * @param defaultState - */ export function table_resetPagination< TFeatures extends TableFeatures, TData extends RowData, @@ -74,11 +52,6 @@ export function table_resetPagination< ) } -/** - * - * @param table - * @param updater - */ export function table_setPageIndex< TFeatures extends TableFeatures, TData extends RowData, @@ -101,11 +74,6 @@ export function table_setPageIndex< }) } -/** - * - * @param table - * @param defaultState - */ export function table_resetPageIndex< TFeatures extends TableFeatures, TData extends RowData, @@ -118,11 +86,6 @@ export function table_resetPageIndex< ) } -/** - * - * @param table - * @param defaultState - */ export function table_resetPageSize< TFeatures extends TableFeatures, TData extends RowData, @@ -135,11 +98,6 @@ export function table_resetPageSize< ) } -/** - * - * @param table - * @param updater - */ export function table_setPageSize< TFeatures extends TableFeatures, TData extends RowData, @@ -157,11 +115,6 @@ export function table_setPageSize< }) } -/** - * - * @param table - * @returns - */ export function table_getPageOptions< TFeatures extends TableFeatures, TData extends RowData, @@ -174,11 +127,6 @@ export function table_getPageOptions< return pageOptions } -/** - * - * @param table - * @returns - */ export function table_getCanPreviousPage< TFeatures extends TableFeatures, TData extends RowData, @@ -186,11 +134,6 @@ export function table_getCanPreviousPage< return (table.getState().pagination?.pageIndex ?? 0) > 0 } -/** - * - * @param table - * @returns - */ export function table_getCanNextPage< TFeatures extends TableFeatures, TData extends RowData, @@ -210,11 +153,6 @@ export function table_getCanNextPage< return pageIndex < pageCount - 1 } -/** - * - * @param table - * @returns - */ export function table_previousPage< TFeatures extends TableFeatures, TData extends RowData, @@ -222,11 +160,6 @@ export function table_previousPage< return table_setPageIndex(table, (old) => old - 1) } -/** - * - * @param table - * @returns - */ export function table_nextPage< TFeatures extends TableFeatures, TData extends RowData, @@ -236,11 +169,6 @@ export function table_nextPage< }) } -/** - * - * @param table - * @returns - */ export function table_firstPage< TFeatures extends TableFeatures, TData extends RowData, @@ -248,11 +176,6 @@ export function table_firstPage< return table_setPageIndex(table, 0) } -/** - * - * @param table - * @returns - */ export function table_lastPage< TFeatures extends TableFeatures, TData extends RowData, @@ -260,11 +183,6 @@ export function table_lastPage< return table_setPageIndex(table, table_getPageCount(table) - 1) } -/** - * - * @param table - * @returns - */ export function table_getPageCount< TFeatures extends TableFeatures, TData extends RowData, @@ -278,11 +196,6 @@ export function table_getPageCount< ) } -/** - * - * @param table - * @returns - */ export function table_getRowCount< TFeatures extends TableFeatures, TData extends RowData, diff --git a/packages/table-core/src/features/row-pinning/RowPinning.ts b/packages/table-core/src/features/row-pinning/RowPinning.ts index c40921018d..cfc60e4a2d 100644 --- a/packages/table-core/src/features/row-pinning/RowPinning.ts +++ b/packages/table-core/src/features/row-pinning/RowPinning.ts @@ -53,23 +53,24 @@ export const RowPinning: TableFeature = { constructRowAPIs: ( row: Row & Partial, - table: Table & - Partial>, ): void => { assignAPIs(row, [ { - fn: () => row_getCanPin(row, table), + fn: () => row_getCanPin(row), }, { - fn: () => row_getIsPinned(row, table), + fn: () => row_getIsPinned(row), }, { - fn: () => row_getPinnedIndex(row, table), - memoDeps: () => [table.getRowModel().rows, table.getState().rowPinning], + fn: () => row_getPinnedIndex(row), + memoDeps: () => [ + row.table.getRowModel().rows, + row.table.getState().rowPinning, + ], }, { fn: (position, includeLeafRows, includeParentRows) => - row_pin(row, table, position, includeLeafRows, includeParentRows), + row_pin(row, position, includeLeafRows, includeParentRows), }, ]) }, diff --git a/packages/table-core/src/features/row-pinning/RowPinning.utils.ts b/packages/table-core/src/features/row-pinning/RowPinning.utils.ts index 62b3f9d942..96f4ad36bb 100644 --- a/packages/table-core/src/features/row-pinning/RowPinning.utils.ts +++ b/packages/table-core/src/features/row-pinning/RowPinning.utils.ts @@ -7,10 +7,6 @@ import type { RowPinningPosition, RowPinningState } from './RowPinning.types' // State Utils -/** - * - * @returns - */ export function getDefaultRowPinningState(): RowPinningState { return structuredClone({ top: [], @@ -18,11 +14,6 @@ export function getDefaultRowPinningState(): RowPinningState { }) } -/** - * - * @param table - * @param updater - */ export function table_setRowPinning< TFeatures extends TableFeatures, TData extends RowData, @@ -33,11 +24,6 @@ export function table_setRowPinning< table.options.onRowPinningChange?.(updater) } -/** - * - * @param table - * @param defaultState - */ export function table_resetRowPinning< TFeatures extends TableFeatures, TData extends RowData, @@ -52,12 +38,6 @@ export function table_resetRowPinning< // Table Utils -/** - * - * @param table - * @param position - * @returns - */ export function table_getIsSomeRowsPinned< TFeatures extends TableFeatures, TData extends RowData, @@ -73,12 +53,6 @@ export function table_getIsSomeRowsPinned< return Boolean(rowPinning?.[position].length) } -/** - * - * @param table - * @param position - * @returns - */ function table_getPinnedRows< TFeatures extends TableFeatures, TData extends RowData, @@ -95,7 +69,7 @@ function table_getPinnedRows< // account for expanded parent rows, but not pagination or filtering pinnedRowIds.map((rowId) => { const row = table.getRow(rowId, true) - return row_getIsAllParentsExpanded(row, table) ? row : null + return row_getIsAllParentsExpanded(row) ? row : null }) : // else get only visible rows that are pinned pinnedRowIds.map( @@ -105,11 +79,6 @@ function table_getPinnedRows< return rows.filter((r) => !!r).map((d) => ({ ...d, position })) } -/** - * - * @param table - * @returns - */ export function table_getTopRows< TFeatures extends TableFeatures, TData extends RowData, @@ -117,11 +86,6 @@ export function table_getTopRows< return table_getPinnedRows(table, 'top') } -/** - * - * @param table - * @returns - */ export function table_getBottomRows< TFeatures extends TableFeatures, TData extends RowData, @@ -129,11 +93,6 @@ export function table_getBottomRows< return table_getPinnedRows(table, 'bottom') } -/** - * - * @param table - * @returns - */ export function table_getCenterRows< TFeatures extends TableFeatures, TData extends RowData, @@ -148,41 +107,23 @@ export function table_getCenterRows< // Row Utils -/** - * - * @param row - * @param table - * @returns - */ export function row_getCanPin< TFeatures extends TableFeatures, TData extends RowData, ->( - row: Row, - table: Table_Internal, -): boolean { - const { enableRowPinning } = table.options +>(row: Row): boolean { + const { enableRowPinning } = row.table.options if (typeof enableRowPinning === 'function') { return enableRowPinning(row) } return enableRowPinning ?? true } -/** - * - * @param row - * @param table - * @returns - */ export function row_getIsPinned< TFeatures extends TableFeatures, TData extends RowData, ->( - row: Row, - table: Table_Internal, -): RowPinningPosition { +>(row: Row): RowPinningPosition { const { top, bottom } = - table.getState().rowPinning ?? getDefaultRowPinningState() + row.table.getState().rowPinning ?? getDefaultRowPinningState() return top.includes(row.id) ? 'top' @@ -191,37 +132,24 @@ export function row_getIsPinned< : false } -/** - * - * @param row - * @param table - * @returns - */ export function row_getPinnedIndex< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal): number { - const position = row_getIsPinned(row, table) +>(row: Row): number { + const position = row_getIsPinned(row) if (!position) return -1 const visiblePinnedRowIds = ( - position === 'top' ? table_getTopRows(table) : table_getBottomRows(table) + position === 'top' + ? table_getTopRows(row.table) + : table_getBottomRows(row.table) ).map(({ id }) => id) return visiblePinnedRowIds.indexOf(row.id) } -/** - * - * @param row - * @param table - * @param position - * @param includeLeafRows - * @param includeParentRows - */ export function row_pin( row: Row, - table: Table_Internal, position: RowPinningPosition, includeLeafRows?: boolean, includeParentRows?: boolean, @@ -234,7 +162,7 @@ export function row_pin( : [] const rowIds = new Set([...parentRowIds, row.id, ...leafRowIds]) - table_setRowPinning(table, (old) => { + table_setRowPinning(row.table, (old) => { if (position === 'bottom') { return { top: old.top.filter((d) => !rowIds.has(d)), diff --git a/packages/table-core/src/features/row-selection/RowSelection.ts b/packages/table-core/src/features/row-selection/RowSelection.ts index f4264ada12..4f24f2d961 100644 --- a/packages/table-core/src/features/row-selection/RowSelection.ts +++ b/packages/table-core/src/features/row-selection/RowSelection.ts @@ -72,33 +72,31 @@ export const RowSelection: TableFeature = { constructRowAPIs: ( row: Row & Partial, - table: Table & - Partial>, ): void => { assignAPIs(row, [ { - fn: (value, opts) => row_toggleSelected(row, table, value, opts), + fn: (value, opts) => row_toggleSelected(row, value, opts), }, { - fn: () => row_getIsSelected(row, table), + fn: () => row_getIsSelected(row), }, { - fn: () => row_getIsSomeSelected(row, table), + fn: () => row_getIsSomeSelected(row), }, { - fn: () => row_getIsAllSubRowsSelected(row, table), + fn: () => row_getIsAllSubRowsSelected(row), }, { - fn: () => row_getCanSelect(row, table), + fn: () => row_getCanSelect(row), }, { - fn: () => row_getCanSelectSubRows(row, table), + fn: () => row_getCanSelectSubRows(row), }, { - fn: () => row_getCanMultiSelect(row, table), + fn: () => row_getCanMultiSelect(row), }, { - fn: () => row_getToggleSelectedHandler(row, table), + fn: () => row_getToggleSelectedHandler(row), }, ]) }, diff --git a/packages/table-core/src/features/row-selection/RowSelection.utils.ts b/packages/table-core/src/features/row-selection/RowSelection.utils.ts index 558b458323..85b728c515 100644 --- a/packages/table-core/src/features/row-selection/RowSelection.utils.ts +++ b/packages/table-core/src/features/row-selection/RowSelection.utils.ts @@ -11,11 +11,6 @@ export function getDefaultRowSelectionState(): RowSelectionState { return structuredClone({}) } -/** - * - * @param table - * @param updater - */ export function table_setRowSelection< TFeatures extends TableFeatures, TData extends RowData, @@ -26,11 +21,6 @@ export function table_setRowSelection< table.options.onRowSelectionChange?.(updater) } -/** - * - * @param table - * @param defaultState - */ export function table_resetRowSelection< TFeatures extends TableFeatures, TData extends RowData, @@ -43,11 +33,6 @@ export function table_resetRowSelection< // Table utils -/** - * - * @param table - * @param value - */ export function table_toggleAllRowsSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -64,7 +49,7 @@ export function table_toggleAllRowsSelected< // All of the rows are flat already, so it wouldn't be worth it if (value) { preGroupedFlatRows.forEach((row) => { - if (!row_getCanSelect(row, table)) { + if (!row_getCanSelect(row)) { return } rowSelection[row.id] = true @@ -79,11 +64,6 @@ export function table_toggleAllRowsSelected< }) } -/** - * - * @param table - * @param value - */ export function table_toggleAllPageRowsSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -104,11 +84,6 @@ export function table_toggleAllPageRowsSelected< }) } -/** - * - * @param table - * @returns - */ export function table_getPreSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, @@ -116,11 +91,6 @@ export function table_getPreSelectedRowModel< return table.getCoreRowModel() } -/** - * - * @param table - * @returns - */ export function table_getSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, @@ -138,11 +108,6 @@ export function table_getSelectedRowModel< return selectRowsFn(table, rowModel) } -/** - * - * @param table - * @returns - */ export function table_getFilteredSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, @@ -160,11 +125,6 @@ export function table_getFilteredSelectedRowModel< return selectRowsFn(table, rowModel) } -/** - * - * @param table - * @returns - */ export function table_getGroupedSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, @@ -182,11 +142,6 @@ export function table_getGroupedSelectedRowModel< return selectRowsFn(table, rowModel) } -/** - * - * @param table - * @returns - */ export function table_getIsAllRowsSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -201,7 +156,7 @@ export function table_getIsAllRowsSelected< if (isAllRowsSelected) { if ( preGroupedFlatRows.some( - (row) => row_getCanSelect(row, table) && !rowSelection[row.id], + (row) => row_getCanSelect(row) && !rowSelection[row.id], ) ) { isAllRowsSelected = false @@ -211,18 +166,13 @@ export function table_getIsAllRowsSelected< return isAllRowsSelected } -/** - * - * @param table - * @returns - */ export function table_getIsAllPageRowsSelected< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { const paginationFlatRows = table .getPaginatedRowModel() - .flatRows.filter((row) => row_getCanSelect(row, table)) + .flatRows.filter((row) => row_getCanSelect(row)) const rowSelection = table.getState().rowSelection ?? {} let isAllPageRowsSelected = !!paginationFlatRows.length @@ -237,11 +187,6 @@ export function table_getIsAllPageRowsSelected< return isAllPageRowsSelected } -/** - * - * @param table - * @returns - */ export function table_getIsSomeRowsSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -253,11 +198,6 @@ export function table_getIsSomeRowsSelected< ) } -/** - * - * @param table - * @returns - */ export function table_getIsSomePageRowsSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -266,18 +206,10 @@ export function table_getIsSomePageRowsSelected< return table_getIsAllPageRowsSelected(table) ? false : paginationFlatRows - .filter((row) => row_getCanSelect(row, table)) - .some( - (row) => - row_getIsSelected(row, table) || row_getIsSomeSelected(row, table), - ) + .filter((row) => row_getCanSelect(row)) + .some((row) => row_getIsSelected(row) || row_getIsSomeSelected(row)) } -/** - * - * @param table - * @returns - */ export function table_getToggleAllRowsSelectedHandler< TFeatures extends TableFeatures, TData extends RowData, @@ -290,11 +222,6 @@ export function table_getToggleAllRowsSelectedHandler< } } -/** - * - * @param table - * @returns - */ export function table_getToggleAllPageRowsSelectedHandler< TFeatures extends TableFeatures, TData extends RowData, @@ -307,30 +234,24 @@ export function table_getToggleAllPageRowsSelectedHandler< } } -/** - * - * @param row - * @param table - * @param value - * @param opts - */ +// Row utils + export function row_toggleSelected< TFeatures extends TableFeatures, TData extends RowData, >( row: Row, - table: Table_Internal, value?: boolean, opts?: { selectChildren?: boolean }, ) { - const isSelected = row_getIsSelected(row, table) + const isSelected = row_getIsSelected(row) - table_setRowSelection(table, (old) => { + table_setRowSelection(row.table, (old) => { value = typeof value !== 'undefined' ? value : !isSelected - if (row_getCanSelect(row, table) && isSelected === value) { + if (row_getCanSelect(row) && isSelected === value) { return old } @@ -341,138 +262,88 @@ export function row_toggleSelected< row.id, value, opts?.selectChildren ?? true, - table, + row.table, ) return selectedRowIds }) } -// Row utils - -/** - * - * @param row - * @param table - * @returns - */ export function row_getIsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const rowSelection = table.getState().rowSelection ?? {} +>(row: Row) { + const rowSelection = row.table.getState().rowSelection ?? {} return isRowSelected(row, rowSelection) } -/** - * - * @param row - * @param table - * @returns - */ export function row_getIsSomeSelected< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const rowSelection = table.getState().rowSelection ?? {} - return isSubRowSelected(row, rowSelection, table) === 'some' +>(row: Row) { + const rowSelection = row.table.getState().rowSelection ?? {} + return isSubRowSelected(row, rowSelection) === 'some' } -/** - * - * @param row - * @param table - * @returns - */ export function row_getIsAllSubRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const rowSelection = table.getState().rowSelection ?? {} - return isSubRowSelected(row, rowSelection, table) === 'all' +>(row: Row) { + const rowSelection = row.table.getState().rowSelection ?? {} + return isSubRowSelected(row, rowSelection) === 'all' } -/** - * - * @param row - * @param table - * @returns - */ export function row_getCanSelect< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - if (typeof table.options.enableRowSelection === 'function') { - return table.options.enableRowSelection(row) +>(row: Row) { + const options = row.table.options + if (typeof options.enableRowSelection === 'function') { + return options.enableRowSelection(row) } - return table.options.enableRowSelection ?? true + return options.enableRowSelection ?? true } -/** - * - * @param row - * @param table - * @returns - */ export function row_getCanSelectSubRows< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - if (typeof table.options.enableSubRowSelection === 'function') { - return table.options.enableSubRowSelection(row) +>(row: Row) { + const options = row.table.options + if (typeof options.enableSubRowSelection === 'function') { + return options.enableSubRowSelection(row) } - return table.options.enableSubRowSelection ?? true + return options.enableSubRowSelection ?? true } -/** - * - * @param row - * @param table - * @returns - */ export function row_getCanMultiSelect< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - if (typeof table.options.enableMultiRowSelection === 'function') { - return table.options.enableMultiRowSelection(row) +>(row: Row) { + const options = row.table.options + if (typeof options.enableMultiRowSelection === 'function') { + return options.enableMultiRowSelection(row) } - return table.options.enableMultiRowSelection ?? true + return options.enableMultiRowSelection ?? true } -/** - * - * @param row - * @param table - * @returns - */ export function row_getToggleSelectedHandler< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table_Internal) { - const canSelect = row_getCanSelect(row, table) +>(row: Row) { + const canSelect = row_getCanSelect(row) return (e: unknown) => { if (!canSelect) return row_toggleSelected( row, - table, ((e as MouseEvent).target as HTMLInputElement).checked, ) } } -/** - * - * @param selectedRowIds - * @param rowId - * @param value - * @param includeChildren - * @param table - */ const mutateRowIsSelected = < TFeatures extends TableFeatures, TData extends RowData, @@ -485,41 +356,24 @@ const mutateRowIsSelected = < ) => { const row = table.getRow(rowId, true) - // const isGrouped = row.getIsGrouped() - - // if ( // TODO: enforce grouping row selection rules - // !isGrouped || - // (isGrouped && table.options.enableGroupingRowSelection) - // ) { if (value) { - if (!row_getCanMultiSelect(row, table)) { + if (!row_getCanMultiSelect(row)) { Object.keys(selectedRowIds).forEach((key) => delete selectedRowIds[key]) } - if (row_getCanSelect(row, table)) { + if (row_getCanSelect(row)) { selectedRowIds[rowId] = true } } else { delete selectedRowIds[rowId] } - // } - if ( - includeChildren && - row.subRows.length && - row_getCanSelectSubRows(row, table) - ) { + if (includeChildren && row.subRows.length && row_getCanSelectSubRows(row)) { row.subRows.forEach((r) => mutateRowIsSelected(selectedRowIds, r.id, value, includeChildren, table), ) } } -/** - * - * @param table - * @param rowModel - * @returns - */ export function selectRowsFn< TFeatures extends TableFeatures, TData extends RowData, @@ -567,12 +421,6 @@ export function selectRowsFn< } } -/** - * - * @param row - * @param selection - * @returns - */ export function isRowSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -586,7 +434,6 @@ export function isSubRowSelected< >( row: Row, selection: Record, - table: Table_Internal, ): boolean | 'some' | 'all' { if (!row.subRows.length) return false @@ -599,7 +446,7 @@ export function isSubRowSelected< return } - if (row_getCanSelect(subRow, table)) { + if (row_getCanSelect(subRow)) { if (isRowSelected(subRow, selection)) { someSelected = true } else { @@ -609,7 +456,7 @@ export function isSubRowSelected< // Check row selection of nested subrows if (subRow.subRows.length) { - const subRowChildrenSelected = isSubRowSelected(subRow, selection, table) + const subRowChildrenSelected = isSubRowSelected(subRow, selection) if (subRowChildrenSelected === 'all') { someSelected = true } else if (subRowChildrenSelected === 'some') { diff --git a/packages/table-core/src/features/row-sorting/RowSorting.ts b/packages/table-core/src/features/row-sorting/RowSorting.ts index 165b9465e7..251bc18e9e 100644 --- a/packages/table-core/src/features/row-sorting/RowSorting.ts +++ b/packages/table-core/src/features/row-sorting/RowSorting.ts @@ -71,44 +71,43 @@ export const RowSorting: TableFeature = { TValue extends CellData = CellData, >( column: Column, - table: Table, ): void => { assignAPIs(column, [ { - fn: () => column_getAutoSortingFn(column, table), + fn: () => column_getAutoSortingFn(column), }, { - fn: () => column_getAutoSortDir(column, table), + fn: () => column_getAutoSortDir(column), }, { - fn: () => column_getSortingFn(column, table), + fn: () => column_getSortingFn(column), }, { - fn: (desc, multi) => column_toggleSorting(column, table, desc, multi), + fn: (desc, multi) => column_toggleSorting(column, desc, multi), }, { - fn: () => column_getFirstSortDir(column, table), + fn: () => column_getFirstSortDir(column), }, { - fn: (multi) => column_getNextSortingOrder(column, table, multi), + fn: (multi) => column_getNextSortingOrder(column, multi), }, { - fn: () => column_getCanSort(column, table), + fn: () => column_getCanSort(column), }, { - fn: () => column_getCanMultiSort(column, table), + fn: () => column_getCanMultiSort(column), }, { - fn: () => column_getIsSorted(column, table), + fn: () => column_getIsSorted(column), }, { - fn: () => column_getSortIndex(column, table), + fn: () => column_getSortIndex(column), }, { - fn: () => column_clearSorting(column, table), + fn: () => column_clearSorting(column), }, { - fn: () => column_getToggleSortingHandler(column, table), + fn: () => column_getToggleSortingHandler(column), }, ]) }, diff --git a/packages/table-core/src/features/row-sorting/RowSorting.utils.ts b/packages/table-core/src/features/row-sorting/RowSorting.utils.ts index b7d1201c97..ef3ff8dee2 100644 --- a/packages/table-core/src/features/row-sorting/RowSorting.utils.ts +++ b/packages/table-core/src/features/row-sorting/RowSorting.utils.ts @@ -49,15 +49,14 @@ export function column_getAutoSortingFn< TValue extends CellData = CellData, >( column: Column_Internal, - table: Table_Internal, ): SortingFn { - const sortingFns = table._processingFns.sortingFns as + const sortingFns = column.table._processingFns.sortingFns as | Record> | undefined let sortingFn: SortingFn | undefined - const firstRows = table.getFilteredRowModel().flatRows.slice(10) + const firstRows = column.table.getFilteredRowModel().flatRows.slice(10) let isString = false @@ -94,11 +93,8 @@ export function column_getAutoSortDir< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -) { - const firstRow = table.getFilteredRowModel().flatRows[0] +>(column: Column_Internal) { + const firstRow = column.table.getFilteredRowModel().flatRows[0] const value = firstRow ? firstRow.getValue(column.id) : undefined @@ -121,16 +117,15 @@ export function column_getSortingFn< TValue extends CellData = CellData, >( column: Column_Internal, - table: Table_Internal, ): SortingFn { - const sortingFns = table._processingFns.sortingFns as + const sortingFns = column.table._processingFns.sortingFns as | Record> | undefined return isFunction(column.columnDef.sortingFn) ? column.columnDef.sortingFn : column.columnDef.sortingFn === 'auto' - ? column_getAutoSortingFn(column, table) + ? column_getAutoSortingFn(column) : (sortingFns?.[column.columnDef.sortingFn as string] ?? sortingFn_basic) } @@ -147,7 +142,6 @@ export function column_toggleSorting< TValue extends CellData = CellData, >( column: Column_Internal, - table: Table_Internal, desc?: boolean, multi?: boolean, ) { @@ -161,10 +155,10 @@ export function column_toggleSorting< // } // this needs to be outside of table.setSorting to be in sync with rerender - const nextSortingOrder = column_getNextSortingOrder(column, table) + const nextSortingOrder = column_getNextSortingOrder(column) const hasManualValue = typeof desc !== 'undefined' - table_setSorting(table, (old) => { + table_setSorting(column.table, (old) => { // Find any existing sorting for this column const existingSorting = old.find((d) => d.id === column.id) const existingIndex = old.findIndex((d) => d.id === column.id) @@ -176,7 +170,7 @@ export function column_toggleSorting< const nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc' // Multi-mode - if (old.length && column_getCanMultiSort(column, table) && multi) { + if (old.length && column_getCanMultiSort(column) && multi) { if (existingSorting) { sortAction = 'toggle' } else { @@ -216,7 +210,8 @@ export function column_toggleSorting< newSorting.splice( 0, newSorting.length - - (table.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER), + (column.table.options.maxMultiSortColCount ?? + Number.MAX_SAFE_INTEGER), ) } else if (sortAction === 'toggle') { // This flips (or sets) the @@ -254,14 +249,11 @@ export function column_getFirstSortDir< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -) { +>(column: Column_Internal) { const sortDescFirst = column.columnDef.sortDescFirst ?? - table.options.sortDescFirst ?? - column_getAutoSortDir(column, table) === 'desc' + column.table.options.sortDescFirst ?? + column_getAutoSortDir(column) === 'desc' return sortDescFirst ? 'desc' : 'asc' } @@ -276,13 +268,9 @@ export function column_getNextSortingOrder< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, - multi?: boolean, -) { - const firstSortDirection = column_getFirstSortDir(column, table) - const isSorted = column_getIsSorted(column, table) +>(column: Column_Internal, multi?: boolean) { + const firstSortDirection = column_getFirstSortDir(column) + const isSorted = column_getIsSorted(column) if (!isSorted) { return firstSortDirection @@ -290,8 +278,8 @@ export function column_getNextSortingOrder< if ( isSorted !== firstSortDirection && - (table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general - (multi ? (table.options.enableMultiRemove ?? true) : true) // If multi, don't allow if enableMultiRemove)) + (column.table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general + (multi ? (column.table.options.enableMultiRemove ?? true) : true) // If multi, don't allow if enableMultiRemove)) ) { return false } @@ -308,13 +296,10 @@ export function column_getCanSort< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -) { +>(column: Column_Internal) { return ( (column.columnDef.enableSorting ?? true) && - (table.options.enableSorting ?? true) && + (column.table.options.enableSorting ?? true) && !!column.accessorFn ) } @@ -329,13 +314,10 @@ export function column_getCanMultiSort< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -): boolean { +>(column: Column_Internal): boolean { return ( column.columnDef.enableMultiSort ?? - table.options.enableMultiSort ?? + column.table.options.enableMultiSort ?? !!column.accessorFn ) } @@ -350,11 +332,10 @@ export function column_getIsSorted< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -): false | SortDirection { - const columnSort = table.getState().sorting?.find((d) => d.id === column.id) +>(column: Column_Internal): false | SortDirection { + const columnSort = column.table + .getState() + .sorting?.find((d) => d.id === column.id) return !columnSort ? false : columnSort.desc ? 'desc' : 'asc' } @@ -368,11 +349,10 @@ export function column_getSortIndex< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -): number { - return table.getState().sorting?.findIndex((d) => d.id === column.id) ?? -1 +>(column: Column_Internal): number { + return ( + column.table.getState().sorting?.findIndex((d) => d.id === column.id) ?? -1 + ) } /** @@ -384,12 +364,9 @@ export function column_clearSorting< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -) { +>(column: Column_Internal) { // clear sorting for just 1 column - table_setSorting(table, (old) => + table_setSorting(column.table, (old) => old.length ? old.filter((d) => d.id !== column.id) : [], ) } @@ -404,21 +381,18 @@ export function column_getToggleSortingHandler< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->( - column: Column_Internal, - table: Table_Internal, -) { - const canSort = column_getCanSort(column, table) +>(column: Column_Internal) { + const canSort = column_getCanSort(column) return (e: unknown) => { if (!canSort) return ;(e as any).persist?.() column_toggleSorting( column, - table, + undefined, - column_getCanMultiSort(column, table) - ? table.options.isMultiSortEvent?.(e) + column_getCanMultiSort(column) + ? column.table.options.isMultiSortEvent?.(e) : false, ) } diff --git a/packages/table-core/src/features/row-sorting/createSortedRowModel.ts b/packages/table-core/src/features/row-sorting/createSortedRowModel.ts index e99d13ffa8..cf417a0cd0 100644 --- a/packages/table-core/src/features/row-sorting/createSortedRowModel.ts +++ b/packages/table-core/src/features/row-sorting/createSortedRowModel.ts @@ -40,7 +40,7 @@ function _createSortedRowModel< // Filter out sortings that correspond to non existing columns const availableSorting = sorting.filter((sort) => - column_getCanSort(table.getColumn(sort.id)!, table), + column_getCanSort(table.getColumn(sort.id)!), ) const columnInfoById: Record< @@ -61,7 +61,7 @@ function _createSortedRowModel< columnInfoById[sortEntry.id] = { sortUndefined: column.columnDef.sortUndefined, invertSorting: column.columnDef.invertSorting, - sortingFn: column_getSortingFn(column, table), + sortingFn: column_getSortingFn(column), } }) diff --git a/packages/table-core/src/helpers/tableHelper.ts b/packages/table-core/src/helpers/tableHelper.ts index 32a6dc9caa..7277722b80 100644 --- a/packages/table-core/src/helpers/tableHelper.ts +++ b/packages/table-core/src/helpers/tableHelper.ts @@ -1,5 +1,4 @@ import { createColumnHelper } from './columnHelper' -import type { ProcessingFns_All } from '../types/ProcessingFns' import type { ColumnHelper } from './columnHelper' import type { RowData } from '../types/type-utils' import type { TableFeatures } from '../types/TableFeatures' @@ -27,7 +26,6 @@ export type TableHelper_Core< > = { columnHelper: ColumnHelper features: TFeatures - processingFns: ProcessingFns_All options: Omit, 'columns' | 'data' | 'state'> tableCreator: ( tableOptions: Omit< @@ -53,7 +51,6 @@ export function constructTableHelper< return { columnHelper: createColumnHelper(), features: tableHelperOptions._features, - processingFns: tableHelperOptions._processingFns ?? {}, options: _tableHelperOptions as any, tableCreator: (tableOptions) => tableCreator({ ...(_tableHelperOptions as any), ...tableOptions }), diff --git a/packages/table-core/src/index.ts b/packages/table-core/src/index.ts index 7cd35cebac..632bcebead 100755 --- a/packages/table-core/src/index.ts +++ b/packages/table-core/src/index.ts @@ -16,7 +16,7 @@ export * from './types/TableState' export * from './types/type-utils' /** - * coreRowModel + * Core */ export * from './core/coreFeatures' @@ -63,6 +63,14 @@ export * from './core/table/Tables' export * from './core/table/Tables.types' export * from './core/table/Tables.utils' +/** + * ProcessingFns + */ + +export * from './fns/aggregationFns' +export * from './fns/filterFns' +export * from './fns/sortingFns' + /** * Features */ @@ -151,11 +159,3 @@ export * from './features/row-sorting/RowSorting' export * from './features/row-sorting/RowSorting.types' export * from './features/row-sorting/RowSorting.utils' export * from './features/row-sorting/createSortedRowModel' - -/** - * ProcessingFns - */ - -export * from './fns/aggregationFns' -export * from './fns/filterFns' -export * from './fns/sortingFns' diff --git a/packages/table-core/src/types/TableFeatures.ts b/packages/table-core/src/types/TableFeatures.ts index 858b1a9b73..dac87a0c44 100644 --- a/packages/table-core/src/types/TableFeatures.ts +++ b/packages/table-core/src/types/TableFeatures.ts @@ -15,7 +15,6 @@ export interface TableFeature { TValue extends CellData = CellData, >( cell: Cell, - table: Table, ) => void constructColumnAPIs?: < TFeatures extends TableFeatures, @@ -23,7 +22,6 @@ export interface TableFeature { TValue extends CellData = CellData, >( column: Column, - table: Table, ) => void constructHeaderAPIs?: < TFeatures extends TableFeatures, @@ -31,11 +29,9 @@ export interface TableFeature { TValue extends CellData = CellData, >( header: Header, - table: Table, ) => void constructRowAPIs?: ( row: Row, - table: Table, ) => void constructTableAPIs?: ( table: Table, diff --git a/packages/table-core/src/utils.ts b/packages/table-core/src/utils.ts index 9b88d8bcec..a07b03360e 100755 --- a/packages/table-core/src/utils.ts +++ b/packages/table-core/src/utils.ts @@ -123,11 +123,12 @@ const pad = (str: number | string, num: number) => { return str } -export function tableMemo, TDepArgs, TResult>( - tableMemoOptions: TableMemoOptions, -) { - const { debug, fnName, onAfterUpdate, ...memoOptions } = tableMemoOptions - +export function tableMemo, TDepArgs, TResult>({ + debug, + fnName, + onAfterUpdate, + ...memoOptions +}: TableMemoOptions) { let beforeCompareTime: number let afterCompareTime: number @@ -149,12 +150,12 @@ export function tableMemo, TDepArgs, TResult>( if (debug) { endCalcTime = performance.now() const compareTime = - Math.round((afterCompareTime - beforeCompareTime) * 100) / 100 + Math.round((afterCompareTime - beforeCompareTime) * 10000) / 10000 const executionTime = - Math.round((endCalcTime - startCalcTime) * 100) / 100 + Math.round((endCalcTime - startCalcTime) * 10000) / 10000 const totalTime = compareTime + executionTime console.info( - `%c⏱ ${pad(`${compareTime.toFixed(1)} ms + ${executionTime.toFixed(1)} ms`, 17)}`, + `%c⏱ ${pad(`${compareTime.toFixed(executionTime < 1 ? 2 : 1)} ms + ${executionTime.toFixed(executionTime < 1 ? 2 : 1)} ms`, 18)}`, `font-size: .6rem; font-weight: bold; color: hsl( ${Math.max(0, Math.min(120 - Math.log10(totalTime) * 60, 120))}deg 100% 31%);`, fnName, @@ -244,5 +245,8 @@ export function callMemoOrStaticFn< args: Array, ) { const { fnKey } = getFunctionNameInfo(staticFn) - return (obj as any)?.[fnKey](...args) ?? staticFn(obj, ...args) + return ( + ((obj as any)[fnKey] as Function | undefined)?.(...args) ?? + staticFn(obj, ...args) + ) }