Merge pull request 'Fixed props in link component' (#130) from fix/link-component into develop

Reviewed-on: http://85.143.176.51:3000/free-land/front-end/pulls/130
This commit is contained in:
Denis Gorbunov 2022-09-22 09:55:19 +00:00
commit 43a73bc538

View File

@ -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({