Skip to content

Commit

Permalink
fix: inject data type to features
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersayshi committed Jan 12, 2025
1 parent 10334bf commit 75ead93
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 51 deletions.
9 changes: 6 additions & 3 deletions packages/angular-table/src/injectTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export function injectTable<
TFeatures extends TableFeatures,
TData extends RowData,
>(
options: () => TableOptions<TFeatures, TData>,
): Table<TFeatures, TData> & Signal<Table<TFeatures, TData>> {
options: () => TableOptions<TFeatures & { Data: TData }, TData>,
): Table<TFeatures & { Data: TData }, TData> &
Signal<Table<TFeatures & { Data: TData }, TData>> {
return lazyInit(() => {
const features = () => {
return {
Expand All @@ -47,7 +48,9 @@ export function injectTable<

// Compose table options using computed.
// This is to allow `tableSignal` to listen and set table option
const updatedOptions = computed<TableOptions<TFeatures, TData>>(() => {
const updatedOptions = computed<
TableOptions<TFeatures & { Data: TData }, TData>
>(() => {
// listen to table state changed
const tableState = state()
// listen to input options changed
Expand Down
8 changes: 5 additions & 3 deletions packages/qwik-table/src/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ import type {
export function useTable<
TFeatures extends TableFeatures,
TData extends RowData,
>(tableOptions: TableOptions<TFeatures, TData>): Table<TFeatures, TData> {
>(
tableOptions: TableOptions<TFeatures & { Data: TData }, TData>,
): Table<TFeatures & { Data: TData }, TData> {
const _features = { ...coreFeatures, ...tableOptions._features }

const state = useSignal(
getInitialTableState(_features, tableOptions.initialState),
)

const statefulOptions: TableOptions<TFeatures, TData> = {
const statefulOptions: TableOptions<TFeatures & { Data: TData }, TData> = {
...tableOptions,
_features,
state: {
Expand All @@ -37,7 +39,7 @@ export function useTable<
}

const table = useStore<{
instance: NoSerialize<Table<TFeatures, TData>>
instance: NoSerialize<Table<TFeatures & { Data: TData }, TData>>
}>({
instance: noSerialize(constructTable(statefulOptions)),
})
Expand Down
5 changes: 2 additions & 3 deletions packages/react-table/src/createTableHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export function createTableHelper<
tableHelperOptions: TableHelperOptions<TFeatures, TData>,
): TableHelper<TFeatures, TData> {
const tableHelper = constructTableHelper(useTable, tableHelperOptions)
return {
...tableHelper,
return Object.assign(tableHelper, {
useTable: tableHelper.tableCreator,
} as unknown as TableHelper<TFeatures, TData>
}) as unknown as TableHelper<TFeatures, TData>
}

// test
Expand Down
6 changes: 4 additions & 2 deletions packages/table-core/src/core/table/constructTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export function getInitialTableState<TFeatures extends TableFeatures>(
export function constructTable<
TFeatures extends TableFeatures,
TData extends RowData,
>(options: TableOptions<TFeatures, TData>): Table<TFeatures, TData> {
>(
options: TableOptions<TFeatures, TData>,
): Table<TFeatures & { Data: TData }, TData> {
const { _features = {} as TFeatures } = options

const featuresList: Array<TableFeature<{}>> = Object.values(_features)
Expand All @@ -35,7 +37,7 @@ export function constructTable<
)
}

const table = {} as unknown as Table<TFeatures, TData>
const table = {} as unknown as Table<TFeatures & { Data: TData }, TData>

const defaultOptions = featuresList.reduce((obj, feature) => {
return Object.assign(obj, feature.getDefaultTableOptions?.(table as any))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
RowData,
Updater,
} from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { GetFeatureData, TableFeatures } from '../../types/TableFeatures'
import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'
import type { Row } from '../../types/Row'
import type { Column } from '../../types/Column'
Expand Down Expand Up @@ -80,7 +80,7 @@ export type FilterFnOption<

export interface ColumnDef_ColumnFiltering<
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
> {
/**
* Enables/disables the **column** filter for this column.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
RowData,
Updater,
} from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { GetFeatureData, TableFeatures } from '../../types/TableFeatures'
import type { Row } from '../../types/Row'
import type { Cell } from '../../types/Cell'
import type { ColumnDefTemplate } from '../../types/ColumnDef'
Expand Down Expand Up @@ -52,7 +52,7 @@ export type AggregationFnOption<

export interface ColumnDef_ColumnGrouping<
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
TValue extends CellData = CellData,
> {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'
import type { Table } from '../../types/Table'
import type { BuiltInSortFn } from '../../fns/sortFns'
import type { OnChangeFn, RowData, Updater } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { GetFeatureData, TableFeatures } from '../../types/TableFeatures'
import type { Row } from '../../types/Row'

export type SortDirection = 'asc' | 'desc'
Expand Down Expand Up @@ -50,7 +50,7 @@ export type SortFnOption<

export interface ColumnDef_RowSorting<
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
> {
/**
* Enables/Disables multi-sorting for this column.
Expand Down
26 changes: 0 additions & 26 deletions packages/table-core/src/types/ColumnDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,6 @@ export type ColumnDefBase<
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)
> &
ExtractFeatureTypes<'ColumnDef', TFeatures> &
ColumnDef_Plugins<TFeatures, TData, TValue>

Expand Down
27 changes: 19 additions & 8 deletions packages/table-core/src/types/TableFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ExtractFeatureTypes<
>

interface FeatureConstructors {
Data?: RowData
CachedRowModel?: any
Cell?: any
Column?: any
Expand All @@ -45,11 +46,16 @@ export interface Plugins {}
export interface TableFeatures
extends Partial<CoreFeatures>,
Partial<StockFeatures>,
Partial<Plugins> {}
Partial<Plugins> {
Data?: RowData
}

export type GetFeatureData<TFeatures extends TableFeatures> =
TFeatures['Data'] extends undefined ? RowData : NonNullable<TFeatures['Data']>

export type ConstructCellAPIs<TConstructors extends FeatureConstructors> = <
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TableFeatures>,
TValue extends CellData = CellData,
>(
cell: Cell<TFeatures, TData, TValue> &
Expand All @@ -65,7 +71,7 @@ export type ConstructCellAPIs<TConstructors extends FeatureConstructors> = <

export type ConstructColumnAPIs<TConstructors extends FeatureConstructors> = <
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue> &
Expand All @@ -77,7 +83,7 @@ export type ConstructColumnAPIs<TConstructors extends FeatureConstructors> = <

export type ConstructHeaderAPIs<TConstructors extends FeatureConstructors> = <
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
TValue extends CellData = CellData,
>(
header: Header<TFeatures, TData, TValue> &
Expand All @@ -92,14 +98,14 @@ export type ConstructHeaderAPIs<TConstructors extends FeatureConstructors> = <

export type ConstructRowAPIs<TConstructors extends FeatureConstructors> = <
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
>(
row: Row<TFeatures, TData> & Partial<TConstructors['Row']>,
) => void

export type ConstructTableAPIs<TConstructors extends FeatureConstructors> = <
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
>(
table: Table_Internal<TFeatures, TData> &
Partial<TConstructors['Table']> & {
Expand All @@ -109,13 +115,18 @@ export type ConstructTableAPIs<TConstructors extends FeatureConstructors> = <

export type GetDefaultColumnDef<TConstructors extends FeatureConstructors> = <
TFeatures extends TableFeatures,
TData extends RowData,
TData extends RowData = GetFeatureData<TFeatures>,
TValue extends CellData = CellData,
>() => ColumnDefBase_All<TFeatures, TData, TValue> &
Partial<TConstructors['ColumnDef']>

export type GetDefaultTableOptions<TConstructors extends FeatureConstructors> =
<TFeatures extends TableFeatures, TData extends RowData>(
<
TFeatures extends TableFeatures,
TData extends RowData = TConstructors['Data'] extends undefined
? RowData
: NonNullable<TConstructors['Data']>,
>(
table: Table_Internal<TFeatures, TData> & Partial<TConstructors['Table']>,
) => Partial<TableOptions_All<TFeatures, TData>> &
Partial<TConstructors['TableOptions']>
Expand Down

0 comments on commit 75ead93

Please sign in to comment.