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 } 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 } from 'components/icon/icon';
11 import { ReactNodeArray } from 'prop-types';
13 interface MPVHideablePanelDataProps {
19 interface MPVHideablePanelActionProps {
20 doHidePanel: () => void;
23 type MPVPanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
25 const MPVHideablePanel = ({doHidePanel, name, visible, ...props}: MPVPanelProps) =>
28 {React.cloneElement((props.children as ReactElement), { doHidePanel, panelName: name })}
32 interface MPVPanelContentDataProps {
34 children: ReactElement;
37 interface MPVPanelContentActionProps {
38 doHidePanel?: () => void;
41 type MPVPanelContentProps = MPVPanelContentDataProps & MPVPanelContentActionProps & GridProps;
43 // Grid item compatible component for layout and MPV props passing
44 export const MPVPanelContent = ({doHidePanel, panelName, ...props}: MPVPanelContentProps) =>
45 <Grid item {...props}>
46 {React.cloneElement(props.children, { doHidePanel, panelName })}
49 export interface MPVContainerDataProps {
50 panelNames?: string[];
53 type MPVContainerProps = MPVContainerDataProps & GridProps;
55 // Grid container compatible component that also handles panel toggling.
56 export const MPVContainer = ({children, panelNames, ...props}: MPVContainerProps) => {
57 if (children === undefined || children === null || children === {}) {
59 } else if (!isArray(children)) {
60 children = [children];
62 const visibility = (children as ReactNodeArray).map(() => true);
63 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
65 let panels: JSX.Element[] = [];
66 let toggles: JSX.Element[] = [];
68 if (isArray(children)) {
69 for (let idx = 0; idx < children.length; idx++) {
70 const toggleFn = (idx: number) => () => {
72 ...panelVisibility.slice(0, idx),
73 !panelVisibility[idx],
74 ...panelVisibility.slice(idx+1)
77 const toggleLabel = panelVisibility[idx] ? 'Hide' : 'Show'
78 const panelName = panelNames === undefined
80 : panelNames[idx] || `Panel ${idx+1}`;
85 <Button onClick={toggleFn(idx)}>{toggleLabel} {panelName}</Button>
89 <MPVHideablePanel visible={panelVisibility[idx]} name={panelName} doHidePanel={toggleFn(idx)}>
92 panels = [...panels, aPanel];
96 return <Grid container {...props}>
98 { panelVisibility.includes(true)
100 : <Grid container alignItems='center' justify='center'>
101 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />