fixed props in link component
This commit is contained in:
parent
0e3f08dea9
commit
4c90885d92
@ -1,17 +1,20 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { NavLink, NavLinkProps } from "react-router-dom";
|
import { NavLink, NavLinkProps, To } from "react-router-dom";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
to: string;
|
to?: To;
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
className?: string;
|
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 {
|
try {
|
||||||
return new URL(to);
|
return new URL(to as string);
|
||||||
} catch {
|
} catch {
|
||||||
let outurl = `${window.location.origin}${
|
let outurl = `${window.location.origin}${
|
||||||
to.startsWith("/") ? to : "/" + to
|
to.startsWith("/") ? to : "/" + to
|
||||||
@ -29,7 +32,7 @@ export default function Link({
|
|||||||
...props
|
...props
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const link =
|
const link =
|
||||||
getURL(to).hostname === window.location.hostname ? (
|
to && getURL(to).hostname === window.location.hostname ? (
|
||||||
<NavLink
|
<NavLink
|
||||||
to={getURL(to).pathname}
|
to={getURL(to).pathname}
|
||||||
style={style}
|
style={style}
|
||||||
@ -39,7 +42,7 @@ export default function Link({
|
|||||||
</NavLink>
|
</NavLink>
|
||||||
) : (
|
) : (
|
||||||
<a
|
<a
|
||||||
href={disabled ? undefined : getURL(to).origin}
|
href={disabled ? undefined : (to && getURL(to).origin) || undefined}
|
||||||
style={
|
style={
|
||||||
typeof style === "function"
|
typeof style === "function"
|
||||||
? style({
|
? style({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user