// Copyright (C) The Arvados Authors. All rights reserved.
//
// SPDX-License-Identifier: AGPL-3.0
import React, { ReactElement, ReactNode, useState } from 'react';
import { Button, Grid } from "@material-ui/core";
import { GridProps } from '@material-ui/core/Grid';
import { isArray } from 'lodash';
import { DefaultView } from 'components/default-view/default-view';
import { InfoIcon } from 'components/icon/icon';
import { ReactNodeArray } from 'prop-types';
interface MPVHideablePanelDataProps {
name: string;
visible: boolean;
children: ReactNode;
}
interface MPVHideablePanelActionProps {
doHidePanel: () => void;
}
type MPVPanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
const MPVHideablePanel = ({doHidePanel, name, visible, ...props}: MPVPanelProps) =>
visible
? <>
{React.cloneElement((props.children as ReactElement), { doHidePanel, panelName: name })}
>
: null;
interface MPVPanelContentDataProps {
panelName?: string;
children: ReactElement;
}
interface MPVPanelContentActionProps {
doHidePanel?: () => void;
}
type MPVPanelContentProps = MPVPanelContentDataProps & MPVPanelContentActionProps & GridProps;
// Grid item compatible component for layout and MPV props passing
export const MPVPanelContent = ({doHidePanel, panelName, ...props}: MPVPanelContentProps) =>
{React.cloneElement(props.children, { doHidePanel, panelName })}
;
export interface MPVContainerDataProps {
panelNames?: string[];
}
type MPVContainerProps = MPVContainerDataProps & GridProps;
// Grid container compatible component that also handles panel toggling.
export const MPVContainer = ({children, panelNames, ...props}: MPVContainerProps) => {
if (children === undefined || children === null || children === {}) {
children = [];
} else if (!isArray(children)) {
children = [children];
}
const visibility = (children as ReactNodeArray).map(() => true);
const [panelVisibility, setPanelVisibility] = useState(visibility);
let panels: JSX.Element[] = [];
let toggles: JSX.Element[] = [];
if (isArray(children)) {
for (let idx = 0; idx < children.length; idx++) {
const toggleFn = (idx: number) => () => {
setPanelVisibility([
...panelVisibility.slice(0, idx),
!panelVisibility[idx],
...panelVisibility.slice(idx+1)
])
};
const toggleLabel = panelVisibility[idx] ? 'Hide' : 'Show'
const panelName = panelNames === undefined
? `Panel ${idx+1}`
: panelNames[idx] || `Panel ${idx+1}`;
toggles = [
...toggles,
];
const aPanel =
{children[idx]}
;
panels = [...panels, aPanel];
};
};
return
{ toggles }
{ panelVisibility.includes(true)
? panels
:
}
;
};