Skip to content

Commit

Permalink
chore: split filtering features and rename (#5413)
Browse files Browse the repository at this point in the history
* chore: split filtering features and rename

* prettier
  • Loading branch information
KevinVandy authored Mar 15, 2024
1 parent b487f56 commit 01129cb
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 339 deletions.
2 changes: 1 addition & 1 deletion packages/table-core/src/aggregationFns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AggregationFn } from './features/Grouping'
import { AggregationFn } from './features/ColumnGrouping'
import { isNumberArray } from './utils'

const sum: AggregationFn<any> = (columnId, _leafRows, childRows) => {
Expand Down
34 changes: 19 additions & 15 deletions packages/table-core/src/core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import { createColumn } from './column'
import { Headers } from './headers'
//

import { ColumnSizing } from '../features/ColumnSizing'
import { Expanding } from '../features/Expanding'
import { Filters } from '../features/Filters'
import { Grouping } from '../features/Grouping'
import { Ordering } from '../features/Ordering'
import { Pagination } from '../features/Pagination'
import { ColumnFaceting } from '../features/ColumnFaceting'
import { ColumnFiltering } from '../features/ColumnFiltering'
import { ColumnGrouping } from '../features/ColumnGrouping'
import { ColumnOrdering } from '../features/ColumnOrdering'
import { ColumnPinning } from '../features/ColumnPinning'
import { ColumnSizing } from '../features/ColumnSizing'
import { ColumnVisibility } from '../features/ColumnVisibility'
import { GlobalFiltering } from '../features/GlobalFiltering'
import { RowExpanding } from '../features/RowExpanding'
import { RowPagination } from '../features/RowPagination'
import { RowPinning } from '../features/RowPinning'
import { RowSelection } from '../features/RowSelection'
import { Sorting } from '../features/Sorting'
import { Visibility } from '../features/Visibility'
import { RowSorting } from '../features/RowSorting'

export interface TableFeature {
createCell?: (cell: any, column: any, row: any, table: any) => any
Expand All @@ -47,15 +49,17 @@ export interface TableFeature {

const features = [
Headers,
Visibility,
Ordering,
ColumnVisibility,
ColumnOrdering,
ColumnPinning,
ColumnFaceting,
ColumnFiltering,
GlobalFiltering, //depends on ColumnFiltering and ColumnFaceting
RowSorting,
ColumnGrouping, //depends on RowSorting
RowExpanding,
RowPagination,
RowPinning,
Filters,
Sorting,
Grouping,
Expanding,
Pagination,
RowSelection,
ColumnSizing,
] as const
Expand Down
85 changes: 85 additions & 0 deletions packages/table-core/src/features/ColumnFaceting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { RowModel } from '..'
import { TableFeature } from '../core/table'
import { Column, Table, RowData } from '../types'

export interface FacetedColumn<TData extends RowData> {
_getFacetedMinMaxValues?: () => undefined | [number, number]
_getFacetedRowModel?: () => RowModel<TData>
_getFacetedUniqueValues?: () => Map<any, number>
/**
* A function that **computes and returns** a min/max tuple derived from `column.getFacetedRowModel`. Useful for displaying faceted result values.
* > ⚠️ Requires that you pass a valid `getFacetedMinMaxValues` function to `options.getFacetedMinMaxValues`. A default implementation is provided via the exported `getFacetedMinMaxValues` function.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/filters#getfacetedminmaxvalues)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/filters)
*/
getFacetedMinMaxValues: () => undefined | [number, number]
/**
* Returns the row model with all other column filters applied, excluding its own filter. Useful for displaying faceted result counts.
* > ⚠️ Requires that you pass a valid `getFacetedRowModel` function to `options.facetedRowModel`. A default implementation is provided via the exported `getFacetedRowModel` function.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/filters#getfacetedrowmodel)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/filters)
*/
getFacetedRowModel: () => RowModel<TData>
/**
* A function that **computes and returns** a `Map` of unique values and their occurrences derived from `column.getFacetedRowModel`. Useful for displaying faceted result values.
* > ⚠️ Requires that you pass a valid `getFacetedUniqueValues` function to `options.getFacetedUniqueValues`. A default implementation is provided via the exported `getFacetedUniqueValues` function.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/filters#getfaceteduniquevalues)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/filters)
*/
getFacetedUniqueValues: () => Map<any, number>
}

export interface FacetedOptions<TData extends RowData> {
getFacetedMinMaxValues?: (
table: Table<TData>,
columnId: string
) => () => undefined | [number, number]
getFacetedRowModel?: (
table: Table<TData>,
columnId: string
) => () => RowModel<TData>
getFacetedUniqueValues?: (
table: Table<TData>,
columnId: string
) => () => Map<any, number>
}

//

export const ColumnFaceting: TableFeature = {
createColumn: <TData extends RowData>(
column: Column<TData, unknown>,
table: Table<TData>
): void => {
column._getFacetedRowModel =
table.options.getFacetedRowModel &&
table.options.getFacetedRowModel(table, column.id)
column.getFacetedRowModel = () => {
if (!column._getFacetedRowModel) {
return table.getPreFilteredRowModel()
}

return column._getFacetedRowModel()
}
column._getFacetedUniqueValues =
table.options.getFacetedUniqueValues &&
table.options.getFacetedUniqueValues(table, column.id)
column.getFacetedUniqueValues = () => {
if (!column._getFacetedUniqueValues) {
return new Map()
}

return column._getFacetedUniqueValues()
}
column._getFacetedMinMaxValues =
table.options.getFacetedMinMaxValues &&
table.options.getFacetedMinMaxValues(table, column.id)
column.getFacetedMinMaxValues = () => {
if (!column._getFacetedMinMaxValues) {
return undefined
}

return column._getFacetedMinMaxValues()
}
},
}
Loading

0 comments on commit 01129cb

Please sign in to comment.