1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { ReactElement, ReactNode, useState } from 'react';
6 import { Button, Grid, StyleRulesCallback, Tooltip, withStyles, WithStyles } from "@material-ui/core";
7 import { GridProps } from '@material-ui/core/Grid';
8 import { isArray } from 'lodash';
9 import { DefaultView } from 'components/default-view/default-view';
10 import { InfoIcon, InvisibleIcon, VisibleIcon } from 'components/icon/icon';
11 import { ReactNodeArray } from 'prop-types';
12 import classNames from 'classnames';
14 type CssRules = 'button' | 'buttonIcon';
16 const styles: StyleRulesCallback<CssRules> = theme => ({
23 padding: '2px 0px 2px 5px',
28 interface MPVHideablePanelDataProps {
34 interface MPVHideablePanelActionProps {
35 doHidePanel: () => void;
38 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
40 const MPVHideablePanel = ({doHidePanel, name, visible, ...props}: MPVHideablePanelProps) =>
43 {React.cloneElement((props.children as ReactElement), { doHidePanel, panelName: name })}
47 interface MPVPanelDataProps {
51 interface MPVPanelActionProps {
52 doHidePanel?: () => void;
55 // Props received by panel implementors
56 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
58 type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps;
60 // Grid item compatible component for layout and MPV props passing
61 export const MPVPanelContent = ({doHidePanel, panelName, ...props}: MPVPanelContentProps) =>
62 <Grid item {...props}>
63 {React.cloneElement(props.children, { doHidePanel, panelName })}
66 export interface MPVContainerDataProps {
67 panelNames?: string[];
70 type MPVContainerProps = MPVContainerDataProps & GridProps;
72 // Grid container compatible component that also handles panel toggling.
73 const MPVContainerComponent = ({children, panelNames, classes, ...props}: MPVContainerProps & WithStyles<CssRules>) => {
74 if (children === undefined || children === null || children === {}) {
76 } else if (!isArray(children)) {
77 children = [children];
79 const visibility = (children as ReactNodeArray).map(() => true);
80 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
82 let panels: JSX.Element[] = [];
83 let toggles: JSX.Element[] = [];
85 if (isArray(children)) {
86 for (let idx = 0; idx < children.length; idx++) {
87 const toggleFn = (idx: number) => () => {
89 ...panelVisibility.slice(0, idx),
90 !panelVisibility[idx],
91 ...panelVisibility.slice(idx+1)
94 const toggleIcon = panelVisibility[idx]
95 ? <VisibleIcon className={classNames(classes.buttonIcon)} />
96 : <InvisibleIcon className={classNames(classes.buttonIcon)}/>
97 const panelName = panelNames === undefined
99 : panelNames[idx] || `Panel ${idx+1}`;
100 const toggleVariant = panelVisibility[idx]
103 const toggleTooltip = panelVisibility[idx]
104 ? `Hide ${panelName} panel`
105 : `Show ${panelName} panel`;
109 <Tooltip title={toggleTooltip} disableFocusListener>
110 <Button variant={toggleVariant} size="small" color="primary"
111 className={classNames(classes.button)}
112 onClick={toggleFn(idx)}>
120 <MPVHideablePanel visible={panelVisibility[idx]} name={panelName} doHidePanel={toggleFn(idx)}>
123 panels = [...panels, aPanel];
127 return <Grid container {...props}>
131 { panelVisibility.includes(true)
133 : <Grid container alignItems='center' justify='center'>
134 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
139 export const MPVContainer = withStyles(styles)(MPVContainerComponent);