Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(routing): Forwards refs for Link component #48

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/routing/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { forwardRef } from "react";

import { AnyRoute, Route, RouteArguments } from "./createRoute";
import { useHref } from "./useHref";
Expand Down Expand Up @@ -38,25 +38,20 @@ export type LinkProps<
} & RouteArguments<TRouteParams, TRouteQuery, TRouteMeta> &
Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "onClick">;

/**
* @public
*
* Renders an anchor tag pointing at the provided Route
*
* The query/params/meta props are conditionally required based on the
* route passed as the To parameter
*/
export function Link<TRoute extends AnyRoute>({
to,
children,
testId,
preloadOnHoverMs,
preloadOnInteraction,
onMouseDown: _onMouseDown,
onMouseEnter: _onMouseEnter,
onMouseLeave: _onMouseLeave,
...rest
}: LinkProps<TRoute>) {
function LinkInner<TRoute extends AnyRoute>(
{
to,
children,
testId,
preloadOnHoverMs,
preloadOnInteraction,
onMouseDown: _onMouseDown,
onMouseEnter: _onMouseEnter,
onMouseLeave: _onMouseLeave,
...rest
}: LinkProps<TRoute>,
ref: React.ForwardedRef<HTMLAnchorElement>
) {
// @ts-ignore, these fields _might_ exist, so typechecking doesn't believe they exist
// and everything that consumes params/query already checks for undefined
const { params, query, meta, ...props } = rest;
Expand Down Expand Up @@ -95,13 +90,13 @@ export function Link<TRoute extends AnyRoute>({
return (
<a
{...props}
ref={ref}
href={href}
data-testid={testId}
onMouseDown={onMouseDown ?? _onMouseDown}
onMouseEnter={onMouseEnter ?? _onMouseEnter}
onMouseLeave={onMouseLeave ?? _onMouseLeave}
onClick={(e) => {
e.preventDefault();
if (props.onClick?.(e) === false) {
return;
}
Expand All @@ -112,10 +107,23 @@ export function Link<TRoute extends AnyRoute>({
return;
}

e.preventDefault();
to.navigate({ params, query, meta });
}}
>
{children}
</a>
);
}

/**
* @public
*
* Renders an anchor tag pointing at the provided Route
*
* The query/params/meta props are conditionally required based on the
* route passed as the To parameter
*/
export const Link = forwardRef(LinkInner) as <TRoute extends AnyRoute>(
props: LinkProps<TRoute> & { ref?: React.ForwardedRef<HTMLAnchorElement> }
) => ReturnType<typeof LinkInner>;
Loading
Loading