diff --git a/examples/angular/grouping/src/app/app.component.ts b/examples/angular/grouping/src/app/app.component.ts index c4e711d994..524f3c152f 100644 --- a/examples/angular/grouping/src/app/app.component.ts +++ b/examples/angular/grouping/src/app/app.component.ts @@ -3,19 +3,18 @@ import { ChangeDetectionStrategy, Component, computed, - effect, signal, } from '@angular/core' import { - createAngularTable, FlexRenderDirective, + GroupingState, + Updater, + createAngularTable, getCoreRowModel, getExpandedRowModel, getFilteredRowModel, getGroupedRowModel, getPaginationRowModel, - GroupingState, - Updater, } from '@tanstack/angular-table' import { columns } from './columns' import { makeData } from './makeData' @@ -34,7 +33,7 @@ export class AppComponent { stringifiedGrouping = computed(() => JSON.stringify(this.grouping(), null, 2)) - table = createAngularTable(() => ({ + tableOptions = computed(() => ({ data: this.data(), columns: columns, state: { @@ -55,6 +54,8 @@ export class AppComponent { debugTable: true, })) + table = createAngularTable(this.tableOptions) + onPageInputChange(event: any): void { const page = event.target.value ? Number(event.target.value) - 1 : 0 this.table.setPageIndex(page) diff --git a/packages/angular-table/src/index.ts b/packages/angular-table/src/index.ts index 20e2db99b3..b702caeb74 100644 --- a/packages/angular-table/src/index.ts +++ b/packages/angular-table/src/index.ts @@ -1,96 +1,72 @@ +import { computed, signal } from '@angular/core' import { - computed, - effect, - inject, - Injector, - runInInjectionContext, - type Signal, - signal, - untracked, -} from '@angular/core' -import { - createTable, RowData, - type Table, TableOptions, TableOptionsResolved, + TableState, + createTable, + type Table, } from '@tanstack/table-core' -import { proxifyTable } from './proxy' import { lazyInit } from './lazy-signal-initializer' +import { proxifyTable } from './proxy' export * from '@tanstack/table-core' export { - FlexRenderDirective, FlexRenderComponent, + FlexRenderDirective, injectFlexRenderContext, } from './flex-render' export function createAngularTable( options: () => TableOptions -): Table & Signal> { - const injector = inject(Injector) +): Table { + return lazyInit(() => { + const resolvedOptions = { + state: {}, + onStateChange: () => {}, + renderFallbackValue: null, + ...options(), + } - return lazyInit(() => - runInInjectionContext(injector, () => { - const resolvedOptionsSignal = computed>( - () => { - return { - state: {}, - onStateChange: () => {}, - renderFallbackValue: null, - ...options(), - } - } - ) + const table = createTable(resolvedOptions) - const notifier = signal([], { equal: () => false }) - const table = createTable(untracked(resolvedOptionsSignal)) - const state = signal(table.initialState) + // By default, manage table state here using the table's initial state + const state = signal(table.initialState) - function updateOptions() { - const tableState = untracked(state) - const resolvedOptions = untracked(resolvedOptionsSignal) - untracked(() => { - table.setOptions(prev => ({ - ...prev, - ...resolvedOptions, - state: { ...tableState, ...resolvedOptions.state }, - onStateChange: updater => { - const value = - updater instanceof Function ? updater(tableState) : updater - state.set(value) - resolvedOptions.onStateChange?.(updater) - }, - })) - }) + // Compose table options using computed. + // This is to allow `tableSignal` to listen and set table option + const updatedOptions = computed>(() => { + // listen to table state changed + const tableState = state() + // listen to input options changed + const tableOptions = options() + return { + ...table.options, + ...resolvedOptions, + ...tableOptions, + state: { ...tableState, ...tableOptions.state }, + onStateChange: updater => { + const value = + updater instanceof Function ? updater(tableState) : updater + state.set(value) + resolvedOptions.onStateChange?.(updater) + }, } + }) - updateOptions() - - let firstRender = true - effect(() => { - void [state(), resolvedOptionsSignal()] - if (firstRender) { - return (firstRender = false) - } - untracked(() => { - updateOptions() - notifier.set([]) - }) - }) - - const tableSignal = computed( - () => { - notifier() - return table - }, - { - equal: () => false, - } - ) + // convert table instance to signal for proxify to listen to any table state and options changes + const tableSignal = computed( + () => { + table.setOptions(updatedOptions()) + return table + }, + { + equal: () => false, + } + ) - return proxifyTable(tableSignal) - }) - ) + // proxify Table instance to provide ability for consumer to listen to any table state changes + return proxifyTable(tableSignal) + }) }