Skip to content

Commit 1d035e5

Browse files
committed
chore: refactor some static function calls
1 parent 078cbde commit 1d035e5

24 files changed

Lines changed: 213 additions & 170 deletions

File tree

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import {
77
} from '@angular/core'
88
import {
99
FlexRenderDirective,
10-
GroupingState,
11-
Updater,
12-
injectTable,
1310
getCoreRowModel,
1411
getExpandedRowModel,
1512
getFilteredRowModel,
1613
getGroupedRowModel,
1714
getPaginationRowModel,
15+
injectTable,
16+
tableOptions,
1817
} from '@tanstack/angular-table'
1918
import { columns } from './columns'
2019
import { makeData } from './makeData'
20+
import type { GroupingState, Updater } from '@tanstack/angular-table'
2121

2222
@Component({
2323
selector: 'app-root',
@@ -33,26 +33,28 @@ export class AppComponent {
3333

3434
stringifiedGrouping = computed(() => JSON.stringify(this.grouping(), null, 2))
3535

36-
tableOptions = computed(() => ({
37-
data: this.data(),
38-
columns: columns,
39-
state: {
40-
grouping: this.grouping(),
41-
},
42-
onGroupingChange: (updaterOrValue: Updater<GroupingState>) => {
43-
const groupingState =
44-
typeof updaterOrValue === 'function'
45-
? updaterOrValue([...this.grouping()])
46-
: updaterOrValue
47-
this.grouping.set(groupingState)
48-
},
49-
getExpandedRowModel: getExpandedRowModel(),
50-
getGroupedRowModel: getGroupedRowModel(),
51-
getCoreRowModel: getCoreRowModel(),
52-
getPaginationRowModel: getPaginationRowModel(),
53-
getFilteredRowModel: getFilteredRowModel(),
54-
debugTable: true,
55-
}))
36+
tableOptions = computed(() =>
37+
tableOptions({
38+
data: this.data(),
39+
columns: columns,
40+
state: {
41+
grouping: this.grouping(),
42+
},
43+
onGroupingChange: (updaterOrValue: Updater<GroupingState>) => {
44+
const groupingState =
45+
typeof updaterOrValue === 'function'
46+
? updaterOrValue([...this.grouping()])
47+
: updaterOrValue
48+
this.grouping.set(groupingState)
49+
},
50+
getExpandedRowModel: getExpandedRowModel(),
51+
getGroupedRowModel: getGroupedRowModel(),
52+
getCoreRowModel: getCoreRowModel(),
53+
getPaginationRowModel: getPaginationRowModel(),
54+
getFilteredRowModel: getFilteredRowModel(),
55+
debugTable: true,
56+
}),
57+
)
5658

5759
table = injectTable(this.tableOptions)
5860

examples/react/expanding/src/main.tsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
import React, { HTMLProps } from 'react'
1+
import React, { type HTMLProps } from 'react'
22
import ReactDOM from 'react-dom/client'
33

44
import './index.css'
55

66
import {
7-
Column,
8-
Table,
9-
ExpandedState,
10-
useTable,
7+
flexRender,
118
getCoreRowModel,
12-
getPaginationRowModel,
13-
getFilteredRowModel,
149
getExpandedRowModel,
10+
getFilteredRowModel,
11+
getPaginationRowModel,
12+
useTable,
13+
} from '@tanstack/react-table'
14+
import { makeData } from './makeData'
15+
import type { Person } from './makeData'
16+
import type {
17+
Column,
1518
ColumnDef,
16-
flexRender,
19+
ExpandedState,
20+
Table,
1721
} from '@tanstack/react-table'
18-
import { makeData, Person } from './makeData'
1922

2023
function App() {
2124
const rerender = React.useReducer(() => ({}), {})[1]
2225

23-
const columns = React.useMemo<ColumnDef<Person>[]>(
26+
const columns = React.useMemo<Array<ColumnDef<Person>>>(
2427
() => [
2528
{
2629
accessorKey: 'firstName',
@@ -274,22 +277,22 @@ function Filter({
274277
<div className="flex space-x-2">
275278
<input
276279
type="number"
277-
value={(columnFilterValue as [number, number])?.[0] ?? ''}
280+
value={(columnFilterValue as [number, number])[0]}
278281
onChange={(e) =>
279282
column.setFilterValue((old: [number, number]) => [
280283
e.target.value,
281-
old?.[1],
284+
old[1],
282285
])
283286
}
284287
placeholder={`Min`}
285288
className="w-24 border shadow rounded"
286289
/>
287290
<input
288291
type="number"
289-
value={(columnFilterValue as [number, number])?.[1] ?? ''}
292+
value={(columnFilterValue as [number, number])[1]}
290293
onChange={(e) =>
291294
column.setFilterValue((old: [number, number]) => [
292-
old?.[0],
295+
old[0],
293296
e.target.value,
294297
])
295298
}

examples/svelte/column-groups/src/App.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
createTable,
55
FlexRender,
66
getCoreRowModel,
7+
tableOptions,
78
} from '@tanstack/svelte-table'
89
import './index.css'
910
@@ -95,11 +96,11 @@
9596
},
9697
]
9798
98-
const options = {
99+
const options = tableOptions({
99100
data: defaultData,
100101
columns: defaultColumns,
101102
getCoreRowModel: getCoreRowModel(),
102-
}
103+
})
103104
104105
const table = createTable(options)
105106
</script>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { flattenBy } from '../../utils'
22
import { _createCell } from '../cells/_createCell'
3+
import { table_getColumn } from '../columns/Columns.utils'
34
import type { Cell, Column, Row, RowData, Table } from '../../types'
45

56
export function row_getValue<TData extends RowData>(
@@ -11,7 +12,7 @@ export function row_getValue<TData extends RowData>(
1112
return row._valuesCache[columnId]
1213
}
1314

14-
const column = table.getColumn(columnId)
15+
const column = table_getColumn(table, columnId)
1516

1617
if (!column?.accessorFn) {
1718
return undefined
@@ -31,7 +32,7 @@ export function row_getUniqueValues<TData extends RowData>(
3132
return row._uniqueValuesCache[columnId]
3233
}
3334

34-
const column = table.getColumn(columnId)
35+
const column = table_getColumn(table, columnId)
3536

3637
if (!column?.accessorFn) {
3738
return undefined

packages/table-core/src/features/column-faceting/ColumnFaceting.utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { table_getPreFilteredRowModel } from '../column-filtering/ColumnFiltering.utils'
12
import type { CellData, Column, RowData, RowModel, Table } from '../../types'
23
import type { TableOptions_ColumnFaceting } from './ColumnFaceting.types'
34

@@ -27,7 +28,7 @@ export function column_getFacetedRowModel<
2728
): () => RowModel<TData> {
2829
return (
2930
table.options.getFacetedRowModel?.(table, column.id) ??
30-
(() => table.getPreFilteredRowModel()) // TODO - reference static function
31+
(() => table_getPreFilteredRowModel(table))
3132
)
3233
}
3334

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { filterFns } from '../../fns/filterFns'
22
import { functionalUpdate, isFunction } from '../../utils'
3+
import { table_getCoreRowModel } from '../../core/row-models/RowModels.utils'
34
import type { BuiltInFilterFn } from '../../fns/filterFns'
4-
import type { Column, RowData, Table, Updater } from '../../types'
5+
import type { Column, RowData, RowModel, Table, Updater } from '../../types'
56
import type { ColumnFiltersState, FilterFn } from './ColumnFiltering.types'
67

78
export function column_getAutoFilterFn<TData extends RowData>(
89
column: Column<TData, unknown>,
910
table: Table<TData>,
1011
) {
11-
const firstRow = table.getCoreRowModel().flatRows[0]
12+
const firstRow = table_getCoreRowModel(table).flatRows[0]
1213

1314
const value = firstRow?.getValue(column.id)
1415

@@ -84,7 +85,7 @@ export function column_setFilterValue<TData extends RowData>(
8485
table: Table<TData>,
8586
value: any,
8687
) {
87-
table.setColumnFilters((old) => {
88+
table_setColumnFilters(table, (old) => {
8889
const filterFn = column.getFilterFn()
8990
const previousFilter = old.find((d) => d.id === column.id)
9091

@@ -147,24 +148,27 @@ export function table_resetColumnFilters<TData extends RowData>(
147148
table: Table<TData>,
148149
defaultState?: boolean,
149150
) {
150-
table.setColumnFilters(defaultState ? [] : table.initialState.columnFilters)
151+
table_setColumnFilters(
152+
table,
153+
defaultState ? [] : table.initialState.columnFilters,
154+
)
151155
}
152156

153157
export function table_getPreFilteredRowModel<TData extends RowData>(
154158
table: Table<TData>,
155159
) {
156-
return table.getCoreRowModel()
160+
return table_getCoreRowModel(table)
157161
}
158162

159163
export function table_getFilteredRowModel<TData extends RowData>(
160164
table: Table<TData>,
161-
) {
165+
): RowModel<TData> {
162166
if (!table._getFilteredRowModel && table.options.getFilteredRowModel) {
163167
table._getFilteredRowModel = table.options.getFilteredRowModel(table)
164168
}
165169

166170
if (table.options.manualFiltering || !table._getFilteredRowModel) {
167-
return table.getPreFilteredRowModel()
171+
return table_getPreFilteredRowModel(table)
168172
}
169173

170174
return table._getFilteredRowModel()

packages/table-core/src/features/column-grouping/ColumnGrouping.utils.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { aggregationFns } from '../../fns/aggregationFns'
22
import { isFunction } from '../../utils'
3+
import { table_getFilteredRowModel } from '../column-filtering/ColumnFiltering.utils'
34
import type { BuiltInAggregationFn } from '../../fns/aggregationFns'
4-
import type { Cell, Column, Row, RowData, Table, Updater } from '../../types'
5+
import type {
6+
Cell,
7+
Column,
8+
Row,
9+
RowData,
10+
RowModel,
11+
Table,
12+
Updater,
13+
} from '../../types'
514
import type { GroupingState } from './ColumnGrouping.types'
615

716
export function column_toggleGrouping<TData extends RowData, TValue>(
817
column: Column<TData, TValue>,
918
table: Table<TData>,
1019
) {
11-
table.setGrouping((old) => {
20+
table_setGrouping(table, (old) => {
1221
// Find any existing grouping for this column
1322
if (old.includes(column.id)) {
1423
return old.filter((d) => d !== column.id)
@@ -96,24 +105,24 @@ export function table_resetGrouping<TData extends RowData>(
96105
table: Table<TData>,
97106
defaultState?: boolean,
98107
) {
99-
table.setGrouping(defaultState ? [] : table.initialState.grouping)
108+
table_setGrouping(table, defaultState ? [] : table.initialState.grouping)
100109
}
101110

102111
export function table_getPreGroupedRowModel<TData extends RowData>(
103112
table: Table<TData>,
104-
) {
105-
return table.getFilteredRowModel()
113+
): RowModel<TData> {
114+
return table_getFilteredRowModel(table)
106115
}
107116

108117
export function table_getGroupedRowModel<TData extends RowData>(
109118
table: Table<TData>,
110-
) {
119+
): RowModel<TData> {
111120
if (!table._getGroupedRowModel && table.options.getGroupedRowModel) {
112121
table._getGroupedRowModel = table.options.getGroupedRowModel(table)
113122
}
114123

115124
if (table.options.manualGrouping || !table._getGroupedRowModel) {
116-
return table.getPreGroupedRowModel()
125+
return table_getPreGroupedRowModel(table)
117126
}
118127

119128
return table._getGroupedRowModel()

packages/table-core/src/features/column-ordering/ColumnOrdering.utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ export function table_resetColumnOrder<TData extends RowData>(
4343
table: Table<TData>,
4444
defaultState?: boolean,
4545
) {
46-
table.setColumnOrder(defaultState ? [] : table.initialState.columnOrder)
46+
table_setColumnOrder(
47+
table,
48+
defaultState ? [] : table.initialState.columnOrder,
49+
)
4750
}
4851

4952
export function table_getOrderColumnsFn<TData extends RowData>(

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function column_pin<TData extends RowData, TValue>(
1414
.map((d) => d.id)
1515
.filter(Boolean)
1616

17-
table.setColumnPinning((old) => {
17+
table_setColumnPinning(table, (old) => {
1818
if (position === 'right') {
1919
return {
2020
left: (old.left ?? []).filter((d) => !columnIds.includes(d)),
@@ -128,7 +128,8 @@ export function table_resetColumnPinning<TData extends RowData>(
128128
table: Table<TData>,
129129
defaultState?: boolean,
130130
) {
131-
table.setColumnPinning(
131+
table_setColumnPinning(
132+
table,
132133
defaultState
133134
? getDefaultColumnPinningState()
134135
: table.initialState.columnPinning,

packages/table-core/src/features/column-resizing/ColumnResizing.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import type {
1616
export const ColumnResizing: TableFeature = {
1717
_getInitialState: (state): TableState_ColumnResizing => {
1818
return {
19-
columnSizing: {},
2019
columnSizingInfo: getDefaultColumnSizingInfoState(),
2120
...state,
2221
}

0 commit comments

Comments
 (0)