Skip to content

Commit

Permalink
re-arrange where plugins are added
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jan 3, 2025
1 parent 5d0987d commit 9330b1d
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 429 deletions.
5 changes: 5 additions & 0 deletions packages/angular-table/src/constructTableHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import type {
} from '@tanstack/table-core'
import type { Signal } from '@angular/core'

// NOTE: This is a custom fork of the constructTableHelper function from the core package
// It is used to create a table helper that can be used to create tables in Angular
// It is a fork because the core package does not support Angular's reactive system
// So we need to create a custom function to create a table helper that can be used in Angular

/**
* Options for creating a table helper to share common options across multiple tables
* coreColumnsFeature, data, and state are excluded from this type and reserved for only the `useTable`/`createTable` functions
Expand Down
22 changes: 16 additions & 6 deletions packages/angular-table/src/reactivity.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { computed, signal } from '@angular/core'
import { toComputed } from './proxy'
import type { Signal } from '@angular/core'
import type { Table, TableFeature } from '@tanstack/table-core'
import type {
RowData,
Table,
TableFeature,
TableFeatures,
} from '@tanstack/table-core'

// TODO: remove this once we have a better way to define plugins
declare module '@tanstack/table-core' {
interface TableOptions_Plugins {
interface TableOptions_Plugins<
TFeatures extends TableFeatures,
TData extends RowData,
> {
enableExperimentalReactivity?: boolean
}

interface Table_Plugins {
_rootNotifier?: Signal<Table<any, any>>
_setRootNotifier?: (signal: Signal<Table<any, any>>) => void
interface Table_Plugins<
TFeatures extends TableFeatures,
TData extends RowData,
> {
_rootNotifier?: Signal<Table<TFeatures, TData>>
_setRootNotifier?: (signal: Signal<Table<TFeatures, TData>>) => void
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/react-table/src/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
Table,
TableFeatures,
TableOptions,
TableOptions_Internal,
TableState,
} from '@tanstack/table-core'

Expand All @@ -35,9 +34,7 @@ function useTableRef<TFeatures extends TableFeatures, TData extends RowData>(
export function useTable<
TFeatures extends TableFeatures,
TData extends RowData,
>(
tableOptions: TableOptions_Internal<TFeatures, Array<TData>>,
): Table<TFeatures, TData> {
>(tableOptions: TableOptions<TFeatures, TData>): Table<TFeatures, TData> {
const _features = { ...coreFeatures, ...tableOptions._features }

const [state, setState] = useState<TableState<TFeatures>>(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { functionalUpdate, isDev, isFunction } from '../../utils'
import type { CellData, RowData, Updater } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table_Internal } from '../../types/Table'
import type { Column } from '../../types/Column'
import type { Column_Internal } from '../../types/Column'
import type {
ColumnDef_ColumnFiltering,
ColumnFiltersState,
FilterFn,
} from './columnFilteringFeature.types'
Expand All @@ -17,7 +16,7 @@ export function column_getAutoFilterFn<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(column: Column<TFeatures, TData, TValue>) {
>(column: Column_Internal<TFeatures, TData, TValue>) {
const filterFns = column._table._rowModelFns.filterFns as
| Record<string, FilterFn<TFeatures, TData>>
| undefined
Expand Down Expand Up @@ -54,9 +53,7 @@ export function column_getFilterFn<
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
column: Column_Internal<TFeatures, TData, TValue>,
): FilterFn<TFeatures, TData> | undefined {
let filterFn = null
const filterFns = column._table._rowModelFns.filterFns as
Expand All @@ -81,11 +78,7 @@ export function column_getCanFilter<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
) {
>(column: Column_Internal<TFeatures, TData, TValue>) {
return (
(column.columnDef.enableColumnFilter ?? true) &&
(column._table.options.enableColumnFilters ?? true) &&
Expand All @@ -98,23 +91,15 @@ export function column_getIsFiltered<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
) {
>(column: Column_Internal<TFeatures, TData, TValue>) {
return column_getFilterIndex(column) > -1
}

export function column_getFilterValue<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
) {
>(column: Column_Internal<TFeatures, TData, TValue>) {
return column._table.options.state?.columnFilters?.find(
(d) => d.id === column.id,
)?.value
Expand All @@ -124,11 +109,7 @@ export function column_getFilterIndex<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
): number {
>(column: Column_Internal<TFeatures, TData, TValue>): number {
return (
column._table.options.state?.columnFilters?.findIndex(
(d) => d.id === column.id,
Expand All @@ -140,12 +121,7 @@ export function column_setFilterValue<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> & {
columnDef: ColumnDef_ColumnFiltering<TFeatures, TData>
},
value: any,
) {
>(column: Column_Internal<TFeatures, TData, TValue>, value: any) {
table_setColumnFilters(column._table, (old) => {
const filterFn = column_getFilterFn(column)
const previousFilter = old.find((d) => d.id === column.id)
Expand Down Expand Up @@ -223,11 +199,14 @@ export function shouldAutoRemoveFilter<
>(
filterFn?: FilterFn<TFeatures, TData>,
value?: any,
column?: Column<TFeatures, TData, TValue>,
column?: Column_Internal<TFeatures, TData, TValue>,
) {
return (
(filterFn && filterFn.autoRemove
? filterFn.autoRemove(value, column as Column<TFeatures, TData, unknown>)
? filterFn.autoRemove(
value,
column as Column_Internal<TFeatures, TData, unknown>,
)
: false) ||
typeof value === 'undefined' ||
(typeof value === 'string' && !value)
Expand Down
28 changes: 13 additions & 15 deletions packages/table-core/src/types/Cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ import type { CellData, RowData } from './type-utils'
import type { ExtractFeatureTypes, TableFeatures } from './TableFeatures'
import type { Cell_Cell } from '../core/cells/coreCellsFeature.types'

export interface Cell_Plugins {}
/**
* Use this interface as a target for declaration merging to add your own plugin properties.
* Note: This will affect the types of all tables in your project.
*/
export interface Cell_Plugins<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
> {}

export interface Cell_Core<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
> extends Cell_Cell<TFeatures, TData, TValue>,
Cell_Plugins {}

// export type Cell<
// TFeatures extends TableFeatures,
// TData extends RowData,
// TValue extends CellData = CellData,
// > = Cell_Cell<TFeatures, TData, TValue> &
// UnionToIntersection<
// 'columnGroupingFeature' extends keyof TFeatures
// ? Cell_ColumnGrouping
// : never
// >
> extends Cell_Cell<TFeatures, TData, TValue> {}

export type Cell<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
> = Cell_Core<TFeatures, TData, TValue> & ExtractFeatureTypes<TFeatures, 'Cell'>
> = Cell_Core<TFeatures, TData, TValue> &
ExtractFeatureTypes<TFeatures, 'Cell'> &
Cell_Plugins<TFeatures, TData, TValue>
54 changes: 12 additions & 42 deletions packages/table-core/src/types/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,29 @@ import type { ExtractFeatureTypes, TableFeatures } from './TableFeatures'
import type { RowData } from './type-utils'
import type { ColumnDefBase_All } from './ColumnDef'

export interface Column_Plugins {}
/**
* Use this interface as a target for declaration merging to add your own plugin properties.
* Note: This will affect the types of all tables in your project.
*/
export interface Column_Plugins<
TFeatures extends TableFeatures,
TData extends RowData,
TValue = unknown,
> {}

export interface Column_Core<
TFeatures extends TableFeatures,
TData extends RowData,
TValue = unknown,
> extends Column_Column<TFeatures, TData, TValue>,
Column_Plugins {}

// export type Column<
// TFeatures extends TableFeatures,
// TData extends RowData,
// TValue = unknown,
// > = Column_Core<TFeatures, TData, TValue> &
// UnionToIntersection<
// | ('columnFacetingFeature' extends keyof TFeatures
// ? Column_ColumnFaceting<TFeatures, TData>
// : never)
// | ('columnFilteringFeature' extends keyof TFeatures
// ? Column_ColumnFiltering<TFeatures, TData>
// : never)
// | ('columnGroupingFeature' extends keyof TFeatures
// ? Column_ColumnGrouping<TFeatures, TData>
// : never)
// | ('columnOrderingFeature' extends keyof TFeatures
// ? Column_ColumnOrdering
// : never)
// | ('columnPinningFeature' extends keyof TFeatures
// ? Column_ColumnPinning
// : never)
// | ('columnResizingFeature' extends keyof TFeatures
// ? Column_ColumnResizing
// : never)
// | ('columnSizingFeature' extends keyof TFeatures
// ? Column_ColumnSizing
// : never)
// | ('columnVisibilityFeature' extends keyof TFeatures
// ? Column_ColumnVisibility
// : never)
// | ('globalFilteringFeature' extends keyof TFeatures
// ? Column_GlobalFiltering
// : never)
// | ('rowSortingFeature' extends keyof TFeatures
// ? Column_RowSorting<TFeatures, TData>
// : never)
// >
> extends Column_Column<TFeatures, TData, TValue> {}

export type Column<
TFeatures extends TableFeatures,
TData extends RowData,
TValue = unknown,
> = Column_Core<TFeatures, TData, TValue> &
ExtractFeatureTypes<TFeatures, 'Column'>
ExtractFeatureTypes<TFeatures, 'Column'> &
Column_Plugins<TFeatures, TData, TValue>

export type Column_Internal<
TFeatures extends TableFeatures,
Expand Down
47 changes: 12 additions & 35 deletions packages/table-core/src/types/ColumnDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import type { ColumnDef_ColumnVisibility } from '../features/column-visibility/c
import type { ColumnDef_GlobalFiltering } from '../features/global-filtering/globalFilteringFeature.types'
import type { ColumnDef_RowSorting } from '../features/row-sorting/rowSortingFeature.types'

export interface ColumnDef_Plugins {}
/**
* Use this interface as a target for declaration merging to add your own plugin properties.
* Note: This will affect the types of all tables in your project.
*/
export interface ColumnDef_Plugins<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
> {}

export interface ColumnMeta<
TFeatures extends TableFeatures,
Expand Down Expand Up @@ -57,51 +65,20 @@ interface ColumnDefBase_Core<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
> extends ColumnDef_Plugins {
> {
getUniqueValues?: AccessorFn<TData, Array<unknown>>
footer?: ColumnDefTemplate<HeaderContext<TFeatures, TData, TValue>>
cell?: ColumnDefTemplate<CellContext<TFeatures, TData, TValue>>
meta?: ColumnMeta<TFeatures, TData, TValue>
}

// export type ColumnDefBase<
// TFeatures extends TableFeatures,
// TData extends RowData,
// TValue extends CellData = CellData,
// > = ColumnDefBase_Core<TFeatures, TData, TValue> &
// UnionToIntersection<
// | ('columnVisibilityFeature' extends keyof TFeatures
// ? ColumnDef_ColumnVisibility
// : never)
// | ('columnPinningFeature' extends keyof TFeatures
// ? ColumnDef_ColumnPinning
// : never)
// | ('columnFilteringFeature' extends keyof TFeatures
// ? ColumnDef_ColumnFiltering<TFeatures, TData>
// : never)
// | ('globalFilteringFeature' extends keyof TFeatures
// ? ColumnDef_GlobalFiltering
// : never)
// | ('rowSortingFeature' extends keyof TFeatures
// ? ColumnDef_RowSorting<TFeatures, TData>
// : never)
// | ('columnGroupingFeature' extends keyof TFeatures
// ? ColumnDef_ColumnGrouping<TFeatures, TData, TValue>
// : never)
// | ('columnSizingFeature' extends keyof TFeatures
// ? ColumnDef_ColumnSizing
// : never)
// | ('columnResizingFeature' extends keyof TFeatures
// ? ColumnDef_ColumnResizing
// : never)
// >

export type ColumnDefBase<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
> = ColumnDefBase_Core<TFeatures, TData, TValue> &
ExtractFeatureTypes<TFeatures, 'ColumnDef'>
ExtractFeatureTypes<TFeatures, 'ColumnDef'> &
ColumnDef_Plugins<TFeatures, TData, TValue>

export type ColumnDefBase_All<
TFeatures extends TableFeatures,
Expand Down
Loading

0 comments on commit 9330b1d

Please sign in to comment.