mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-11 15:23:17 +01:00
refactor table sorting to composables and shared SortableTableHeaderCell
This commit is contained in:
32
resources/js/utils/useTableSortState.ts
Normal file
32
resources/js/utils/useTableSortState.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { useStorage } from '@vueuse/core';
|
||||
import type { SortDirection } from '@/utils/useSortableTable';
|
||||
|
||||
export interface TableSortState<TColumn extends string> {
|
||||
sortColumn: TColumn;
|
||||
sortDirection: SortDirection;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sort a table page remembers between visits, persisted in localStorage.
|
||||
*
|
||||
* Pages that persist more than the sort (filters, for example) pass the wider defaults
|
||||
* and get them back on `tableState`; `mergeDefaults` is forwarded to `useStorage` so
|
||||
* nested state can be merged key by key rather than wholesale.
|
||||
*/
|
||||
export function useTableSortState<
|
||||
TColumn extends string,
|
||||
TState extends TableSortState<TColumn> = TableSortState<TColumn>,
|
||||
>(
|
||||
key: string,
|
||||
defaults: TState,
|
||||
mergeDefaults: boolean | ((storageValue: TState, defaults: TState) => TState) = true
|
||||
) {
|
||||
const tableState = useStorage<TState>(key, defaults, undefined, { mergeDefaults });
|
||||
|
||||
function handleSort(column: TColumn, direction: SortDirection) {
|
||||
tableState.value.sortColumn = column;
|
||||
tableState.value.sortDirection = direction;
|
||||
}
|
||||
|
||||
return { tableState, handleSort };
|
||||
}
|
||||
Reference in New Issue
Block a user