clear-react-router 2.1.9 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -121,7 +121,7 @@ Component for client-side navigation with prefetch support, active state detecti
121
121
  | Prop | Type | Default | Description |
122
122
  |------|------|---------|-------------|
123
123
  | `to` | `string` | required | Target path |
124
- | `search` | `string \| undefined` | `undefined` | Query string appended to the target path |
124
+ | `search` | `string \| Record<string, string \| number \| boolean \| null \| undefined> \| undefined` | `undefined` | Query string or object appended to the target path |
125
125
  | `state` | `unknown` | `undefined` | Arbitrary value attached to the navigation entry |
126
126
  | `as` | `(props: ElementProps<T>, state: { isActive: boolean; isPending: boolean }) => ReactElement` | renders `<a>` | Render function for using a custom element/component instead of the default <a>. Receives the props to spread onto your element (href, ref, event handlers, className, style, children) as the first argument, and `{ isActive, isPending }` as a separate second argument — kept separate so these values are never accidentally forwarded to the DOM |
127
127
  | `exact` | `boolean` | `false` | When `false`, the link is also considered active if the current URL starts with `to` (useful for nested routes) |
@@ -235,8 +235,8 @@ import { Button } from '@mui/material';
235
235
  Dashboard
236
236
  </Link>
237
237
 
238
- // With dynamic style
239
- <Link to="/profile" style={({ isActive }) => ({ fontWeight: isActive ? 'bold' : 'normal' })}>
238
+ // With dynamic style and search object
239
+ <Link to={{ pathname: '/profile', search: { user: 'John Doe' } }} style={({ isActive }) => ({ fontWeight: isActive ? 'bold' : 'normal' })}>
240
240
  Profile
241
241
  </Link>
242
242
 
@@ -538,16 +538,16 @@ const App = () => <Router routes={routes} errorBoundary={ErrorBoundary} />
538
538
 
539
539
  ### `useNavigate()`
540
540
 
541
- Returns function to navigate programmatically. Accepts a string (pathname), an object of type Location, or `-1` to go back.
541
+ Returns a function to navigate programmatically. Accepts a string (pathname, optionally with a `?query`), a `NavigationLocation` object, or `-1` to go back.
542
542
 
543
543
  ```tsx
544
- type Location = { pathname: string; search?: string; state?: unknown }
544
+ type NavigationLocation = { pathname: string; search?: string | Record<string, string | number | boolean | null | undefined>; state?: unknown }
545
545
 
546
546
  const navigate = useNavigate();
547
547
 
548
- navigate('/about'); // string
549
- navigate({ pathname: '/user/123', state: { from: 'home' } }); // Location
550
- navigate(-1); // go back
548
+ navigate('/about'); // string
549
+ navigate({ pathname: '/user/123', search: { user: 'Jane Doe' }, state: { from: 'home' } }); // NavigationLocation
550
+ navigate(-1); // go back
551
551
  ```
552
552
 
553
553
  **Note:** Navigation state can be accessed via `useLocation()`:
@@ -1,12 +1,12 @@
1
1
  import { type CSSProperties, ReactNode, ReactElement } from 'react';
2
- import { ElementProps, RouterProps } from '../types';
2
+ import { ElementProps, RouterProps, SearchObject } from '../types';
3
3
  type ElementState = {
4
4
  isActive: boolean;
5
5
  isPending: boolean;
6
6
  };
7
7
  type LinkProps<T extends HTMLElement = HTMLAnchorElement> = {
8
8
  to: string;
9
- search?: string;
9
+ search?: string | SearchObject;
10
10
  state?: unknown;
11
11
  children?: ReactNode;
12
12
  as?: (props: ElementProps<T>, state: ElementState) => ReactElement;
@@ -1 +1 @@
1
- export declare const useNavigate: () => (arg: import("..").Location | string | -1) => Promise<void>;
1
+ export declare const useNavigate: () => (arg: import("..").NavigationLocation | string | -1) => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -13,4 +13,4 @@ export { useSearchParams } from './hooks/useSearchParams';
13
13
  export { useSubmitAction } from './hooks/useSubmitAction';
14
14
  export { createRouter } from './creators/createRouter';
15
15
  export { lazy } from './utils/lazy';
16
- export type { RouteItem, BlockerState, Location, RouterProps, ElementProps } from './types';
16
+ export type { RouteItem, BlockerState, Location, RouterProps, ElementProps, NavigationLocation } from './types';
package/dist/index.js CHANGED
@@ -209,6 +209,14 @@ var restoreScroll = (scrollMap, pathname, behavior) => {
209
209
  });
210
210
  });
211
211
  };
212
+ var formatSearchObject = (searchObject) => {
213
+ const params = new URLSearchParams();
214
+ Object.entries(searchObject).forEach(([key, value]) => {
215
+ if (value !== void 0 && value !== null) params.set(key, String(value));
216
+ });
217
+ const result = params.toString();
218
+ return result ? `?${result}` : "";
219
+ };
212
220
  var findRoute = (pathname, includeAll) => {
213
221
  if (includeAll) return routerConfig.routes.find((el) => el.path === "*" || comparePaths(el, pathname));
214
222
  return routerConfig.routes.find((el) => comparePaths(el, pathname));
@@ -591,8 +599,18 @@ var createRouterInstance = () => {
591
599
  return async (arg) => {
592
600
  if (arg === -1) return history.go(arg);
593
601
  if (typeof arg === "string") {
594
- if (arg !== location.pathname) await navigate({ pathname: arg });
595
- } else if (JSON.stringify(arg) !== JSON.stringify(location)) await navigate(arg);
602
+ const [pathname, search = ""] = arg.split("?");
603
+ if (pathname !== location.pathname || search !== location.search) await navigate({
604
+ pathname,
605
+ search
606
+ });
607
+ return;
608
+ }
609
+ const search = typeof arg.search === "object" ? formatSearchObject(arg.search) : arg.search;
610
+ await navigate({
611
+ ...arg,
612
+ search
613
+ });
596
614
  };
597
615
  },
598
616
  useAction: (action, options = {}) => {
@@ -839,12 +857,13 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
839
857
  const { defaultPrefetch: configPrefetch, defaultHoverPrefetchDelay: configPrefetchDelay } = routerConfig;
840
858
  const prefetch = linkPrefetch || configPrefetch;
841
859
  const prefetchDelay = hoverPrefetchDelay ?? configPrefetchDelay;
860
+ const searchString = typeof search === "object" ? formatSearchObject(search) : search;
842
861
  const location = useMemo(() => ({
843
862
  pathname: to,
844
- search,
863
+ search: searchString,
845
864
  state
846
865
  }), [
847
- search,
866
+ searchString,
848
867
  state,
849
868
  to
850
869
  ]);
package/dist/types.d.ts CHANGED
@@ -140,7 +140,7 @@ export type RouterType = {
140
140
  useContextState(): ReturnType<typeof useGlobalState<Record<string, unknown>>>;
141
141
  useLoaderState<T>(): LoaderState<T>;
142
142
  useParams<T>(): T;
143
- useNavigate(): (arg: Location | string | -1) => Promise<void>;
143
+ useNavigate(): (arg: NavigationLocation | string | -1) => Promise<void>;
144
144
  useAction(action: string, options?: Options): (arg: FormData) => Promise<{
145
145
  data: unknown;
146
146
  error: Error | null;
@@ -172,4 +172,8 @@ export type ElementProps<T extends HTMLElement = HTMLElement> = {
172
172
  onMouseLeave(event: MouseEvent): void;
173
173
  children?: ReactNode;
174
174
  };
175
+ export type SearchObject = Record<string, string | number | boolean | null | undefined>;
176
+ export type NavigationLocation = Omit<Location, 'search'> & {
177
+ search?: string | SearchObject;
178
+ };
175
179
  export {};
@@ -1,5 +1,5 @@
1
- import { Location, RouteItem, RouteItemData, RouterState, ScrollMap, ScrollRestorationBehavior } from '../types';
2
1
  import { Store } from '../create';
2
+ import { Location, RouteItem, RouteItemData, RouterState, ScrollMap, ScrollRestorationBehavior, SearchObject } from '../types';
3
3
  export declare const getParams: (location: Location, routeItem?: RouteItem) => Record<string, string>;
4
4
  export declare const parseWindowLocation: (location: typeof window.location) => Location;
5
5
  export declare const comparePaths: (route: RouteItem, pathname: string) => boolean;
@@ -15,3 +15,4 @@ export declare const getPartialLoaderArgs: (contextState: RouterState["contextSt
15
15
  export declare const isVerticalScroll: (el: Element | null) => boolean;
16
16
  export declare const updateScrollMap: (routeItemDataState: Store<RouteItemData>, scrollMapState: Store<ScrollMap>) => void;
17
17
  export declare const restoreScroll: (scrollMap: ScrollMap, pathname: string, behavior: ScrollRestorationBehavior) => void;
18
+ export declare const formatSearchObject: (searchObject: SearchObject) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.1.9",
3
+ "version": "2.2.0",
4
4
  "description": "A lightweight routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {