( options: () => TableOptions<TData> )
| 26 | } from './flex-render/flex-render-component' |
| 27 | |
| 28 | export function createAngularTable<TData extends RowData>( |
| 29 | options: () => TableOptions<TData> |
| 30 | ): Table<TData> & Signal<Table<TData>> { |
| 31 | return lazyInit(() => { |
| 32 | const resolvedOptions = { |
| 33 | state: {}, |
| 34 | onStateChange: () => {}, |
| 35 | renderFallbackValue: null, |
| 36 | ...options(), |
| 37 | } |
| 38 | |
| 39 | const table = createTable(resolvedOptions) |
| 40 | |
| 41 | // By default, manage table state here using the table's initial state |
| 42 | const state = signal<TableState>(table.initialState) |
| 43 | |
| 44 | // Compose table options using computed. |
| 45 | // This is to allow `tableSignal` to listen and set table option |
| 46 | const updatedOptions = computed<TableOptionsResolved<TData>>(() => { |
| 47 | // listen to table state changed |
| 48 | const tableState = state() |
| 49 | // listen to input options changed |
| 50 | const tableOptions = options() |
| 51 | return { |
| 52 | ...table.options, |
| 53 | ...resolvedOptions, |
| 54 | ...tableOptions, |
| 55 | state: { ...tableState, ...tableOptions.state }, |
| 56 | onStateChange: updater => { |
| 57 | const value = |
| 58 | updater instanceof Function ? updater(tableState) : updater |
| 59 | state.set(value) |
| 60 | resolvedOptions.onStateChange?.(updater) |
| 61 | }, |
| 62 | } |
| 63 | }) |
| 64 | |
| 65 | // convert table instance to signal for proxify to listen to any table state and options changes |
| 66 | const tableSignal = computed( |
| 67 | () => { |
| 68 | table.setOptions(updatedOptions()) |
| 69 | return table |
| 70 | }, |
| 71 | { |
| 72 | equal: () => false, |
| 73 | } |
| 74 | ) |
| 75 | |
| 76 | // proxify Table instance to provide ability for consumer to listen to any table state changes |
| 77 | return proxifyTable(tableSignal) |
| 78 | }) |
| 79 | } |
no test coverage detected
searching dependent graphs…