Skip to content

Commit 173db1d

Browse files
committed
createTableHook add typed helpers
1 parent 6002827 commit 173db1d

3 files changed

Lines changed: 40 additions & 17 deletions

File tree

packages/angular-table/src/helpers/createTableHook.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030
TableOptions,
3131
TableState,
3232
} from '@tanstack/table-core'
33-
import type { Type } from '@angular/core'
33+
import type { Signal, Type } from '@angular/core'
3434

3535
type RenderableComponent =
3636
| Type<any>
@@ -247,7 +247,20 @@ export type AppAngularTable<
247247
TTableComponents extends Record<string, RenderableComponent>,
248248
TCellComponents extends Record<string, RenderableComponent>,
249249
THeaderComponents extends Record<string, RenderableComponent>,
250-
> = AngularTable<TFeatures, TData, TSelected> & NoInfer<TTableComponents>
250+
> = AngularTable<TFeatures, TData, TSelected> &
251+
NoInfer<TTableComponents> & {
252+
appCell: <TValue>(
253+
cell: Cell<TFeatures, TData, TValue>,
254+
) => Cell<TFeatures, TData, TValue> & NoInfer<TCellComponents>
255+
256+
appHeader: <TValue>(
257+
header: Header<TFeatures, TData, TValue>,
258+
) => Header<TFeatures, TData, TValue> & NoInfer<THeaderComponents>
259+
260+
appFooter: <TValue>(
261+
footer: Header<TFeatures, TData, TValue>,
262+
) => Header<TFeatures, TData, TValue> & NoInfer<THeaderComponents>
263+
}
251264

252265
// =============================================================================
253266
// CreateTableHook Options and Props
@@ -305,7 +318,9 @@ export function createTableHook<
305318
TCellComponents,
306319
THeaderComponents
307320
>) {
308-
function injectTableContext<TData extends RowData = RowData>() {
321+
function injectTableContext<TData extends RowData = RowData>(): Signal<
322+
AngularTable<TFeatures, TData>
323+
> {
309324
return _injectTableContext<TFeatures, TData>()
310325
}
311326

@@ -345,9 +360,21 @@ export function createTableHook<
345360
TCellComponents,
346361
THeaderComponents
347362
> {
363+
function appCell(cell: Cell<TFeatures, TData, any>) {
364+
return cell as Cell<TFeatures, TData, any> & TCellComponents
365+
}
366+
367+
function appHeader(header: Cell<TFeatures, TData, any>) {
368+
return header as Cell<TFeatures, TData, any> & TCellComponents
369+
}
370+
371+
function appFooter(footer: Cell<TFeatures, TData, any>) {
372+
return footer as Cell<TFeatures, TData, any> & TCellComponents
373+
}
374+
348375
const appTableFeatures: TableFeature<{}> = {
349376
constructTableAPIs: (table) => {
350-
Object.assign(table, tableComponents)
377+
Object.assign(table, tableComponents, { appCell, appHeader, appFooter })
351378
},
352379
assignCellPrototype(prototype) {
353380
Object.assign(prototype, cellComponents)
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import { Directive, InjectionToken, inject, input } from '@angular/core'
2-
import { RowData, Table, TableFeatures } from '@tanstack/table-core'
2+
import { RowData, TableFeatures, TableState } from '@tanstack/table-core'
3+
import { AngularTable } from '../injectTable'
34
import type { Signal } from '@angular/core'
45

56
export const TanStackTableToken = new InjectionToken<
6-
TanStackTableContext<any, any>['table']
7+
Signal<AngularTable<any, any>>
78
>('[TanStack Table] Table Context')
89

9-
export interface TanStackTableContext<
10-
TFeatures extends TableFeatures,
11-
TData extends RowData,
12-
> {
13-
table: Signal<Table<TFeatures, TData>>
14-
}
15-
1610
@Directive({
1711
selector: '[tanStackTable]',
1812
exportAs: 'table',
@@ -26,15 +20,17 @@ export interface TanStackTableContext<
2620
export class TanStackTable<
2721
TFeatures extends TableFeatures,
2822
TData extends RowData,
29-
> implements TanStackTableContext<TFeatures, TData> {
30-
readonly table = input.required<Table<TFeatures, TData>>({
23+
TSelected extends {} = TableState<TFeatures>,
24+
> {
25+
readonly table = input.required<AngularTable<TFeatures, TData, TSelected>>({
3126
alias: 'tanStackTable',
3227
})
3328
}
3429

3530
export function injectTableContext<
3631
TFeatures extends TableFeatures,
3732
TData extends RowData,
38-
>(): TanStackTableContext<TFeatures, TData>['table'] {
33+
TSelected extends {} = TableState<TFeatures>,
34+
>(): Signal<AngularTable<TFeatures, TData, TSelected>> {
3935
return inject(TanStackTableToken)
4036
}

packages/angular-table/src/injectTable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { Signal, ValueEqualityFn } from '@angular/core'
2222
export type AngularTable<
2323
TFeatures extends TableFeatures,
2424
TData extends RowData,
25-
TSelected = {},
25+
TSelected = TableState<TFeatures>,
2626
> = Table<TFeatures, TData> & {
2727
/**
2828
* The selected state from the table store, based on the selector provided.

0 commit comments

Comments
 (0)