Skip to content

Commit

Permalink
chore: refactor feature create partial types, add feature jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jul 18, 2024
1 parent 08bac54 commit 4dc1c1d
Show file tree
Hide file tree
Showing 16 changed files with 281 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import {
column_getFacetedRowModel,
column_getFacetedUniqueValues,
} from './ColumnFaceting.utils'
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'

/**
* The Column Faceting feature adds column faceting APIs to the column objects.
*/
export const ColumnFaceting: TableFeature = {
_createColumn: <
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
column: Column<TFeatures, TData, TValue> &
Partial<Column_ColumnFaceting<TFeatures, TData>>,
table: Table<TFeatures, TData>,
): void => {
column.getFacetedMinMaxValues = column_getFacetedMinMaxValues(column, table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ import type { Column } from '../../types/Column'
import type {
ColumnDef_ColumnFiltering,
ColumnFiltersState,
Column_ColumnFiltering,
Row_ColumnFiltering,
TableOptions_ColumnFiltering,
TableState_ColumnFiltering,
Table_ColumnFiltering,
} from './ColumnFiltering.types'

/**
* The Column Filtering feature adds column filtering state and APIs to the table, row, and column objects.
*
* **Note:** This does not include Global Filtering. The GlobalFiltering feature has been split out into its own standalone feature.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-filtering)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-filtering)
*/
export const ColumnFiltering: TableFeature = {
_getDefaultColumnDef: <
TFeatures extends TableFeatures,
Expand All @@ -43,7 +52,8 @@ export const ColumnFiltering: TableFeature = {
},

_getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
table: Partial<Table<TFeatures, TData>>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnFiltering<TFeatures, TData>>,
): TableOptions_ColumnFiltering<TFeatures, TData> => {
return {
onColumnFiltersChange: makeStateUpdater('columnFilters', table),
Expand All @@ -57,8 +67,10 @@ export const ColumnFiltering: TableFeature = {
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
column: Column<TFeatures, TData, TValue> &
Partial<Column_ColumnFiltering<TFeatures, TData>>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnFiltering<TFeatures, TData>>,
): void => {
column.getAutoFilterFn = () => column_getAutoFilterFn(column, table)

Expand All @@ -77,15 +89,15 @@ export const ColumnFiltering: TableFeature = {
},

_createRow: <TFeatures extends TableFeatures, TData extends RowData>(
row: Row<TFeatures, TData>,
_table: Table<TFeatures, TData>,
row: Row<TFeatures, TData> & Partial<Row_ColumnFiltering<TFeatures, TData>>,
): void => {
row.columnFilters = {}
row.columnFiltersMeta = {}
},

_createTable: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData> & Table_ColumnFiltering<TFeatures, TData>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnFiltering<TFeatures, TData>>,
): void => {
table.setColumnFilters = (updater: Updater<ColumnFiltersState>) =>
table_setColumnFilters(table, updater)
Expand Down
60 changes: 35 additions & 25 deletions packages/table-core/src/features/column-grouping/ColumnGrouping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ import type { Row } from '../../types/Row'
import type { Cell } from '../../types/Cell'
import type { Column } from '../../types/Column'
import type {
Cell_ColumnGrouping,
ColumnDef_ColumnGrouping,
Column_ColumnGrouping,
Row_ColumnGrouping,
TableOptions_ColumnGrouping,
TableState_ColumnGrouping,
Table_ColumnGrouping,
} from './ColumnGrouping.types'

/**
* The (Column) Grouping feature adds column grouping state and APIs to the table, row, column, and cell objects.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-grouping)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-grouping)
*/
export const ColumnGrouping: TableFeature = {
_getDefaultColumnDef: <
TFeatures extends TableFeatures,
Expand All @@ -50,21 +58,34 @@ export const ColumnGrouping: TableFeature = {
},

_getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
table: Partial<Table<TFeatures, TData>>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnGrouping<TFeatures, TData>>,
): TableOptions_ColumnGrouping<TFeatures, TData> => {
return {
onGroupingChange: makeStateUpdater('grouping', table),
groupedColumnMode: 'reorder',
}
},

_createCell: <TFeatures extends TableFeatures, TData extends RowData, TValue>(
cell: Cell<TFeatures, TData, TValue> & Partial<Cell_ColumnGrouping>,
): void => {
cell.getIsGrouped = () => cell_getIsGrouped(cell)

cell.getIsPlaceholder = () => cell_getIsPlaceholder(cell)

cell.getIsAggregated = () => cell_getIsAggregated(cell)
},

_createColumn: <
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
column: Column<TFeatures, TData, TValue> &
Partial<Column_ColumnGrouping<TFeatures, TData>>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnGrouping<TFeatures, TData>>,
): void => {
column.toggleGrouping = () => column_toggleGrouping(column, table)

Expand All @@ -83,22 +104,10 @@ export const ColumnGrouping: TableFeature = {
column.getAggregationFn = () => column_getAggregationFn(column, table)
},

_createTable: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData> & Table_ColumnGrouping<TFeatures, TData>,
): void => {
table.setGrouping = (updater) => table_setGrouping(table, updater)

table.resetGrouping = (defaultState) =>
table_resetGrouping(table, defaultState)

table.getPreGroupedRowModel = () => table_getPreGroupedRowModel(table)

table.getGroupedRowModel = () => table_getGroupedRowModel(table)
},

_createRow: <TFeatures extends TableFeatures, TData extends RowData>(
row: Row<TFeatures, TData>,
table: Table<TFeatures, TData>,
row: Row<TFeatures, TData> & Partial<Row_ColumnGrouping>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnGrouping<TFeatures, TData>>,
): void => {
row.getIsGrouped = () => row_getIsGrouped(row)

Expand All @@ -108,16 +117,17 @@ export const ColumnGrouping: TableFeature = {
row._groupingValuesCache = {}
},

_createCell: <TFeatures extends TableFeatures, TData extends RowData, TValue>(
cell: Cell<TFeatures, TData, TValue>,
_table: Table<TFeatures, TData>,
_createTable: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData> &
Partial<Table_ColumnGrouping<TFeatures, TData>>,
): void => {
const { column, row } = cell
table.setGrouping = (updater) => table_setGrouping(table, updater)

cell.getIsGrouped = () => cell_getIsGrouped(column, row)
table.resetGrouping = (defaultState) =>
table_resetGrouping(table, defaultState)

cell.getIsPlaceholder = () => cell_getIsPlaceholder(cell, column)
table.getPreGroupedRowModel = () => table_getPreGroupedRowModel(table)

cell.getIsAggregated = () => cell_getIsAggregated(cell, row)
table.getGroupedRowModel = () => table_getGroupedRowModel(table)
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,28 @@ export function cell_getIsGrouped<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>, row: Row<TFeatures, TData>) {
return column.getIsGrouped() && column.id === row.groupingColumnId
>(cell: Cell<TFeatures, TData, TValue>) {
return (
cell.column.getIsGrouped() && cell.column.id === cell.row.groupingColumnId
)
}

export function cell_getIsPlaceholder<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
cell: Cell<TFeatures, TData, TValue>,
column: Column<TFeatures, TData, TValue>,
) {
return !cell.getIsGrouped() && column.getIsGrouped()
>(cell: Cell<TFeatures, TData, TValue>) {
return !cell.getIsGrouped() && cell.column.getIsGrouped()
}

export function cell_getIsAggregated<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(cell: Cell<TFeatures, TData, TValue>, row: Row<TFeatures, TData>) {
>(cell: Cell<TFeatures, TData, TValue>) {
return (
!cell.getIsGrouped() && !cell.getIsPlaceholder() && !!row.subRows.length
!cell.getIsGrouped() &&
!cell.getIsPlaceholder() &&
!!cell.row.subRows.length
)
}
17 changes: 13 additions & 4 deletions packages/table-core/src/features/column-ordering/ColumnOrdering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from './ColumnOrdering.utils'
import type {
ColumnOrderDefaultOptions,
Column_ColumnOrdering,
TableState_ColumnOrdering,
Table_ColumnOrdering,
} from './ColumnOrdering.types'
Expand All @@ -18,6 +19,11 @@ import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Column } from '../../types/Column'

/**
* The Column Ordering feature adds column ordering state and APIs to the table and column objects.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-ordering)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-ordering)
*/
export const ColumnOrdering: TableFeature = {
_getInitialState: (state): TableState_ColumnOrdering => {
return {
Expand All @@ -27,7 +33,8 @@ export const ColumnOrdering: TableFeature = {
},

_getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
table: Partial<Table<TFeatures, TData>>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnOrdering<TFeatures, TData>>,
): ColumnOrderDefaultOptions => {
return {
onColumnOrderChange: makeStateUpdater('columnOrder', table),
Expand All @@ -39,8 +46,9 @@ export const ColumnOrdering: TableFeature = {
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
column: Column<TFeatures, TData, TValue> & Partial<Column_ColumnOrdering>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnOrdering<TFeatures, TData>>,
): void => {
column.getIndex = memo(
(position) => [column_getVisibleLeafColumns(table, position)],
Expand All @@ -56,7 +64,8 @@ export const ColumnOrdering: TableFeature = {
},

_createTable: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData> & Table_ColumnOrdering<TFeatures, TData>,
table: Table<TFeatures, TData> &
Partial<Table_ColumnOrdering<TFeatures, TData>>,
): void => {
table.setColumnOrder = (updater) => table_setColumnOrder(table, updater)

Expand Down
Loading

0 comments on commit 4dc1c1d

Please sign in to comment.