saleor-dashboard/src/components/Link.tsx
Jonatan Witoszek 1e38c14116
Use links instead of onClick navigate function (#1969)
Add links instead of navigate + onClick in:
* Lists - ex. product list (except Plugins, see below)
* SortableTables - ex. product variants
* Sidebar
* Buttons that open new page - ex. "Create product"
* Backlinks
* Menus - ex. "Account Settings"
* Links that actually used onClick - ex. warehouse shipping zone, reset password
2022-05-06 10:59:55 +02:00

96 lines
2.1 KiB
TypeScript

import { Typography } from "@material-ui/core";
import { TypographyProps } from "@material-ui/core/Typography";
import { makeStyles } from "@saleor/macaw-ui";
import { isExternalURL } from "@saleor/utils/urls";
import classNames from "classnames";
import React from "react";
import { Link as RouterLink } from "react-router-dom";
const useStyles = makeStyles(
theme => ({
primary: {
color: theme.palette.textHighlighted.active
},
root: {
cursor: "pointer",
display: "inline"
},
secondary: {
color: theme.palette.primary.main
},
underline: {
textDecoration: "underline"
},
noUnderline: {
textDecoration: "none"
},
disabled: {
cursor: "default",
color: theme.palette.textHighlighted.inactive
}
}),
{ name: "Link" }
);
interface LinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
href?: string;
color?: "primary" | "secondary";
underline?: boolean;
typographyProps?: TypographyProps;
onClick?: React.MouseEventHandler;
disabled?: boolean;
}
const Link: React.FC<LinkProps> = props => {
const {
className,
children,
color = "primary",
underline = false,
onClick,
disabled,
href,
...linkProps
} = props;
const classes = useStyles(props);
const commonLinkProps = {
className: classNames(className, {
[classes.root]: true,
[classes[color]]: true,
[classes.underline]: underline,
[classes.noUnderline]: !underline,
[classes.disabled]: disabled
}),
onClick: event => {
if (disabled || !onClick) {
return;
}
event.preventDefault();
onClick(event);
},
...linkProps
};
return (
<>
{!!href && !isExternalURL(href) ? (
<RouterLink to={disabled ? undefined : href} {...commonLinkProps}>
{children}
</RouterLink>
) : (
<Typography
component="a"
href={disabled ? undefined : href}
{...commonLinkProps}
>
{children}
</Typography>
)}
</>
);
};
Link.displayName = "Link";
export default Link;