Files
redash/client/app/components/Link.tsx
Rafael Wendel a2c96c1e6d Embed "external" link type into <Link> component (#5432)
* feature: add external link

* refactor: split external link into own component

* refactor: added link with icon

* refactor: remove reduntant tab index

* refactor: simplify props

* refactor: fix types

* refactor: bring types and components together

* refactor: improve treatment of target
2021-03-26 15:24:07 -03:00

62 lines
1.6 KiB
TypeScript

import React from "react";
import Button, { ButtonProps as AntdButtonProps } from "antd/lib/button";
function DefaultLinkComponent({ children, ...props }: React.AnchorHTMLAttributes<HTMLAnchorElement>) {
return <a {...props}>{children}</a>;
}
Link.Component = DefaultLinkComponent;
interface LinkProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, "role" | "type" | "target"> {
href: string;
}
function Link({ children, ...props }: LinkProps) {
return <Link.Component {...props}>{children}</Link.Component>;
}
interface LinkWithIconProps extends LinkProps {
children: string;
icon: JSX.Element;
alt: string;
target?: "_self" | "_blank" | "_parent" | "_top";
}
function LinkWithIcon({ icon, alt, children, ...props }: LinkWithIconProps) {
return (
<Link.Component {...props}>
{children} {icon} <span className="sr-only">{alt}</span>
</Link.Component>
);
}
Link.WithIcon = LinkWithIcon;
function ExternalLink({
icon = <i className="fa fa-external-link" aria-hidden="true" />,
alt = "(opens in a new tab)",
...props
}: Omit<LinkWithIconProps, "target">) {
return <Link.WithIcon target="_blank" rel="noopener noreferrer" icon={icon} alt={alt} {...props} />;
}
Link.External = ExternalLink;
// Ant Button will render an <a> if href is present.
function DefaultButtonLinkComponent(props: ButtonProps) {
return <Button {...props} />;
}
ButtonLink.Component = DefaultButtonLinkComponent;
interface ButtonProps extends AntdButtonProps {
href: string;
}
function ButtonLink(props: ButtonProps) {
return <ButtonLink.Component {...props} />;
}
Link.Button = ButtonLink;
export default Link;