Skip to content

Commit

Permalink
simplify api assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Oct 23, 2024
1 parent b015b47 commit 57602b3
Show file tree
Hide file tree
Showing 38 changed files with 250 additions and 160 deletions.
12 changes: 5 additions & 7 deletions packages/table-core/src/core/cells/Cells.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { assignAPIs } from '../../utils'
import { cell_getContext, cell_getValue, cell_renderValue } from './Cells.utils'
import type { Table_Internal } from '../../types/Table'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Cell } from '../../types/Cell'

export const Cells: TableFeature = {
constructCell: <
constructCellAPIs: <
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
cell: Cell<TFeatures, TData, TValue>,
table: Table_Internal<TFeatures, TData>,
) => {
assignAPIs(cell, table, [
assignAPIs(cell, [
{
fn: () => cell_getValue(cell),
},
{
fn: () => cell_renderValue(cell, table),
fn: () => cell_renderValue(cell),
},
{
fn: () => cell_getContext(cell, table),
memoDeps: () => [cell, table],
fn: () => cell_getContext(cell),
memoDeps: () => [cell],
},
])
},
Expand Down
6 changes: 5 additions & 1 deletion packages/table-core/src/core/cells/Cells.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CellData, Getter, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Table, Table_Internal } from '../../types/Table'
import type { Row } from '../../types/Row'
import type { Cell } from '../../types/Cell'
import type { Column } from '../../types/Column'
Expand Down Expand Up @@ -41,6 +41,10 @@ export interface Cell_CoreProperties<
* @link [Guide](https://tanstack.com/table/v8/docs/guide/cells)
*/
row: Row<TFeatures, TData>
/**
* @deprecated Reference to the table instance.
*/
table: Table_Internal<TFeatures, TData>
}

export interface Cell_Cell<
Expand Down
13 changes: 7 additions & 6 deletions packages/table-core/src/core/cells/Cells.utils.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { callMemoOrStaticFn } from '../../utils'
import { row_getValue } from '../rows/Rows.utils'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Cell } from '../../types/Cell'

export function cell_getValue<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(cell: Cell<TFeatures, TData, TValue>): TValue {
return cell.row.getValue(cell.column.id)
return callMemoOrStaticFn(cell.row, row_getValue, [cell.column.id])
}

export function cell_renderValue<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(cell: Cell<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
return cell.getValue() ?? table.options.renderFallbackValue
>(cell: Cell<TFeatures, TData, TValue>) {
return cell.getValue() ?? cell.table.options.renderFallbackValue
}

export function cell_getContext<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(cell: Cell<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
>(cell: Cell<TFeatures, TData, TValue>) {
return {
table,
table: cell.table,
column: cell.column,
row: cell.row,
cell: cell,
Expand Down
7 changes: 4 additions & 3 deletions packages/table-core/src/core/cells/constructCell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Row } from '../../types/Row'
import type { Cell } from '../../types/Cell'
Expand All @@ -19,10 +19,11 @@ export function constructCell<
column,
id: `${row.id}_${column.id}`,
row,
table,
}

for (const feature of Object.values(table._features)) {
feature?.constructCell?.(cell as Cell<TFeatures, TData, TValue>, table)
for (const feature of Object.values(table._features) as Array<TableFeature>) {
feature.constructCellAPIs?.(cell as Cell<TFeatures, TData, TValue>, table)
}

return cell as Cell<TFeatures, TData, TValue>
Expand Down
10 changes: 5 additions & 5 deletions packages/table-core/src/core/columns/Columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ import type { Table_Internal } from '../../types/Table'
import type { Column } from '../../types/Column'

export const Columns: TableFeature = {
constructColumn: <
constructColumnAPIs: <
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table_Internal<TFeatures, TData>,
) => {
assignAPIs(column, table, [
assignAPIs(column, [
{
fn: () => column_getFlatColumns(column),
memoDeps: () => [table.options.columns],
},
{
fn: () => column_getLeafColumns(column, table),
fn: () => column_getLeafColumns(column),
memoDeps: () => [
table.getState().columnOrder,
table.getState().grouping,
Expand All @@ -40,10 +40,10 @@ export const Columns: TableFeature = {
])
},

constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
constructTableAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table_Internal<TFeatures, TData>,
) => {
assignAPIs(table, table, [
assignAPIs(table, [
{
fn: () => table_getDefaultColumnDef(table),
memoDeps: () => [table.options.defaultColumn],
Expand Down
5 changes: 5 additions & 0 deletions packages/table-core/src/core/columns/Columns.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Table_Internal } from '../../types/Table'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { AccessorFn, ColumnDef } from '../../types/ColumnDef'
Expand Down Expand Up @@ -47,6 +48,10 @@ export interface Column_CoreProperties<
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-defs)
*/
parent?: Column<TFeatures, TData, TValue>
/**
* @deprecated Reference to the table instance.
*/
table: Table_Internal<TFeatures, TData>
}

export interface Column_Column<
Expand Down
4 changes: 2 additions & 2 deletions packages/table-core/src/core/columns/Columns.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ export function column_getLeafColumns<
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): Array<Column<TFeatures, TData, TValue>> {
if (column.columns.length) {
const leafColumns = column.columns.flatMap(
(col) => col.getLeafColumns(), // recursive
)

return table_getOrderColumnsFn(table)(leafColumns) as any
return table_getOrderColumnsFn(column.table)(leafColumns as any) as any
}

return [column]
Expand Down Expand Up @@ -144,3 +143,4 @@ export function table_getColumn<

return column
}

13 changes: 7 additions & 6 deletions packages/table-core/src/core/columns/constructColumn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type {
AccessorFn,
Expand Down Expand Up @@ -74,16 +74,17 @@ export function constructColumn<
}

const column: Column_CoreProperties<TFeatures, TData, TValue> = {
id: `${String(id)}`,
accessorFn,
parent: parent,
depth,
columnDef: resolvedColumnDef as ColumnDef<TFeatures, TData, TValue>,
columns: [],
depth,
id: `${String(id)}`,
parent: parent,
table,
}

for (const feature of Object.values(table._features)) {
feature?.constructColumn?.(
for (const feature of Object.values(table._features) as Array<TableFeature>) {
feature.constructColumnAPIs?.(
column as Column<TFeatures, TData, TValue>,
table,
)
Expand Down
10 changes: 5 additions & 5 deletions packages/table-core/src/core/headers/Headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ import type { Table, Table_Internal } from '../../types/Table'
import type { Header } from '../../types/Header'

export const Headers: TableFeature = {
constructHeader: <
constructHeaderAPIs: <
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
header: Header<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): void => {
assignAPIs(header, table, [
assignAPIs(header, [
{
fn: () => header_getLeafHeaders(header),
memoDeps: () => [table.options.columns],
},
{
fn: () => header_getContext(header, table),
fn: () => header_getContext(header),
memoDeps: () => [table.options.columns],
},
])
},

constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
constructTableAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table_Internal<TFeatures, TData>,
): void => {
assignAPIs(table, table, [
assignAPIs(table, [
{
fn: () => table_getHeaderGroups(table),
memoDeps: () => [
Expand Down
6 changes: 5 additions & 1 deletion packages/table-core/src/core/headers/Headers.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Table, Table_Internal } from '../../types/Table'
import type { Header } from '../../types/Header'
import type { HeaderGroup } from '../../types/HeaderGroup'
import type { Column } from '../../types/Column'
Expand Down Expand Up @@ -128,6 +128,10 @@ export interface Header_CoreProperties<
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
*/
subHeaders: Array<Header<TFeatures, TData, TValue>>
/**
* @deprecated Reference to the table instance.
*/
table: Table_Internal<TFeatures, TData>
}

export interface Header_Header<
Expand Down
4 changes: 2 additions & 2 deletions packages/table-core/src/core/headers/Headers.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export function header_getContext<
TFeatures extends TableFeatures,
TData extends RowData,
TValue,
>(header: Header<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
>(header: Header<TFeatures, TData, TValue>) {
return {
column: header.column,
header,
table,
table: header.table,
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/table-core/src/core/headers/constructHeader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Header } from '../../types/Header'
import type { Column } from '../../types/Column'
Expand Down Expand Up @@ -31,10 +31,11 @@ export function constructHeader<
placeholderId: options.placeholderId,
rowSpan: 0,
subHeaders: [],
table,
}

for (const feature of Object.values(table._features)) {
feature?.constructHeader?.(
for (const feature of Object.values(table._features) as Array<TableFeature>) {
feature.constructHeaderAPIs?.(
header as Header<TFeatures, TData, TValue>,
table,
)
Expand Down
4 changes: 2 additions & 2 deletions packages/table-core/src/core/row-models/RowModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import type { RowData } from '../../types/type-utils'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'

export const RowModels: TableFeature = {
constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
constructTableAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData>,
): void => {
assignAPIs(table, table, [
assignAPIs(table, [
{
fn: () => table_getCoreRowModel(table),
},
Expand Down
2 changes: 0 additions & 2 deletions packages/table-core/src/core/row-models/createCoreRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ function _createCoreRowModel<
): Array<Row<TFeatures, TData>> => {
const rows = [] as Array<Row<TFeatures, TData>>

console.time('constructing rows')
for (let i = 0; i < originalRows.length; i++) {
const originalRow = originalRows[i]!
// Make the row
Expand Down Expand Up @@ -76,7 +75,6 @@ function _createCoreRowModel<
}
}
}
console.timeEnd('constructing rows')

return rows
}
Expand Down
22 changes: 11 additions & 11 deletions packages/table-core/src/core/rows/Rows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,44 @@ import type { Table } from '../../types/Table'
import type { Row } from '../../types/Row'

export const Rows: TableFeature = {
constructRow: <TFeatures extends TableFeatures, TData extends RowData>(
constructRowAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
row: Row<TFeatures, TData>,
table: Table<TFeatures, TData>,
): void => {
assignAPIs(row, table, [
assignAPIs(row, [
{
fn: () => row_getAllCellsByColumnId(row, table),
fn: () => row_getAllCellsByColumnId(row),
memoDeps: () => [row.getAllCells()],
},
{
fn: () => row_getAllCells(row, table),
fn: () => row_getAllCells(row),
memoDeps: () => [table.getAllLeafColumns()],
},
{
fn: () => row_getLeafRows(row),
},
{
fn: () => row_getParentRow(row, table),
fn: () => row_getParentRow(row),
},
{
fn: () => row_getParentRows(row, table),
fn: () => row_getParentRows(row),
},
{
fn: (columnId) => row_getUniqueValues(row, table, columnId),
fn: (columnId) => row_getUniqueValues(row, columnId),
},
{
fn: (columnId) => row_getValue(row, table, columnId),
fn: (columnId) => row_getValue(row, columnId),
},
{
fn: (columnId) => row_renderValue(row, table, columnId),
fn: (columnId) => row_renderValue(row, columnId),
},
])
},

constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
constructTableAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData>,
): void => {
assignAPIs(table, table, [
assignAPIs(table, [
{
fn: (row, index, parent) => table_getRowId(row, table, index, parent),
},
Expand Down
Loading

0 comments on commit 57602b3

Please sign in to comment.