|
| 1 | +import * as React from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import { PanelStack2 } from "@blueprintjs/core"; |
| 4 | +import styles from "./PanelStack.module.css"; |
| 5 | + |
| 6 | +const PanelsType = PropTypes.arrayOf( |
| 7 | + PropTypes.shape({ |
| 8 | + id: PropTypes.string, |
| 9 | + title: PropTypes.string, |
| 10 | + content: PropTypes.node, |
| 11 | + }) |
| 12 | +); |
| 13 | + |
| 14 | +export const panelStackHandlers = {}; |
| 15 | + |
| 16 | +const Panel = ({ content }) => { |
| 17 | + return React.createElement("div", { style: { padding: 10 } }, content); |
| 18 | +}; |
| 19 | +Panel.propTypes = { |
| 20 | + content: PropTypes.node, |
| 21 | +}; |
| 22 | + |
| 23 | +const PanelStack = ({ panels, ns, size, ...propsRest }) => { |
| 24 | + const [stack, setStack] = React.useState([ |
| 25 | + { |
| 26 | + renderPanel: Panel, |
| 27 | + title: panels[0].title, |
| 28 | + props: { content: panels[0].content }, |
| 29 | + }, |
| 30 | + ]); |
| 31 | + const addToPanelStack = (newPanel) => |
| 32 | + setStack((stack) => [...stack, newPanel]); |
| 33 | + const removeFromPanelStack = () => setStack((stack) => stack.slice(0, -1)); |
| 34 | + React.useEffect(() => { |
| 35 | + panelStackHandlers[ns] = { |
| 36 | + openPanel: (panelId) => { |
| 37 | + const { title, content } = panels.find(({ id }) => id == panelId); |
| 38 | + addToPanelStack({ |
| 39 | + renderPanel: Panel, |
| 40 | + title, |
| 41 | + props: { content }, |
| 42 | + }); |
| 43 | + }, |
| 44 | + closePanel: removeFromPanelStack, |
| 45 | + }; |
| 46 | + return () => delete panelStackHandlers[ns]; |
| 47 | + }, [panels]); |
| 48 | + return React.createElement( |
| 49 | + "div", |
| 50 | + { style: { width: size[0], height: size[1] } }, |
| 51 | + React.createElement(PanelStack2, { |
| 52 | + className: styles.panelStack, |
| 53 | + onOpen: addToPanelStack, |
| 54 | + onClose: removeFromPanelStack, |
| 55 | + stack: stack, |
| 56 | + ...propsRest, |
| 57 | + }) |
| 58 | + ); |
| 59 | +}; |
| 60 | +PanelStack.propTypes = { |
| 61 | + panels: PanelsType, |
| 62 | + ns: PropTypes.string, |
| 63 | + size: PropTypes.arrayOf(PropTypes.number), |
| 64 | + propsRest: PropTypes.object, |
| 65 | +}; |
| 66 | + |
| 67 | +export default PanelStack; |
0 commit comments