Skip to content

Commit

Permalink
make all util functions use table internal type
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Sep 29, 2024
1 parent 6be9060 commit 38e49f2
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 726 deletions.
3 changes: 2 additions & 1 deletion examples/react/basic-table-helper/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react'
import ReactDOM from 'react-dom/client'
import { createTableHelper, flexRender } from '@tanstack/react-table'
import type { ColumnDef } from '@tanstack/react-table'
import './index.css'

// This example uses the new `createTableHelper` method to create a re-usable table helper object instead of independently using the standalone `useTable` hook and `createColumnHelper` method. You can choose to use either way.
Expand Down Expand Up @@ -94,7 +95,7 @@ const columns = [
header: 'Profile Progress',
footer: (info) => info.column.id,
}),
]
] as Array<ColumnDef<typeof tableHelper.features, Person, unknown>>

function App() {
// 6. Store data with a stable reference
Expand Down
2 changes: 0 additions & 2 deletions examples/react/basic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ function App() {
// add additional table options here
})

console.log('table', table)

// 7. Render your table markup from the table instance APIs
return (
<div className="p-2">
Expand Down
49 changes: 25 additions & 24 deletions packages/react-table/src/tableHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { constructTableHelper } from '@tanstack/table-core'
import { useTable } from './useTable'
import type {
ColumnDef,
RowData,
Table,
TableFeatures,
Expand Down Expand Up @@ -36,27 +37,27 @@ export function createTableHelper<

// test

// type Person = {
// firstName: string
// lastName: string
// age: number
// }

// const tableHelper = createTableHelper({
// _features: { RowSelection: {} },
// TData: {} as Person,
// })

// const columns = [
// tableHelper.columnHelper.accessor('firstName', { header: 'First Name' }),
// tableHelper.columnHelper.accessor('lastName', { header: 'Last Name' }),
// tableHelper.columnHelper.accessor('age', { header: 'Age' }),
// tableHelper.columnHelper.display({ header: 'Actions', id: 'actions' }),
// ]

// const data: Array<Person> = []

// tableHelper.useTable({
// columns,
// data,
// })
type Person = {
firstName: string
lastName: string
age: number
}

const tableHelper = createTableHelper({
_features: { RowSelection: {} },
TData: {} as Person,
})

const columns = [
tableHelper.columnHelper.accessor('firstName', { header: 'First Name' }),
tableHelper.columnHelper.accessor('lastName', { header: 'Last Name' }),
tableHelper.columnHelper.accessor('age', { header: 'Age' }),
tableHelper.columnHelper.display({ header: 'Actions', id: 'actions' }),
] as Array<ColumnDef<typeof tableHelper.features, Person, unknown>>

const data: Array<Person> = []

tableHelper.useTable({
columns,
data,
})
21 changes: 12 additions & 9 deletions packages/table-core/src/core/table/Tables.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import { createCoreRowModel } from './createCoreRowModel'
import type { RowData, Updater } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../types/RowModel'
import type { Table } from '../../types/Table'
import type { Table_Internal } from '../../types/Table'
import type { TableOptions } from '../../types/TableOptions'
import type { TableState } from '../../types/TableState'

export function table_reset<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): void {
>(table: Table_Internal<TFeatures, TData>): void {
table_setState(table, table.initialState)
}

export function table_mergeOptions<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>, newOptions: TableOptions<TFeatures, TData>) {
>(
table: Table_Internal<TFeatures, TData>,
newOptions: TableOptions<TFeatures, TData>,
) {
if (table.options.mergeOptions) {
return table.options.mergeOptions(table.options, newOptions)
}
Expand All @@ -33,7 +36,7 @@ export function table_setOptions<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
updater: Updater<TableOptions<TFeatures, TData>>,
): void {
const newOptions = functionalUpdate(updater, table.options)
Expand All @@ -43,22 +46,22 @@ export function table_setOptions<
export function table_getInitialState<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): TableState<TFeatures> {
>(table: Table_Internal<TFeatures, TData>): TableState<TFeatures> {
return structuredClone(table.initialState)
}

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

export function table_setState<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
updater: Updater<TableState<TFeatures>>,
): void {
table.options.onStateChange?.(updater)
Expand All @@ -67,7 +70,7 @@ export function table_setState<
export function table_getCoreRowModel<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): RowModel<TFeatures, TData> {
>(table: Table_Internal<TFeatures, TData>): RowModel<TFeatures, TData> {
if (!table._rowModels.Core) {
table._rowModels.Core =
table.options._rowModels?.Core?.(table) ??
Expand All @@ -80,6 +83,6 @@ export function table_getCoreRowModel<
export function table_getRowModel<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): RowModel<TFeatures, TData> {
>(table: Table_Internal<TFeatures, TData>): RowModel<TFeatures, TData> {
return table_getPaginatedRowModel(table)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { table_getPreFilteredRowModel } from '../column-filtering/ColumnFilterin
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../types/RowModel'
import type { Table } from '../../types/Table'
import type { Table_Internal } from '../../types/Table'
import type { Column } from '../../types/Column'

export function column_getFacetedMinMaxValues<
Expand All @@ -11,7 +11,7 @@ export function column_getFacetedMinMaxValues<
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
): () => [number, number] | undefined {
return (
table.options._rowModels?.FacetedMinMax?.(table, column.id) ??
Expand All @@ -25,7 +25,7 @@ export function column_getFacetedRowModel<
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> | undefined,
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
): () => RowModel<TFeatures, TData> {
return (
table.options._rowModels?.Faceted?.(table, column?.id ?? '') ??
Expand All @@ -39,7 +39,7 @@ export function column_getFacetedUniqueValues<
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
): () => Map<any, number> {
return (
table.options._rowModels?.FacetedUnique?.(table, column.id) ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ import type { BuiltInFilterFn } from '../../fns/filterFns'
import type { CellData, RowData, Updater } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../types/RowModel'
import type { Table } from '../../types/Table'
import type { Table_Internal } from '../../types/Table'
import type { Column } from '../../types/Column'
import type {
ColumnDef_ColumnFiltering,
ColumnFiltersState,
FilterFn,
TableOptions_ColumnFiltering,
} from './ColumnFiltering.types'

export function column_getAutoFilterFn<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
>(
column: Column<TFeatures, TData, TValue>,
table: Table_Internal<TFeatures, TData>,
) {
const firstRow = table_getCoreRowModel(table).flatRows[0]

const value = firstRow ? row_getValue(firstRow, table, column.id) : undefined
Expand Down Expand Up @@ -58,9 +60,7 @@ export function column_getFilterFn<
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
table: Table_Internal<TFeatures, TData>,
): FilterFn<TFeatures, TData> | undefined {
return isFunction(column.columnDef.filterFn)
? column.columnDef.filterFn
Expand All @@ -78,9 +78,7 @@ export function column_getCanFilter<
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
table: Table_Internal<TFeatures, TData>,
) {
return (
(column.columnDef.enableColumnFilter ?? true) &&
Expand All @@ -98,9 +96,7 @@ export function column_getIsFiltered<
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
table: Table_Internal<TFeatures, TData>,
) {
return column_getFilterIndex(column, table) > -1
}
Expand All @@ -113,9 +109,7 @@ export function column_getFilterValue<
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
table: Table_Internal<TFeatures, TData>,
) {
return table_getState(table).columnFilters?.find((d) => d.id === column.id)
?.value
Expand All @@ -129,9 +123,7 @@ export function column_getFilterIndex<
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
table: Table_Internal<TFeatures, TData>,
): number {
return (
table_getState(table).columnFilters?.findIndex((d) => d.id === column.id) ??
Expand All @@ -147,9 +139,7 @@ export function column_setFilterValue<
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
table: Table_Internal<TFeatures, TData>,
value: any,
) {
table_setColumnFilters(table, (old) => {
Expand Down Expand Up @@ -188,9 +178,7 @@ export function table_setColumnFilters<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
table: Table_Internal<TFeatures, TData>,
updater: Updater<ColumnFiltersState>,
) {
const leafColumns = table.getAllLeafColumns()
Expand All @@ -217,12 +205,7 @@ export function table_setColumnFilters<
export function table_resetColumnFilters<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
defaultState?: boolean,
) {
>(table: Table_Internal<TFeatures, TData>, defaultState?: boolean) {
table_setColumnFilters(
table,
defaultState ? [] : table.initialState.columnFilters ?? [],
Expand All @@ -232,22 +215,14 @@ export function table_resetColumnFilters<
export function table_getPreFilteredRowModel<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
) {
>(table: Table_Internal<TFeatures, TData>) {
return table_getCoreRowModel(table)
}

export function table_getFilteredRowModel<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
): RowModel<TFeatures, TData> {
>(table: Table_Internal<TFeatures, TData>): RowModel<TFeatures, TData> {
if (!table._rowModels.Filtered) {
table._rowModels.Filtered = table.options._rowModels?.Filtered?.(table)
}
Expand Down
Loading

0 comments on commit 38e49f2

Please sign in to comment.