Skip to content

Commit

Permalink
fix getdefaultoptions type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Oct 2, 2024
1 parent 99df25a commit f5210b8
Show file tree
Hide file tree
Showing 24 changed files with 68 additions and 36 deletions.
8 changes: 4 additions & 4 deletions docs/guide/custom-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface TableFeature<TFeatures extends TableFeatures, TFns extends Fns<
constructRow?: (row: Row<TFeatures, TFns, TData>, table: Table<TFeatures, TFns, TData>) => void
constructTable?: (table: Table<TFeatures, TFns, TData>) => void
getDefaultColumnDef?: () => Partial<ColumnDef<TFeatures, TFns, TData, unknown>>
getDefaultOptions?: (
getDefaultTableOptions?: (
table: Table<TFeatures, TFns, TData>
) => Partial<TableOptions<TFeatures, TFns, TData>>
getInitialState?: (initialState?: InitialTableState) => Partial<TableState>
Expand All @@ -56,9 +56,9 @@ This might be a bit confusing, so let's break down what each of these methods do

<br />

##### getDefaultOptions
##### getDefaultTableOptions

The `getDefaultOptions` method in a table feature is responsible for setting the default table options for that feature. For example, in the [Column Sizing](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnSizing.ts) feature, the `getDefaultOptions` method sets the default `columnResizeMode` option with a default value of `"onEnd"`.
The `getDefaultTableOptions` method in a table feature is responsible for setting the default table options for that feature. For example, in the [Column Sizing](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnSizing.ts) feature, the `getDefaultTableOptions` method sets the default `columnResizeMode` option with a default value of `"onEnd"`.

<br />

Expand Down Expand Up @@ -187,7 +187,7 @@ export const DensityFeature: TableFeature<any> = { //Use the TableFeature type!!
},

// define the new feature's default options
getDefaultOptions: <TFeatures extends TableFeatures, TFns extends Fns<TFeatures, TFns, TData>, TData extends RowData>(
getDefaultTableOptions: <TFeatures extends TableFeatures, TFns extends Fns<TFeatures, TFns, TData>, TData extends RowData>(
table: Partial<Table<TFeatures, TFns, TData>>
): DensityOptions => {
return {
Expand Down
2 changes: 1 addition & 1 deletion examples/react/custom-features/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const DensityFeature: TableFeature = {
},

// define the new feature's default options
getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/table/constructTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function constructTable<
const table = {} as unknown as Table<TFeatures, TFns, TData>

const defaultOptions = featuresList.reduce((obj, feature) => {
return Object.assign(obj, feature.getDefaultOptions?.(table))
return Object.assign(obj, feature.getDefaultTableOptions?.(table))
}, {}) as TableOptions<TFeatures, TFns, TData>

const initialState = getInitialTableState(_features, options.initialState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
column_getFilterValue,
column_getIsFiltered,
column_setFilterValue,
getDefaultColumnFiltersState,
table_getFilteredRowModel,
table_getPreFilteredRowModel,
table_resetColumnFilters,
Expand Down Expand Up @@ -41,7 +42,7 @@ export const ColumnFiltering: TableFeature = {
state: TableState<TFeatures>,
): TableState<TFeatures> & TableState_ColumnFiltering => {
return {
columnFilters: [],
columnFilters: getDefaultColumnFiltersState(),
...state,
}
},
Expand All @@ -56,7 +57,7 @@ export const ColumnFiltering: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import type {
FilterFn,
} from './ColumnFiltering.types'

export function getDefaultColumnFiltersState(): ColumnFiltersState {
return structuredClone([])
}

export function column_getAutoFilterFn<
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
column_getIsGrouped,
column_getToggleGroupingHandler,
column_toggleGrouping,
getDefaultGroupingState,
row_getGroupingValue,
row_getIsGrouped,
table_getGroupedRowModel,
Expand Down Expand Up @@ -45,7 +46,7 @@ export const ColumnGrouping: TableFeature = {
state: TableState<TFeatures>,
): TableState<TFeatures> & TableState_ColumnGrouping => {
return {
grouping: [],
grouping: getDefaultGroupingState(),
...state,
}
},
Expand All @@ -62,7 +63,7 @@ export const ColumnGrouping: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import type {
Row_ColumnGrouping,
} from './ColumnGrouping.types'

export function getDefaultGroupingState(): GroupingState {
return structuredClone([])
}

export function column_toggleGrouping<
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
column_getIndex,
column_getIsFirstColumn,
column_getIsLastColumn,
getDefaultColumnOrderState,
table_resetColumnOrder,
table_setColumnOrder,
} from './ColumnOrdering.utils'
Expand All @@ -30,12 +31,12 @@ export const ColumnOrdering: TableFeature = {
state: TableState<TFeatures>,
): TableState<TFeatures> & TableState_ColumnOrdering => {
return {
columnOrder: [],
columnOrder: getDefaultColumnOrderState(),
...state,
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import type { Column } from '../../types/Column'
import type { ColumnPinningPosition } from '../column-pinning/ColumnPinning.types'
import type { ColumnOrderState } from './ColumnOrdering.types'

export function getDefaultColumnOrderState(): ColumnOrderState {
return structuredClone([])
}

export function column_getIndex<
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
Expand Down Expand Up @@ -103,7 +107,7 @@ export function table_getOrderColumnsFn<
const targetColumnId = columnOrderCopy.shift()
const foundIndex = columnsCopy.findIndex((d) => d.id === targetColumnId)
if (foundIndex > -1) {
orderedColumns.push(columnsCopy.splice(foundIndex, 1)[0])
orderedColumns.push(columnsCopy.splice(foundIndex, 1)[0]!)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const ColumnPinning: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const ColumnResizing: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
column_getStart,
column_resetSize,
getDefaultColumnSizingColumnDef,
getDefaultColumnSizingState,
header_getSize,
header_getStart,
table_getCenterTotalSize,
Expand Down Expand Up @@ -43,7 +44,7 @@ export const ColumnSizing: TableFeature = {
state: TableState<TFeatures>,
): TableState<TFeatures> => {
return {
columnSizing: {},
columnSizing: getDefaultColumnSizingState(),
...state,
}
},
Expand All @@ -52,7 +53,7 @@ export const ColumnSizing: TableFeature = {
return getDefaultColumnSizingColumnDef()
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import type {
ColumnSizingState,
} from './ColumnSizing.types'

export function getDefaultColumnSizingState(): ColumnSizingState {
return structuredClone({})
}

export function getDefaultColumnSizingColumnDef() {
return structuredClone({
size: 150,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
column_getIsVisible,
column_getToggleVisibilityHandler,
column_toggleVisibility,
getDefaultColumnVisibilityState,
row_getAllVisibleCells,
row_getVisibleCells,
table_getIsAllColumnsVisible,
Expand All @@ -32,7 +33,6 @@ import type { Column } from '../../types/Column'
import type {
Column_ColumnVisibility,
Row_ColumnVisibility,
TableState_ColumnVisibility,
Table_ColumnVisibility,
VisibilityDefaultOptions,
} from './ColumnVisibility.types'
Expand All @@ -46,12 +46,12 @@ export const ColumnVisibility: TableFeature = {
state: TableState<TFeatures>,
): TableState<TFeatures> => {
return {
columnVisibility: {},
columnVisibility: getDefaultColumnVisibilityState(),
...state,
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import type {
} from './ColumnVisibility.types'
import type { Row } from '../../types/Row'

export function getDefaultColumnVisibilityState(): ColumnVisibilityState {
return structuredClone({})
}

export function column_toggleVisibility<
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const GlobalFiltering: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assignAPIs, makeStateUpdater } from '../../utils'
import {
getDefaultExpandedState,
row_getCanExpand,
row_getIsAllParentsExpanded,
row_getIsExpanded,
Expand All @@ -26,7 +27,6 @@ import type { Row } from '../../types/Row'
import type {
Row_RowExpanding,
TableOptions_RowExpanding,
TableState_RowExpanding,
Table_RowExpanding,
} from './RowExpanding.types'

Expand All @@ -40,12 +40,12 @@ export const RowExpanding: TableFeature = {
state: TableState<TFeatures>,
): TableState<TFeatures> => {
return {
expanded: {},
expanded: getDefaultExpandedState(),
...state,
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import type { Table_Internal } from '../../types/Table'
import type { Row } from '../../types/Row'
import type { ExpandedState, ExpandedStateList } from './RowExpanding.types'

export function getDefaultExpandedState(): ExpandedState {
return structuredClone({})
}

/**
*
* @param table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const RowPagination: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/features/row-pinning/RowPinning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const RowPinning: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { table_getState } from '../../core/table/Tables.utils'
import { table_getFilteredRowModel } from '../column-filtering/ColumnFiltering.utils'
import { table_getSortedRowModel } from '../row-sorting/RowSorting.utils'
import {
getDefaultRowSelectionState,
row_getCanMultiSelect,
row_getCanSelect,
row_getCanSelectSubRows,
Expand Down Expand Up @@ -50,12 +51,12 @@ export const RowSelection: TableFeature = {
state: TableState<TFeatures>,
): TableState<TFeatures> & TableState_RowSelection => {
return {
rowSelection: {},
rowSelection: getDefaultRowSelectionState(),
...state,
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import type { RowSelectionState } from './RowSelection.types'

// State utils

export function getDefaultRowSelectionState(): RowSelectionState {
return structuredClone({})
}

/**
*
* @param table
Expand Down
5 changes: 2 additions & 3 deletions packages/table-core/src/features/row-sorting/RowSorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ export const RowSorting: TableFeature = {
}
},

getDefaultOptions: <
getDefaultTableOptions: <
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
>(
table: Table<TFeatures, TFns, TData> &
Partial<Table_RowSorting<TFeatures, TFns, TData>>,
table: Table<TFeatures, TFns, TData>,
): TableOptions_RowSorting => {
return {
onSortingChange: makeStateUpdater('sorting', table),
Expand Down
Loading

0 comments on commit f5210b8

Please sign in to comment.