|
1 |
| -import React from "react"; |
| 1 | +import React, { useEffect } from "react"; |
| 2 | +import { ChevronDownIcon, ChevronUpIcon } from "@chakra-ui/icons"; |
2 | 3 | import { HStack } from "@chakra-ui/react";
|
3 | 4 | import clsx from "clsx";
|
4 | 5 |
|
| 6 | +import { PanelMinHeight } from "@/constants"; |
| 7 | + |
5 | 8 | import styles from "./index.module.scss";
|
6 | 9 |
|
| 10 | +import useCustomSettingStore, { page } from "@/pages/customSetting"; |
| 11 | + |
7 | 12 | const PanelHeader: React.FC<{
|
8 | 13 | children?: React.ReactNode;
|
9 | 14 | className?: string;
|
10 | 15 | style?: React.CSSProperties;
|
11 | 16 | actions?: React.ReactNode[];
|
12 | 17 | title?: React.ReactNode | string;
|
| 18 | + pageId?: page; |
| 19 | + panelId?: string; |
13 | 20 | }> = (props) => {
|
14 |
| - const { title, actions, children, style = {}, className } = props; |
| 21 | + const { title, actions, children, style = {}, className, pageId, panelId } = props; |
| 22 | + const store = useCustomSettingStore(); |
| 23 | + |
| 24 | + const [isUp, setIsUp] = React.useState(false); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (!pageId || !panelId) return; |
| 28 | + const { id = "", style } = store.getLayoutInfo(pageId, panelId); |
| 29 | + if (!id) return; |
| 30 | + |
| 31 | + if (style.height < PanelMinHeight + 10) { |
| 32 | + // 10px is the margin |
| 33 | + setIsUp(false); |
| 34 | + } else { |
| 35 | + setIsUp(true); |
| 36 | + } |
| 37 | + }, [pageId, panelId, store]); |
| 38 | + |
| 39 | + const _defaultActions = [ |
| 40 | + <div |
| 41 | + key="zoomIn" |
| 42 | + className="cursor-pointer" |
| 43 | + onClick={() => { |
| 44 | + if (!pageId || !panelId) return; |
| 45 | + const { id = "", style } = store.getLayoutInfo(pageId, panelId); |
| 46 | + |
| 47 | + const element = document.getElementById(id); |
| 48 | + if (element !== null) { |
| 49 | + // get element height |
| 50 | + const currentHeight = element.style.height; |
| 51 | + if (currentHeight === `${PanelMinHeight}px`) { |
| 52 | + const height = element.getAttribute("data-height"); |
| 53 | + element.style.height = height || "100px"; |
| 54 | + setIsUp(true); |
| 55 | + } else { |
| 56 | + setIsUp(false); |
| 57 | + element.setAttribute("data-height", style.height + "px"); |
| 58 | + element.style.height = `${PanelMinHeight}px`; |
| 59 | + } |
| 60 | + } |
| 61 | + }} |
| 62 | + > |
| 63 | + {isUp ? <ChevronDownIcon fontSize={16} /> : <ChevronUpIcon fontSize={16} />} |
| 64 | + </div>, |
| 65 | + ]; |
| 66 | + |
15 | 67 | return title ? (
|
16 | 68 | <div className={styles.sectionHeader + " flex justify-between"}>
|
17 | 69 | <h4>{title}</h4>
|
18 |
| - <HStack spacing="2">{actions}</HStack> |
| 70 | + <HStack spacing="2">{actions ? actions : _defaultActions}</HStack> |
19 | 71 | </div>
|
20 | 72 | ) : (
|
21 | 73 | <div style={style} className={clsx(styles.sectionHeader + " flex justify-between", className)}>
|
|
0 commit comments