Skip to content

Commit

Permalink
progress on using static functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Aug 15, 2024
1 parent 4316af3 commit c578c2b
Show file tree
Hide file tree
Showing 38 changed files with 762 additions and 433 deletions.
6 changes: 5 additions & 1 deletion examples/react/column-pinning/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { makeData } from './makeData'
import type {
ColumnDef,
ColumnOrderState,
ColumnPinningState,
ColumnVisibilityState,
} from '@tanstack/react-table'
import type { Person } from './makeData'
Expand Down Expand Up @@ -80,7 +81,10 @@ function App() {
const [columnVisibility, setColumnVisibility] =
React.useState<ColumnVisibilityState>({})
const [columnOrder, setColumnOrder] = React.useState<ColumnOrderState>([])
const [columnPinning, setColumnPinning] = React.useState({})
const [columnPinning, setColumnPinning] = React.useState<ColumnPinningState>({
left: [],
right: [],
})

const [isSplit, setIsSplit] = React.useState(false)
const rerender = () => setData(() => makeData(5000))
Expand Down
6 changes: 4 additions & 2 deletions packages/solid-table/src/FlexRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export function flexRender<TProps>(
}

export function FlexRender<TProps>(
props: { Comp: ((_props: TProps) => JSX.Element) | JSX.Element | undefined } & TProps,
props: {
Comp: ((_props: TProps) => JSX.Element) | JSX.Element | undefined
} & TProps,
): JSX.Element {
return flexRender(props.Comp, props)
}
}
2 changes: 1 addition & 1 deletion packages/table-core/src/core/headers/buildHeaderGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function buildHeaderGroups<
maxDepth = Math.max(maxDepth, depth)

columns
.filter((column) => column.getIsVisible())
.filter((column) => column_getIsVisible(column, table))
.forEach((column) => {
if (column.columns.length) {
findMaxDepth(column.columns, depth + 1)
Expand Down
3 changes: 2 additions & 1 deletion packages/table-core/src/core/table/createCoreRowModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { _createRow } from '../rows/createRow'
import { getMemoOptions, memo } from '../../utils'
import { table_getRowId } from '../rows/Rows.utils'
import { table_autoResetPageIndex } from '../../features/row-pagination/RowPagination.utils'
import type { RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../types/RowModel'
Expand Down Expand Up @@ -73,7 +74,7 @@ export function createCoreRowModel<
return rowModel
},
getMemoOptions(table.options, 'debugTable', 'getRowModel', () =>
table._autoResetPageIndex(),
table_autoResetPageIndex(table),
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export function column_getFacetedRowModel<
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
column: Column<TFeatures, TData, TValue> | undefined,
table: Table<TFeatures, TData>,
): () => RowModel<TFeatures, TData> {
return (
table.options._rowModels?.Faceted?.(table, column.id) ??
table.options._rowModels?.Faceted?.(table, column?.id ?? '') ??
(() => table_getPreFilteredRowModel(table))
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getMemoOptions, memo } from '../../utils'
import { row_getUniqueValues } from '../../core/rows/Rows.utils'
import { column_getFacetedRowModel } from './ColumnFaceting.utils'
import type { RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
Expand All @@ -13,7 +14,7 @@ export function createFacetedMinMaxValues<
) => () => undefined | [number, number] {
return (table, columnId) =>
memo(
() => [table.getColumn(columnId)?.getFacetedRowModel()],
() => [column_getFacetedRowModel(table.getColumn(columnId), table)()],
(facetedRowModel) => {
if (!facetedRowModel) return undefined

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { getMemoOptions, memo } from '../../utils'
import { filterRows } from '../column-filtering/filterRowsUtils'
import { _table_getState } from '../../core/table/Tables.utils'
import {
table_getFilteredRowModel,
table_getPreFilteredRowModel,
} from '../column-filtering/ColumnFiltering.utils'
import type { RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../types/RowModel'
Expand All @@ -17,10 +21,10 @@ export function createFacetedRowModel<
return (table, columnId) =>
memo(
() => [
table.getPreFilteredRowModel(),
table_getPreFilteredRowModel(table),
_table_getState(table).columnFilters,
_table_getState(table).globalFilter,
table.getFilteredRowModel(),
table_getFilteredRowModel(table),
],
(preRowModel, columnFilters, globalFilter) => {
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getMemoOptions, memo } from '../../utils'
import { row_getUniqueValues } from '../../core/rows/Rows.utils'
import { column_getFacetedRowModel } from './ColumnFaceting.utils'
import type { RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
Expand All @@ -13,7 +14,7 @@ export function createFacetedUniqueValues<
) => () => Map<any, number> {
return (table, columnId) =>
memo(
() => [table.getColumn(columnId)?.getFacetedRowModel()],
() => [column_getFacetedRowModel(table.getColumn(columnId), table)()],
(facetedRowModel) => {
if (!facetedRowModel) return new Map()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import type { TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../types/RowModel'
import type { Table } from '../../types/Table'
import type { Column } from '../../types/Column'
import type { ColumnFiltersState, FilterFn } from './ColumnFiltering.types'
import type {
ColumnDef_ColumnFiltering,
ColumnFiltersState,
FilterFn,
TableOptions_ColumnFiltering,
} from './ColumnFiltering.types'

export function column_getAutoFilterFn<
TFeatures extends TableFeatures,
Expand Down Expand Up @@ -49,11 +54,18 @@ export function column_getFilterFn<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
) {
return isFunction(column.columnDef.filterFn)
? column.columnDef.filterFn
: column.columnDef.filterFn === 'auto'
? column.getAutoFilterFn()
? column_getAutoFilterFn(column, table)
: table.options.filterFns?.[column.columnDef.filterFn as string] ??
filterFns[column.columnDef.filterFn as BuiltInFilterFn]
}
Expand All @@ -62,7 +74,14 @@ export function column_getCanFilter<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
) {
return (
(column.columnDef.enableColumnFilter ?? true) &&
(table.options.enableColumnFilters ?? true) &&
Expand All @@ -75,15 +94,29 @@ export function column_getIsFiltered<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
) {
return column_getFilterIndex(column, table) > -1
}

export function column_getFilterValue<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
) {
return _table_getState(table).columnFilters?.find((d) => d.id === column.id)
?.value
}
Expand All @@ -93,8 +126,12 @@ export function column_getFilterIndex<
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
): number {
return (
_table_getState(table).columnFilters?.findIndex(
Expand All @@ -108,12 +145,16 @@ export function column_setFilterValue<
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
value: any,
) {
table_setColumnFilters(table, (old) => {
const filterFn = column.getFilterFn()
const filterFn = column_getFilterFn(column, table)
const previousFilter = old.find((d) => d.id === column.id)

const newFilter = functionalUpdate(
Expand Down Expand Up @@ -147,17 +188,22 @@ export function column_setFilterValue<
export function table_setColumnFilters<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>, updater: Updater<ColumnFiltersState>) {
>(
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
updater: Updater<ColumnFiltersState>,
) {
const leafColumns = table.getAllLeafColumns()

const updateFn = (old: ColumnFiltersState) => {
return functionalUpdate(updater, old).filter((filter) => {
const column = leafColumns.find((d) => d.id === filter.id)

if (column) {
const filterFn = column.getFilterFn()
const filterFn = column_getFilterFn(column, table)

if (shouldAutoRemoveFilter(filterFn, filter.value, column)) {
if (shouldAutoRemoveFilter(filterFn, filter.value, column as any)) {
return false
}
}
Expand All @@ -172,7 +218,12 @@ export function table_setColumnFilters<
export function table_resetColumnFilters<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>, defaultState?: boolean) {
>(
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
defaultState?: boolean,
) {
table_setColumnFilters(
table,
defaultState ? [] : table.initialState.columnFilters ?? [],
Expand All @@ -182,14 +233,22 @@ export function table_resetColumnFilters<
export function table_getPreFilteredRowModel<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>) {
>(
table: Table<TFeatures, TData> & {
options: TableOptions_ColumnFiltering<TFeatures, TData>
},
) {
return table_getCoreRowModel(table)
}

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

0 comments on commit c578c2b

Please sign in to comment.