import {useState, Children, Fragment } from 'react'; import { MenuToggle, MenuToggleElement, Select, SelectOption, SelectList } from '@patternfly/react-core'; import { constants } from '../common/constants'; const { NONE_TYPE, SERIAL_CONSOLE_TYPE, VNC_CONSOLE_TYPE, DESKTOP_VIEWER_CONSOLE_TYPE } = constants; const getChildTypeName = (child: any) => child && child.props && child.props.type ? child.props.type : (child && child.type && child.type.displayName) || null; const isChildOfType = (child: any, type: string) => { if (child && child.props && child.props.type) { return child.props.type === type; } else if (child && child.type) { return child.type.displayName === type; } return false; }; export interface AccessConsolesProps { /** * Child element can be either * - , or * - or has a property "type" of value either SERIAL_CONSOLE_TYPE or VNC_CONSOLE_TYPE (useful when wrapping (composing) basic console components */ children?: React.ReactElement[] | React.ReactNode; /** Placeholder text for the console selection */ textSelectConsoleType?: string; /** The value for the Serial Console option. This can be overriden by the type property of the child component */ textSerialConsole?: string; /** The value for the VNC Console option. This can be overriden by the type property of the child component */ textVncConsole?: string; /** The value for the Desktop Viewer Console option. This can be overriden by the type property of the child component */ textDesktopViewerConsole?: string; /** Initial selection of the Select */ preselectedType?: string; // NONE_TYPE | SERIAL_CONSOLE_TYPE | VNC_CONSOLE_TYPE | DESKTOP_VIEWER_CONSOLE_TYPE; } export const AccessConsoles: React.FunctionComponent = ({ children, textSelectConsoleType = 'Select console type', textSerialConsole = 'Serial console', textVncConsole = 'VNC console', textDesktopViewerConsole = 'Desktop viewer', preselectedType = null }) => { const typeMap = { [SERIAL_CONSOLE_TYPE]: textSerialConsole, [VNC_CONSOLE_TYPE]: textVncConsole, [DESKTOP_VIEWER_CONSOLE_TYPE]: textDesktopViewerConsole }; const [type, setType] = useState( preselectedType !== NONE_TYPE ? { value: preselectedType, toString: () => typeMap[preselectedType] } : null ); const [isOpen, setIsOpen] = useState(false); const getConsoleForType = (type: any) => Children.map(children as React.ReactElement[], (child: any) => { if (getChildTypeName(child) === type.value) { return {child}; } else { return null; } }); const toggle = (toggleRef: React.Ref) => ( {type.toString()} ); const onToggleClick = () => { setIsOpen(!isOpen); }; const onSelect = (_event: React.MouseEvent, value: string | number) => { setType(value as unknown as React.SetStateAction<{ value: string; toString: () => string }>); setIsOpen(false); }; const selectOptions: any[] = []; Children.toArray(children as React.ReactElement[]).forEach((child: any) => { if (isChildOfType(child, VNC_CONSOLE_TYPE)) { selectOptions.push( textVncConsole }} > {textVncConsole} ); } else if (isChildOfType(child, SERIAL_CONSOLE_TYPE)) { selectOptions.push( textSerialConsole }} > {textSerialConsole} ); } else if (isChildOfType(child, DESKTOP_VIEWER_CONSOLE_TYPE)) { selectOptions.push( textDesktopViewerConsole }} > {textDesktopViewerConsole} ); } else { const typeText = getChildTypeName(child); selectOptions.push( typeText }}> {typeText} ); } }); return (
{Children.toArray(children).length > 1 && (
)} {type && getConsoleForType(type)}
); }; AccessConsoles.displayName = 'AccessConsoles';