Skip to content

Commit

Permalink
combine faceting features, improve debugging options
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jan 10, 2025
1 parent 10334bf commit 963b69c
Show file tree
Hide file tree
Showing 46 changed files with 281 additions and 267 deletions.
3 changes: 1 addition & 2 deletions examples/react/filters/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ function App() {
},
onColumnFiltersChange: setColumnFilters,
debugTable: true,
debugHeaders: true,
debugColumns: false,
debugColumns: true,
})

return (
Expand Down
4 changes: 3 additions & 1 deletion packages/table-core/src/core/cells/coreCellsFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export const coreCellsFeature: TableFeature<{
// Cell: Cell_Cell<TableFeatures, RowData, CellData>
// TableOptions: TableOptions_Cell
}> = {
feature: 'coreCellsFeature',

constructCellAPIs: (cell) => {
assignAPIs(cell, [
assignAPIs('coreCellsFeature', cell, [
{
fn: () => cell_getValue(cell),
fnName: 'cell_getValue',
Expand Down
6 changes: 0 additions & 6 deletions packages/table-core/src/core/cells/coreCellsFeature.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ export interface Cell_Cell<
}

export interface TableOptions_Cell {
/**
* Set this option to `true` to output cell debugging information to the console.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugcells]
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
debugCells?: boolean
/**
* Value used when the desired value is not found in the data.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#renderfallbackvalue)
Expand Down
6 changes: 4 additions & 2 deletions packages/table-core/src/core/columns/coreColumnsFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export const coreColumnsFeature: TableFeature<{
// Table: Table_Columns<TableFeatures, RowData>
// TableOptions: TableOptions_Columns<TableFeatures, RowData>
}> = {
feature: 'coreColumnsFeature',

constructColumnAPIs: (column) => {
const { _table: table } = column
assignAPIs(column, [
assignAPIs('coreColumnsFeature', column, [
{
fn: () => column_getFlatColumns(column),
fnName: 'column_getFlatColumns',
Expand All @@ -38,7 +40,7 @@ export const coreColumnsFeature: TableFeature<{
},

constructTableAPIs: (table) => {
assignAPIs(table, [
assignAPIs('coreColumnsFeature', table, [
{
fn: () => table_getDefaultColumnDef(table),
fnName: 'table_getDefaultColumnDef',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ export interface TableOptions_Columns<
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
columns: Array<ColumnDef<TFeatures, TData, TValue>>
/**
* Set this option to `true` to output column debugging information to the console.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugcolumns)
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
debugColumns?: boolean
/**
* Default column options to use for all column defs supplied to the table.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#defaultcolumn)
Expand Down
7 changes: 4 additions & 3 deletions packages/table-core/src/core/headers/coreHeadersFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import type { TableFeature } from '../../types/TableFeatures'
export const coreHeadersFeature: TableFeature<{
// Header: Header_Header<TableFeatures, RowData, CellData>
// Table: Table_Headers<TableFeatures, RowData>
// TableOptions: TableOptions_Headers
}> = {
feature: 'coreHeadersFeature',

constructHeaderAPIs: (header) => {
assignAPIs(header, [
assignAPIs('coreHeadersFeature', header, [
{
fn: () => header_getLeafHeaders(header),
fnName: 'header_getLeafHeaders',
Expand All @@ -36,7 +37,7 @@ export const coreHeadersFeature: TableFeature<{
},

constructTableAPIs: (table) => {
assignAPIs(table, [
assignAPIs('coreHeadersFeature', table, [
{
fn: () => table_getHeaderGroups(table),
fnName: 'table_getHeaderGroups',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import type { Header } from '../../types/Header'
import type { HeaderGroup } from '../../types/HeaderGroup'
import type { Column } from '../../types/Column'

export interface TableOptions_Headers {
/**
* Set this option to `true` to output header debugging information to the console.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugheaders)
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
debugHeaders?: boolean
}

export interface Table_Headers<
TFeatures extends TableFeatures,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
export const coreRowModelsFeature: TableFeature<{
Table: Table_RowModels<TableFeatures, RowData>
}> = {
feature: 'coreRowModelsFeature',

constructTableAPIs: (table) => {
assignAPIs(table, [
assignAPIs('coreRowModelsFeature', table, [
{
fn: () => table_getCoreRowModel(table),
fnName: 'table_getCoreRowModel',
Expand Down
7 changes: 4 additions & 3 deletions packages/table-core/src/core/row-models/createCoreRowModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { constructRow } from '../rows/constructRow'
import { isDev, tableMemo } from '../../utils'
import { tableMemo } from '../../utils'
import { table_autoResetPageIndex } from '../../features/row-pagination/rowPaginationFeature.utils'
import type { Table_Internal } from '../../types/Table'
import type { RowModel } from './coreRowModelsFeature.types'
Expand All @@ -15,11 +15,12 @@ export function createCoreRowModel<
) => () => RowModel<TFeatures, TData> {
return (table: Table_Internal<TFeatures, TData>) =>
tableMemo({
debug: isDev && (table.options.debugAll ?? table.options.debugTable),
feature: 'coreRowModelsFeature',
fn: (data) => _createCoreRowModel(table, data),
fnName: 'table.getCoreRowModel',
memoDeps: () => [table.options.data],
fn: (data) => _createCoreRowModel(table, data),
onAfterUpdate: () => table_autoResetPageIndex(table),
table,
})
}

Expand Down
5 changes: 3 additions & 2 deletions packages/table-core/src/core/rows/coreRowsFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export const coreRowsFeature: TableFeature<{
// TableOptions: TableOptions_Rows<TableFeatures, RowData>
// Table: Table_Rows<TableFeatures, RowData>
}> = {
feature: 'coreRowsFeature',
constructRowAPIs: (row) => {
assignAPIs(row, [
assignAPIs('coreRowsFeature', row, [
{
fn: () => row_getAllCellsByColumnId(row),
fnName: 'row_getAllCellsByColumnId',
Expand Down Expand Up @@ -59,7 +60,7 @@ export const coreRowsFeature: TableFeature<{
},

constructTableAPIs: (table) => {
assignAPIs(table, [
assignAPIs('coreRowsFeature', table, [
{
fn: (row, index, parent) => table_getRowId(row, table, index, parent),
fnName: 'table_getRowId',
Expand Down
6 changes: 0 additions & 6 deletions packages/table-core/src/core/rows/coreRowsFeature.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ export interface TableOptions_Rows<
TFeatures extends TableFeatures,
TData extends RowData,
> {
/**
* Set this option to `true` to output row debugging information to the console.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugrows)
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
debugRows?: boolean
/**
* This optional function is used to derive a unique ID for any given row. If not provided the rows index is used (nested rows join together with `.` using their grandparents' index eg. `index.index.index`). If you need to identify individual rows that are originating from any server-side operations, it's suggested you use this function to return an ID that makes sense regardless of network IO/ambiguity eg. a userId, taskId, database ID field, etc.
* @example getRowId: row => row.userId
Expand Down
4 changes: 3 additions & 1 deletion packages/table-core/src/core/table/coreTablesFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const coreTablesFeature: TableFeature<{
// Table: Table_Table<TableFeatures, RowData>
// TableOptions: TableOptions_Table<TableFeatures, RowData>
}> = {
feature: 'coreTablesFeature',

constructTableAPIs: (table) => {
assignAPIs(table, [
assignAPIs('coreTablesFeature', table, [
{
fnName: 'table_getState',
fn: () => table_getState(table),
Expand Down
18 changes: 0 additions & 18 deletions packages/table-core/src/core/table/coreTablesFeature.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@ export interface TableOptions_Table<
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
data: Array<TData>
/**
* Set this option to `true` to output all debugging information to the console.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugall)
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
debugAll?: boolean
/**
* Set this option to `true` to output cache debugging information to the console.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugcache)
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
debugCache?: boolean
/**
* Set this option to `true` to output table debugging information to the console.
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugtable)
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
debugTable?: boolean
/**
* Use this option to optionally pass initial state to the table. This state will be used when resetting various table states either automatically by the table (eg. `options.autoResetPageIndex`) or via functions like `table.resetRowSelection()`. Most reset function allow you optionally pass a flag to reset to a blank/default state instead of the initial state.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
column_getFacetedMinMaxValues,
column_getFacetedRowModel,
column_getFacetedUniqueValues,
table_getGlobalFacetedMinMaxValues,
table_getGlobalFacetedRowModel,
table_getGlobalFacetedUniqueValues,
} from './columnFacetingFeature.utils'
import type { TableFeature } from '../../types/TableFeatures'
// import type {
Expand All @@ -22,8 +25,10 @@ interface ColumnFacetingFeatureConstructors {
*/
export const columnFacetingFeature: TableFeature<ColumnFacetingFeatureConstructors> =
{
feature: 'columnFacetingFeature',

constructColumnAPIs: (column) => {
assignAPIs(column, [
assignAPIs('columnFacetingFeature', column, [
{
fn: () => column_getFacetedMinMaxValues(column, column._table),
fnName: 'column_getFacetedMinMaxValues',
Expand All @@ -38,4 +43,20 @@ export const columnFacetingFeature: TableFeature<ColumnFacetingFeatureConstructo
},
])
},
constructTableAPIs: (table) => {
assignAPIs('columnFacetingFeature', table, [
{
fn: () => table_getGlobalFacetedMinMaxValues(table),
fnName: 'table_getGlobalFacetedMinMaxValues',
},
{
fn: () => table_getGlobalFacetedRowModel(table),
fnName: 'table_getGlobalFacetedRowModel',
},
{
fn: () => table_getGlobalFacetedUniqueValues(table),
fnName: 'table_getGlobalFacetedUniqueValues',
},
])
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,27 @@ export interface CachedRowModel_Faceted<
facetedMinMaxValues?: (columnId: string) => [number, number]
facetedUniqueValues?: (columnId: string) => Map<any, number>
}

export interface Table_ColumnFaceting<
TFeatures extends TableFeatures,
TData extends RowData,
> {
/**
* Returns the min and max values for the global filter.
* [API Docs](https://tanstack.com/table/v8/docs/api/features/global-faceting#getglobalfacetedminmaxvalues)
* [Guide](https://tanstack.com/table/v8/docs/guide/global-faceting)
*/
getGlobalFacetedMinMaxValues: () => undefined | [number, number]
/**
* Returns the row model for the table after **global** filtering has been applied.
* [API Docs](https://tanstack.com/table/v8/docs/api/features/global-faceting#getglobalfacetedrowmodel)
* [Guide](https://tanstack.com/table/v8/docs/guide/global-faceting)
*/
getGlobalFacetedRowModel: () => RowModel<TFeatures, TData>
/**
* Returns the faceted unique values for the global filter.
* [API Docs](https://tanstack.com/table/v8/docs/api/features/global-faceting#getglobalfaceteduniquevalues)
* [Guide](https://tanstack.com/table/v8/docs/guide/global-faceting)
*/
getGlobalFacetedUniqueValues: () => Map<any, number>
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,33 @@ export function column_getFacetedUniqueValues<
(() => new Map<any, number>())
)
}

export function table_getGlobalFacetedMinMaxValues<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>): () => undefined | [number, number] {
return (
table.options._rowModels?.facetedMinMaxValues?.(table, '__global__') ??
(() => undefined)
)
}

export function table_getGlobalFacetedRowModel<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>): () => RowModel<TFeatures, TData> {
return (
table.options._rowModels?.facetedRowModel?.(table, '__global__') ??
(() => table.getPreFilteredRowModel())
)
}

export function table_getGlobalFacetedUniqueValues<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>): () => Map<any, number> {
return (
table.options._rowModels?.facetedUniqueValues?.(table, '__global__') ??
(() => new Map())
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDev, tableMemo } from '../../utils'
import { tableMemo } from '../../utils'
import { column_getFacetedRowModel } from './columnFacetingFeature.utils'
import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'
import type { Table_Internal } from '../../types/Table'
Expand All @@ -14,13 +14,15 @@ export function createFacetedMinMaxValues<
) => () => undefined | [number, number] {
return (table, columnId) =>
tableMemo({
debug: isDev && (table.options.debugAll ?? table.options.debugTable),
feature: 'columnFacetingFeature',
fn: (facetedRowModel) =>
_createFacetedMinMaxValues(columnId, facetedRowModel),
fnName: 'table.getFacetedMinMaxValues',
memoDeps: () => [
// TODO fix
column_getFacetedRowModel(table.getColumn(columnId), table)(),
],
fn: (facetedRowModel) =>
_createFacetedMinMaxValues(columnId, facetedRowModel),
table,
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDev, tableMemo } from '../../utils'
import { tableMemo } from '../../utils'
import { filterRows } from '../column-filtering/filterRowsUtils'
import type { Table_Internal } from '../../types/Table'
import type {
Expand All @@ -19,7 +19,8 @@ export function createFacetedRowModel<
) => () => RowModel<TFeatures, TData> {
return (table, columnId) =>
tableMemo({
debug: isDev && (table.options.debugAll ?? table.options.debugTable),
feature: 'columnFacetingFeature',
table,
fnName: 'createFacetedRowModel',
memoDeps: () => [
table.getPreFilteredRowModel(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDev, tableMemo } from '../../utils'
import { tableMemo } from '../../utils'
import { column_getFacetedRowModel } from './columnFacetingFeature.utils'
import type { Table_Internal } from '../../types/Table'
import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'
Expand All @@ -14,7 +14,8 @@ export function createFacetedUniqueValues<
) => () => Map<any, number> {
return (table, columnId) =>
tableMemo({
debug: isDev && (table.options.debugAll ?? table.options.debugTable),
feature: 'columnFacetingFeature',
table,
fnName: 'table.getFacetedUniqueValues',
memoDeps: () => [
column_getFacetedRowModel(table.getColumn(columnId), table)(),
Expand Down
Loading

0 comments on commit 963b69c

Please sign in to comment.