Skip to content

Commit

Permalink
simplify functions by including table ref on rows, cols, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Oct 27, 2024
1 parent c7eeb5b commit 4f502ca
Show file tree
Hide file tree
Showing 52 changed files with 455 additions and 986 deletions.
8 changes: 4 additions & 4 deletions examples/react/filters/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ function App() {

const table = useTable({
_features,
_processingFns: {
filterFns, // client side filtering
sortingFns,
},
_rowModels: {
filteredRowModel: createFilteredRowModel(), // client side filtering
sortedRowModel: createSortedRowModel(),
paginatedRowModel: createPaginatedRowModel(),
},
_processingFns: {
filterFns, // client side filtering
sortingFns,
},
columns,
data,
state: {
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"sideEffects": false,
"engines": {
"node": ">=12"
"node": ">=16"
},
"files": [
"dist/",
Expand Down
4 changes: 1 addition & 3 deletions packages/table-core/src/core/cells/Cells.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { callMemoOrStaticFn } from '../../utils'
import { row_getValue } from '../rows/Rows.utils'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Cell } from '../../types/Cell'
Expand All @@ -9,7 +7,7 @@ export function cell_getValue<
TData extends RowData,
TValue extends CellData = CellData,
>(cell: Cell<TFeatures, TData, TValue>): TValue {
return callMemoOrStaticFn(cell.row, row_getValue, [cell.column.id])
return cell.row.getValue(cell.column.id)
}

export function cell_renderValue<
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/cells/constructCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function constructCell<
}

for (const feature of Object.values(table._features) as Array<TableFeature>) {
feature.constructCellAPIs?.(cell as Cell<TFeatures, TData, TValue>, table)
feature.constructCellAPIs?.(cell as Cell<TFeatures, TData, TValue>)
}

return cell as Cell<TFeatures, TData, TValue>
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/columns/Columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const Columns: TableFeature = {
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table_Internal<TFeatures, TData>,
) => {
const { table } = column
assignAPIs(column, [
{
fn: () => column_getFlatColumns(column),
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/columns/Columns.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function table_getDefaultColumnDef<
return {
header: (props) => {
const resolvedColumnDef = props.header.column
.columnDef as ColumnDefResolved<TFeatures, TData>
.columnDef as ColumnDefResolved<{}, TData>

if (resolvedColumnDef.accessorKey) {
return resolvedColumnDef.accessorKey
Expand Down
7 changes: 2 additions & 5 deletions packages/table-core/src/core/columns/constructColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function constructColumn<
const resolvedColumnDef = {
...defaultColumn,
...columnDef,
} as ColumnDefResolved<TFeatures, TData, TValue>
} as ColumnDefResolved<{}, TData, TValue>

const accessorKey = resolvedColumnDef.accessorKey

Expand Down Expand Up @@ -84,10 +84,7 @@ export function constructColumn<
}

for (const feature of Object.values(table._features) as Array<TableFeature>) {
feature.constructColumnAPIs?.(
column as Column<TFeatures, TData, TValue>,
table,
)
feature.constructColumnAPIs?.(column as Column<TFeatures, TData, TValue>)
}

return column as Column<TFeatures, TData, TValue>
Expand Down
7 changes: 3 additions & 4 deletions packages/table-core/src/core/headers/Headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from './Headers.utils'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table, Table_Internal } from '../../types/Table'
import type { Table_Internal } from '../../types/Table'
import type { Header } from '../../types/Header'

export const Headers: TableFeature = {
Expand All @@ -24,16 +24,15 @@ export const Headers: TableFeature = {
TValue extends CellData = CellData,
>(
header: Header<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): void => {
assignAPIs(header, [
{
fn: () => header_getLeafHeaders(header),
memoDeps: () => [table.options.columns],
memoDeps: () => [header.column.table.options.columns],
},
{
fn: () => header_getContext(header),
memoDeps: () => [table.options.columns],
memoDeps: () => [header.column.table.options.columns],
},
])
},
Expand Down
4 changes: 2 additions & 2 deletions packages/table-core/src/core/headers/Headers.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table, Table_Internal } from '../../types/Table'
import type { Table } from '../../types/Table'
import type { Header } from '../../types/Header'
import type { HeaderGroup } from '../../types/HeaderGroup'
import type { Column } from '../../types/Column'
Expand Down Expand Up @@ -131,7 +131,7 @@ export interface Header_CoreProperties<
/**
* @deprecated Reference to the table instance.
*/
table: Table_Internal<TFeatures, TData>
table: Table<TFeatures, TData>
}

export interface Header_Header<
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/headers/Headers.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function header_getContext<
return {
column: header.column,
header,
table: header.table,
table: header.column.table,
}
}

Expand Down
4 changes: 2 additions & 2 deletions 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(column, table))
.filter((column) => column_getIsVisible(column))
.forEach((column) => {
if (column.columns.length) {
findMaxDepth(column.columns, depth + 1)
Expand Down Expand Up @@ -135,7 +135,7 @@ export function buildHeaderGroups<
headers: Array<Header<TFeatures, TData, TValue>>,
): Array<{ colSpan: number; rowSpan: number }> => {
const filteredHeaders = headers.filter((header) =>
column_getIsVisible(header.column, table),
column_getIsVisible(header.column),
)

return filteredHeaders.map((header) => {
Expand Down
5 changes: 1 addition & 4 deletions packages/table-core/src/core/headers/constructHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ export function constructHeader<
}

for (const feature of Object.values(table._features) as Array<TableFeature>) {
feature.constructHeaderAPIs?.(
header as Header<TFeatures, TData, TValue>,
table,
)
feature.constructHeaderAPIs?.(header as Header<TFeatures, TData, TValue>)
}

return header as Header<TFeatures, TData, TValue>
Expand Down
3 changes: 1 addition & 2 deletions packages/table-core/src/core/rows/Rows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type { Row } from '../../types/Row'
export const Rows: TableFeature = {
constructRowAPIs: <TFeatures extends TableFeatures, TData extends RowData>(
row: Row<TFeatures, TData>,
table: Table<TFeatures, TData>,
): void => {
assignAPIs(row, [
{
Expand All @@ -28,7 +27,7 @@ export const Rows: TableFeature = {
},
{
fn: () => row_getAllCells(row),
memoDeps: () => [table.getAllLeafColumns()],
memoDeps: () => [row.table.getAllLeafColumns()],
},
{
fn: () => row_getLeafRows(row),
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/rows/constructRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const constructRow = <
}

for (const feature of Object.values(table._features) as Array<TableFeature>) {
feature.constructRowAPIs?.(row as Row<TFeatures, TData>, table)
feature.constructRowAPIs?.(row as Row<TFeatures, TData>)
}

return row as Row<TFeatures, TData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import type { Column_ColumnFaceting } from './ColumnFaceting.types'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Column } from '../../types/Column'

/**
Expand All @@ -21,17 +20,16 @@ export const ColumnFaceting: TableFeature = {
>(
column: Column<TFeatures, TData, TValue> &
Partial<Column_ColumnFaceting<TFeatures, TData>>,
table: Table<TFeatures, TData>,
): void => {
assignAPIs(column, [
{
fn: () => column_getFacetedMinMaxValues(column, table),
fn: () => column_getFacetedMinMaxValues(column, column.table),
},
{
fn: () => column_getFacetedRowModel(column, table),
fn: () => column_getFacetedRowModel(column, column.table),
},
{
fn: () => column_getFacetedUniqueValues(column, table),
fn: () => column_getFacetedUniqueValues(column, column.table),
},
])
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { isDev, tableMemo } from '../../utils'
import { filterRows } from '../column-filtering/filterRowsUtils'

import type { ColumnFiltersState } from '../column-filtering/ColumnFiltering.types'
import type {
ColumnFiltersState,
Row_ColumnFiltering,
} from '../column-filtering/ColumnFiltering.types'
import type { RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../core/row-models/RowModels.types'
Expand Down Expand Up @@ -55,7 +57,9 @@ function _createFacetedRowModel<
globalFilter ? '__global__' : undefined,
].filter(Boolean) as Array<string>

const filterRowsImpl = (row: Row<TFeatures, TData>) => {
const filterRowsImpl = (
row: Row<TFeatures, TData> & Partial<Row_ColumnFiltering<TFeatures, TData>>,
) => {
// Horizontally filter rows through each column
for (const colId of filterableIds) {
if (row.columnFilters?.[colId] === false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isDev, tableMemo } from '../../utils'
import { column_getFacetedRowModel } from './ColumnFaceting.utils'
import type { RowModel } from '../../core/row-models/RowModels.types'
import type { RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
Expand All @@ -19,17 +20,16 @@ export function createFacetedUniqueValues<
column_getFacetedRowModel(table.getColumn(columnId), table)(),
],
fn: (facetedRowModel) =>
_createFacetedUniqueValues(table, columnId, facetedRowModel),
_createFacetedUniqueValues(columnId, facetedRowModel),
})
}

function _createFacetedUniqueValues<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData>,
columnId: string,
facetedRowModel?: ReturnType<ReturnType<typeof column_getFacetedRowModel>>,
facetedRowModel: RowModel<TFeatures, TData> | undefined,
): Map<any, number> {
if (!facetedRowModel) return new Map()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,28 @@ export const ColumnFiltering: TableFeature = {
>(
column: Column<TFeatures, TData, TValue> &
Partial<Column_ColumnFiltering<TFeatures, TData>>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnFiltering<TFeatures, TData>>,
): void => {
assignAPIs(column, [
{
fn: () => column_getAutoFilterFn(column, table),
fn: () => column_getAutoFilterFn(column),
},
{
fn: () => column_getFilterFn(column, table),
fn: () => column_getFilterFn(column),
},
{
fn: () => column_getCanFilter(column, table),
fn: () => column_getCanFilter(column),
},
{
fn: () => column_getIsFiltered(column, table),
fn: () => column_getIsFiltered(column),
},
{
fn: () => column_getFilterValue(column, table),
fn: () => column_getFilterValue(column),
},
{
fn: () => column_getFilterIndex(column, table),
fn: () => column_getFilterIndex(column),
},
{
fn: (value) => column_setFilterValue(column, table, value),
fn: (value) => column_setFilterValue(column, value),
},
])
},
Expand Down
Loading

0 comments on commit 4f502ca

Please sign in to comment.