Chart.tsx 1.8 KB
Newer Older
1
import React, {FunctionComponent, useCallback, useEffect, useState} from 'react';
2 3 4 5
import {
    WithStyled,
    backgroundColor,
    borderRadius,
6
    headerHeight,
7 8
    math,
    primaryColor,
9
    rem,
10
    sameBorder,
11
    size,
12
    transitionProps
13 14
} from '~/utils/style';

15
import ee from '~/utils/event';
16 17
import styled from 'styled-components';

18
const Div = styled.div<{maximized?: boolean; divWidth?: string; divHeight?: string}>`
19 20
    ${props =>
        size(
21 22
            props.maximized ? `calc(100vh - ${headerHeight} - ${rem(40)})` : props.divHeight || 'auto',
            props.maximized ? '100%' : props.divWidth || '100%'
23
        )}
24
    background-color: ${backgroundColor};
25 26
    ${sameBorder({radius: math(`${borderRadius} * 2`)})}
    ${transitionProps(['border-color', 'box-shadow'])}
27
    position: relative;
28 29 30 31 32 33 34

    &:hover {
        border-color: ${primaryColor};
        box-shadow: 0 5px 6px 0 rgba(0, 0, 0, 0.05);
    }
`;

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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 已提交
59 60
        <Div
            maximized={maximized}
61 62
            divWidth={width}
            divHeight={height}
P
Peter Pan 已提交
63 64
            className={`${maximized ? 'maximized' : ''} ${className ?? ''}`}
        >
65 66 67
            {children}
        </Div>
    );
68 69 70
};

export default Chart;