Skip to content

Commit e6315c3

Browse files
committed
refactor table components to use inject functions and update documentation
1 parent 9d782a8 commit e6315c3

3 files changed

Lines changed: 83 additions & 32 deletions

File tree

examples/angular/composable-tables/src/app/app.component.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<h1>Composable Tables Example</h1>
22
<p class="description">
3-
Both tables below use the same <code>useAppTable</code> hook and shareable
4-
components, but with different data types and column configurations.
3+
Both tables below use the same <code>injectAppTable</code> function and
4+
shareable components, but with different data types and column
5+
configurations.
56
</p>
67

78
<users-table />

examples/angular/composable-tables/src/app/table.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import {
1717
sortFns,
1818
tableFeatures,
1919
} from '@tanstack/angular-table'
20+
2021
// Import table-level components
2122
import {
2223
PaginationControls,
2324
RowCount,
2425
TableToolbar,
2526
} from './components/table-components'
27+
// Import cell-level components
2628
import {
2729
CategoryCell,
2830
NumberCell,
@@ -32,43 +34,33 @@ import {
3234
StatusCell,
3335
TextCell,
3436
} from './components/cell-components'
37+
// Import header-level components (both use injectTableHeaderContext())
3538
import {
3639
ColumnFilter,
3740
FooterColumnId,
3841
FooterSum,
3942
SortIndicator,
4043
} from './components/header-components'
4144

42-
// Import table-level components
43-
// import {
44-
// PaginationControls,
45-
// RowCount,
46-
// TableToolbar,
47-
// } from '../components/table-components'
48-
49-
// Import cell-level components
50-
51-
// Import header/footer-level components (both use useHeaderContext)
52-
5345
/**
5446
* Create the custom table hook with all pre-bound components.
5547
* This exports:
5648
* - createAppColumnHelper: Create column definitions with TFeatures already bound
57-
* - useAppTable: Hook for creating tables with TFeatures baked in
58-
* - useTableContext: Access table instance in tableComponents
59-
* - useCellContext: Access cell instance in cellComponents
60-
* - useHeaderContext: Access header instance in headerComponents
49+
* - injectAppTable: Function for creating tables with TFeatures baked in
50+
* - injectTableContext: Access table instance in tableComponents
51+
* - injectTableCellContext: Access cell instance in cellComponents
52+
* - injectTableHeaderContext: Access header instance in headerComponents
53+
* - injectFlexRenderHeaderContext: Access FlexRenderContext with header-level typings
54+
* - injectFlexRenderCellContext: Access FlexRenderContext with header-level typings
6155
*/
6256
export const {
6357
createAppColumnHelper,
6458
injectAppTable,
6559
injectTableContext,
6660
injectTableCellContext,
6761
injectTableHeaderContext,
68-
// useAppTable,
69-
// useTableContext,
70-
// useCellContext,
71-
// useHeaderContext,
62+
// injectFlexRenderHeaderContext
63+
// injectFlexRenderCellContext
7264
} = createTableHook({
7365
// Features are set once here and shared across all tables
7466
_features: tableFeatures({

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

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,53 @@ export type CreateTableContextOptions<
302302
headerComponents?: THeaderComponents
303303
}
304304

305+
export type CreateTableHookResult<
306+
TFeatures extends TableFeatures,
307+
TTableComponents extends Record<string, RenderableComponent>,
308+
TCellComponents extends Record<string, RenderableComponent>,
309+
THeaderComponents extends Record<string, RenderableComponent>,
310+
> = {
311+
createAppColumnHelper: <TData extends RowData>() => AppColumnHelper<
312+
TFeatures,
313+
TData,
314+
TCellComponents,
315+
THeaderComponents
316+
>
317+
injectTableContext: <TData extends RowData = RowData>() => Signal<
318+
AngularTable<TFeatures, TData>
319+
>
320+
injectTableHeaderContext: <
321+
TValue extends CellData = CellData,
322+
TRowData extends RowData = RowData,
323+
>() => Signal<Header<TFeatures, TRowData, TValue>>
324+
injectTableCellContext: <
325+
TValue extends CellData = CellData,
326+
TRowData extends RowData = RowData,
327+
>() => Signal<Cell<TFeatures, TRowData, TValue>>
328+
injectFlexRenderHeaderContext: <
329+
TData extends RowData,
330+
TValue extends CellData,
331+
>() => HeaderContext<TFeatures, TData, TValue>
332+
injectFlexRenderCellContext: <
333+
TData extends RowData,
334+
TValue extends CellData,
335+
>() => CellContext<TFeatures, TData, TValue>
336+
injectAppTable: <TData extends RowData, TSelected = {}>(
337+
tableOptions: () => Omit<
338+
TableOptions<TFeatures, TData>,
339+
'_features' | '_rowModels'
340+
>,
341+
selector?: (state: TableState<TFeatures>) => TSelected,
342+
) => AppAngularTable<
343+
TFeatures,
344+
TData,
345+
TSelected,
346+
TTableComponents,
347+
TCellComponents,
348+
THeaderComponents
349+
>
350+
}
351+
305352
export function createTableHook<
306353
TFeatures extends TableFeatures,
307354
const TTableComponents extends Record<string, RenderableComponent>,
@@ -317,32 +364,43 @@ export function createTableHook<
317364
TTableComponents,
318365
TCellComponents,
319366
THeaderComponents
320-
>) {
367+
>): CreateTableHookResult<
368+
TFeatures,
369+
TTableComponents,
370+
TCellComponents,
371+
THeaderComponents
372+
> {
321373
function injectTableContext<TData extends RowData = RowData>(): Signal<
322374
AngularTable<TFeatures, TData>
323375
> {
324376
return _injectTableContext<TFeatures, TData>()
325377
}
326378

327-
function injectTableHeaderContext<TValue extends CellData = CellData>() {
328-
return _injectTableHeaderContext<TFeatures, any, TValue>()
379+
function injectTableHeaderContext<
380+
TValue extends CellData = CellData,
381+
TRowData extends RowData = RowData,
382+
>(): Signal<Header<TFeatures, TRowData, TValue>> {
383+
return _injectTableHeaderContext<TFeatures, TRowData, TValue>()
329384
}
330385

331-
function injectTableCellContext<TValue extends CellData = CellData>() {
332-
return _injectTableCellContext<TFeatures, any, TValue>()
386+
function injectTableCellContext<
387+
TValue extends CellData = CellData,
388+
TRowData extends RowData = RowData,
389+
>(): Signal<Cell<TFeatures, TRowData, TValue>> {
390+
return _injectTableCellContext<TFeatures, TRowData, TValue>()
333391
}
334392

335393
function injectFlexRenderHeaderContext<
336394
TData extends RowData,
337395
TValue extends CellData,
338-
>() {
396+
>(): HeaderContext<TFeatures, TData, TValue> {
339397
return injectFlexRenderContext<HeaderContext<TFeatures, TData, TValue>>()
340398
}
341399

342400
function injectFlexRenderCellContext<
343401
TData extends RowData,
344402
TValue extends CellData,
345-
>() {
403+
>(): CellContext<TFeatures, TData, TValue> {
346404
return injectFlexRenderContext<CellContext<TFeatures, TData, TValue>>()
347405
}
348406

@@ -364,12 +422,12 @@ export function createTableHook<
364422
return cell as Cell<TFeatures, TData, any> & TCellComponents
365423
}
366424

367-
function appHeader(header: Cell<TFeatures, TData, any>) {
368-
return header as Cell<TFeatures, TData, any> & TCellComponents
425+
function appHeader(header: Header<TFeatures, TData, any>) {
426+
return header as Header<TFeatures, TData, any> & THeaderComponents
369427
}
370428

371-
function appFooter(footer: Cell<TFeatures, TData, any>) {
372-
return footer as Cell<TFeatures, TData, any> & TCellComponents
429+
function appFooter(footer: Header<TFeatures, TData, any>) {
430+
return footer as Header<TFeatures, TData, any> & THeaderComponents
373431
}
374432

375433
const appTableFeatures: TableFeature<{}> = {

0 commit comments

Comments
 (0)