Chart.tsx 1.8 KB
Newer Older
1
import React, {FunctionComponent, useCallback, useEffect, useState} from 'react';
P
Peter Pan 已提交
2
import {WithStyled, borderRadius, headerHeight, math, rem, sameBorder, size, transitionProps} from '~/utils/style';
3

4
import ee from '~/utils/event';
5 6
import styled from 'styled-components';

7
const Div = styled.div<{maximized?: boolean; divWidth?: string; divHeight?: string}>`
8 9
    ${props =>
        size(
10 11
            props.maximized ? `calc(100vh - ${headerHeight} - ${rem(40)})` : props.divHeight || 'auto',
            props.maximized ? '100%' : props.divWidth || '100%'
12
        )}
P
Peter Pan 已提交
13
    background-color: var(--background-color);
14
    ${sameBorder({radius: math(`${borderRadius} * 2`)})}
P
Peter Pan 已提交
15
    ${transitionProps(['border-color', 'box-shadow', 'background-color'])}
16
    position: relative;
17 18

    &:hover {
P
Peter Pan 已提交
19
        border-color: var(--primary-color);
20 21 22 23
        box-shadow: 0 5px 6px 0 rgba(0, 0, 0, 0.05);
    }
`;

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
type ChartProps = {
    cid: symbol;
    width?: string;
    height?: string;
};

const Chart: FunctionComponent<ChartProps & WithStyled> = ({cid, width, height, className, children}) => {
    const [maximized, setMaximized] = useState(false);
    const toggleMaximze = useCallback(
        (id: symbol, value: boolean) => {
            if (id === cid) {
                setMaximized(value);
            }
        },
        [cid]
    );
    useEffect(() => {
        ee.on('toggle-chart-size', toggleMaximze);
        return () => {
            ee.off('toggle-chart-size', toggleMaximze);
        };
    }, [toggleMaximze]);

    return (
P
Peter Pan 已提交
48 49
        <Div
            maximized={maximized}
50 51
            divWidth={width}
            divHeight={height}
P
Peter Pan 已提交
52 53
            className={`${maximized ? 'maximized' : ''} ${className ?? ''}`}
        >
54 55 56
            {children}
        </Div>
    );
57 58 59
};

export default Chart;