|
| 1 | +import { RowModel } from '..' |
| 2 | +import { TableFeature } from '../core/table' |
| 3 | +import { Column, Table, RowData } from '../types' |
| 4 | + |
| 5 | +export interface FacetedColumn<TData extends RowData> { |
| 6 | + _getFacetedMinMaxValues?: () => undefined | [number, number] |
| 7 | + _getFacetedRowModel?: () => RowModel<TData> |
| 8 | + _getFacetedUniqueValues?: () => Map<any, number> |
| 9 | + /** |
| 10 | + * A function that **computes and returns** a min/max tuple derived from `column.getFacetedRowModel`. Useful for displaying faceted result values. |
| 11 | + * > ⚠️ Requires that you pass a valid `getFacetedMinMaxValues` function to `options.getFacetedMinMaxValues`. A default implementation is provided via the exported `getFacetedMinMaxValues` function. |
| 12 | + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/filters#getfacetedminmaxvalues) |
| 13 | + * @link [Guide](https://tanstack.com/table/v8/docs/guide/filters) |
| 14 | + */ |
| 15 | + getFacetedMinMaxValues: () => undefined | [number, number] |
| 16 | + /** |
| 17 | + * Returns the row model with all other column filters applied, excluding its own filter. Useful for displaying faceted result counts. |
| 18 | + * > ⚠️ Requires that you pass a valid `getFacetedRowModel` function to `options.facetedRowModel`. A default implementation is provided via the exported `getFacetedRowModel` function. |
| 19 | + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/filters#getfacetedrowmodel) |
| 20 | + * @link [Guide](https://tanstack.com/table/v8/docs/guide/filters) |
| 21 | + */ |
| 22 | + getFacetedRowModel: () => RowModel<TData> |
| 23 | + /** |
| 24 | + * A function that **computes and returns** a `Map` of unique values and their occurrences derived from `column.getFacetedRowModel`. Useful for displaying faceted result values. |
| 25 | + * > ⚠️ Requires that you pass a valid `getFacetedUniqueValues` function to `options.getFacetedUniqueValues`. A default implementation is provided via the exported `getFacetedUniqueValues` function. |
| 26 | + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/filters#getfaceteduniquevalues) |
| 27 | + * @link [Guide](https://tanstack.com/table/v8/docs/guide/filters) |
| 28 | + */ |
| 29 | + getFacetedUniqueValues: () => Map<any, number> |
| 30 | +} |
| 31 | + |
| 32 | +export interface FacetedOptions<TData extends RowData> { |
| 33 | + getFacetedMinMaxValues?: ( |
| 34 | + table: Table<TData>, |
| 35 | + columnId: string |
| 36 | + ) => () => undefined | [number, number] |
| 37 | + getFacetedRowModel?: ( |
| 38 | + table: Table<TData>, |
| 39 | + columnId: string |
| 40 | + ) => () => RowModel<TData> |
| 41 | + getFacetedUniqueValues?: ( |
| 42 | + table: Table<TData>, |
| 43 | + columnId: string |
| 44 | + ) => () => Map<any, number> |
| 45 | +} |
| 46 | + |
| 47 | +// |
| 48 | + |
| 49 | +export const ColumnFaceting: TableFeature = { |
| 50 | + createColumn: <TData extends RowData>( |
| 51 | + column: Column<TData, unknown>, |
| 52 | + table: Table<TData> |
| 53 | + ): void => { |
| 54 | + column._getFacetedRowModel = |
| 55 | + table.options.getFacetedRowModel && |
| 56 | + table.options.getFacetedRowModel(table, column.id) |
| 57 | + column.getFacetedRowModel = () => { |
| 58 | + if (!column._getFacetedRowModel) { |
| 59 | + return table.getPreFilteredRowModel() |
| 60 | + } |
| 61 | + |
| 62 | + return column._getFacetedRowModel() |
| 63 | + } |
| 64 | + column._getFacetedUniqueValues = |
| 65 | + table.options.getFacetedUniqueValues && |
| 66 | + table.options.getFacetedUniqueValues(table, column.id) |
| 67 | + column.getFacetedUniqueValues = () => { |
| 68 | + if (!column._getFacetedUniqueValues) { |
| 69 | + return new Map() |
| 70 | + } |
| 71 | + |
| 72 | + return column._getFacetedUniqueValues() |
| 73 | + } |
| 74 | + column._getFacetedMinMaxValues = |
| 75 | + table.options.getFacetedMinMaxValues && |
| 76 | + table.options.getFacetedMinMaxValues(table, column.id) |
| 77 | + column.getFacetedMinMaxValues = () => { |
| 78 | + if (!column._getFacetedMinMaxValues) { |
| 79 | + return undefined |
| 80 | + } |
| 81 | + |
| 82 | + return column._getFacetedMinMaxValues() |
| 83 | + } |
| 84 | + }, |
| 85 | +} |
0 commit comments