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-11-09 10:35:12 +01:00
|
|
|
const iconEl = (
|
|
|
|
|
<Icon role="button" className={cx(useStyles(getStyle), className)} onClick={onClick} name={icon} {...rest} />
|
|
|
|
|
);
|
2021-04-07 08:42:43 +03:00
|
|
|
|
2021-11-02 15:27:07 +00:00
|
|
|
const ariaLabel = typeof tooltip === 'string' ? tooltip : undefined;
|
2021-04-07 08:42:43 +03:00
|
|
|
return (
|
|
|
|
|
<Tooltip content={tooltip} placement={tooltipPlacement}>
|
|
|
|
|
{(() => {
|
2021-05-06 12:32:45 +03:00
|
|
|
if (to) {
|
2021-04-07 08:42:43 +03:00
|
|
|
return (
|
2021-11-02 15:27:07 +00:00
|
|
|
<Link aria-label={ariaLabel} to={to} target={target}>
|
2021-04-07 08:42:43 +03:00
|
|
|
{iconEl}
|
2021-05-06 12:32:45 +03:00
|
|
|
</Link>
|
2021-04-07 08:42:43 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return iconEl;
|
|
|
|
|
})()}
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getStyle = () => css`
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
`;
|