2020-08-28 10:51:41 +00:00
|
|
|
import React, { ReactNode } from "react";
|
2020-08-31 13:04:34 +00:00
|
|
|
import { CommonComponentProps, Classes } from "./common";
|
2020-08-28 10:51:41 +00:00
|
|
|
import styled from "styled-components";
|
2020-08-31 04:59:18 +00:00
|
|
|
import Icon, { IconName, IconSize } from "./Icon";
|
2020-08-31 13:04:34 +00:00
|
|
|
import Text, { TextType, FontWeight } from "./Text";
|
2020-10-09 05:25:00 +00:00
|
|
|
import TooltipComponent from "components/ads/Tooltip";
|
|
|
|
|
import { Position } from '@blueprintjs/core/lib/esm/common/position';
|
2020-08-28 10:51:41 +00:00
|
|
|
|
2020-09-16 11:50:47 +00:00
|
|
|
export type MenuItemProps = CommonComponentProps & {
|
2020-08-28 10:51:41 +00:00
|
|
|
icon?: IconName;
|
|
|
|
|
text: string;
|
|
|
|
|
label?: ReactNode;
|
2020-09-16 11:50:47 +00:00
|
|
|
href?: string;
|
2020-10-09 05:25:00 +00:00
|
|
|
ellipsize?: number;
|
2020-08-28 10:51:41 +00:00
|
|
|
onSelect?: () => void;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-16 11:50:47 +00:00
|
|
|
const ItemRow = styled.a<{ disabled?: boolean }>`
|
2020-08-28 10:51:41 +00:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: space-between;
|
2020-09-16 11:50:47 +00:00
|
|
|
text-decoration: none;
|
2020-09-22 11:56:11 +00:00
|
|
|
padding: 0px ${props => props.theme.spaces[6]}px;
|
2020-09-23 14:06:50 +00:00
|
|
|
.${Classes.TEXT} {
|
|
|
|
|
color: ${props => props.theme.colors.menuItem.normalText};
|
|
|
|
|
}
|
|
|
|
|
.${Classes.ICON} {
|
|
|
|
|
svg {
|
|
|
|
|
path {
|
|
|
|
|
fill: ${props => props.theme.colors.menuItem.normalIcon};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-22 11:56:11 +00:00
|
|
|
height: 38px;
|
2020-08-28 10:51:41 +00:00
|
|
|
|
2020-09-02 09:02:29 +00:00
|
|
|
${props =>
|
|
|
|
|
!props.disabled
|
|
|
|
|
? `
|
|
|
|
|
&:hover {
|
2020-09-22 11:56:11 +00:00
|
|
|
text-decoration: none;
|
2020-09-23 14:06:50 +00:00
|
|
|
cursor: pointer;
|
|
|
|
|
background-color: ${props.theme.colors.menuItem.hoverBg};
|
2020-09-02 09:02:29 +00:00
|
|
|
.${Classes.TEXT} {
|
2020-09-23 14:06:50 +00:00
|
|
|
color: ${props.theme.colors.menuItem.hoverText};
|
2020-09-02 09:02:29 +00:00
|
|
|
}
|
|
|
|
|
.${Classes.ICON} {
|
|
|
|
|
path {
|
2020-09-23 14:06:50 +00:00
|
|
|
fill: ${props.theme.colors.menuItem.hoverIcon};
|
2020-09-02 09:02:29 +00:00
|
|
|
}
|
2020-08-28 10:51:41 +00:00
|
|
|
}
|
2020-09-02 09:02:29 +00:00
|
|
|
}`
|
|
|
|
|
: `
|
|
|
|
|
&:hover {
|
2020-09-22 11:56:11 +00:00
|
|
|
text-decoration: none;
|
|
|
|
|
cursor: default;
|
2020-08-28 10:51:41 +00:00
|
|
|
}
|
2020-09-02 09:02:29 +00:00
|
|
|
`}
|
2020-08-28 10:51:41 +00:00
|
|
|
`;
|
|
|
|
|
|
2020-09-16 11:50:47 +00:00
|
|
|
const IconContainer = styled.span`
|
2020-08-28 10:51:41 +00:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
2020-09-01 07:24:53 +00:00
|
|
|
.${Classes.ICON} {
|
2020-08-28 10:51:41 +00:00
|
|
|
margin-right: ${props => props.theme.spaces[5]}px;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
function MenuItem(props: MenuItemProps) {
|
2020-10-09 05:25:00 +00:00
|
|
|
return (
|
|
|
|
|
props.ellipsize && props.text.length > props.ellipsize ? (
|
|
|
|
|
<TooltipComponent
|
|
|
|
|
position={Position.BOTTOM}
|
|
|
|
|
content={props.text}
|
|
|
|
|
>
|
|
|
|
|
<MenuItemContent {...props} />
|
|
|
|
|
</ TooltipComponent>
|
|
|
|
|
) : <MenuItemContent {...props} />
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function MenuItemContent(props: MenuItemProps) {
|
2020-08-28 10:51:41 +00:00
|
|
|
return (
|
2020-09-14 16:58:54 +00:00
|
|
|
<ItemRow
|
2020-09-16 11:50:47 +00:00
|
|
|
href={props.href}
|
2020-09-14 16:58:54 +00:00
|
|
|
onClick={props.onSelect}
|
|
|
|
|
disabled={props.disabled}
|
|
|
|
|
data-cy={props.cypressSelector}
|
|
|
|
|
>
|
2020-08-28 10:51:41 +00:00
|
|
|
<IconContainer>
|
2020-08-31 04:59:18 +00:00
|
|
|
{props.icon ? <Icon name={props.icon} size={IconSize.LARGE} /> : null}
|
2020-08-31 13:04:34 +00:00
|
|
|
{props.text ? (
|
|
|
|
|
<Text type={TextType.H5} weight={FontWeight.NORMAL}>
|
2020-10-09 05:25:00 +00:00
|
|
|
{props.ellipsize ? ellipsize(props.ellipsize, props.text) : props.text}
|
2020-08-31 13:04:34 +00:00
|
|
|
</Text>
|
|
|
|
|
) : null}
|
2020-08-28 10:51:41 +00:00
|
|
|
</IconContainer>
|
2020-09-22 11:56:11 +00:00
|
|
|
{props.label ? props.label : null}
|
2020-08-28 10:51:41 +00:00
|
|
|
</ItemRow>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 05:25:00 +00:00
|
|
|
function ellipsize(length: number, text: string) {
|
|
|
|
|
return text.length > length ? text.slice(0, length).concat(" ...") : text
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-28 10:51:41 +00:00
|
|
|
export default MenuItem;
|