-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular-table): added injector optional parameter for more flexi…
…bility (#5525) * feat(angular-table): improve adapter implementation (#5524) * Added optional injector for more flexibility * Replace runInInjectionContext with injector in effect * feat(angular-table): Added proxifyTable back * feat(angular-table): adding back notifier signal for table changed * feat(angular-table): Improve logic in setting table options *set table options inside computed before returning the table instance *remove redundant signals and effect *remove injector as it no longer required *update Grouping example to show how to pass signal when creating table
- Loading branch information
Showing
2 changed files
with
55 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TData extends RowData>( | ||
options: () => TableOptions<TData> | ||
): Table<TData> & Signal<Table<TData>> { | ||
const injector = inject(Injector) | ||
): Table<TData> { | ||
return lazyInit(() => { | ||
const resolvedOptions = { | ||
state: {}, | ||
onStateChange: () => {}, | ||
renderFallbackValue: null, | ||
...options(), | ||
} | ||
|
||
return lazyInit(() => | ||
runInInjectionContext(injector, () => { | ||
const resolvedOptionsSignal = computed<TableOptionsResolved<TData>>( | ||
() => { | ||
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<TableState>(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<TableOptionsResolved<TData>>(() => { | ||
// 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) | ||
}) | ||
} |