Skip to content

Commit ed66ab9

Browse files
committed
chore: refactor to more static functions in core
1 parent 69ffb37 commit ed66ab9

27 files changed

Lines changed: 175 additions & 113 deletions

File tree

examples/angular/column-pinning-sticky/src/app/app.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import {
44
computed,
55
signal,
66
} from '@angular/core'
7-
import type { Column, ColumnDef } from '@tanstack/angular-table'
87
import {
8+
type Column,
9+
type ColumnDef,
910
type ColumnOrderState,
1011
type ColumnPinningState,
1112
type ColumnVisibilityState,

examples/angular/filters/src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
computed,
55
signal,
66
} from '@angular/core'
7-
import type { ColumnDef } from '@tanstack/angular-table'
87
import {
8+
type ColumnDef,
99
type ColumnFiltersState,
1010
FlexRenderDirective,
1111
getCoreRowModel,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { column_getIsVisible } from '../../features/column-visibility/ColumnVisibility.utils'
12
import { _createHeader } from './_createHeader'
23
import type { Column, Header, HeaderGroup, RowData, Table } from '../../types'
34

@@ -122,7 +123,7 @@ export function buildHeaderGroups<TData extends RowData>(
122123
headers: Array<Header<TData, unknown>>,
123124
): Array<{ colSpan: number; rowSpan: number }> => {
124125
const filteredHeaders = headers.filter((header) =>
125-
header.column.getIsVisible(),
126+
column_getIsVisible(header.column, table),
126127
)
127128

128129
return filteredHeaders.map((header) => {

packages/table-core/src/core/rows/Rows.utils.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { flattenBy } from '../../utils'
22
import { _createCell } from '../cells/_createCell'
33
import { table_getColumn } from '../columns/Columns.utils'
4+
import { table_getPrePaginationRowModel } from '../../features/row-pagination/RowPagination.utils'
5+
import {
6+
table_getCoreRowModel,
7+
table_getRowModel,
8+
} from '../row-models/RowModels.utils'
49
import type { Cell, Column, Row, RowData, Table } from '../../types'
510

611
export function row_getValue<TData extends RowData>(
@@ -39,7 +44,7 @@ export function row_getUniqueValues<TData extends RowData>(
3944
}
4045

4146
if (!column.columnDef.getUniqueValues) {
42-
row._uniqueValuesCache[columnId] = [row.getValue(columnId)]
47+
row._uniqueValuesCache[columnId] = [row_getValue(row, table, columnId)]
4348
return row._uniqueValuesCache[columnId]
4449
}
4550

@@ -69,7 +74,7 @@ export function row_getParentRow<TData extends RowData>(
6974
row: Row<TData>,
7075
table: Table<TData>,
7176
) {
72-
return row.parentId ? table.getRow(row.parentId, true) : undefined
77+
return row.parentId ? table_getRow(table, row.parentId, true) : undefined
7378
}
7479

7580
export function row_getParentRows<TData extends RowData>(
@@ -126,12 +131,13 @@ export function table_getRow<TData extends RowData>(
126131
table: Table<TData>,
127132
rowId: string,
128133
searchAll?: boolean,
129-
) {
130-
let row = (searchAll ? table.getPrePaginationRowModel() : table.getRowModel())
131-
.rowsById[rowId]
134+
): Row<TData> {
135+
let row = (
136+
searchAll ? table_getPrePaginationRowModel(table) : table_getRowModel(table)
137+
).rowsById[rowId]
132138

133139
if (!row) {
134-
row = table.getCoreRowModel().rowsById[rowId]
140+
row = table_getCoreRowModel(table).rowsById[rowId]
135141
if (!row) {
136142
if (process.env.NODE_ENV !== 'production') {
137143
throw new Error(`getRow could not find row with ID: ${rowId}`)

packages/table-core/src/features/column-faceting/getFacetedMinMaxValues.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getMemoOptions, memo } from '../../utils'
2+
import { row_getUniqueValues } from '../../core/rows/Rows.utils'
23
import type { RowData, Table } from '../../types'
34

45
export function getFacetedMinMaxValues<TData extends RowData>(): (
@@ -21,7 +22,7 @@ export function getFacetedMinMaxValues<TData extends RowData>(): (
2122
const facetedMinMaxValues: [any, any] = [firstValue, firstValue]
2223

2324
for (const row of facetedRowModel.flatRows) {
24-
const values = row.getUniqueValues<number>(columnId)
25+
const values = row_getUniqueValues(row, table, columnId)
2526

2627
for (const value of values) {
2728
if (value < facetedMinMaxValues[0]) {

packages/table-core/src/features/column-faceting/getFacetedUniqueValues.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getMemoOptions, memo } from '../../utils'
2-
import type { RowData, Table } from '../../types'
2+
import { row_getUniqueValues } from '../../core/rows/Rows.utils'
3+
import type { Row, RowData, Table } from '../../types'
34

45
export function getFacetedUniqueValues<TData extends RowData>(): (
56
table: Table<TData>,
@@ -14,7 +15,7 @@ export function getFacetedUniqueValues<TData extends RowData>(): (
1415
const facetedUniqueValues = new Map<any, number>()
1516

1617
for (const row of facetedRowModel.flatRows) {
17-
const values = row.getUniqueValues<number>(columnId)
18+
const values = row_getUniqueValues(row, table, columnId)
1819

1920
for (const value of values) {
2021
if (facetedUniqueValues.has(value)) {

packages/table-core/src/features/column-filtering/ColumnFiltering.utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { filterFns } from '../../fns/filterFns'
22
import { functionalUpdate, isFunction } from '../../utils'
33
import { table_getCoreRowModel } from '../../core/row-models/RowModels.utils'
4+
import { row_getValue } from '../../core/rows/Rows.utils'
45
import type { BuiltInFilterFn } from '../../fns/filterFns'
56
import type { Column, RowData, RowModel, Table, Updater } from '../../types'
67
import type { ColumnFiltersState, FilterFn } from './ColumnFiltering.types'
@@ -11,7 +12,7 @@ export function column_getAutoFilterFn<TData extends RowData>(
1112
) {
1213
const firstRow = table_getCoreRowModel(table).flatRows[0]
1314

14-
const value = firstRow?.getValue(column.id)
15+
const value = firstRow ? row_getValue(firstRow, table, column.id) : undefined
1516

1617
if (typeof value === 'string') {
1718
return filterFns.includesString

packages/table-core/src/features/column-filtering/getFilteredRowModel.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { getMemoOptions, memo } from '../../utils'
2+
import { table_getColumn } from '../../core/columns/Columns.utils'
3+
import { table_getGlobalFilterFn } from '../global-filtering/GlobalFiltering.utils'
24
import { filterRows } from './filterRowsUtils'
5+
import { column_getFilterFn } from './ColumnFiltering.utils'
36
import type { Row, RowData, RowModel, Table } from '../../types'
47
import type { ResolvedColumnFilter } from './ColumnFiltering.types'
58

@@ -26,13 +29,13 @@ export function getFilteredRowModel<TData extends RowData>(): (
2629
const resolvedGlobalFilters: Array<ResolvedColumnFilter<TData>> = []
2730

2831
columnFilters.forEach((d) => {
29-
const column = table.getColumn(d.id)
32+
const column = table_getColumn(table, d.id)
3033

3134
if (!column) {
3235
return
3336
}
3437

35-
const filterFn = column.getFilterFn()
38+
const filterFn = column_getFilterFn(column, table)
3639

3740
if (!filterFn) {
3841
if (process.env.NODE_ENV !== 'production') {
@@ -52,7 +55,7 @@ export function getFilteredRowModel<TData extends RowData>(): (
5255

5356
const filterableIds = columnFilters.map((d) => d.id)
5457

55-
const globalFilterFn = table.getGlobalFilterFn()
58+
const globalFilterFn = table_getGlobalFilterFn(table)
5659

5760
const globallyFilterableColumns = table
5861
.getAllLeafColumns()

packages/table-core/src/features/column-grouping/getGroupedRowModel.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { _createRow } from '../../core/rows/_createRow'
22
import { flattenBy, getMemoOptions, memo } from '../../utils'
3+
import { table_getColumn } from '../../core/columns/Columns.utils'
4+
import { row_getGroupingValue } from './ColumnGrouping.utils'
35
import type { Row, RowData, RowModel, Table } from '../../types'
46

57
export function getGroupedRowModel<TData extends RowData>(): (
@@ -15,7 +17,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
1517

1618
// Filter the grouping list down to columns that exist
1719
const existingGrouping = grouping.filter((columnId) =>
18-
table.getColumn(columnId),
20+
table_getColumn(table, columnId),
1921
)
2022

2123
const groupedFlatRows: Array<Row<TData>> = []
@@ -52,7 +54,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
5254
const columnId: string = existingGrouping[depth]!
5355

5456
// Group the rows together for this level
55-
const rowGroupsMap = groupBy(rows, columnId)
57+
const rowGroupsMap = groupBy(rows, columnId, table)
5658

5759
// Peform aggregations for each group
5860
const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map(
@@ -169,11 +171,12 @@ export function getGroupedRowModel<TData extends RowData>(): (
169171
function groupBy<TData extends RowData>(
170172
rows: Array<Row<TData>>,
171173
columnId: string,
174+
table: Table<TData>,
172175
) {
173176
const groupMap = new Map<any, Array<Row<TData>>>()
174177

175178
return rows.reduce((map, row) => {
176-
const resKey = `${row.getGroupingValue(columnId)}`
179+
const resKey = `${row_getGroupingValue(row, table, columnId)}`
177180
const previous = map.get(resKey)
178181
if (!previous) {
179182
map.set(resKey, [row])

packages/table-core/src/features/column-pinning/ColumnPinning.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { buildHeaderGroups } from '../../core/headers/buildHeaderGroups'
22
import { getMemoOptions, makeStateUpdater, memo } from '../../utils'
33
import {
44
column_getCanPin,
5+
column_getIsPinned,
56
column_getPinnedIndex,
67
column_pin,
78
getDefaultColumnPinningState,
@@ -48,6 +49,8 @@ export const ColumnPinning: TableFeature = {
4849
column.getCanPin = () => column_getCanPin(column, table)
4950

5051
column.getPinnedIndex = () => column_getPinnedIndex(column, table)
52+
53+
column.getIsPinned = () => column_getIsPinned(column, table)
5154
},
5255

5356
_createRow: <TData extends RowData>(

0 commit comments

Comments
 (0)