ChartToolbox.tsx 3.7 KB
Newer Older
1
import React, {FunctionComponent, useCallback, useState} from 'react';
P
Peter Pan 已提交
2
import {WithStyled, em, rem, transitionProps} from '~/utils/style';
3 4

import Icon from '~/components/Icon';
5
import type {Icons} from '~/components/Icon';
6
import Tippy from '@tippyjs/react';
7 8
import styled from 'styled-components';

P
Peter Pan 已提交
9
const Toolbox = styled.div<{reversed?: boolean}>`
10 11
    font-size: ${em(16)};
    line-height: 1;
12
    height: 1em;
13
    display: flex;
P
Peter Pan 已提交
14 15
    flex-direction: ${props => (props.reversed ? 'row-reverse' : 'row')};
    align-items: center;
16 17
`;

P
Peter Pan 已提交
18
const ToolboxItem = styled.a<{active?: boolean; reversed?: boolean}>`
19
    cursor: pointer;
P
Peter Pan 已提交
20
    color: ${props => (props.active ? 'var(--primary-color)' : 'var(--text-lighter-color)')};
21 22 23
    ${transitionProps('color')}

    &:hover {
P
Peter Pan 已提交
24
        color: ${props => (props.active ? 'var(--primary-focused-color)' : 'var(--text-light-color)')};
25 26 27
    }

    &:active {
P
Peter Pan 已提交
28
        color: ${props => (props.active ? 'var(--primary-active-color)' : 'var(--text-color)')};
29 30 31
    }

    & + & {
P
Peter Pan 已提交
32
        margin: ${props => (props.reversed ? `0 ${rem(14)} 0 0` : `0 0 0 ${rem(14)}`)};
33 34 35 36
    }
`;

type BaseChartToolboxItem = {
37
    icon: Icons;
38 39 40 41 42 43 44 45 46 47
    tooltip?: string;
};

type NormalChartToolboxItem = {
    toggle?: false;
    onClick?: () => unknown;
} & BaseChartToolboxItem;

type ToggleChartToolboxItem = {
    toggle: true;
48
    activeIcon?: Icons;
49 50 51 52 53 54 55 56
    activeTooltip?: string;
    onClick?: (value: boolean) => unknown;
} & BaseChartToolboxItem;

export type ChartTooboxItem = NormalChartToolboxItem | ToggleChartToolboxItem;

type ChartToolboxProps = {
    items: ChartTooboxItem[];
P
Peter Pan 已提交
57
    reversed?: boolean;
58
    tooltipPlacement?: 'top' | 'bottom' | 'left' | 'right';
59 60
};

P
Peter Pan 已提交
61
const ChartToolbox: FunctionComponent<ChartToolboxProps & WithStyled> = ({
62
    tooltipPlacement,
P
Peter Pan 已提交
63 64 65 66
    items,
    reversed,
    className
}) => {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    const [activeStatus, setActiveStatus] = useState<boolean[]>(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]
    );

85 86 87 88 89 90 91 92 93 94 95 96 97
    const getToolboxItem = useCallback(
        (item: ChartTooboxItem, index: number) => (
            <ToolboxItem
                key={index}
                reversed={reversed}
                active={item.toggle && !item.activeIcon && activeStatus[index]}
                onClick={() => onClick(index)}
            >
                <Icon type={item.toggle ? (activeStatus[index] && item.activeIcon) || item.icon : item.icon} />
            </ToolboxItem>
        ),
        [activeStatus, onClick, reversed]
    );
98 99 100

    return (
        <>
P
Peter Pan 已提交
101
            <Toolbox className={className} reversed={reversed}>
102 103 104 105 106 107 108
                {items.map((item, index) =>
                    item.tooltip ? (
                        <Tippy
                            content={
                                item.toggle ? (activeStatus[index] && item.activeTooltip) || item.tooltip : item.tooltip
                            }
                            placement={tooltipPlacement || 'top'}
P
Peter Pan 已提交
109
                            theme="tooltip"
110 111 112 113 114 115 116 117
                            key={index}
                        >
                            {getToolboxItem(item, index)}
                        </Tippy>
                    ) : (
                        getToolboxItem(item, index)
                    )
                )}
118 119 120 121 122 123
            </Toolbox>
        </>
    );
};

export default ChartToolbox;