UNPKG

@pastable/react

Version:
49 lines (48 loc) 3.93 kB
import { SetStateAction } from "react"; import { ObjectLiteral } from "@pastable/typings"; import { Formater } from "@pastable/utils"; /** Get/set page history with query params */ export declare const useQueryParams: <QP = ObjectLiteral<any>>(props?: UseQueryParamsProps<QP>) => readonly [Partial<QP>, (values: Partial<QP>, mode?: HistoryMode, toPath?: UseSetQueryParamsProps["toPath"]) => void, (keys?: (keyof QP)[]) => void]; export interface UseQueryParamsProps<QP = ObjectLiteral> extends Pick<UseSetQueryParamsProps, "toPath"> { /** Getter used to parse query params as object from query string */ getterFormater?: Formater<QP[keyof QP], any, keyof QP>; /** Setter used to serialize query string from query param object */ setterFormater?: Formater<QP[keyof QP], any, keyof QP>; /** Set default values for keys not yet in query params */ defaultValues?: QP; } export declare const getLocation: () => Location; /** Get parsed query params, might be formated using given method */ export declare const useCurrentQueryParams: <QP = ObjectLiteral<any>, F extends Formater<any, any, string> = Formater<any, any, string>>(formater?: F) => Partial<QP>; /** Control a queryParam from its key like a useState */ export declare const useQueryParamsState: <Value, Key extends string = any, QP = any>(key: Key, props?: UseQueryParamsStateProps<Value, Key, QP>) => undefined extends QP ? [Value, UseQueryParamsSetState<Value>] : [QP[Key extends keyof QP ? Key : never], UseQueryParamsSetState<QP[Key extends keyof QP ? Key : never]>]; export interface UseQueryParamsStateProps<Value, K, QP = ObjectLiteral> extends Pick<UseSetQueryParamsProps, "toPath"> { /** Getter used to parse query params as object from query string */ getterFormater?: Formater<QP[K extends keyof QP ? K : never], any, K>; /** Setter used to serialize query string from query param object */ setterFormater?: Formater<QP[K extends keyof QP ? K : never], any, K>; /** Set default values for keys not yet in query params */ defaultValue?: Value | QP[K extends keyof QP ? K : never]; } export declare type UseQueryParamsSetState<T = any> = (action: SetStateAction<T>) => void; export declare type UseQueryParamsState<T = any> = [T, UseQueryParamsSetState<T>]; export declare const getHistory: () => History; export declare type HistoryMode = "push" | "replace"; /** Update page history by merging current queryParams with values */ export declare const useSetQueryParams: <QP = ObjectLiteral<any>>({ toPath: toPathProp, formater, }?: UseSetQueryParamsProps) => (values: Partial<QP>, mode?: HistoryMode, toPath?: UseSetQueryParamsProps["toPath"]) => void; export declare type UseSetQueryParamsReturn<QP = ObjectLiteral> = ({ toPath: toPathProp, formater, }?: UseSetQueryParamsProps) => (values: Partial<QP>, mode?: HistoryMode, toPath?: UseSetQueryParamsProps["toPath"]) => void; export declare type UseSetQueryParamsToPathFn = (currentPathname: string) => string; export interface UseSetQueryParamsProps { /** * Allow overriding the pathname on which the history will be pushed/replaced, * defaults to current history.location.pathname * Either pass a static toPath or a function that will be given basePath as argument */ toPath?: string | UseSetQueryParamsToPathFn; /** Custom formater fn to be passed to useQueryParamsMerger */ formater?: Formater; } /** Merge current queryParams with values and return the resulting query string */ export declare const useQueryParamsMerger: <F extends Function = Formater<any, any, string>, QP = ObjectLiteral<any>>(customFormater?: F) => (values: Partial<QP>) => string; /** Remove object keys if not valid as query params */ export declare const formatObjToQueryString: <Value = ObjectLiteral<any>, F extends Function = Formater<any, any, string>>(obj: Value, customFormater?: F) => string;