import React, { useRef, useState } from 'react'; import AppBar from '@mui/material/AppBar'; import Divider from '@mui/material/Divider'; import IconButton from '@mui/material/IconButton'; import Popover, { PopoverOrigin } from '@mui/material/Popover'; import Tooltip from '@mui/material/Tooltip'; import './appbar-buttons.css'; import Box from '@mui/material/Box'; import ActionConfig from '../../classes/action-config'; import ToolbarPosition, { defaultPosition } from './ToolbarPositionInterface'; /** * Common button * @param props.configuration the configuration * @returns common button menu entry that uses the onClick of the configuration. */ export const ToolbarButtonOption = (props: { configuration: ActionConfig }) => { const selected = props.configuration.selected && props.configuration.selected(); return ( {typeof props.configuration.icon === 'function' ? props.configuration.icon() : props.configuration.icon} ); }; const verticalAligment: { anchorOrigin: PopoverOrigin; transformOrigin: PopoverOrigin } = { anchorOrigin: { vertical: 48, horizontal: 'right', }, transformOrigin: { vertical: 'top', horizontal: 'right', }, }; const horizontalAligment: { anchorOrigin: PopoverOrigin; transformOrigin: PopoverOrigin } = { anchorOrigin: { vertical: 'center', horizontal: -3, }, transformOrigin: { vertical: 'center', horizontal: 'right', }, }; /** * Submenu button and popover * @param props.configuration the configuration * @returns submenu entry that contains one ToolbarMenuItem for each option. Inserts a divider for null options. */ export const ToolbarSubmenu = (props: { configuration: ActionConfig; vertical?: boolean; elevation?: number; }) => { const [open, setOpen] = useState(false); const itemRef = useRef(null); const orientationProps = props.vertical ? verticalAligment : horizontalAligment; return ( !props.configuration.useClickToClose && setOpen(false)} onMouseEnter={() => { if (props.configuration.disabled && props.configuration.disabled()) return; if (!props.configuration.useClickToClose) setOpen(true); }} > setOpen(true) }} /> setOpen(false)} anchorEl={itemRef.current} container={itemRef.current} anchorOrigin={orientationProps.anchorOrigin} transformOrigin={orientationProps.transformOrigin} PaperProps={{ onMouseLeave: () => !props.configuration.useClickToClose && setOpen(false), square: true, }} sx={{ zIndex: props.configuration.useClickToClose ? '1' : '-1', }} elevation={props.elevation} >
e.stopPropagation()}> {props.configuration.options?.map((o, i) => { if (o?.visible === false) return null; if (!o?.render) { return ( ); } else { return {o.render(() => setOpen(false))}; } })}
); }; /** * Wrapper for all menu entries * @param props.configuration the configuration * @returns menu item wich contains a submenu if options is set or a button if onClick is set or null otherwise. */ export const ToolbarMenuItem = (props: { configuration: ActionConfig | null; vertical?: boolean; elevation?: number; }) => { if (props.configuration === null) return ( ); if (props.configuration.visible === false) { return null; } if (props.configuration.render) { return <>{props.configuration.render(null)}; } if (!props.configuration.options && props.configuration.onClick) return ; else { if (props.configuration.options) return ( ); else return null; } }; // const getOrientationProps = (orientation: 'horizontal' | 'vertical'): [top:number, number, ] /** * The entry point for create a Toolbar * @param props.configurations the configurations array * @returns toolbar wich contains a button/submenu for each configuration in the array */ const Toolbar = (props: { configurations: ActionConfig[]; position?: ToolbarPosition; rerender?: number; }) => { const position = props.position || defaultPosition; return ( {props.configurations.map((c, i) => { return ( ); })} ); }; export default Toolbar;