( options: TableOptions<TData> )
| 31 | } |
| 32 | |
| 33 | export function useQwikTable<TData extends RowData>( |
| 34 | options: TableOptions<TData> |
| 35 | ) { |
| 36 | // Compose in the generic options to the user options |
| 37 | const resolvedOptions: TableOptionsResolved<TData> = { |
| 38 | state: {}, |
| 39 | onStateChange: () => {}, |
| 40 | renderFallbackValue: null, |
| 41 | ...options, |
| 42 | } |
| 43 | |
| 44 | // Create a new table instance and store it in a Qwik store |
| 45 | const table = Qwik.useStore<{ |
| 46 | instance: Qwik.NoSerialize<Table<TData>> |
| 47 | }>({ |
| 48 | instance: Qwik.noSerialize(createTable(resolvedOptions)), |
| 49 | }) |
| 50 | |
| 51 | // By default, manage table state here using the table's initial state |
| 52 | const state = Qwik.useSignal(table.instance!.initialState) |
| 53 | |
| 54 | // Compose the default state above with any user state. This will allow the user |
| 55 | // to only control a subset of the state if desired. |
| 56 | table.instance!.setOptions(prev => ({ |
| 57 | ...prev, |
| 58 | ...options, |
| 59 | state: { |
| 60 | ...state.value, |
| 61 | ...options.state, |
| 62 | }, |
| 63 | // Similarly, we'll maintain both our internal state and any user-provided |
| 64 | // state. |
| 65 | onStateChange: updater => { |
| 66 | state.value = updater instanceof Function ? updater(state.value) : updater |
| 67 | options.onStateChange?.(updater) |
| 68 | }, |
| 69 | })) |
| 70 | |
| 71 | return table.instance! |
| 72 | } |
no test coverage detected
searching dependent graphs…