import React, {FunctionComponent, useCallback, useState} from 'react'; import {WithStyled, em, rem, transitionProps} from '~/utils/style'; import Icon from '~/components/Icon'; import type {Icons} from '~/components/Icon'; import Tippy from '@tippyjs/react'; import styled from 'styled-components'; const Toolbox = styled.div<{reversed?: boolean}>` font-size: ${em(16)}; line-height: 1; height: 1em; display: flex; flex-direction: ${props => (props.reversed ? 'row-reverse' : 'row')}; align-items: center; `; const ToolboxItem = styled.a<{active?: boolean; reversed?: boolean}>` cursor: pointer; color: ${props => (props.active ? 'var(--primary-color)' : 'var(--text-lighter-color)')}; ${transitionProps('color')} &:hover { color: ${props => (props.active ? 'var(--primary-focused-color)' : 'var(--text-light-color)')}; } &:active { color: ${props => (props.active ? 'var(--primary-active-color)' : 'var(--text-color)')}; } & + & { margin: ${props => (props.reversed ? `0 ${rem(14)} 0 0` : `0 0 0 ${rem(14)}`)}; } `; type BaseChartToolboxItem = { icon: Icons; tooltip?: string; }; type NormalChartToolboxItem = { toggle?: false; onClick?: () => unknown; } & BaseChartToolboxItem; type ToggleChartToolboxItem = { toggle: true; activeIcon?: Icons; activeTooltip?: string; onClick?: (value: boolean) => unknown; } & BaseChartToolboxItem; export type ChartTooboxItem = NormalChartToolboxItem | ToggleChartToolboxItem; type ChartToolboxProps = { items: ChartTooboxItem[]; reversed?: boolean; tooltipPlacement?: 'top' | 'bottom' | 'left' | 'right'; }; const ChartToolbox: FunctionComponent = ({ tooltipPlacement, items, reversed, className }) => { const [activeStatus, setActiveStatus] = useState(new Array(items.length).fill(false)); const onClick = useCallback( (index: number) => { const item = items[index]; if (item.toggle) { item.onClick?.(!activeStatus[index]); setActiveStatus(m => { const n = [...m]; n.splice(index, 1, !m[index]); return n; }); } else { item.onClick?.(); } }, [items, activeStatus] ); const getToolboxItem = useCallback( (item: ChartTooboxItem, index: number) => ( onClick(index)} > ), [activeStatus, onClick, reversed] ); return ( <> {items.map((item, index) => item.tooltip ? ( {getToolboxItem(item, index)} ) : ( getToolboxItem(item, index) ) )} ); }; export default ChartToolbox;