solid-awesome-hooks
Version:
A collection of awesome hooks for solid-js
24 lines (23 loc) • 981 B
JavaScript
import { createMemo, createSignal } from "solid-js";
export var SortState;
(function (SortState) {
SortState[SortState["ASCENDING"] = 1] = "ASCENDING";
SortState[SortState["DESCENDING"] = -1] = "DESCENDING";
})(SortState || (SortState = {}));
export const useSortState = (initialSortState = SortState.ASCENDING) => {
const [order, setOrder] = createSignal(initialSortState);
const toggleOrder = () => setOrder((state) => (state === SortState.ASCENDING ? SortState.DESCENDING : SortState.ASCENDING));
const isAscending = createMemo(() => order() === SortState.ASCENDING);
const isDescending = createMemo(() => order() === SortState.DESCENDING);
const resetOrder = () => setOrder(initialSortState);
return {
order,
setOrder,
isAscending,
isDescending,
/** Switches order to another one */
toggleOrder,
/** Resets sort order to the initial */
resetOrder,
};
};