2021-04-07 08:42:43 +03:00
|
|
|
import { Icon, IconName, useStyles, Tooltip } from '@grafana/ui';
|
|
|
|
|
import { PopoverContent } from '@grafana/ui/src/components/Tooltip/Tooltip';
|
|
|
|
|
import { TooltipPlacement } from '@grafana/ui/src/components/Tooltip/PopoverController';
|
|
|
|
|
import React, { FC } from 'react';
|
2021-05-06 12:32:45 +03:00
|
|
|
import { css, cx } from '@emotion/css';
|
|
|
|
|
import { Link } from 'react-router-dom';
|
2021-04-07 08:42:43 +03:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
tooltip: PopoverContent;
|
|
|
|
|
icon: IconName;
|
|
|
|
|
|
2021-05-06 12:32:45 +03:00
|
|
|
className?: string;
|
2021-04-07 08:42:43 +03:00
|
|
|
tooltipPlacement?: TooltipPlacement;
|
2021-05-06 12:32:45 +03:00
|
|
|
to?: string;
|
2021-04-07 08:42:43 +03:00
|
|
|
target?: string;
|
2021-05-06 12:32:45 +03:00
|
|
|
onClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
|
|
|
|
|
'data-testid'?: string;
|
2021-04-07 08:42:43 +03:00
|
|
|
}
|
|
|
|
|
|
2021-05-06 12:32:45 +03:00
|
|
|
export const ActionIcon: FC<Props> = ({
|
|
|
|
|
tooltip,
|
|
|
|
|
icon,
|
|
|
|
|
to,
|
|
|
|
|
target,
|
|
|
|
|
onClick,
|
|
|
|
|
className,
|
|
|
|
|
tooltipPlacement = 'top',
|
|
|
|
|
...rest
|
|
|
|
|
}) => {
|
2021-12-21 16:06:23 +01:00
|
|
|
const ariaLabel = typeof tooltip === 'string' ? tooltip : undefined;
|
2021-11-09 10:35:12 +01:00
|
|
|
const iconEl = (
|
2021-12-21 16:06:23 +01:00
|
|
|
<Icon
|
|
|
|
|
role="button"
|
|
|
|
|
className={cx(useStyles(getStyle), className)}
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
name={icon}
|
|
|
|
|
{...rest}
|
|
|
|
|
aria-label={ariaLabel}
|
|
|
|
|
/>
|
2021-11-09 10:35:12 +01:00
|
|
|
);
|
2021-04-07 08:42:43 +03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip content={tooltip} placement={tooltipPlacement}>
|
2022-01-10 13:11:43 +01:00
|
|
|
{to ? (
|
|
|
|
|
<GoTo url={to} label={ariaLabel} target={target}>
|
|
|
|
|
{iconEl}
|
|
|
|
|
</GoTo>
|
|
|
|
|
) : (
|
|
|
|
|
iconEl
|
|
|
|
|
)}
|
2021-04-07 08:42:43 +03:00
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-10 13:11:43 +01:00
|
|
|
interface GoToProps {
|
|
|
|
|
url: string;
|
|
|
|
|
label?: string;
|
|
|
|
|
target?: string;
|
2022-02-04 12:36:44 +11:00
|
|
|
children?: React.ReactNode;
|
2022-01-10 13:11:43 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 12:36:44 +11:00
|
|
|
const GoTo = React.forwardRef<HTMLAnchorElement, GoToProps>(({ url, label, target, children }, ref) => {
|
2022-01-10 13:11:43 +01:00
|
|
|
const absoluteUrl = url?.startsWith('http');
|
|
|
|
|
|
|
|
|
|
return absoluteUrl ? (
|
2022-02-04 12:36:44 +11:00
|
|
|
<a ref={ref} aria-label={label} href={url} target={target}>
|
2022-01-10 13:11:43 +01:00
|
|
|
{children}
|
|
|
|
|
</a>
|
|
|
|
|
) : (
|
2022-02-04 12:36:44 +11:00
|
|
|
<Link ref={ref} aria-label={label} to={url} target={target}>
|
2022-01-10 13:11:43 +01:00
|
|
|
{children}
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
2022-02-04 12:36:44 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
GoTo.displayName = 'GoTo';
|
2022-01-10 13:11:43 +01:00
|
|
|
|
2021-04-07 08:42:43 +03:00
|
|
|
export const getStyle = () => css`
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
`;
|