// Libraries import React, { PureComponent } from 'react'; // Components import CustomScrollbar from 'app/core/components/CustomScrollbar/CustomScrollbar'; import { FadeIn } from 'app/core/components/Animations/FadeIn'; import { PanelOptionSection } from './PanelOptionSection'; interface Props { children: JSX.Element; heading: string; renderToolbar?: () => JSX.Element; toolbarItems?: EditorToolBarView[]; } export interface EditorToolBarView { title?: string; heading?: string; imgSrc?: string; icon?: string; disabled?: boolean; onClick?: () => void; render: (closeFunction?: any) => JSX.Element | JSX.Element[]; } interface State { openView?: EditorToolBarView; isOpen: boolean; fadeIn: boolean; } export class EditorTabBody extends PureComponent { static defaultProps = { toolbarItems: [], }; constructor(props) { super(props); this.state = { openView: null, fadeIn: false, isOpen: false, }; } componentDidMount() { this.setState({ fadeIn: true }); } onToggleToolBarView = (item: EditorToolBarView) => { this.setState({ openView: item, isOpen: !this.state.isOpen, }); }; onCloseOpenView = () => { this.setState({ isOpen: false }); }; static getDerivedStateFromProps(props, state) { if (state.openView) { const activeToolbarItem = props.toolbarItems.find( item => item.title === state.openView.title && item.icon === state.openView.icon ); if (activeToolbarItem) { return { ...state, openView: activeToolbarItem, }; } } return state; } renderButton(view: EditorToolBarView) { const onClick = () => { if (view.onClick) { view.onClick(); } this.onToggleToolBarView(view); }; return (
); } renderOpenView(view: EditorToolBarView) { return ( {view.render()} ); } render() { const { children, renderToolbar, heading, toolbarItems } = this.props; const { openView, fadeIn, isOpen } = this.state; return ( <>
{heading}
{renderToolbar && renderToolbar()} {toolbarItems.length > 0 && ( <>
{toolbarItems.map(item => this.renderButton(item))} )}
{openView && this.renderOpenView(openView)} {children}
); } }