Farmbot-Web-App/frontend/link.tsx

32 lines
878 B
TypeScript
Raw Permalink Normal View History

import * as React from "react";
import { navigate } from "takeme";
2018-09-14 06:58:59 -06:00
interface LinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
to: string;
2018-09-21 08:09:30 -06:00
children?: React.ReactChild | React.ReactChild[];
style?: React.CSSProperties;
className?: string;
2019-08-23 15:18:28 -06:00
disabled?: boolean;
}
export const maybeStripLegacyUrl =
(url: string) => url.startsWith("/app") ? url.replace("/app", "") : url;
2018-09-21 08:09:30 -06:00
export const clickHandler =
(props: LinkProps) => (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
/** BEGIN LEGACY SHIMS */
const { onClick, to } = props;
navigate(maybeStripLegacyUrl(to));
2020-01-03 13:04:45 -07:00
onClick?.(e);
2018-09-21 08:09:30 -06:00
};
2018-09-17 09:55:18 -06:00
export class Link extends React.Component<LinkProps, {}> {
render() {
const { props } = this;
2019-08-23 15:18:28 -06:00
return props.disabled
? <a {...props} />
: <a {...props} href={props.to} onClick={clickHandler(props)} />;
2018-09-17 09:55:18 -06:00
}
}