import * as React from 'react'; import { Colors } from '../../models/colors'; import { Query } from '../../models/query'; import { iconStyles, tooltipWrapper } from './tooltip.style'; export interface TooltipProps { icon?: JSX.Element; id?: string; event?: string; eventOff?: string; globalEventOff?: string; palette?: Colors; query?: Query; } type TooltipContainerProps = TooltipProps & { render: (close: (e: React.MouseEvent) => void) => JSX.Element; }; interface TooltipState { show: boolean; } class Tooltip extends React.PureComponent { constructor(props: TooltipContainerProps) { super(props); this.state = { show: false }; } unclicker = (e: React.MouseEvent) => { e.nativeEvent.stopImmediatePropagation(); }; open = () => { this.setState({ show: true }); document.addEventListener('click', this.close); }; close = () => { this.setState({ show: false }); document.removeEventListener('click', this.close); }; onClick = () => { this.state.show === false ? this.open() : this.close(); }; onKeyDown = (e: React.KeyboardEvent) => { switch (e.keyCode) { case 13: // enter this.state.show === false ? this.open() : this.close(); break; case 27: // escape if (this.state.show) this.close(); } }; render() { const event = this.props.event || 'click'; return ( {this.props.icon} {this.state.show && this.props.query === 'large' && (
{this.props.render(this.close)}
)}
{this.state.show && this.props.query === Query.small && (
{this.props.render(this.close)}
)}
); } } export default Tooltip;