fixed props in link component

This commit is contained in:
Maximus 2022-09-09 18:57:43 +03:00
parent 0e3f08dea9
commit 4c90885d92

View File

@ -1,17 +1,20 @@
import React from "react";
import { NavLink, NavLinkProps } from "react-router-dom";
import { NavLink, NavLinkProps, To } from "react-router-dom";
import classNames from "classnames";
type Props = {
to: string;
to?: To;
children: React.ReactNode;
disabled?: boolean;
className?: string;
} & NavLinkProps;
} & Omit<NavLinkProps, "to">;
function getURL(to: string): URL {
function getURL(to: To): URL {
if (typeof to !== "string") {
return getURL(to.pathname ?? "");
}
try {
return new URL(to);
return new URL(to as string);
} catch {
let outurl = `${window.location.origin}${
to.startsWith("/") ? to : "/" + to
@ -29,7 +32,7 @@ export default function Link({
...props
}: Props) {
const link =
getURL(to).hostname === window.location.hostname ? (
to && getURL(to).hostname === window.location.hostname ? (
<NavLink
to={getURL(to).pathname}
style={style}
@ -39,7 +42,7 @@ export default function Link({
</NavLink>
) : (
<a
href={disabled ? undefined : getURL(to).origin}
href={disabled ? undefined : (to && getURL(to).origin) || undefined}
style={
typeof style === "function"
? style({