Skip to content

Commit

Permalink
Merge pull request #11 from maxholman/master
Browse files Browse the repository at this point in the history
fix: onclick when using nav api + types
  • Loading branch information
maxholman authored Jun 4, 2024
2 parents 981156c + dc68e9e commit 9ee5412
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/hooks.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`useMatch 1`] = `
exports[`Router preset pathname 1`] = `
<DocumentFragment>
<pre>
{
Expand Down
2 changes: 1 addition & 1 deletion __tests__/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const EffCee = (_: ExtractRouteParams<typeof login.path>) => {
return <pre>{JSON.stringify(match, null, 2)}</pre>;
};

test('useMatch', async () => {
test('Router preset pathname', async () => {
const { asFragment } = render(
<Router
pathname={login.build({
Expand Down
13 changes: 5 additions & 8 deletions lib/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import {
forwardRef,
isValidElement,
useMemo,
type ComponentPropsWithRef,
type PropsWithChildren,
type ReactElement,
} from 'react';
import { Route } from './Route.js';
import { RoutesContext } from './RoutesContext.js';
import type { MatchResult } from './matcher.js';
import type { Params } from './types.js';
import type { Params, RouteComponentProps } from './types.js';
import { useLocation, useRouter } from './use-router.js';
import { flattenFragments } from './util.js';

export type RouteComponent = ComponentPropsWithRef<typeof Route>;

export const Routes = forwardRef<HTMLElement, PropsWithChildren>(
({ children }, forwardedRef) => {
const [url] = useLocation();
Expand All @@ -24,16 +21,16 @@ export const Routes = forwardRef<HTMLElement, PropsWithChildren>(
const { pathname } = url;

const [child, matchResult] = useMemo((): [
ReactElement<RouteComponent> | null,
ReactElement<RouteComponentProps> | null,
MatchResult<Params> | false,
] => {
let match: MatchResult<Params> | false = false;
let matchChild: ReactElement<RouteComponent> | null = null;
let matchChild: ReactElement<RouteComponentProps> | null = null;

flattenFragments(children)
.filter(
(c): c is ReactElement<RouteComponent> =>
isValidElement<RouteComponent>(c) &&
(c): c is ReactElement<RouteComponentProps> =>
isValidElement<RouteComponentProps>(c) &&
// supports native routes, and also custom route components
(c.type === Route || 'path' in c.props),
)
Expand Down
2 changes: 1 addition & 1 deletion lib/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const Link = forwardRef<
const newProps: LinkChildProps = {
...props,
href: isSameOrigin ? urlRhs(destAsUrl) : href.toString(),
...(!hasNav && { onClick: handleClick }),
onClick: hasNav ? onClick : handleClick,
...(!isSameOrigin && { rel: 'no-opener noreferrer' }),
ref,
};
Expand Down
10 changes: 7 additions & 3 deletions lib/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { ReactElement } from 'react';
import { parse } from 'regexparam';
import { pathCache } from './path-cache.js';
import type { RouteComponent } from './Routes.js';
import type { ExtractRouteParams, Params, RoutingProps } from './types.js';
import type {
ExtractRouteParams,
Params,
RouteComponentProps,
RoutingProps,
} from './types.js';

export type Match<TPath extends string = '/'> =
| MatchResult<ExtractRouteParams<TPath>>
Expand All @@ -15,7 +19,7 @@ export interface MatchResult<P extends Params> {
}

export type Matcher = (
component: ReactElement<RouteComponent>,
component: ReactElement<RouteComponentProps>,
pathname: string,
) => Match | false;

Expand Down
2 changes: 1 addition & 1 deletion lib/path-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RoutingProps } from './types.js';

export function pathCache<P extends RoutingProps<string>, T>(
export function pathCache<P extends RoutingProps, T>(
map: WeakMap<P, T>,
props: P,
builder: (props: P) => T,
Expand Down
7 changes: 4 additions & 3 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FC, ReactNode } from 'react';

type Path = string | undefined;
type Path = string;
type MaybePath = Path | undefined;

export type Params = Record<string, string>;

Expand Down Expand Up @@ -35,12 +36,12 @@ export type ExtractRouteParams<PathType extends Path> = string extends PathType
: // eslint-disable-next-line @typescript-eslint/ban-types
{};

export interface RoutingProps<TPath extends Path> {
export interface RoutingProps<TPath extends MaybePath = Path> {
path: TPath;
wildcard?: boolean | undefined;
}

export type RouteComponentProps<TPath extends Path> =
export type RouteComponentProps<TPath extends Path = Path> =
| {
children: ReactNode;
// path?: never;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@block65/mrr",
"version": "5.3.0",
"version": "6.0.0-rc1",
"private": false,
"license": "MIT",
"sideEffects": false,
Expand Down

0 comments on commit 9ee5412

Please sign in to comment.