/* @flow */ import * as React from 'react'; import classNames from 'classnames'; import LinkBase from '../../components/link/LinkBase'; import Tooltip from '../../components/tooltip'; import LeftSidebarLinkCallout from './LeftSidebarLinkCallout'; import RemoveButton from './RemoveButton'; import type { Callout } from './Callout'; import './styles/LeftSidebarLink.scss'; export type Props = { callout?: Callout, className?: string, customTheme?: Object, htmlAttributes?: Object, icon?: ?React.Element, isScrolling?: boolean, message: string, newItemBadge?: React.Element | null, onClickRemove?: Function, removeButtonHtmlAttributes?: Object, routerLink?: React.ComponentType, routerProps?: Object, selected?: boolean, showTooltip?: boolean, }; type State = { isTextOverflowed: boolean, }; class LeftSidebarLink extends React.Component { constructor(props: Props) { super(props); this.state = { isTextOverflowed: false, }; } componentDidMount() { if (!this.leftSidebarLinkText) { return; } const { offsetWidth, scrollWidth } = this.leftSidebarLinkText; if (offsetWidth < scrollWidth) { // eslint-disable-next-line react/no-did-mount-set-state this.setState({ isTextOverflowed: true, }); } } leftSidebarLinkText: ?HTMLElement; render() { const { callout, className = '', customTheme = {}, htmlAttributes = {}, icon, isScrolling = false, message, newItemBadge, onClickRemove, removeButtonHtmlAttributes = {}, routerLink: RouterLink, routerProps = {}, selected = false, showTooltip = true, } = this.props; const { secondaryColor } = customTheme; const LinkComponent = RouterLink || LinkBase; const routerLinkProps = RouterLink ? routerProps : {}; const linkComponent = ( {icon} { this.leftSidebarLinkText = leftSidebarLinkText; }} className="left-sidebar-link-text" > {message} {newItemBadge} ); let component = linkComponent; if (callout) { component = {linkComponent}; } else if (showTooltip) { component = ( {linkComponent} ); } return onClickRemove ? (
{component}
) : ( component ); } } export default LeftSidebarLink;