Skip to content

Commit ca9a18e

Browse files
committed
feat: add parsers
1 parent c4745fc commit ca9a18e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/router/link.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { QueryString } from "../types/query-string";
55
import { AnyJson } from "../types";
66
import { Paths } from "../types/paths";
77
import { TextFragment } from "../utils/text-fragment";
8+
import { QueryStringMapper } from "../utils/mappers";
89

910
const isLeftClick = (e: React.MouseEvent) => e.button === 0;
1011

@@ -16,24 +17,25 @@ type QueryAndPaths<Path extends string> = (Paths.Has<Path> extends true ? { path
1617
(QueryString.Has<Path> extends true ? { query: NonNullable<QueryString.Parse<Path>> } : { query?: never });
1718

1819
export type LinkProps<Path extends string> = Omit<AnchorProps, "href" | "onClick"> & {
19-
href: Path;
20-
state?: AnyJson;
21-
replace?: boolean;
2220
fragments?: TextFragment[];
21+
href: Path;
2322
onClick?: (event: Parameters<NonNullable<AnchorProps["onClick"]>>[0], pathAndQuery: QueryAndPaths<Path>) => void;
23+
parsers?: Partial<QueryStringMapper<string>>;
24+
replace?: boolean;
25+
state?: AnyJson;
2426
} & QueryAndPaths<Path>;
2527

2628
const httpRegex = /^https?:\/\//;
2729

2830
export const Link: <TPath extends string>(props: LinkProps<TPath>) => React.ReactElement = forwardRef(
2931
<TPath extends string>(
30-
{ href, state, replace = false, onClick, query, paths, fragments, ...props }: LinkProps<TPath>,
32+
{ href, state, replace = false, onClick, parsers, query, paths, fragments, ...props }: LinkProps<TPath>,
3133
ref: React.Ref<HTMLAnchorElement>
3234
) => {
3335
const { push, replace: _replace } = useNavigation();
3436
const contextHref = useHref();
3537
const basename = useBasename();
36-
const _href = httpRegex.test(href) ? href : join(basename, mergeUrlEntities(href, paths, query, undefined, fragments));
38+
const _href = httpRegex.test(href) ? href : join(basename, mergeUrlEntities(href, paths, query, parsers, fragments));
3739
const _onClick: NonNullable<AnchorProps["onClick"]> = (event) => {
3840
if (props.target === undefined && props.target !== "_self") event.preventDefault();
3941
if (_href === contextHref) return;

0 commit comments

Comments
 (0)