UNPKG

nuqs

Version:

Type-safe search params state manager for React - Like useState, but stored in the URL query string

51 lines (48 loc) 1.47 kB
'use client'; import "../debounce-CmqzOOem.js"; import { context, renderQueryString } from "../context-BWo_DoaC.js"; import { resetQueues } from "../reset-BmRuoRYl.js"; import { createElement } from "react"; //#region src/adapters/testing.ts function NuqsTestingAdapter({ resetUrlUpdateQueueOnMount = true, defaultOptions,...props }) { if (resetUrlUpdateQueueOnMount) resetQueues(); const useAdapter = () => ({ searchParams: new URLSearchParams(props.searchParams), updateUrl(search, options) { props.onUrlUpdate?.({ searchParams: search, queryString: renderQueryString(search), options }); }, getSearchParamsSnapshot() { return new URLSearchParams(props.searchParams); }, rateLimitFactor: props.rateLimitFactor ?? 0 }); return createElement(context.Provider, { value: { useAdapter, defaultOptions } }, props.children); } /** * A higher order component that wraps the children with the NuqsTestingAdapter * * It allows creating wrappers for testing purposes by providing only the * necessary props to the NuqsTestingAdapter. * * Usage: * ```tsx * render(<MyComponent />, { * wrapper: withNuqsTestingAdapter({ searchParams: '?foo=bar' }) * }) * ``` */ function withNuqsTestingAdapter(props = {}) { return function NuqsTestingAdapterWrapper({ children }) { return createElement(NuqsTestingAdapter, props, children); }; } //#endregion export { NuqsTestingAdapter, withNuqsTestingAdapter }; //# sourceMappingURL=testing.js.map