Skip to content

Commit

Permalink
chore: more progress on tfeatures static refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jul 25, 2024
1 parent fbb3bd1 commit 80d074d
Show file tree
Hide file tree
Showing 42 changed files with 1,502 additions and 327 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions packages/table-core/src/core/columns/Columns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getMemoOptions, memo } from '../../utils'
import { _table_getState } from '../table/Tables.utils'
import { _createColumn } from './createColumn'
import {
column_getFlatColumns,
Expand Down Expand Up @@ -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,
],
Expand Down Expand Up @@ -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,
],
Expand Down
8 changes: 4 additions & 4 deletions packages/table-core/src/core/headers/Headers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getMemoOptions, memo } from '../../utils'
import { _table_getState } from '../table/Tables.utils'
import { _createHeader } from './createHeader'
import {
header_getContext,
Expand All @@ -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'
Expand All @@ -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),
Expand Down
21 changes: 11 additions & 10 deletions packages/table-core/src/core/headers/Headers.utils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -43,22 +45,21 @@ export function table_getHeaderGroups<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>) {
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(
Expand Down
31 changes: 31 additions & 0 deletions packages/table-core/src/core/table/Tables.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,44 @@ export function table_setOptions<
table.options = table_mergeOptions(table, newOptions)
}

export function table_getInitialState<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): TableState<TFeatures> {
return structuredClone(table.initialState) as TableState<TFeatures>
}

/**
* 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<TFeatures, TData>): Partial<TableState<TableFeatures>> {
return table_getInitialState(table) as Partial<TableState<TableFeatures>>
}

export function table_getState<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): TableState<TFeatures> {
return table.options.state as TableState<TFeatures>
}

/**
* 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<TFeatures, TData>): Partial<TableState<TableFeatures>> {
return table_getState(table) as Partial<TableState<TableFeatures>>
}

export function table_setState<
TFeatures extends TableFeatures,
TData extends RowData,
Expand Down
10 changes: 7 additions & 3 deletions packages/table-core/src/core/table/createTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -69,8 +73,8 @@ export function _createTable<
const _features = {
...coreFeatures,
...builtInFeatures,
...(options._features ?? {}),
}
...options._features,
} as CoreTableFeatures & TFeatures

const featuresList: Array<TableFeature> = Object.values(_features)

Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -44,13 +51,6 @@ export const ColumnFiltering: TableFeature = {
}
},

_getInitialState: (state): TableState_ColumnFiltering => {
return {
columnFilters: [],
...state,
}
},

_getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData> &
Partial<Table_ColumnFiltering<TFeatures, TData>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -81,15 +84,23 @@ export function column_getFilterValue<
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
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<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
return table.getState().columnFilters.findIndex((d) => d.id === column.id)
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): number {
return (
_table_getState(table).columnFilters?.findIndex(
(d) => d.id === column.id,
) ?? -1
)
}

export function column_setFilterValue<
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 = {}
Expand All @@ -37,7 +41,7 @@ export function createFilteredRowModel<
ResolvedColumnFilter<TFeatures, TData>
> = []

columnFilters.forEach((d) => {
columnFilters?.forEach((d) => {
const column = table_getColumn(table, d.id)

if (!column) {
Expand All @@ -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)

Expand All @@ -75,7 +79,7 @@ export function createFilteredRowModel<
globalFilterFn &&
globallyFilterableColumns.length
) {
filterableIds.push('__global__')
filterableIds?.push('__global__')

globallyFilterableColumns.forEach((column) => {
resolvedGlobalFilters.push({
Expand Down Expand Up @@ -135,7 +139,7 @@ export function createFilteredRowModel<

const filterRowsImpl = (row: Row<TFeatures, TData>) => {
// Horizontally filter rows through each column
for (const columnId of filterableIds) {
for (const columnId of filterableIds ?? []) {
if (row.columnFilters[columnId] === false) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -50,13 +57,6 @@ export const ColumnGrouping: TableFeature = {
}
},

_getInitialState: (state): TableState_ColumnGrouping => {
return {
grouping: [],
...state,
}
},

_getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData> &
Partial<Table_ColumnGrouping<TFeatures, TData>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -42,16 +43,22 @@ export function column_getIsGrouped<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
return table.getState().grouping.includes(column.id)
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): 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<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
return table.getState().grouping.indexOf(column.id)
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): number {
return _table_getState(table).grouping?.indexOf(column.id) ?? -1
}

export function column_getToggleGroupingHandler<
Expand Down
Loading

0 comments on commit 80d074d

Please sign in to comment.