// Libs import * as React from 'react'; import classNames from 'classnames'; // Misc import MoreIcon from 'material-design-icons/navigation/svg/production/ic_more_vert_24px.svg'; import PlusIcon from 'material-design-icons/hardware/svg/production/ic_keyboard_arrow_down_24px.svg'; import { ButtonProps, Button, ButtonComponentProps } from '../button/Button'; import guid from '../utils/Guid'; import { Dropdown } from '../dropdown'; import Collapse from '@mui/material/Collapse'; import { OmitInputHeaderContext, InputHeaderContextProps, InputHeaderContextState, InputHeaderComponentContextState, } from '../inputHeader/InputHeaderContext'; import { withInputHeaderContext } from './withInputHeaderContext'; import type { TippyProps } from '@tippy.js/react'; import { withThemeContext } from '../themeProvider/withThemeContext'; import { ThemeContextProps } from '../themeProvider/ThemeContext'; export type InputHeaderRawProps = React.PropsWithChildren< { onExpanded?: () => void; onCollapsed?: () => void; onChange?: (collapsed: boolean) => void; className?: string; title?: string | JSX.Element; icon?: string | JSX.Element; subTitle?: string | JSX.Element; mainButton?: React.ReactNode; extraButtons?: JSX.Element[]; collapsible?: boolean; collapsed?: boolean; collapsedDefault?: boolean; ignoreContext?: boolean; showExpandAll?: boolean; type?: 'hero' | 'standard' | 'small'; noBg?: boolean; headerClassName?: string; contentClassName?: string; extraButtonsButtonProps?: ButtonProps; collapseButtonsButtonProps?: ButtonProps; shouldToggleCollapseOnHeaderClick?: boolean; mountOnEnter?: boolean; unmountOnExit?: boolean; tooltip?: string | JSX.Element; tooltipProps?: TippyProps; stopsPropagation?: boolean; /*something like div, span, button ...*/ ButtonsContainer?: | React.ComponentType<{ className: string; onClick: (e: React.MouseEvent) => void; children: React.ReactNode; }> | 'div'; } & InputHeaderContextProps & ThemeContextProps >; export type InputHeaderProps = OmitInputHeaderContext; export interface InputHeaderApi { expand: () => void; collapse: () => void; toggle: () => void; } export interface InputHeaderState { collapsed: boolean; inputHeaderContext: InputHeaderContextState; } const CollapseExpandButtonComponent: React.FC = ( props ) => ( {props.children} ); export class InputHeaderRaw extends React.PureComponent implements InputHeaderApi { public static defaultProps: InputHeaderProps = { ignoreContext: false, showExpandAll: false, collapsedDefault: true, type: 'standard', noBg: false, shouldToggleCollapseOnHeaderClick: false, stopsPropagation: true, ButtonsContainer: 'div', }; public componentId = guid(); constructor(props: InputHeaderRawProps) { super(props); this.state = { collapsed: props.collapsedDefault, inputHeaderContext: { registerInputHeader: this.registerInputHeader, unregisterInputHeader: this.unregisterInputHeader, stateChanged: () => this.forceUpdate(), components: {}, }, }; this.registerInputHeader = this.registerInputHeader.bind(this); this.unregisterInputHeader = this.unregisterInputHeader.bind(this); this.expand = this.expand.bind(this); this.collapse = this.collapse.bind(this); this.toggle = this.toggle.bind(this); } public componentWillUnmount() { if (!this.props.ignoreContext && this.props.inputHeaderContext) { this.props.inputHeaderContext.unregisterInputHeader(this.componentId); } } public componentDidMount() { if (!this.props.ignoreContext && this.props.inputHeaderContext) { this.props.inputHeaderContext.registerInputHeader(this.componentId, { componentId: this.componentId, componentApi: { expand: this.expand, collapse: this.collapse, toggle: this.toggle, }, props: { ...{}, ...(this.props as InputHeaderProps) }, state: { ...{}, ...this.state }, }); } } public expand() { this.setState({ collapsed: false }, () => { this.props.inputHeaderContext?.stateChanged?.(); this.props.onChange?.(true); this.props.onExpanded?.(); }); } public collapse() { this.setState({ collapsed: true }, () => { this.props.inputHeaderContext?.stateChanged?.(); this.props.onChange?.(false); this.props.onCollapsed?.(); }); } public toggle() { this.setState({ collapsed: !this.state.collapsed }, () => { this.props.inputHeaderContext?.stateChanged?.(); this.props.onChange?.(this.state.collapsed); if (this.state.collapsed) { this.props.onCollapsed?.(); } if (!this.state.collapsed) { this.props.onExpanded?.(); } }); } public render() { const collapsed = this.props.collapsed !== undefined ? !this.props.collapsed : !this.state.collapsed; const showButtons = Boolean( this.props.mainButton || this.props.extraButtons?.length || this.props.collapsible ); const ButtonContainer = this.props.ButtonsContainer ?? 'div'; return (
{this.props.icon && (
{this.props.icon}
)} {this.props.title && (
{this.renderTitle()}
)}
{this.props.subTitle && (
{this.props.subTitle}
)} {showButtons && ( {this.props.mainButton && this.props.mainButton} {this.props.extraButtons ? ( } showArrow={false} headerClassName={'line-height--0'} className={''} > {this.renderExtraButtons()} ) : null} {this.props.collapsible && ( )} )} {this.props.showExpandAll && Object.keys(this.state.inputHeaderContext.components).length > 0 && this.renderCollapseExpandAll()}
{this.props.collapsible ? ( {this.props.children} ) : ( this.props.children )}
); } private toggleClick = () => this.toggle(); private registerInputHeader( componentId: string, component: InputHeaderComponentContextState ) { this.setState((previousState) => { let components = Object.assign( {}, previousState.inputHeaderContext.components ); components[componentId] = component; return { inputHeaderContext: { ...previousState.inputHeaderContext, components }, }; }); } private unregisterInputHeader(componentId: string) { this.setState((previousState) => { let components = Object.assign( {}, previousState.inputHeaderContext.components ); delete components[componentId]; return { inputHeaderContext: { ...previousState.inputHeaderContext, components }, }; }); } private getTypeClass() { switch (this.props.type) { case 'hero': return 'input__header--hero'; case 'small': return 'input__header--small'; default: return ''; } } private mainButtonClick = (e: React.MouseEvent) => { if (this.props.stopsPropagation) { e.stopPropagation(); } }; private renderExtraButtons() { let arr = []; if (typeof this.props.extraButtons === 'function') { arr = this.props.extraButtons; } else { arr = this.props.extraButtons; } return arr.map((extraButton, index) =>
  • {extraButton}
  • ); } private renderCollapseExpandAll() { let allCollapsed = true; let allExpanded = true; Object.keys(this.state.inputHeaderContext.components).forEach((key) => { const component = this.state.inputHeaderContext.components[key]; if (component && component.props.collapsible) { if (component.state.collapsed) { allExpanded = false; } else { allCollapsed = false; } } }); return (
    {(allCollapsed || !allExpanded) && ( )} {(allExpanded || !allCollapsed) && ( )}
    ); } private containerClick = (e: React.MouseEvent) => e.stopPropagation(); private expandAllClick = () => { Object.keys(this.state.inputHeaderContext.components).forEach((key) => { const component = this.state.inputHeaderContext.components[key]; component.componentApi.expand(); }); }; private collapseAllClick = () => { Object.keys(this.state.inputHeaderContext.components).forEach((key) => { const component = this.state.inputHeaderContext.components[key]; component.componentApi.collapse(); }); }; private renderTitle() { if (!this.props.tooltip) { return this.props.title; } return ( {this.props.title} {this.renderTooltip()} ); } private renderTooltip( content: JSX.Element = ? ) { if (this.props.tooltip) { const Tippy = require('@tippy.js/react').default; return ( {content} ); } return content; } } export const InputHeader = withThemeContext< InputHeaderRawProps, InstanceType >(withInputHeaderContext(InputHeaderRaw), 'inputHeader'); export default InputHeader;