Chart.tsx 2.4 KB
Newer Older
P
Peter Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright 2020 Baidu Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
import React, {FunctionComponent, useCallback, useEffect, useState} from 'react';
P
Peter Pan 已提交
18
import {WithStyled, borderRadius, headerHeight, math, rem, sameBorder, size, transitionProps} from '~/utils/style';
19

20
import ee from '~/utils/event';
21 22
import styled from 'styled-components';

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

    &:hover {
P
Peter Pan 已提交
35
        border-color: var(--primary-color);
36 37 38 39
        box-shadow: 0 5px 6px 0 rgba(0, 0, 0, 0.05);
    }
`;

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

export default Chart;