Skip to content

Commit 57602b3

Browse files
committed
simplify api assignment
1 parent b015b47 commit 57602b3

File tree

38 files changed

+250
-160
lines changed

38 files changed

+250
-160
lines changed

packages/table-core/src/core/cells/Cells.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import { assignAPIs } from '../../utils'
22
import { cell_getContext, cell_getValue, cell_renderValue } from './Cells.utils'
3-
import type { Table_Internal } from '../../types/Table'
43
import type { CellData, RowData } from '../../types/type-utils'
54
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
65
import type { Cell } from '../../types/Cell'
76

87
export const Cells: TableFeature = {
9-
constructCell: <
8+
constructCellAPIs: <
109
TFeatures extends TableFeatures,
1110
TData extends RowData,
1211
TValue extends CellData = CellData,
1312
>(
1413
cell: Cell<TFeatures, TData, TValue>,
15-
table: Table_Internal<TFeatures, TData>,
1614
) => {
17-
assignAPIs(cell, table, [
15+
assignAPIs(cell, [
1816
{
1917
fn: () => cell_getValue(cell),
2018
},
2119
{
22-
fn: () => cell_renderValue(cell, table),
20+
fn: () => cell_renderValue(cell),
2321
},
2422
{
25-
fn: () => cell_getContext(cell, table),
26-
memoDeps: () => [cell, table],
23+
fn: () => cell_getContext(cell),
24+
memoDeps: () => [cell],
2725
},
2826
])
2927
},

packages/table-core/src/core/cells/Cells.types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CellData, Getter, RowData } from '../../types/type-utils'
22
import type { TableFeatures } from '../../types/TableFeatures'
3-
import type { Table } from '../../types/Table'
3+
import type { Table, Table_Internal } from '../../types/Table'
44
import type { Row } from '../../types/Row'
55
import type { Cell } from '../../types/Cell'
66
import type { Column } from '../../types/Column'
@@ -41,6 +41,10 @@ export interface Cell_CoreProperties<
4141
* @link [Guide](https://tanstack.com/table/v8/docs/guide/cells)
4242
*/
4343
row: Row<TFeatures, TData>
44+
/**
45+
* @deprecated Reference to the table instance.
46+
*/
47+
table: Table_Internal<TFeatures, TData>
4448
}
4549

4650
export interface Cell_Cell<

packages/table-core/src/core/cells/Cells.utils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1+
import { callMemoOrStaticFn } from '../../utils'
2+
import { row_getValue } from '../rows/Rows.utils'
13
import type { CellData, RowData } from '../../types/type-utils'
24
import type { TableFeatures } from '../../types/TableFeatures'
3-
import type { Table } from '../../types/Table'
45
import type { Cell } from '../../types/Cell'
56

67
export function cell_getValue<
78
TFeatures extends TableFeatures,
89
TData extends RowData,
910
TValue extends CellData = CellData,
1011
>(cell: Cell<TFeatures, TData, TValue>): TValue {
11-
return cell.row.getValue(cell.column.id)
12+
return callMemoOrStaticFn(cell.row, row_getValue, [cell.column.id])
1213
}
1314

1415
export function cell_renderValue<
1516
TFeatures extends TableFeatures,
1617
TData extends RowData,
1718
TValue extends CellData = CellData,
18-
>(cell: Cell<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
19-
return cell.getValue() ?? table.options.renderFallbackValue
19+
>(cell: Cell<TFeatures, TData, TValue>) {
20+
return cell.getValue() ?? cell.table.options.renderFallbackValue
2021
}
2122

2223
export function cell_getContext<
2324
TFeatures extends TableFeatures,
2425
TData extends RowData,
2526
TValue extends CellData = CellData,
26-
>(cell: Cell<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
27+
>(cell: Cell<TFeatures, TData, TValue>) {
2728
return {
28-
table,
29+
table: cell.table,
2930
column: cell.column,
3031
row: cell.row,
3132
cell: cell,

packages/table-core/src/core/cells/constructCell.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CellData, RowData } from '../../types/type-utils'
2-
import type { TableFeatures } from '../../types/TableFeatures'
2+
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
33
import type { Table } from '../../types/Table'
44
import type { Row } from '../../types/Row'
55
import type { Cell } from '../../types/Cell'
@@ -19,10 +19,11 @@ export function constructCell<
1919
column,
2020
id: `${row.id}_${column.id}`,
2121
row,
22+
table,
2223
}
2324

24-
for (const feature of Object.values(table._features)) {
25-
feature?.constructCell?.(cell as Cell<TFeatures, TData, TValue>, table)
25+
for (const feature of Object.values(table._features) as Array<TableFeature>) {
26+
feature.constructCellAPIs?.(cell as Cell<TFeatures, TData, TValue>, table)
2627
}
2728

2829
return cell as Cell<TFeatures, TData, TValue>

packages/table-core/src/core/columns/Columns.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ import type { Table_Internal } from '../../types/Table'
1515
import type { Column } from '../../types/Column'
1616

1717
export const Columns: TableFeature = {
18-
constructColumn: <
18+
constructColumnAPIs: <
1919
TFeatures extends TableFeatures,
2020
TData extends RowData,
2121
TValue extends CellData = CellData,
2222
>(
2323
column: Column<TFeatures, TData, TValue>,
2424
table: Table_Internal<TFeatures, TData>,
2525
) => {
26-
assignAPIs(column, table, [
26+
assignAPIs(column, [
2727
{
2828
fn: () => column_getFlatColumns(column),
2929
memoDeps: () => [table.options.columns],
3030
},
3131
{
32-
fn: () => column_getLeafColumns(column, table),
32+
fn: () => column_getLeafColumns(column),
3333
memoDeps: () => [
3434
table.getState().columnOrder,
3535
table.getState().grouping,
@@ -40,10 +40,10 @@ export const Columns: TableFeature = {
4040
])
4141
},
4242

43-
constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
43+
constructTableAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
4444
table: Table_Internal<TFeatures, TData>,
4545
) => {
46-
assignAPIs(table, table, [
46+
assignAPIs(table, [
4747
{
4848
fn: () => table_getDefaultColumnDef(table),
4949
memoDeps: () => [table.options.defaultColumn],

packages/table-core/src/core/columns/Columns.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Table_Internal } from '../../types/Table'
12
import type { CellData, RowData } from '../../types/type-utils'
23
import type { TableFeatures } from '../../types/TableFeatures'
34
import type { AccessorFn, ColumnDef } from '../../types/ColumnDef'
@@ -47,6 +48,10 @@ export interface Column_CoreProperties<
4748
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-defs)
4849
*/
4950
parent?: Column<TFeatures, TData, TValue>
51+
/**
52+
* @deprecated Reference to the table instance.
53+
*/
54+
table: Table_Internal<TFeatures, TData>
5055
}
5156

5257
export interface Column_Column<

packages/table-core/src/core/columns/Columns.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ export function column_getLeafColumns<
2626
TValue extends CellData = CellData,
2727
>(
2828
column: Column<TFeatures, TData, TValue>,
29-
table: Table<TFeatures, TData>,
3029
): Array<Column<TFeatures, TData, TValue>> {
3130
if (column.columns.length) {
3231
const leafColumns = column.columns.flatMap(
3332
(col) => col.getLeafColumns(), // recursive
3433
)
3534

36-
return table_getOrderColumnsFn(table)(leafColumns) as any
35+
return table_getOrderColumnsFn(column.table)(leafColumns as any) as any
3736
}
3837

3938
return [column]
@@ -144,3 +143,4 @@ export function table_getColumn<
144143

145144
return column
146145
}
146+

packages/table-core/src/core/columns/constructColumn.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CellData, RowData } from '../../types/type-utils'
2-
import type { TableFeatures } from '../../types/TableFeatures'
2+
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
33
import type { Table } from '../../types/Table'
44
import type {
55
AccessorFn,
@@ -74,16 +74,17 @@ export function constructColumn<
7474
}
7575

7676
const column: Column_CoreProperties<TFeatures, TData, TValue> = {
77-
id: `${String(id)}`,
7877
accessorFn,
79-
parent: parent,
80-
depth,
8178
columnDef: resolvedColumnDef as ColumnDef<TFeatures, TData, TValue>,
8279
columns: [],
80+
depth,
81+
id: `${String(id)}`,
82+
parent: parent,
83+
table,
8384
}
8485

85-
for (const feature of Object.values(table._features)) {
86-
feature?.constructColumn?.(
86+
for (const feature of Object.values(table._features) as Array<TableFeature>) {
87+
feature.constructColumnAPIs?.(
8788
column as Column<TFeatures, TData, TValue>,
8889
table,
8990
)

packages/table-core/src/core/headers/Headers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ import type { Table, Table_Internal } from '../../types/Table'
1818
import type { Header } from '../../types/Header'
1919

2020
export const Headers: TableFeature = {
21-
constructHeader: <
21+
constructHeaderAPIs: <
2222
TFeatures extends TableFeatures,
2323
TData extends RowData,
2424
TValue extends CellData = CellData,
2525
>(
2626
header: Header<TFeatures, TData, TValue>,
2727
table: Table<TFeatures, TData>,
2828
): void => {
29-
assignAPIs(header, table, [
29+
assignAPIs(header, [
3030
{
3131
fn: () => header_getLeafHeaders(header),
3232
memoDeps: () => [table.options.columns],
3333
},
3434
{
35-
fn: () => header_getContext(header, table),
35+
fn: () => header_getContext(header),
3636
memoDeps: () => [table.options.columns],
3737
},
3838
])
3939
},
4040

41-
constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
41+
constructTableAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
4242
table: Table_Internal<TFeatures, TData>,
4343
): void => {
44-
assignAPIs(table, table, [
44+
assignAPIs(table, [
4545
{
4646
fn: () => table_getHeaderGroups(table),
4747
memoDeps: () => [

packages/table-core/src/core/headers/Headers.types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CellData, RowData } from '../../types/type-utils'
22
import type { TableFeatures } from '../../types/TableFeatures'
3-
import type { Table } from '../../types/Table'
3+
import type { Table, Table_Internal } from '../../types/Table'
44
import type { Header } from '../../types/Header'
55
import type { HeaderGroup } from '../../types/HeaderGroup'
66
import type { Column } from '../../types/Column'
@@ -128,6 +128,10 @@ export interface Header_CoreProperties<
128128
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
129129
*/
130130
subHeaders: Array<Header<TFeatures, TData, TValue>>
131+
/**
132+
* @deprecated Reference to the table instance.
133+
*/
134+
table: Table_Internal<TFeatures, TData>
131135
}
132136

133137
export interface Header_Header<

0 commit comments

Comments
 (0)