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<{size?: number; reversed?: boolean}>`
10 11
    font-size: ${em(16)};
    line-height: 1;
12
    height: 1em;
P
Peter Pan 已提交
13 14 15 16 17 18
    display: grid;
    grid-template-columns: ${props => (props.size == null ? 'repeat(auto-fill, 1em)' : `repeat(${props.size}, 1em)`)};
    grid-gap: ${rem(14)};
    place-items: center;
    justify-content: ${props => (props.reversed ? 'end' : 'start')};
    align-content: center;
19 20
`;

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

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

    &:active {
P
Peter Pan 已提交
31
        color: ${props => (props.active ? 'var(--primary-active-color)' : 'var(--text-color)')};
32 33 34 35
    }
`;

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

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

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

export type ChartTooboxItem = NormalChartToolboxItem | ToggleChartToolboxItem;

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

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

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

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

export default ChartToolbox;