diff --git a/package.json b/package.json index 812b0564fa..e66b590443 100644 --- a/package.json +++ b/package.json @@ -37,10 +37,6 @@ ] }, "size-limit": [ - { - "path": "packages/table-core/dist/cjs/index.cjs", - "limit": "20 KB" - }, { "path": "packages/table-core/dist/esm/index.js", "limit": "15 KB" diff --git a/packages/table-core/src/core/columns/Columns.ts b/packages/table-core/src/core/columns/Columns.ts index e3f70f8b1b..b19fcdccc4 100644 --- a/packages/table-core/src/core/columns/Columns.ts +++ b/packages/table-core/src/core/columns/Columns.ts @@ -1,4 +1,5 @@ import { getMemoOptions, memo } from '../../utils' +import { _table_getState } from '../table/Tables.utils' import { _createColumn } from './createColumn' import { column_getFlatColumns, @@ -32,8 +33,8 @@ export const Columns: TableFeature = { column.getLeafColumns = memo( () => [ - table.getState().columnOrder, - table.getState().grouping, + _table_getState(table).columnOrder, + _table_getState(table).grouping, table.options.columns, table.options.groupedColumnMode, ], @@ -71,8 +72,8 @@ export const Columns: TableFeature = { table.getAllLeafColumns = memo( () => [ - table.getState().columnOrder, - table.getState().grouping, + _table_getState(table).columnOrder, + _table_getState(table).grouping, table.options.columns, table.options.groupedColumnMode, ], diff --git a/packages/table-core/src/core/headers/Headers.ts b/packages/table-core/src/core/headers/Headers.ts index f1e0020923..17cede06b7 100644 --- a/packages/table-core/src/core/headers/Headers.ts +++ b/packages/table-core/src/core/headers/Headers.ts @@ -1,4 +1,5 @@ import { getMemoOptions, memo } from '../../utils' +import { _table_getState } from '../table/Tables.utils' import { _createHeader } from './createHeader' import { header_getContext, @@ -8,7 +9,6 @@ import { table_getHeaderGroups, table_getLeafHeaders, } from './Headers.utils' -import type { Header_Header } from './Headers.types' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeature, TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' @@ -34,9 +34,9 @@ export const Headers: TableFeature = { table.getHeaderGroups = memo( () => [ table.options.columns, - table.getState().columnOrder, - table.getState().grouping, - table.getState().columnPinning, + _table_getState(table).columnOrder, + _table_getState(table).grouping, + _table_getState(table).columnPinning, table.options.groupedColumnMode, ], () => table_getHeaderGroups(table), diff --git a/packages/table-core/src/core/headers/Headers.utils.ts b/packages/table-core/src/core/headers/Headers.utils.ts index 1d5db492c0..70e32261a0 100644 --- a/packages/table-core/src/core/headers/Headers.utils.ts +++ b/packages/table-core/src/core/headers/Headers.utils.ts @@ -1,5 +1,7 @@ import { table_getAllColumns } from '../columns/Columns.utils' import { table_getVisibleLeafColumns } from '../../features/column-visibility/ColumnVisibility.utils' +import { _table_getState } from '../table/Tables.utils' +import { getDefaultColumnPinningState } from '../../features/column-pinning/ColumnPinning.utils' import { buildHeaderGroups } from './buildHeaderGroups' import type { Header } from '../../types/Header' import type { RowData } from '../../types/type-utils' @@ -43,22 +45,21 @@ export function table_getHeaderGroups< TFeatures extends TableFeatures, TData extends RowData, >(table: Table) { - const { left, right } = table.getState().columnPinning + const { left, right } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() const allColumns = table_getAllColumns(table) const leafColumns = table_getVisibleLeafColumns(table) - const leftColumns = - left - ?.map((columnId) => leafColumns.find((d) => d.id === columnId)!) - .filter(Boolean) ?? [] + const leftColumns = left + .map((columnId) => leafColumns.find((d) => d.id === columnId)!) + .filter(Boolean) - const rightColumns = - right - ?.map((columnId) => leafColumns.find((d) => d.id === columnId)!) - .filter(Boolean) ?? [] + const rightColumns = right + .map((columnId) => leafColumns.find((d) => d.id === columnId)!) + .filter(Boolean) const centerColumns = leafColumns.filter( - (column) => !left?.includes(column.id) && !right?.includes(column.id), + (column) => !left.includes(column.id) && !right.includes(column.id), ) const headerGroups = buildHeaderGroups( diff --git a/packages/table-core/src/core/table/Tables.utils.ts b/packages/table-core/src/core/table/Tables.utils.ts index cc58f38c01..3045683750 100644 --- a/packages/table-core/src/core/table/Tables.utils.ts +++ b/packages/table-core/src/core/table/Tables.utils.ts @@ -40,6 +40,25 @@ export function table_setOptions< table.options = table_mergeOptions(table, newOptions) } +export function table_getInitialState< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table): TableState { + return structuredClone(table.initialState) as TableState +} + +/** + * For internal use only. Assumes any features may or may not be present. + * @param table + * @returns + */ +export function _table_getInitialState< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table): Partial> { + return table_getInitialState(table) as Partial> +} + export function table_getState< TFeatures extends TableFeatures, TData extends RowData, @@ -47,6 +66,18 @@ export function table_getState< return table.options.state as TableState } +/** + * For internal use only. Assumes any features may or may not be present. + * @param table + * @returns + */ +export function _table_getState< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table): Partial> { + return table_getState(table) as Partial> +} + export function table_setState< TFeatures extends TableFeatures, TData extends RowData, diff --git a/packages/table-core/src/core/table/createTable.ts b/packages/table-core/src/core/table/createTable.ts index 3d67350bc8..d89119d37c 100644 --- a/packages/table-core/src/core/table/createTable.ts +++ b/packages/table-core/src/core/table/createTable.ts @@ -20,7 +20,11 @@ import { RowSorting } from '../../features/row-sorting/RowSorting' import { Tables } from './Tables' import type { Table_CoreProperties } from './Tables.types' import type { RowData } from '../../types/type-utils' -import type { TableFeature, TableFeatures } from '../../types/TableFeatures' +import type { + CoreTableFeatures, + TableFeature, + TableFeatures, +} from '../../types/TableFeatures' import type { Table } from '../../types/Table' import type { TableOptions } from '../../types/TableOptions' import type { TableState } from '../../types/TableState' @@ -69,8 +73,8 @@ export function _createTable< const _features = { ...coreFeatures, ...builtInFeatures, - ...(options._features ?? {}), - } + ...options._features, + } as CoreTableFeatures & TFeatures const featuresList: Array = Object.values(_features) diff --git a/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts b/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts index 985c2e8553..aeb040f3df 100644 --- a/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts +++ b/packages/table-core/src/features/column-faceting/createFacetedRowModel.ts @@ -1,5 +1,6 @@ import { getMemoOptions, memo } from '../../utils' import { filterRows } from '../column-filtering/filterRowsUtils' +import { _table_getState } from '../../core/table/Tables.utils' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../types/RowModel' @@ -17,20 +18,21 @@ export function createFacetedRowModel< memo( () => [ table.getPreFilteredRowModel(), - table.getState().columnFilters, - table.getState().globalFilter, + _table_getState(table).columnFilters, + _table_getState(table).globalFilter, table.getFilteredRowModel(), ], (preRowModel, columnFilters, globalFilter) => { if ( !preRowModel.rows.length || - (!columnFilters.length && !globalFilter) + (!columnFilters?.length && !globalFilter) ) { return preRowModel } const filterableIds = [ - ...columnFilters.map((d) => d.id).filter((d) => d !== columnId), + ...(columnFilters?.map((d) => d.id).filter((d) => d !== columnId) ?? + []), globalFilter ? '__global__' : undefined, ].filter(Boolean) as Array diff --git a/packages/table-core/src/features/column-filtering/ColumnFiltering.ts b/packages/table-core/src/features/column-filtering/ColumnFiltering.ts index 33d4e5d38a..00a036a573 100644 --- a/packages/table-core/src/features/column-filtering/ColumnFiltering.ts +++ b/packages/table-core/src/features/column-filtering/ColumnFiltering.ts @@ -35,6 +35,13 @@ import type { * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-filtering) */ export const ColumnFiltering: TableFeature = { + _getInitialState: (state): TableState_ColumnFiltering => { + return { + columnFilters: [], + ...state, + } + }, + _getDefaultColumnDef: < TFeatures extends TableFeatures, TData extends RowData, @@ -44,13 +51,6 @@ export const ColumnFiltering: TableFeature = { } }, - _getInitialState: (state): TableState_ColumnFiltering => { - return { - columnFilters: [], - ...state, - } - }, - _getDefaultOptions: ( table: Table & Partial>, 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 06b2fb650d..1fe688c26a 100644 --- a/packages/table-core/src/features/column-filtering/ColumnFiltering.utils.ts +++ b/packages/table-core/src/features/column-filtering/ColumnFiltering.utils.ts @@ -1,7 +1,10 @@ import { filterFns } from '../../fns/filterFns' import { functionalUpdate, isFunction } from '../../utils' import { row_getValue } from '../../core/rows/Rows.utils' -import { table_getCoreRowModel } from '../../core/table/Tables.utils' +import { + _table_getState, + table_getCoreRowModel, +} from '../../core/table/Tables.utils' import type { BuiltInFilterFn } from '../../fns/filterFns' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' @@ -81,15 +84,23 @@ export function column_getFilterValue< TData extends RowData, TValue extends CellData = CellData, >(column: Column, table: Table) { - return table.getState().columnFilters.find((d) => d.id === column.id)?.value + return _table_getState(table).columnFilters?.find((d) => d.id === column.id) + ?.value } export function column_getFilterIndex< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { - return table.getState().columnFilters.findIndex((d) => d.id === column.id) +>( + column: Column, + table: Table, +): number { + return ( + _table_getState(table).columnFilters?.findIndex( + (d) => d.id === column.id, + ) ?? -1 + ) } export function column_setFilterValue< diff --git a/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts b/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts index 79972b771b..d06f21dd0d 100644 --- a/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts +++ b/packages/table-core/src/features/column-filtering/createFilteredRowModel.ts @@ -1,6 +1,7 @@ import { getMemoOptions, memo } from '../../utils' import { table_getColumn } from '../../core/columns/Columns.utils' import { table_getGlobalFilterFn } from '../global-filtering/GlobalFiltering.utils' +import { _table_getState } from '../../core/table/Tables.utils' import { filterRows } from './filterRowsUtils' import { column_getFilterFn } from './ColumnFiltering.utils' import type { RowData } from '../../types/type-utils' @@ -18,11 +19,14 @@ export function createFilteredRowModel< memo( () => [ table.getPreFilteredRowModel(), - table.getState().columnFilters, - table.getState().globalFilter, + _table_getState(table).columnFilters, + _table_getState(table).globalFilter, ], (rowModel, columnFilters, globalFilter) => { - if (!rowModel.rows.length || (!columnFilters.length && !globalFilter)) { + if ( + !rowModel.rows.length || + (!columnFilters?.length && !globalFilter) + ) { for (const row of rowModel.flatRows) { row.columnFilters = {} row.columnFiltersMeta = {} @@ -37,7 +41,7 @@ export function createFilteredRowModel< ResolvedColumnFilter > = [] - columnFilters.forEach((d) => { + columnFilters?.forEach((d) => { const column = table_getColumn(table, d.id) if (!column) { @@ -62,7 +66,7 @@ export function createFilteredRowModel< }) }) - const filterableIds = columnFilters.map((d) => d.id) + const filterableIds = columnFilters?.map((d) => d.id) const globalFilterFn = table_getGlobalFilterFn(table) @@ -75,7 +79,7 @@ export function createFilteredRowModel< globalFilterFn && globallyFilterableColumns.length ) { - filterableIds.push('__global__') + filterableIds?.push('__global__') globallyFilterableColumns.forEach((column) => { resolvedGlobalFilters.push({ @@ -135,7 +139,7 @@ export function createFilteredRowModel< const filterRowsImpl = (row: Row) => { // Horizontally filter rows through each column - for (const columnId of filterableIds) { + for (const columnId of filterableIds ?? []) { if (row.columnFilters[columnId] === false) { return false } diff --git a/packages/table-core/src/features/column-grouping/ColumnGrouping.ts b/packages/table-core/src/features/column-grouping/ColumnGrouping.ts index 4af68b133f..4607ff91cc 100644 --- a/packages/table-core/src/features/column-grouping/ColumnGrouping.ts +++ b/packages/table-core/src/features/column-grouping/ColumnGrouping.ts @@ -39,6 +39,13 @@ import type { * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-grouping) */ export const ColumnGrouping: TableFeature = { + _getInitialState: (state): TableState_ColumnGrouping => { + return { + grouping: [], + ...state, + } + }, + _getDefaultColumnDef: < TFeatures extends TableFeatures, TData extends RowData, @@ -50,13 +57,6 @@ export const ColumnGrouping: TableFeature = { } }, - _getInitialState: (state): TableState_ColumnGrouping => { - return { - grouping: [], - ...state, - } - }, - _getDefaultOptions: ( table: Table & Partial>, 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 9718b03908..f9c513a4a3 100644 --- a/packages/table-core/src/features/column-grouping/ColumnGrouping.utils.ts +++ b/packages/table-core/src/features/column-grouping/ColumnGrouping.utils.ts @@ -1,6 +1,7 @@ import { aggregationFns } from '../../fns/aggregationFns' import { isFunction } from '../../utils' import { table_getFilteredRowModel } from '../column-filtering/ColumnFiltering.utils' +import { _table_getState } from '../../core/table/Tables.utils' import type { BuiltInAggregationFn } from '../../fns/aggregationFns' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' @@ -42,16 +43,22 @@ export function column_getIsGrouped< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { - return table.getState().grouping.includes(column.id) +>( + column: Column, + table: Table, +): boolean { + return !!_table_getState(table).grouping?.includes(column.id) } export function column_getGroupedIndex< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { - return table.getState().grouping.indexOf(column.id) +>( + column: Column, + table: Table, +): number { + return _table_getState(table).grouping?.indexOf(column.id) ?? -1 } export function column_getToggleGroupingHandler< diff --git a/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts b/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts index 054537c8fb..b587bcc6a5 100644 --- a/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts +++ b/packages/table-core/src/features/column-grouping/createGroupedRowModel.ts @@ -1,6 +1,7 @@ import { _createRow } from '../../core/rows/createRow' import { flattenBy, getMemoOptions, memo } from '../../utils' import { table_getColumn } from '../../core/columns/Columns.utils' +import { _table_getState } from '../../core/table/Tables.utils' import { row_getGroupingValue } from './ColumnGrouping.utils' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' @@ -14,9 +15,9 @@ export function createGroupedRowModel< >(): (table: Table) => () => RowModel { return (table) => memo( - () => [table.getState().grouping, table.getPreGroupedRowModel()], + () => [_table_getState(table).grouping, table.getPreGroupedRowModel()], (grouping, rowModel) => { - if (!rowModel.rows.length || !grouping.length) { + if (!rowModel.rows.length || !grouping?.length) { rowModel.rows.forEach((row) => { row.depth = 0 row.parentId = undefined diff --git a/packages/table-core/src/features/column-ordering/ColumnOrdering.ts b/packages/table-core/src/features/column-ordering/ColumnOrdering.ts index ecc89af161..6263e1f455 100644 --- a/packages/table-core/src/features/column-ordering/ColumnOrdering.ts +++ b/packages/table-core/src/features/column-ordering/ColumnOrdering.ts @@ -1,5 +1,6 @@ import { getMemoOptions, makeStateUpdater, memo } from '../../utils' import { column_getVisibleLeafColumns } from '../column-visibility/ColumnVisibility.utils' +import { _table_getState } from '../../core/table/Tables.utils' import { column_getIndex, column_getIsFirstColumn, @@ -52,9 +53,9 @@ export const ColumnOrdering: TableFeature = { column.getIndex = memo( (position) => [ position, - table.getState().columnOrder, - table.getState().columnPinning, - table.getState().grouping, + _table_getState(table).columnOrder, + _table_getState(table).columnPinning, + _table_getState(table).grouping, ], (position) => column_getIndex(column, table, position), getMemoOptions(table.options, 'debugColumns', 'getIndex'), diff --git a/packages/table-core/src/features/column-pinning/ColumnPinning.ts b/packages/table-core/src/features/column-pinning/ColumnPinning.ts index b5cb7c756b..ad0107133b 100644 --- a/packages/table-core/src/features/column-pinning/ColumnPinning.ts +++ b/packages/table-core/src/features/column-pinning/ColumnPinning.ts @@ -1,5 +1,6 @@ import { buildHeaderGroups } from '../../core/headers/buildHeaderGroups' import { getMemoOptions, makeStateUpdater, memo } from '../../utils' +import { _table_getState } from '../../core/table/Tables.utils' import { column_getCanPin, column_getIsPinned, @@ -78,17 +79,23 @@ export const ColumnPinning: TableFeature = { Partial>, ): void => { row.getCenterVisibleCells = memo( - () => [row._getAllVisibleCells(), table.getState().columnPinning], + () => [row._getAllVisibleCells(), _table_getState(table).columnPinning], () => row_getCenterVisibleCells(row, table), getMemoOptions(table.options, 'debugRows', 'getCenterVisibleCells'), ) row.getLeftVisibleCells = memo( - () => [row._getAllVisibleCells(), table.getState().columnPinning.left], + () => [ + row._getAllVisibleCells(), + _table_getState(table).columnPinning?.left, + ], () => row_getLeftVisibleCells(row, table), getMemoOptions(table.options, 'debugRows', 'getLeftVisibleCells'), ) row.getRightVisibleCells = memo( - () => [row._getAllVisibleCells(), table.getState().columnPinning.right], + () => [ + row._getAllVisibleCells(), + _table_getState(table).columnPinning?.right, + ], () => row_getRightVisibleCells(row, table), getMemoOptions(table.options, 'debugRows', 'getRightVisibleCells'), ) @@ -115,7 +122,7 @@ export const ColumnPinning: TableFeature = { () => [ table.getAllColumns(), table.getVisibleLeafColumns(), - table.getState().columnPinning.left, + _table_getState(table).columnPinning?.left, ], (allColumns, leafColumns, left) => { const orderedLeafColumns = @@ -132,7 +139,7 @@ export const ColumnPinning: TableFeature = { () => [ table.getAllColumns(), table.getVisibleLeafColumns(), - table.getState().columnPinning.right, + _table_getState(table).columnPinning?.right, ], (allColumns, leafColumns, right) => { const orderedLeafColumns = @@ -149,8 +156,8 @@ export const ColumnPinning: TableFeature = { () => [ table.getAllColumns(), table.getVisibleLeafColumns(), - table.getState().columnPinning.left, - table.getState().columnPinning.right, + _table_getState(table).columnPinning?.left, + _table_getState(table).columnPinning?.right, ], (allColumns, leafColumns, left, right) => { leafColumns = leafColumns.filter( diff --git a/packages/table-core/src/features/column-pinning/ColumnPinning.types.ts b/packages/table-core/src/features/column-pinning/ColumnPinning.types.ts index b758d60c67..af9afa5b3a 100644 --- a/packages/table-core/src/features/column-pinning/ColumnPinning.types.ts +++ b/packages/table-core/src/features/column-pinning/ColumnPinning.types.ts @@ -8,8 +8,8 @@ import type { Column } from '../../types/Column' export type ColumnPinningPosition = false | 'left' | 'right' export interface ColumnPinningState { - left?: Array - right?: Array + left: Array + right: Array } export interface TableState_ColumnPinning { 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 afd356854b..6ff21ce18b 100644 --- a/packages/table-core/src/features/column-pinning/ColumnPinning.utils.ts +++ b/packages/table-core/src/features/column-pinning/ColumnPinning.utils.ts @@ -3,6 +3,7 @@ import { row_getAllVisibleCells, } from '../column-visibility/ColumnVisibility.utils' import { table_getAllColumns } from '../../core/columns/Columns.utils' +import { _table_getState } from '../../core/table/Tables.utils' import type { Row } from '../../types/Row' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' @@ -31,9 +32,9 @@ export function column_pin< table_setColumnPinning(table, (old) => { if (position === 'right') { return { - left: (old.left ?? []).filter((d) => !columnIds.includes(d)), + left: old.left.filter((d) => !columnIds.includes(d)), right: [ - ...(old.right ?? []).filter((d) => !columnIds.includes(d)), + ...old.right.filter((d) => !columnIds.includes(d)), ...columnIds, ], } @@ -41,17 +42,14 @@ export function column_pin< if (position === 'left') { return { - left: [ - ...(old.left ?? []).filter((d) => !columnIds.includes(d)), - ...columnIds, - ], - right: (old.right ?? []).filter((d) => !columnIds.includes(d)), + left: [...old.left.filter((d) => !columnIds.includes(d)), ...columnIds], + right: old.right.filter((d) => !columnIds.includes(d)), } } return { - left: (old.left ?? []).filter((d) => !columnIds.includes(d)), - right: (old.right ?? []).filter((d) => !columnIds.includes(d)), + left: old.left.filter((d) => !columnIds.includes(d)), + right: old.right.filter((d) => !columnIds.includes(d)), } }) } @@ -82,10 +80,11 @@ export function column_getIsPinned< ): ColumnPinningPosition | false { const leafColumnIds = column.getLeafColumns().map((d) => d.id) - const { left, right } = table.getState().columnPinning + const { left, right } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() - const isLeft = leafColumnIds.some((d) => left?.includes(d)) - const isRight = leafColumnIds.some((d) => right?.includes(d)) + const isLeft = leafColumnIds.some((d) => left.includes(d)) + const isRight = leafColumnIds.some((d) => right.includes(d)) return isLeft ? 'left' : isRight ? 'right' : false } @@ -98,7 +97,7 @@ export function column_getPinnedIndex< const position = column_getIsPinned(column, table) return position - ? table.getState().columnPinning[position]?.indexOf(column.id) ?? -1 + ? _table_getState(table).columnPinning?.[position].indexOf(column.id) ?? -1 : 0 } @@ -106,9 +105,10 @@ export function row_getCenterVisibleCells< TFeatures extends TableFeatures, TData extends RowData, >(row: Row, table: Table) { - const allCells = row._getAllVisibleCells() - const { left, right } = table.getState().columnPinning - const leftAndRight: Array = [...(left ?? []), ...(right ?? [])] + const allCells = row_getAllVisibleCells(row, table) + const { left, right } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() + const leftAndRight: Array = [...left, ...right] return allCells.filter((d) => !leftAndRight.includes(d.column.id)) } @@ -117,8 +117,9 @@ export function row_getLeftVisibleCells< TData extends RowData, >(row: Row, table: Table) { const allCells = row_getAllVisibleCells(row, table) - const { left } = table.getState().columnPinning - const cells = (left ?? []) + const { left } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() + const cells = left .map((columnId) => allCells.find((cell) => cell.column.id === columnId)!) .filter(Boolean) .map((d) => ({ ...d, position: 'left' }) as Cell) @@ -131,8 +132,9 @@ export function row_getRightVisibleCells< TData extends RowData, >(row: Row, table: Table) { const allCells = row_getAllVisibleCells(row, table) - const { right } = table.getState().columnPinning - const cells = (right ?? []) + const { right } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() + const cells = right .map((columnId) => allCells.find((cell) => cell.column.id === columnId)!) .filter(Boolean) .map( @@ -165,20 +167,21 @@ export function table_getIsSomeColumnsPinned< TFeatures extends TableFeatures, TData extends RowData, >(table: Table, position?: ColumnPinningPosition) { - const pinningState = table.getState().columnPinning + const pinningState = _table_getState(table).columnPinning if (!position) { - return Boolean(pinningState.left?.length || pinningState.right?.length) + return Boolean(pinningState?.left.length || pinningState?.right.length) } - return Boolean(pinningState[position]?.length) + return Boolean(pinningState?.[position].length) } export function table_getLeftLeafColumns< TFeatures extends TableFeatures, TData extends RowData, >(table: Table) { - const { left } = table.getState().columnPinning - return (left ?? []) + const { left } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() + return left .map( (columnId) => table_getAllColumns(table).find((column) => column.id === columnId)!, @@ -190,8 +193,9 @@ export function table_getRightLeafColumns< TFeatures extends TableFeatures, TData extends RowData, >(table: Table) { - const { right } = table.getState().columnPinning - return (right ?? []) + const { right } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() + return right .map( (columnId) => table_getAllColumns(table).find((column) => column.id === columnId)!, @@ -203,8 +207,9 @@ export function table_getCenterLeafColumns< TFeatures extends TableFeatures, TData extends RowData, >(table: Table) { - const { left, right } = table.getState().columnPinning - const leftAndRight: Array = [...(left ?? []), ...(right ?? [])] + const { left, right } = + _table_getState(table).columnPinning ?? getDefaultColumnPinningState() + const leftAndRight: Array = [...left, ...right] return table_getAllColumns(table).filter((d) => !leftAndRight.includes(d.id)) } 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 c865635768..1ea1a1624b 100644 --- a/packages/table-core/src/features/column-resizing/ColumnResizing.utils.ts +++ b/packages/table-core/src/features/column-resizing/ColumnResizing.utils.ts @@ -1,5 +1,6 @@ import { table_setColumnSizing } from '../column-sizing/ColumnSizing.utils' import { table_getColumn } from '../../core/columns/Columns.utils' +import { _table_getState } from '../../core/table/Tables.utils' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' @@ -35,7 +36,7 @@ export function column_getIsResizing< TData extends RowData, TValue extends CellData = CellData, >(column: Column, table: Table) { - return table.getState().columnSizingInfo.isResizingColumn === column.id + return _table_getState(table).columnSizingInfo?.isResizingColumn === column.id } export function header_getResizeHandler< diff --git a/packages/table-core/src/features/column-sizing/ColumnSizing.ts b/packages/table-core/src/features/column-sizing/ColumnSizing.ts index 3b2f451b37..a247b5ee21 100644 --- a/packages/table-core/src/features/column-sizing/ColumnSizing.ts +++ b/packages/table-core/src/features/column-sizing/ColumnSizing.ts @@ -1,11 +1,12 @@ import { getMemoOptions, makeStateUpdater, memo } from '../../utils' import { column_getVisibleLeafColumns } from '../column-visibility/ColumnVisibility.utils' +import { _table_getState } from '../../core/table/Tables.utils' import { column_getAfter, column_getSize, column_getStart, column_resetSize, - defaultColumnSizing, + getDefaultColumnSizingState, header_getSize, header_getStart, table_getCenterTotalSize, @@ -37,9 +38,6 @@ import type { Column } from '../../types/Column' * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing) */ export const ColumnSizing: TableFeature = { - _getDefaultColumnDef: (): ColumnDef_ColumnSizing => { - return defaultColumnSizing - }, _getInitialState: (state): TableState_ColumnSizing => { return { columnSizing: {}, @@ -47,6 +45,10 @@ export const ColumnSizing: TableFeature = { } }, + _getDefaultColumnDef: (): ColumnDef_ColumnSizing => { + return getDefaultColumnSizingState() + }, + _getDefaultOptions: ( table: Table & Partial, ): ColumnSizingDefaultOptions => { @@ -69,7 +71,7 @@ export const ColumnSizing: TableFeature = { (position) => [ position, column_getVisibleLeafColumns(table, position), - table.getState().columnSizing, + _table_getState(table).columnSizing, ], (position, columns) => column_getStart(columns, column, table, position), getMemoOptions(table.options, 'debugColumns', 'getStart'), @@ -79,7 +81,7 @@ export const ColumnSizing: TableFeature = { (position) => [ position, column_getVisibleLeafColumns(table, position), - table.getState().columnSizing, + _table_getState(table).columnSizing, ], (position, columns) => column_getAfter(columns, column, table, position), getMemoOptions(table.options, 'debugColumns', 'getAfter'), 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 d7374321ba..7fb9ccdbf8 100644 --- a/packages/table-core/src/features/column-sizing/ColumnSizing.utils.ts +++ b/packages/table-core/src/features/column-sizing/ColumnSizing.utils.ts @@ -1,3 +1,4 @@ +import { _table_getState } from '../../core/table/Tables.utils' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' @@ -5,6 +6,14 @@ import type { Header } from '../../types/Header' import type { Column } from '../../types/Column' import type { ColumnSizingState } from './ColumnSizing.types' +export function getDefaultColumnSizingState() { + return structuredClone({ + size: 150, + minSize: 20, + maxSize: Number.MAX_SAFE_INTEGER, + }) +} + export function column_getSize< TFeatures extends TableFeatures, TData extends RowData, @@ -13,14 +22,15 @@ export function column_getSize< column: Column, table: Table, ): number { - const columnSize = table.getState().columnSizing[column.id] + const defaultSizes = getDefaultColumnSizingState() + const columnSize = _table_getState(table).columnSizing?.[column.id] return Math.min( Math.max( - column.columnDef.minSize ?? defaultColumnSizing.minSize, - columnSize ?? column.columnDef.size ?? defaultColumnSizing.size, + column.columnDef.minSize ?? defaultSizes.minSize, + columnSize ?? column.columnDef.size ?? defaultSizes.size, ), - column.columnDef.maxSize ?? defaultColumnSizing.maxSize, + column.columnDef.maxSize ?? defaultSizes.maxSize, ) } @@ -159,9 +169,3 @@ export function table_getRightTotalSize< }, 0) ?? 0 ) } - -export const defaultColumnSizing = { - size: 150, - minSize: 20, - maxSize: Number.MAX_SAFE_INTEGER, -} diff --git a/packages/table-core/src/features/column-visibility/ColumnVisibility.ts b/packages/table-core/src/features/column-visibility/ColumnVisibility.ts index 1b4bdaec15..b223db6bfd 100644 --- a/packages/table-core/src/features/column-visibility/ColumnVisibility.ts +++ b/packages/table-core/src/features/column-visibility/ColumnVisibility.ts @@ -1,4 +1,5 @@ import { getMemoOptions, makeStateUpdater, memo } from '../../utils' +import { _table_getState } from '../../core/table/Tables.utils' import { column_getCanHide, column_getIsVisible, @@ -74,7 +75,7 @@ export const ColumnVisibility: TableFeature = { Partial>, ): void => { row._getAllVisibleCells = memo( - () => [row.getAllCells(), table.getState().columnVisibility], + () => [row.getAllCells(), _table_getState(table).columnVisibility], () => row_getAllVisibleCells(row, table), getMemoOptions(table.options, 'debugRows', '_getAllVisibleCells'), ) @@ -95,13 +96,13 @@ export const ColumnVisibility: TableFeature = { Partial>, ): void => { table.getVisibleFlatColumns = memo( - () => [table.getState().columnVisibility, table.options.columns], + () => [_table_getState(table).columnVisibility, table.options.columns], () => table_getVisibleFlatColumns(table), getMemoOptions(table.options, 'debugColumns', 'getVisibleFlatColumns'), ) table.getVisibleLeafColumns = memo( - () => [table.getState().columnVisibility, table.options.columns], + () => [_table_getState(table).columnVisibility, table.options.columns], () => table_getVisibleFlatColumns(table), getMemoOptions(table.options, 'debugColumns', 'getVisibleLeafColumns'), ) 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 50d1ee3245..3dea53ccba 100644 --- a/packages/table-core/src/features/column-visibility/ColumnVisibility.utils.ts +++ b/packages/table-core/src/features/column-visibility/ColumnVisibility.utils.ts @@ -3,13 +3,21 @@ import { table_getAllLeafColumns, } from '../../core/columns/Columns.utils' import { row_getAllCells } from '../../core/rows/Rows.utils' +import { + _table_getInitialState, + _table_getState, +} from '../../core/table/Tables.utils' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' import type { Cell } from '../../types/Cell' import type { Column } from '../../types/Column' import type { ColumnPinningPosition } from '../column-pinning/ColumnPinning.types' -import type { ColumnVisibilityState } from './ColumnVisibility.types' +import type { + ColumnDef_ColumnVisibility, + ColumnVisibilityState, + TableOptions_ColumnVisibility, +} from './ColumnVisibility.types' import type { Row } from '../../types/Row' export function column_toggleVisibility< @@ -17,8 +25,12 @@ export function column_toggleVisibility< TData extends RowData, TValue extends CellData = CellData, >( - column: Column, - table: Table, + column: Column & { + columnDef: Partial + }, + table: Table & { + options: Partial + }, visible?: boolean, ): void { if (column_getCanHide(column, table)) { @@ -33,12 +45,19 @@ export function column_getIsVisible< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial + }, + table: Table & { + options: Partial + }, +): boolean { const childColumns = column.columns return ( (childColumns.length - ? childColumns.some((c) => c.getIsVisible()) - : table.getState().columnVisibility[column.id]) ?? true + ? childColumns.some((c) => column_getIsVisible(c, table)) + : _table_getState(table).columnVisibility?.[column.id]) ?? true ) } @@ -46,7 +65,14 @@ export function column_getCanHide< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial + }, + table: Table & { + options: Partial + }, +) { return ( (column.columnDef.enableHiding ?? true) && (table.options.enableHiding ?? true) @@ -57,7 +83,14 @@ export function column_getToggleVisibilityHandler< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial + }, + table: Table & { + options: Partial + }, +) { return (e: unknown) => { column_toggleVisibility( column, @@ -70,7 +103,12 @@ export function column_getToggleVisibilityHandler< export function column_getVisibleLeafColumns< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, position?: ColumnPinningPosition | 'center') { +>( + table: Table & { + options: Partial + }, + position?: ColumnPinningPosition | 'center', +) { return !position ? table.getVisibleLeafColumns() : position === 'center' @@ -83,7 +121,12 @@ export function column_getVisibleLeafColumns< export function row_getAllVisibleCells< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>( + row: Row, + table: Table & { + options: Partial + }, +) { return row_getAllCells(row, table).filter((cell) => column_getIsVisible(cell.column, table), ) @@ -103,7 +146,11 @@ export function row_getVisibleCells< export function table_getVisibleFlatColumns< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return table_getAllFlatColumns(table).filter((column) => column_getIsVisible(column, table), ) @@ -112,7 +159,11 @@ export function table_getVisibleFlatColumns< export function table_getVisibleLeafColumns< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return table_getAllLeafColumns(table).filter((column) => column_getIsVisible(column, table), ) @@ -121,24 +172,39 @@ export function table_getVisibleLeafColumns< export function table_setColumnVisibility< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: Updater) { +>( + table: Table & { + options: Partial + }, + updater: Updater, +) { table.options.onColumnVisibilityChange?.(updater) } export function table_resetColumnVisibility< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean) { +>( + table: Table & { + options: Partial + }, + defaultState?: boolean, +) { table_setColumnVisibility( table, - defaultState ? {} : table.initialState.columnVisibility ?? {}, + defaultState ? {} : _table_getInitialState(table).columnVisibility ?? {}, ) } export function table_toggleAllColumnsVisible< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, value?: boolean) { +>( + table: Table & { + options: Partial + }, + value?: boolean, +) { value = value ?? !table_getIsAllColumnsVisible(table) table_setColumnVisibility( @@ -146,7 +212,7 @@ export function table_toggleAllColumnsVisible< table.getAllLeafColumns().reduce( (obj, column) => ({ ...obj, - [column.id]: !value ? !column.getCanHide() : value, + [column.id]: !value ? !column_getCanHide(column, table) : value, }), {}, ), @@ -156,21 +222,37 @@ export function table_toggleAllColumnsVisible< export function table_getIsAllColumnsVisible< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - return !table.getAllLeafColumns().some((column) => !column.getIsVisible()) +>( + table: Table & { + options: Partial + }, +) { + return !table + .getAllLeafColumns() + .some((column) => !column_getIsVisible(column, table)) } export function table_getIsSomeColumnsVisible< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - return table.getAllLeafColumns().some((column) => column.getIsVisible()) +>( + table: Table & { + options: Partial + }, +) { + return table + .getAllLeafColumns() + .some((column) => column_getIsVisible(column, table)) } export function table_getToggleAllColumnsVisibilityHandler< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return (e: unknown) => { table_toggleAllColumnsVisible( table, 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 203a600d31..5fb4ce50f3 100644 --- a/packages/table-core/src/features/global-faceting/GlobalFaceting.utils.ts +++ b/packages/table-core/src/features/global-faceting/GlobalFaceting.utils.ts @@ -4,6 +4,11 @@ import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../types/RowModel' import type { Table } from '../../types/Table' +/** + * + * @param table + * @returns + */ export function table_getGlobalFacetedMinMaxValues< TFeatures extends TableFeatures, TData extends RowData, @@ -14,6 +19,11 @@ export function table_getGlobalFacetedMinMaxValues< ) } +/** + * + * @param table + * @returns + */ export function table_getGlobalFacetedRowModel< TFeatures extends TableFeatures, TData extends RowData, @@ -24,6 +34,11 @@ 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.utils.ts b/packages/table-core/src/features/global-filtering/GlobalFiltering.utils.ts index 26b4ebc8c4..0843efa4c7 100644 --- a/packages/table-core/src/features/global-filtering/GlobalFiltering.utils.ts +++ b/packages/table-core/src/features/global-filtering/GlobalFiltering.utils.ts @@ -1,17 +1,44 @@ import { filterFns } from '../../fns/filterFns' import { isFunction } from '../../utils' -import type { FilterFn } from '../column-filtering/ColumnFiltering.types' +import { _table_getInitialState } from '../../core/table/Tables.utils' +import type { + ColumnDef_GlobalFiltering, + TableOptions_GlobalFiltering, +} from './GlobalFiltering.types' +import type { + ColumnDef_ColumnFiltering, + FilterFn, + TableOptions_ColumnFiltering, +} from '../column-filtering/ColumnFiltering.types' import type { CellData, RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table } 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, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial< + ColumnDef_GlobalFiltering & ColumnDef_ColumnFiltering + > + }, + table: Table & { + options: Partial< + TableOptions_GlobalFiltering & + TableOptions_ColumnFiltering + > + }, +): boolean { return ( (column.columnDef.enableGlobalFilter ?? true) && (table.options.enableGlobalFilter ?? true) && @@ -25,11 +52,21 @@ export function table_getGlobalAutoFilterFn() { return filterFns.includesString } +/** + * + * @param table + * @returns + */ export function table_getGlobalFilterFn< TFeatures extends TableFeatures, TData extends RowData, >( - table: Table, + table: Table & { + options: Partial< + TableOptions_GlobalFiltering & + TableOptions_ColumnFiltering + > + }, ): FilterFn | FilterFn | undefined { const { globalFilterFn: globalFilterFn } = table.options @@ -41,19 +78,42 @@ export function table_getGlobalFilterFn< filterFns[globalFilterFn as BuiltInFilterFn] } +/** + * + * @param table + * @param updater + */ export function table_setGlobalFilter< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: any) { +>( + table: Table & { + options: Partial> + }, + updater: any, +) { table.options.onGlobalFilterChange?.(updater) } +/** + * + * @param table + * @param defaultState + */ export function table_resetGlobalFilter< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean) { +>( + table: Table & { + options: Partial< + TableOptions_GlobalFiltering & + TableOptions_ColumnFiltering + > + }, + defaultState?: boolean, +) { table_setGlobalFilter( table, - defaultState ? undefined : table.initialState.globalFilter, + defaultState ? undefined : _table_getInitialState(table).globalFilter, ) } 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 916b91e380..f78409d488 100644 --- a/packages/table-core/src/features/row-expanding/RowExpanding.utils.ts +++ b/packages/table-core/src/features/row-expanding/RowExpanding.utils.ts @@ -1,18 +1,40 @@ import { table_getPrePaginationRowModel } from '../row-pagination/RowPagination.utils' import { table_getSortedRowModel } from '../row-sorting/RowSorting.utils' import { table_getRow } from '../../core/rows/Rows.utils' -import { table_getRowModel } from '../../core/table/Tables.utils' +import { + _table_getInitialState, + _table_getState, + table_getInitialState, + table_getRowModel, +} from '../../core/table/Tables.utils' import type { RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../types/RowModel' import type { Table } from '../../types/Table' import type { Row } from '../../types/Row' -import type { ExpandedState, ExpandedStateList } from './RowExpanding.types' - +import type { + ExpandedState, + ExpandedStateList, + TableOptions_RowExpanding, +} from './RowExpanding.types' + +/** + * + * @param table + * @param registered + * @param queued + * @returns + */ export function table_autoResetExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, registered: boolean, queued: boolean) { +>( + table: Table & { + options: Partial> + }, + registered: boolean, + queued: boolean, +) { if (!registered) { table._queue(() => { registered = true @@ -34,17 +56,37 @@ export function table_autoResetExpanded< } } +/** + * + * @param table + * @param updater + */ export function table_setExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: Updater) { +>( + table: Table & { + options: Partial> + }, + updater: Updater, +) { table.options.onExpandedChange?.(updater) } +/** + * + * @param table + * @param expanded + */ export function table_toggleAllRowsExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, expanded?: boolean) { +>( + table: Table & { + options: Partial> + }, + expanded?: boolean, +) { if (expanded ?? !table_getIsAllRowsExpanded(table)) { table_setExpanded(table, true) } else { @@ -52,45 +94,94 @@ export function table_toggleAllRowsExpanded< } } +/** + * + * @param table + * @param defaultState + */ export function table_resetExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean) { - table_setExpanded(table, defaultState ? {} : table.initialState.expanded!) +>( + table: Table & { + options: Partial> + }, + defaultState?: boolean, +) { + table_setExpanded( + table, + defaultState ? {} : _table_getInitialState(table).expanded ?? {}, + ) } +/** + * + * @param table + * @returns + */ export function table_getCanSomeRowsExpand< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { return table_getPrePaginationRowModel(table).flatRows.some((row) => row_getCanExpand(row, table), ) } +/** + * + * @param table + * @returns + */ export function table_getToggleAllRowsExpandedHandler< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { return (e: unknown) => { ;(e as any).persist?.() table_toggleAllRowsExpanded(table) } } +/** + * + * @param table + * @returns + */ export function table_getIsSomeRowsExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - const expanded = table.getState().expanded +>( + table: Table & { + options: Partial> + }, +) { + const expanded = _table_getState(table).expanded ?? {} return expanded === true || Object.values(expanded).some(Boolean) } +/** + * + * @param table + * @returns + */ export function table_getIsAllRowsExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - const expanded = table.getState().expanded +>( + table: Table & { + options: Partial> + }, +) { + const expanded = _table_getState(table).expanded ?? {} // If expanded is true, save some cycles and return true if (expanded === true) { @@ -114,16 +205,25 @@ export function table_getIsAllRowsExpanded< return true } +/** + * + * @param table + * @returns + */ export function table_getExpandedDepth< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { let maxDepth = 0 const rowIds = - table.getState().expanded === true + _table_getState(table).expanded === true ? Object.keys(table_getRowModel(table).rowsById) - : Object.keys(table.getState().expanded) + : Object.keys(_table_getState(table).expanded ?? {}) rowIds.forEach((id) => { const splitId = id.split('.') @@ -133,17 +233,35 @@ export function table_getExpandedDepth< return maxDepth } +/** + * + * @param table + * @returns + */ export function table_getPreExpandedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): RowModel { +>( + table: Table & { + options: Partial> + }, +): RowModel { return table_getSortedRowModel(table) } +/** + * + * @param table + * @returns + */ export function table_getExpandedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): RowModel { +>( + table: Table & { + options: Partial> + }, +): RowModel { if (!table._rowModels.Expanded) { table._rowModels.Expanded = table.options._rowModels?.Expanded?.(table) } @@ -155,12 +273,20 @@ export function table_getExpandedRowModel< return table._rowModels.Expanded() } +/** + * + * @param row + * @param table + * @param expanded + */ export function row_toggleExpanded< TFeatures extends TableFeatures, TData extends RowData, >( row: Row, - table: Table, + table: Table & { + options: Partial> + }, expanded?: boolean, ) { table_setExpanded(table, (old) => { @@ -194,11 +320,22 @@ export function row_toggleExpanded< }) } +/** + * + * @param row + * @param table + * @returns + */ export function row_getIsExpanded< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { - const expanded = table.getState().expanded +>( + row: Row, + table: Table & { + options: Partial> + }, +) { + const expanded = _table_getState(table).expanded ?? {} return !!( table.options.getIsRowExpanded?.(row) ?? @@ -206,20 +343,42 @@ export function row_getIsExpanded< ) } +/** + * + * @param row + * @param table + * @returns + */ export function row_getCanExpand< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>( + row: Row, + table: Table & { + options: Partial> + }, +) { return ( table.options.getRowCanExpand?.(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) { +>( + row: Row, + table: Table & { + options: Partial> + }, +) { let isFullyExpanded = true let currentRow = row @@ -231,10 +390,21 @@ export function row_getIsAllParentsExpanded< return isFullyExpanded } +/** + * + * @param row + * @param table + * @returns + */ export function row_getToggleExpandedHandler< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>( + row: Row, + table: Table & { + options: Partial> + }, +) { const canExpand = row_getCanExpand(row, table) return () => { diff --git a/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts b/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts index 32eaa8fcbd..29bca3a6e3 100644 --- a/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts +++ b/packages/table-core/src/features/row-expanding/createExpandedRowModel.ts @@ -1,4 +1,5 @@ import { getMemoOptions, memo } from '../../utils' +import { _table_getState } from '../../core/table/Tables.utils' import { row_getIsExpanded } from './RowExpanding.utils' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' @@ -13,14 +14,14 @@ export function createExpandedRowModel< return (table) => memo( () => [ - table.getState().expanded, + _table_getState(table).expanded, table.getPreExpandedRowModel(), table.options.paginateExpandedRows, ], (expanded, rowModel, paginateExpandedRows) => { if ( !rowModel.rows.length || - (expanded !== true && !Object.keys(expanded).length) + (expanded !== true && !Object.keys(expanded ?? {}).length) ) { return rowModel } diff --git a/packages/table-core/src/features/row-pagination/RowPagination.ts b/packages/table-core/src/features/row-pagination/RowPagination.ts index d54ea80879..5a8c1530e3 100644 --- a/packages/table-core/src/features/row-pagination/RowPagination.ts +++ b/packages/table-core/src/features/row-pagination/RowPagination.ts @@ -20,6 +20,7 @@ import { table_setPageSize, table_setPagination, } from './RowPagination.utils' +import type { TableState } from '../../types/TableState' import type { PaginationDefaultOptions, TableState_RowPagination, @@ -80,8 +81,8 @@ export const RowPagination: TableFeature = { table.setPageSize = (updater) => table_setPageSize(table, updater) table.getPageOptions = memo( - () => [table.getPageCount()], - (pageCount) => table_getPageOptions(pageCount), + () => [table_getPageCount(table)], + () => table_getPageOptions(table), getMemoOptions(table.options, 'debugTable', 'getPageOptions'), ) 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 2766fecb3f..d8268b1f89 100644 --- a/packages/table-core/src/features/row-pagination/RowPagination.utils.ts +++ b/packages/table-core/src/features/row-pagination/RowPagination.utils.ts @@ -1,14 +1,25 @@ import { functionalUpdate } from '../../utils' import { table_getExpandedRowModel } from '../row-expanding/RowExpanding.utils' +import { + _table_getInitialState, + _table_getState, +} from '../../core/table/Tables.utils' import type { RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../types/RowModel' import type { Table } from '../../types/Table' -import type { PaginationState } from './RowPagination.types' +import type { + PaginationState, + TableOptions_RowPagination, +} from './RowPagination.types' const defaultPageIndex = 0 const defaultPageSize = 10 +/** + * + * @returns + */ export function getDefaultPaginationState(): PaginationState { return structuredClone({ pageIndex: defaultPageIndex, @@ -16,10 +27,23 @@ export function getDefaultPaginationState(): PaginationState { }) } +/** + * + * @param table + * @param registered + * @param queued + * @returns + */ export function table_autoResetPageIndex< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, registered: boolean, queued: boolean) { +>( + table: Table & { + options: Partial + }, + registered: boolean, + queued: boolean, +) { if (!registered) { table._queue(() => { registered = true @@ -41,10 +65,21 @@ export function table_autoResetPageIndex< } } +/** + * + * @param table + * @param updater + * @returns + */ export function table_setPagination< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: Updater) { +>( + table: Table & { + options: Partial + }, + updater: Updater, +) { const safeUpdater: Updater = (old) => { const newState = functionalUpdate(updater, old) @@ -54,22 +89,42 @@ export function table_setPagination< return table.options.onPaginationChange?.(safeUpdater) } +/** + * + * @param table + * @param defaultState + */ export function table_resetPagination< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean) { +>( + table: Table & { + options: Partial + }, + defaultState?: boolean, +) { table_setPagination( table, defaultState ? getDefaultPaginationState() - : table.initialState.pagination ?? getDefaultPaginationState(), + : _table_getInitialState(table).pagination ?? getDefaultPaginationState(), ) } +/** + * + * @param table + * @param updater + */ export function table_setPageIndex< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: Updater) { +>( + table: Table & { + options: Partial + }, + updater: Updater, +) { table_setPagination(table, (old) => { let pageIndex = functionalUpdate(updater, old.pageIndex) @@ -88,34 +143,64 @@ export function table_setPageIndex< }) } +/** + * + * @param table + * @param defaultState + */ export function table_resetPageIndex< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean) { +>( + table: Table & { + options: Partial + }, + defaultState?: boolean, +) { table_setPageIndex( table, defaultState ? defaultPageIndex - : table.initialState.pagination?.pageIndex ?? defaultPageIndex, + : _table_getInitialState(table).pagination?.pageIndex ?? defaultPageIndex, ) } +/** + * + * @param table + * @param defaultState + */ export function table_resetPageSize< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean) { +>( + table: Table & { + options: Partial + }, + defaultState?: boolean, +) { table_setPageSize( table, defaultState ? defaultPageSize - : table.initialState.pagination?.pageSize ?? defaultPageSize, + : _table_getInitialState(table).pagination?.pageSize ?? defaultPageSize, ) } +/** + * + * @param table + * @param updater + */ export function table_setPageSize< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: Updater) { +>( + table: Table & { + options: Partial + }, + updater: Updater, +) { table_setPagination(table, (old) => { const pageSize = Math.max(1, functionalUpdate(updater, old.pageSize)) const topRowIndex = old.pageSize * old.pageIndex @@ -129,7 +214,16 @@ export function table_setPageSize< }) } -export function table_getPageOptions(pageCount: number) { +/** + * + * @param table + * @returns + */ +export function table_getPageOptions< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table) { + const pageCount = table_getPageCount(table) let pageOptions: Array = [] if (pageCount && pageCount > 0) { pageOptions = [...new Array(pageCount)].fill(null).map((_, i) => i) @@ -137,18 +231,37 @@ export function table_getPageOptions(pageCount: number) { return pageOptions } +/** + * + * @param table + * @returns + */ export function table_getCanPreviousPage< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - return table.getState().pagination.pageIndex > 0 +>( + table: Table & { + options: Partial + }, +) { + return (_table_getState(table).pagination?.pageIndex ?? 0) > 0 } +/** + * + * @param table + * @returns + */ export function table_getCanNextPage< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - const { pageIndex } = table.getState().pagination +>( + table: Table & { + options: Partial + }, +) { + const pageIndex = + _table_getState(table).pagination?.pageIndex ?? defaultPageIndex const pageCount = table_getPageCount(table) @@ -163,47 +276,101 @@ export function table_getCanNextPage< return pageIndex < pageCount - 1 } +/** + * + * @param table + * @returns + */ export function table_previousPage< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return table_setPageIndex(table, (old) => old - 1) } +/** + * + * @param table + * @returns + */ export function table_nextPage< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return table_setPageIndex(table, (old) => { return old + 1 }) } +/** + * + * @param table + * @returns + */ export function table_firstPage< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return table_setPageIndex(table, 0) } +/** + * + * @param table + * @returns + */ export function table_lastPage< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return table_setPageIndex(table, table_getPageCount(table) - 1) } +/** + * + * @param table + * @returns + */ export function table_getPrePaginationRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): RowModel { +>( + table: Table & { + options: Partial + }, +): RowModel { return table_getExpandedRowModel(table) } +/** + * + * @param table + * @returns + */ export function table_getPaginatedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): RowModel { +>( + table: Table & { + options: Partial + }, +): RowModel { if (!table._rowModels.Paginated) { table._rowModels.Paginated = table.options._rowModels?.Paginated?.(table) } @@ -215,20 +382,41 @@ export function table_getPaginatedRowModel< return table._rowModels.Paginated() } +/** + * + * @param table + * @returns + */ export function table_getPageCount< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return ( table.options.pageCount ?? - Math.ceil(table_getRowCount(table) / table.getState().pagination.pageSize) + Math.ceil( + table_getRowCount(table) / + (_table_getState(table).pagination?.pageSize ?? defaultPageSize), + ) ) } +/** + * + * @param table + * @returns + */ export function table_getRowCount< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial + }, +) { return ( table.options.rowCount ?? table_getPrePaginationRowModel(table).rows.length ) diff --git a/packages/table-core/src/features/row-pagination/createPaginatedRowModel.ts b/packages/table-core/src/features/row-pagination/createPaginatedRowModel.ts index e588f6dac9..7c84a19e73 100644 --- a/packages/table-core/src/features/row-pagination/createPaginatedRowModel.ts +++ b/packages/table-core/src/features/row-pagination/createPaginatedRowModel.ts @@ -1,5 +1,7 @@ import { getMemoOptions, memo } from '../../utils' import { expandRows } from '../row-expanding/createExpandedRowModel' +import { _table_getState } from '../../core/table/Tables.utils' +import { getDefaultPaginationState } from './RowPagination.utils' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../types/RowModel' @@ -15,18 +17,19 @@ export function createPaginatedRowModel< return (table) => memo( () => [ - table.getState().pagination, + _table_getState(table).pagination, table.getPrePaginationRowModel(), table.options.paginateExpandedRows ? undefined - : table.getState().expanded, + : _table_getState(table).expanded, ], (pagination, rowModel) => { if (!rowModel.rows.length) { return rowModel } - const { pageSize, pageIndex } = pagination + const { pageSize, pageIndex } = + pagination ?? getDefaultPaginationState() const { rows, flatRows, rowsById } = rowModel const pageStart = pageSize * pageIndex const pageEnd = pageStart + pageSize diff --git a/packages/table-core/src/features/row-pinning/RowPinning.ts b/packages/table-core/src/features/row-pinning/RowPinning.ts index cda243bda4..01a4e0707f 100644 --- a/packages/table-core/src/features/row-pinning/RowPinning.ts +++ b/packages/table-core/src/features/row-pinning/RowPinning.ts @@ -1,4 +1,5 @@ import { getMemoOptions, makeStateUpdater, memo } from '../../utils' +import { _table_getState } from '../../core/table/Tables.utils' import { getDefaultRowPinningState, row_getCanPin, @@ -55,7 +56,7 @@ export const RowPinning: TableFeature = { row.getIsPinned = () => row_getIsPinned(row, table) row.getPinnedIndex = memo( - () => [table.getRowModel().rows, table.getState().rowPinning], + () => [table.getRowModel().rows, _table_getState(table).rowPinning], () => row_getPinnedIndex(row, table), getMemoOptions(table.options, 'debugRows', 'getPinnedIndex'), ) @@ -77,19 +78,22 @@ export const RowPinning: TableFeature = { table_getIsSomeRowsPinned(table, position) table.getTopRows = memo( - () => [table.getRowModel().rows, table.getState().rowPinning.top], + () => [table.getRowModel().rows, _table_getState(table).rowPinning?.top], () => table_getTopRows(table), getMemoOptions(table.options, 'debugRows', 'getTopRows'), ) table.getBottomRows = memo( - () => [table.getRowModel().rows, table.getState().rowPinning.bottom], + () => [ + table.getRowModel().rows, + _table_getState(table).rowPinning?.bottom, + ], () => table_getBottomRows(table), getMemoOptions(table.options, 'debugRows', 'getBottomRows'), ) table.getCenterRows = memo( - () => [table.getRowModel().rows, table.getState().rowPinning], + () => [table.getRowModel().rows, _table_getState(table).rowPinning], () => table_getCenterRows(table), getMemoOptions(table.options, 'debugRows', 'getCenterRows'), ) 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 6cf9f050a0..75b48fab78 100644 --- a/packages/table-core/src/features/row-pinning/RowPinning.utils.ts +++ b/packages/table-core/src/features/row-pinning/RowPinning.utils.ts @@ -4,16 +4,26 @@ import { table_getRow, } from '../../core/rows/Rows.utils' import { row_getIsAllParentsExpanded } from '../row-expanding/RowExpanding.utils' +import { + _table_getInitialState, + _table_getState, +} from '../../core/table/Tables.utils' import type { RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table } from '../../types/Table' import type { Row } from '../../types/Row' -import type { RowPinningPosition, RowPinningState } from './RowPinning.types' +import type { + RowPinningPosition, + RowPinningState, + TableOptions_RowPinning, +} from './RowPinning.types' + +// State Utils /** - * State Utils + * + * @returns */ - export function getDefaultRowPinningState(): RowPinningState { return structuredClone({ top: [], @@ -21,50 +31,87 @@ export function getDefaultRowPinningState(): RowPinningState { }) } +/** + * + * @param table + * @param updater + */ export function table_setRowPinning< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: Updater): void { +>( + table: Table & { + options: Partial> + }, + updater: Updater, +): void { table.options.onRowPinningChange?.(updater) } +/** + * + * @param table + * @param defaultState + */ export function table_resetRowPinning< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean): void { +>( + table: Table & { + options: Partial> + }, + defaultState?: boolean, +): void { table_setRowPinning( table, defaultState ? getDefaultRowPinningState() - : table.initialState.rowPinning ?? getDefaultRowPinningState(), + : _table_getInitialState(table).rowPinning ?? getDefaultRowPinningState(), ) } +// Table Utils + /** - * Table Utils + * + * @param table + * @param position + * @returns */ - export function table_getIsSomeRowsPinned< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, position?: RowPinningPosition): boolean { - const rowPinning = table.getState().rowPinning +>( + table: Table & { + options: Partial> + }, + position?: RowPinningPosition, +): boolean { + const rowPinning = _table_getState(table).rowPinning if (!position) { - return Boolean(rowPinning.top.length || rowPinning.bottom.length) + return Boolean(rowPinning?.top.length || rowPinning?.bottom.length) } - return Boolean(rowPinning[position].length) + return Boolean(rowPinning?.[position].length) } +/** + * + * @param table + * @param position + * @returns + */ function table_getPinnedRows< TFeatures extends TableFeatures, TData extends RowData, >( - table: Table, + table: Table & { + options: Partial> + }, position: 'top' | 'bottom', ): Array> { const visibleRows = table.getRowModel().rows - const pinnedRowIds = table.getState().rowPinning[position] + const pinnedRowIds = _table_getState(table).rowPinning?.[position] ?? [] const rows = table.options.keepPinnedRows ?? true @@ -79,44 +126,81 @@ function table_getPinnedRows< (rowId) => visibleRows.find((row) => row.id === rowId)!, ) - return rows.filter(Boolean).map((d) => ({ ...d, position })) as Array< + return rows.filter((r) => !!r).map((d) => ({ ...d, position })) as Array< Row > } +/** + * + * @param table + * @returns + */ export function table_getTopRows< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): Array> { +>( + table: Table & { + options: Partial> + }, +): Array> { return table_getPinnedRows(table, 'top') } +/** + * + * @param table + * @returns + */ export function table_getBottomRows< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): Array> { +>( + table: Table & { + options: Partial> + }, +): Array> { return table_getPinnedRows(table, 'bottom') } +/** + * + * @param table + * @returns + */ export function table_getCenterRows< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): Array> { - const { top, bottom } = table.getState().rowPinning +>( + table: Table & { + options: Partial> + }, +): Array> { + const { top, bottom } = + _table_getState(table).rowPinning ?? getDefaultRowPinningState() const allRows = table.getRowModel().rows const topAndBottom = new Set([...top, ...bottom]) return allRows.filter((d) => !topAndBottom.has(d.id)) } +// Row Utils + /** - * Row Utils + * + * @param row + * @param table + * @returns */ - export function row_getCanPin< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table): boolean { +>( + row: Row, + table: Table & { + options: Partial> + }, +): boolean { const { enableRowPinning } = table.options if (typeof enableRowPinning === 'function') { return enableRowPinning(row) @@ -124,14 +208,23 @@ export function row_getCanPin< return enableRowPinning ?? true } +/** + * + * @param row + * @param table + * @returns + */ export function row_getIsPinned< TFeatures extends TableFeatures, TData extends RowData, >( row: Row, - table: Table, + table: Table & { + options: Partial> + }, ): RowPinningPosition { - const { top, bottom } = table.getState().rowPinning + const { top, bottom } = + _table_getState(table).rowPinning ?? getDefaultRowPinningState() return top.includes(row.id) ? 'top' @@ -140,10 +233,21 @@ export function row_getIsPinned< : false } +/** + * + * @param row + * @param table + * @returns + */ export function row_getPinnedIndex< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table): number { +>( + row: Row, + table: Table & { + options: Partial> + }, +): number { const position = row_getIsPinned(row, table) if (!position) return -1 @@ -154,9 +258,19 @@ export function row_getPinnedIndex< return visiblePinnedRowIds.indexOf(row.id) } +/** + * + * @param row + * @param table + * @param position + * @param includeLeafRows + * @param includeParentRows + */ export function row_pin( row: Row, - table: Table, + table: Table & { + options: Partial> + }, position: RowPinningPosition, includeLeafRows?: boolean, includeParentRows?: boolean, diff --git a/packages/table-core/src/features/row-selection/RowSelection.ts b/packages/table-core/src/features/row-selection/RowSelection.ts index a485bb053e..1a01d57331 100644 --- a/packages/table-core/src/features/row-selection/RowSelection.ts +++ b/packages/table-core/src/features/row-selection/RowSelection.ts @@ -1,4 +1,5 @@ import { getMemoOptions, makeStateUpdater, memo } from '../../utils' +import { _table_getState } from '../../core/table/Tables.utils' import { row_getCanMultiSelect, row_getCanSelect, @@ -105,13 +106,13 @@ export const RowSelection: TableFeature = { table.getPreSelectedRowModel = () => table_getPreSelectedRowModel(table) table.getSelectedRowModel = memo( - () => [table.getState().rowSelection, table.getCoreRowModel()], + () => [_table_getState(table).rowSelection, table.getCoreRowModel()], () => table_getSelectedRowModel(table), getMemoOptions(table.options, 'debugTable', 'getSelectedRowModel'), ) table.getFilteredSelectedRowModel = memo( - () => [table.getState().rowSelection, table.getFilteredRowModel()], + () => [_table_getState(table).rowSelection, table.getFilteredRowModel()], () => table_getFilteredSelectedRowModel(table), getMemoOptions( table.options, @@ -121,7 +122,7 @@ export const RowSelection: TableFeature = { ) table.getGroupedSelectedRowModel = memo( - () => [table.getState().rowSelection, table.getSortedRowModel()], + () => [_table_getState(table).rowSelection, table.getSortedRowModel()], () => table_getGroupedSelectedRowModel(table), getMemoOptions(table.options, 'debugTable', 'getGroupedSelectedRowModel'), ) 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 8b36e6957f..767f28dd67 100644 --- a/packages/table-core/src/features/row-selection/RowSelection.utils.ts +++ b/packages/table-core/src/features/row-selection/RowSelection.utils.ts @@ -3,6 +3,8 @@ import { table_getFilteredRowModel } from '../column-filtering/ColumnFiltering.u import { table_getPaginatedRowModel } from '../row-pagination/RowPagination.utils' import { table_getRow } from '../../core/rows/Rows.utils' import { + _table_getInitialState, + _table_getState, table_getCoreRowModel, table_getRowModel, } from '../../core/table/Tables.utils' @@ -11,29 +13,66 @@ import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../types/RowModel' import type { Table } from '../../types/Table' import type { Row } from '../../types/Row' -import type { RowSelectionState } from './RowSelection.types' - +import type { + RowSelectionState, + TableOptions_RowSelection, +} from './RowSelection.types' + +// State utils + +/** + * + * @param table + * @param updater + */ export function table_setRowSelection< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, updater: Updater) { +>( + table: Table & { + options: Partial> + }, + updater: Updater, +) { table.options.onRowSelectionChange?.(updater) } +/** + * + * @param table + * @param defaultState + */ export function table_resetRowSelection< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, defaultState?: boolean) { +>( + table: Table & { + options: Partial> + }, + defaultState?: boolean, +) { table_setRowSelection( table, - defaultState ? {} : table.initialState.rowSelection ?? {}, + defaultState ? {} : _table_getInitialState(table).rowSelection ?? {}, ) } +// Table utils + +/** + * + * @param table + * @param value + */ export function table_toggleAllRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, value?: boolean) { +>( + table: Table & { + options: Partial> + }, + value?: boolean, +) { table_setRowSelection(table, (old) => { value = typeof value !== 'undefined' ? value : !table_getIsAllRowsSelected(table) @@ -61,10 +100,20 @@ export function table_toggleAllRowsSelected< }) } +/** + * + * @param table + * @param value + */ export function table_toggleAllPageRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table, value?: boolean) { +>( + table: Table & { + options: Partial> + }, + value?: boolean, +) { table_setRowSelection(table, (old) => { const resolvedValue = typeof value !== 'undefined' @@ -81,21 +130,38 @@ export function table_toggleAllPageRowsSelected< }) } +/** + * + * @param table + * @returns + */ export function table_getPreSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): RowModel { +>( + table: Table & { + options: Partial> + }, +): RowModel { return table_getCoreRowModel(table) } +/** + * + * @param table + * @returns + */ export function table_getSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - const { rowSelection } = table.getState() +>( + table: Table & { + options: Partial> + }, +) { const rowModel = table_getCoreRowModel(table) - if (!Object.keys(rowSelection).length) { + if (!Object.keys(_table_getState(table).rowSelection ?? {}).length) { return { rows: [], flatRows: [], @@ -106,14 +172,22 @@ export function table_getSelectedRowModel< return selectRowsFn(table, rowModel) } +/** + * + * @param table + * @returns + */ export function table_getFilteredSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - const { rowSelection } = table.getState() +>( + table: Table & { + options: Partial> + }, +) { const rowModel = table_getCoreRowModel(table) - if (!Object.keys(rowSelection).length) { + if (!Object.keys(_table_getState(table).rowSelection ?? {}).length) { return { rows: [], flatRows: [], @@ -124,14 +198,22 @@ export function table_getFilteredSelectedRowModel< return selectRowsFn(table, rowModel) } +/** + * + * @param table + * @returns + */ export function table_getGroupedSelectedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - const { rowSelection } = table.getState() +>( + table: Table & { + options: Partial> + }, +) { const rowModel = table_getCoreRowModel(table) - if (!Object.keys(rowSelection).length) { + if (!Object.keys(_table_getState(table).rowSelection ?? {}).length) { return { rows: [], flatRows: [], @@ -142,12 +224,21 @@ export function table_getGroupedSelectedRowModel< return selectRowsFn(table, rowModel) } +/** + * + * @param table + * @returns + */ export function table_getIsAllRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { const preGroupedFlatRows = table_getFilteredRowModel(table).flatRows - const { rowSelection } = table.getState() + const rowSelection = _table_getState(table).rowSelection ?? {} let isAllRowsSelected = Boolean( preGroupedFlatRows.length && Object.keys(rowSelection).length, @@ -166,14 +257,23 @@ export function table_getIsAllRowsSelected< return isAllRowsSelected } +/** + * + * @param table + * @returns + */ export function table_getIsAllPageRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { const paginationFlatRows = table_getPaginatedRowModel(table).flatRows.filter( (row) => row_getCanSelect(row, table), ) - const { rowSelection } = table.getState() + const rowSelection = _table_getState(table).rowSelection ?? {} let isAllPageRowsSelected = !!paginationFlatRows.length @@ -187,21 +287,41 @@ export function table_getIsAllPageRowsSelected< return isAllPageRowsSelected } +/** + * + * @param table + * @returns + */ export function table_getIsSomeRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { - const totalSelected = Object.keys(table.getState().rowSelection).length +>( + table: Table & { + options: Partial> + }, +) { + const totalSelected = Object.keys( + _table_getState(table).rowSelection ?? {}, + ).length return ( totalSelected > 0 && totalSelected < table_getFilteredRowModel(table).flatRows.length ) } +/** + * + * @param table + * @returns + */ export function table_getIsSomePageRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { const paginationFlatRows = table_getPaginatedRowModel(table).flatRows return table_getIsAllPageRowsSelected(table) ? false @@ -213,10 +333,19 @@ export function table_getIsSomePageRowsSelected< ) } +/** + * + * @param table + * @returns + */ export function table_getToggleAllRowsSelectedHandler< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { return (e: unknown) => { table_toggleAllRowsSelected( table, @@ -225,10 +354,19 @@ export function table_getToggleAllRowsSelectedHandler< } } +/** + * + * @param table + * @returns + */ export function table_getToggleAllPageRowsSelectedHandler< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table) { +>( + table: Table & { + options: Partial> + }, +) { return (e: unknown) => { table_toggleAllPageRowsSelected( table, @@ -237,12 +375,21 @@ export function table_getToggleAllPageRowsSelectedHandler< } } +/** + * + * @param row + * @param table + * @param value + * @param opts + */ export function row_toggleSelected< TFeatures extends TableFeatures, TData extends RowData, >( row: Row, - table: Table, + table: Table & { + options: Partial> + }, value?: boolean, opts?: { selectChildren?: boolean @@ -271,34 +418,80 @@ export function row_toggleSelected< }) } +// Row utils + +/** + * + * @param row + * @param table + * @returns + */ export function row_getIsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { - const { rowSelection } = table.getState() +>( + row: Row, + table: Table & { + options: Partial> + }, +) { + const rowSelection = _table_getState(table).rowSelection ?? {} return isRowSelected(row, rowSelection) } +/** + * + * @param row + * @param table + * @returns + */ export function row_getIsSomeSelected< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { - const { rowSelection } = table.getState() +>( + row: Row, + table: Table & { + options: Partial> + }, +) { + const rowSelection = _table_getState(table).rowSelection ?? {} return isSubRowSelected(row, rowSelection, table) === 'some' } +/** + * + * @param row + * @param table + * @returns + */ export function row_getIsAllSubRowsSelected< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { - const { rowSelection } = table.getState() +>( + row: Row, + table: Table & { + options: Partial> + }, +) { + const rowSelection = _table_getState(table).rowSelection ?? {} return isSubRowSelected(row, rowSelection, table) === 'all' } +/** + * + * @param row + * @param table + * @returns + */ export function row_getCanSelect< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>( + row: Row, + table: Table & { + options: Partial> + }, +) { if (typeof table.options.enableRowSelection === 'function') { return table.options.enableRowSelection(row) } @@ -306,10 +499,21 @@ export function row_getCanSelect< return table.options.enableRowSelection ?? true } +/** + * + * @param row + * @param table + * @returns + */ export function row_getCanSelectSubRows< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>( + row: Row, + table: Table & { + options: Partial> + }, +) { if (typeof table.options.enableSubRowSelection === 'function') { return table.options.enableSubRowSelection(row) } @@ -317,10 +521,21 @@ export function row_getCanSelectSubRows< return table.options.enableSubRowSelection ?? true } +/** + * + * @param row + * @param table + * @returns + */ export function row_getCanMultiSelect< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>( + row: Row, + table: Table & { + options: Partial> + }, +) { if (typeof table.options.enableMultiRowSelection === 'function') { return table.options.enableMultiRowSelection(row) } @@ -328,10 +543,21 @@ export function row_getCanMultiSelect< return table.options.enableMultiRowSelection ?? true } +/** + * + * @param row + * @param table + * @returns + */ export function row_getToggleSelectedHandler< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row, table: Table) { +>( + row: Row, + table: Table & { + options: Partial> + }, +) { const canSelect = row_getCanSelect(row, table) return (e: unknown) => { @@ -344,6 +570,14 @@ export function row_getToggleSelectedHandler< } } +/** + * + * @param selectedRowIds + * @param rowId + * @param value + * @param includeChildren + * @param table + */ const mutateRowIsSelected = < TFeatures extends TableFeatures, TData extends RowData, @@ -352,7 +586,9 @@ const mutateRowIsSelected = < rowId: string, value: boolean, includeChildren: boolean, - table: Table, + table: Table & { + options: Partial> + }, ) => { const row = table_getRow(table, rowId, true) @@ -385,14 +621,22 @@ const mutateRowIsSelected = < } } +/** + * + * @param table + * @param rowModel + * @returns + */ export function selectRowsFn< TFeatures extends TableFeatures, TData extends RowData, >( - table: Table, + table: Table & { + options: Partial> + }, rowModel: RowModel, ): RowModel { - const rowSelection = table.getState().rowSelection + const rowSelection = _table_getState(table).rowSelection ?? {} const newSelectedFlatRows: Array> = [] const newSelectedRowsById: Record> = {} @@ -432,6 +676,12 @@ export function selectRowsFn< } } +/** + * + * @param row + * @param selection + * @returns + */ export function isRowSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -445,7 +695,9 @@ export function isSubRowSelected< >( row: Row, selection: Record, - table: Table, + table: Table & { + options: Partial> + }, ): boolean | 'some' | 'all' { if (!row.subRows.length) return false diff --git a/packages/table-core/src/features/row-sorting/RowSorting.ts b/packages/table-core/src/features/row-sorting/RowSorting.ts index 4d33d19a96..24193c7f4a 100644 --- a/packages/table-core/src/features/row-sorting/RowSorting.ts +++ b/packages/table-core/src/features/row-sorting/RowSorting.ts @@ -17,6 +17,7 @@ import { table_resetSorting, table_setSorting, } from './RowSorting.utils' +import type { ColumnDef } from '../../types/ColumnDef' import type { ColumnDef_RowSorting, Column_RowSorting, 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 6bdaec08f4..e83a2aa40e 100644 --- a/packages/table-core/src/features/row-sorting/RowSorting.utils.ts +++ b/packages/table-core/src/features/row-sorting/RowSorting.utils.ts @@ -3,19 +3,82 @@ import { isFunction } from '../../utils' import { table_getFilteredRowModel } from '../column-filtering/ColumnFiltering.utils' import { row_getValue } from '../../core/rows/Rows.utils' import { table_getGroupedRowModel } from '../column-grouping/ColumnGrouping.utils' +import { + _table_getInitialState, + _table_getState, +} from '../../core/table/Tables.utils' import type { BuiltInSortingFn } from '../../fns/sortingFns' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../types/RowModel' import type { Table } from '../../types/Table' import type { Column } from '../../types/Column' -import type { SortingState } from './RowSorting.types' +import type { + ColumnDef_RowSorting, + SortDirection, + SortingState, + TableOptions_RowSorting, +} from './RowSorting.types' + +// State Utils + +/** + * + * @param table + * @param updater + */ +export function table_setSorting< + TFeatures extends TableFeatures, + TData extends RowData, +>( + table: Table & { + options: Partial> + }, + updater: Updater, +) { + table.options.onSortingChange?.(updater) +} + +/** + * + * @param table + * @param defaultState + */ +export function table_resetSorting< + TFeatures extends TableFeatures, + TData extends RowData, +>( + table: Table & { + options: Partial> + }, + defaultState?: boolean, +) { + table_setSorting( + table, + defaultState ? [] : _table_getInitialState(table).sorting ?? [], + ) +} + +//Column Utils +/** + * + * @param column + * @param table + * @returns + */ export function column_getAutoSortingFn< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +) { const firstRows = table_getFilteredRowModel(table).flatRows.slice(10) let isString = false @@ -43,6 +106,12 @@ export function column_getAutoSortingFn< return sortingFns.basic } +/** + * + * @param column + * @param table + * @returns + */ export function column_getAutoSortDir< TFeatures extends TableFeatures, TData extends RowData, @@ -59,11 +128,24 @@ export function column_getAutoSortDir< return 'desc' } +/** + * + * @param column + * @param table + * @returns + */ export function column_getSortingFn< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +) { return isFunction(column.columnDef.sortingFn) ? column.columnDef.sortingFn : column.columnDef.sortingFn === 'auto' @@ -72,13 +154,24 @@ export function column_getSortingFn< sortingFns[column.columnDef.sortingFn as BuiltInSortingFn] } +/** + * + * @param column + * @param table + * @param desc + * @param multi + */ export function column_toggleSorting< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, >( - column: Column, - table: Table, + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, desc?: boolean, multi?: boolean, ) { @@ -175,11 +268,24 @@ export function column_toggleSorting< }) } +/** + * + * @param column + * @param table + * @returns + */ export function column_getFirstSortDir< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +) { const sortDescFirst = column.columnDef.sortDescFirst ?? table.options.sortDescFirst ?? @@ -187,13 +293,24 @@ export function column_getFirstSortDir< return sortDescFirst ? 'desc' : 'asc' } +/** + * + * @param column + * @param table + * @param multi + * @returns + */ export function column_getNextSortingOrder< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, >( - column: Column, - table: Table, + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, multi?: boolean, ) { const firstSortDirection = column_getFirstSortDir(column, table) @@ -213,11 +330,24 @@ export function column_getNextSortingOrder< return isSorted === 'desc' ? 'asc' : 'desc' } +/** + * + * @param column + * @param table + * @returns + */ export function column_getCanSort< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +) { return ( (column.columnDef.enableSorting ?? true) && (table.options.enableSorting ?? true) && @@ -225,11 +355,24 @@ export function column_getCanSort< ) } +/** + * + * @param column + * @param table + * @returns + */ export function column_getCanMultiSort< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +): boolean { return ( column.columnDef.enableMultiSort ?? table.options.enableMultiSort ?? @@ -237,39 +380,94 @@ export function column_getCanMultiSort< ) } +/** + * + * @param column + * @param table + * @returns + */ export function column_getIsSorted< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { - const columnSort = table.getState().sorting.find((d) => d.id === column.id) +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +): false | SortDirection { + const columnSort = _table_getState(table).sorting?.find( + (d) => d.id === column.id, + ) return !columnSort ? false : columnSort.desc ? 'desc' : 'asc' } +/** + * + * @param column + * @param table + * @returns + */ export function column_getSortIndex< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { - return table.getState().sorting.findIndex((d) => d.id === column.id) +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +): number { + return ( + _table_getState(table).sorting?.findIndex((d) => d.id === column.id) ?? -1 + ) } +/** + * + * @param column + * @param table + */ export function column_clearSorting< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +) { //clear sorting for just 1 column table_setSorting(table, (old) => old.length ? old.filter((d) => d.id !== column.id) : [], ) } +/** + * + * @param column + * @param table + * @returns + */ export function column_getToggleSortingHandler< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, ->(column: Column, table: Table) { +>( + column: Column & { + columnDef: Partial> + }, + table: Table & { + options: Partial> + }, +) { const canSort = column_getCanSort(column, table) return (e: unknown) => { @@ -286,20 +484,13 @@ export function column_getToggleSortingHandler< } } -export function table_setSorting< - TFeatures extends TableFeatures, - TData extends RowData, ->(table: Table, updater: Updater) { - table.options.onSortingChange?.(updater) -} - -export function table_resetSorting< - TFeatures extends TableFeatures, - TData extends RowData, ->(table: Table, defaultState?: boolean) { - table_setSorting(table, defaultState ? [] : table.initialState.sorting ?? []) -} +// Table Utils +/** + * + * @param table + * @returns + */ export function table_getPreSortedRowModel< TFeatures extends TableFeatures, TData extends RowData, @@ -310,7 +501,11 @@ export function table_getPreSortedRowModel< export function table_getSortedRowModel< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table): RowModel { +>( + table: Table & { + options: Partial> + }, +): RowModel { if (!table._rowModels.Sorted) { table._rowModels.Sorted = table.options._rowModels?.Sorted?.(table) } diff --git a/packages/table-core/src/features/row-sorting/createSortedRowModel.ts b/packages/table-core/src/features/row-sorting/createSortedRowModel.ts index a555e5f49c..c54ef4f0b9 100644 --- a/packages/table-core/src/features/row-sorting/createSortedRowModel.ts +++ b/packages/table-core/src/features/row-sorting/createSortedRowModel.ts @@ -1,6 +1,7 @@ import { getMemoOptions, memo } from '../../utils' import { row_getValue } from '../../core/rows/Rows.utils' import { table_getColumn } from '../../core/columns/Columns.utils' +import { _table_getState } from '../../core/table/Tables.utils' import { column_getCanSort, column_getSortingFn } from './RowSorting.utils' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' @@ -15,18 +16,18 @@ export function createSortedRowModel< >(): (table: Table) => () => RowModel { return (table) => memo( - () => [table.getState().sorting, table.getPreSortedRowModel()], + () => [_table_getState(table).sorting, table.getPreSortedRowModel()], (sorting, rowModel) => { - if (!rowModel.rows.length || !sorting.length) { + if (!rowModel.rows.length || !sorting?.length) { return rowModel } - const sortingState = table.getState().sorting + const sortingState = _table_getState(table).sorting const sortedFlatRows: Array> = [] // Filter out sortings that correspond to non existing columns - const availableSorting = sortingState.filter((sort) => + const availableSorting = sortingState?.filter((sort) => column_getCanSort(table_getColumn(table, sort.id)!, table), ) @@ -39,7 +40,7 @@ export function createSortedRowModel< } > = {} - availableSorting.forEach((sortEntry) => { + availableSorting?.forEach((sortEntry) => { const column = table_getColumn(table, sortEntry.id) if (!column) return @@ -56,7 +57,7 @@ export function createSortedRowModel< const sortedData = rows.map((row) => ({ ...row })) sortedData.sort((rowA, rowB) => { - for (const sortEntry of availableSorting) { + for (const sortEntry of availableSorting ?? []) { const columnInfo = columnInfoById[sortEntry.id]! const sortUndefined = columnInfo.sortUndefined const isDesc = sortEntry.desc diff --git a/packages/table-core/src/helpers/tableHelper.ts b/packages/table-core/src/helpers/tableHelper.ts index f8c32ae823..a42bd27a18 100644 --- a/packages/table-core/src/helpers/tableHelper.ts +++ b/packages/table-core/src/helpers/tableHelper.ts @@ -52,7 +52,7 @@ export function _createTableHelper< features: tableHelperOptions._features as TFeatures, options: tableHelperOptions, tableCreator: (tableOptions) => - tableCreator({ ...tableHelperOptions, ...tableOptions }), + tableCreator({ ...tableHelperOptions, ...(tableOptions as any) }), } } diff --git a/packages/table-core/src/types/ColumnDef.ts b/packages/table-core/src/types/ColumnDef.ts index ea6b1d6971..9012f6fb48 100644 --- a/packages/table-core/src/types/ColumnDef.ts +++ b/packages/table-core/src/types/ColumnDef.ts @@ -1,4 +1,9 @@ -import type { CellData, RowData, UnionToIntersection } from './type-utils' +import type { + CellData, + Prettify, + RowData, + UnionToIntersection, +} from './type-utils' import type { TableFeatures } from './TableFeatures' import type { CellContext } from '../core/cells/Cells.types' import type { HeaderContext } from '../core/headers/Headers.types' @@ -83,12 +88,12 @@ export type _ColumnDefBase< meta?: ColumnMeta } -//temp - enable all features for types internally +//temp export type ColumnDefBase< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, -> = _ColumnDefBase +> = _ColumnDefBase export type IdentifiedColumnDef< TFeatures extends TableFeatures, diff --git a/packages/table-core/src/types/TableOptions.ts b/packages/table-core/src/types/TableOptions.ts index 298d88a0a7..9f41f0303d 100644 --- a/packages/table-core/src/types/TableOptions.ts +++ b/packages/table-core/src/types/TableOptions.ts @@ -16,7 +16,7 @@ import type { TableOptions_RowPagination } from '../features/row-pagination/RowP import type { TableOptions_RowPinning } from '../features/row-pinning/RowPinning.types' import type { TableOptions_RowSelection } from '../features/row-selection/RowSelection.types' import type { TableOptions_RowSorting } from '../features/row-sorting/RowSorting.types' -import type { RowData, UnionToIntersection } from './type-utils' +import type { Prettify, RowData, UnionToIntersection } from './type-utils' import type { TableFeatures } from './TableFeatures' export interface TableOptions_Core< @@ -74,10 +74,10 @@ export type _TableOptions< : never) > -export type TableOptions_All = _TableOptions< - TableFeatures, - TData +export type TableOptions_All = Prettify< + _TableOptions > + //temp - enable all features for types internally export type TableOptions< TFeatures extends TableFeatures, diff --git a/packages/table-core/src/types/TableState.ts b/packages/table-core/src/types/TableState.ts index 9fc2f495de..724c926b02 100644 --- a/packages/table-core/src/types/TableState.ts +++ b/packages/table-core/src/types/TableState.ts @@ -11,7 +11,7 @@ import type { TableState_RowPagination } from '../features/row-pagination/RowPag import type { TableState_RowPinning } from '../features/row-pinning/RowPinning.types' import type { TableState_RowSelection } from '../features/row-selection/RowSelection.types' import type { TableState_RowSorting } from '../features/row-sorting/RowSorting.types' -import type { UnionToIntersection } from './type-utils' +import type { Prettify, UnionToIntersection } from './type-utils' import type { TableFeatures } from './TableFeatures' export type _TableState = UnionToIntersection< @@ -45,4 +45,5 @@ export type _TableState = UnionToIntersection< export type TableState_All = _TableState //temp - enable all features for types internally -export type TableState = TableState_All +export type TableState = + Prettify diff --git a/packages/table-core/src/types/type-utils.ts b/packages/table-core/src/types/type-utils.ts index f64c140b7b..92fdbc9d28 100644 --- a/packages/table-core/src/types/type-utils.ts +++ b/packages/table-core/src/types/type-utils.ts @@ -82,3 +82,5 @@ export type Getter = () => NoInfer export type LiteralUnion = | T | (U & Record) + +export type Prettify = { [K in keyof T]: T[K] } & unknown diff --git a/packages/table-core/src/utils.ts b/packages/table-core/src/utils.ts index 7540e7a637..6df411907e 100755 --- a/packages/table-core/src/utils.ts +++ b/packages/table-core/src/utils.ts @@ -126,7 +126,7 @@ export function getMemoOptions< TFeatures extends TableFeatures, TData extends RowData, >( - tableOptions: Partial>, + tableOptions: TableOptions, debugLevel: | 'debugAll' | 'debugCells'