import React, { PureComponent } from 'react'; import CustomScrollbar from 'app/core/components/CustomScrollbar/CustomScrollbar'; import { FadeIn } from 'app/core/components/Animations/FadeIn'; interface Props { children: JSX.Element; heading: string; main?: EditorToolBarView; toolbarItems: EditorToolBarView[]; } export interface EditorToolBarView { title: string; imgSrc?: string; icon?: string; disabled?: boolean; onClick?: () => void; render: (closeFunction: any) => JSX.Element | JSX.Element[]; } interface State { openView?: EditorToolBarView; test: boolean; } export class EditorTabBody extends PureComponent { constructor(props) { super(props); this.state = { openView: null, test: false, }; } componentDidMount() { this.setState({ test: true }); } onToggleToolBarView = (item: EditorToolBarView) => { this.setState({ openView: item === this.state.openView ? null : item, }); }; onCloseOpenView = () => { this.setState({ openView: null }); }; 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; } renderMainSelection(view: EditorToolBarView) { return (
this.onToggleToolBarView(view)} key={view.title + view.icon}>
{view.title}
); } renderButton(view: EditorToolBarView) { const onClick = () => { if (view.onClick) { view.onClick(); } this.onToggleToolBarView(view); }; return (
); } renderOpenView(view: EditorToolBarView) { return (
{view.render(this.onCloseOpenView)}
); } render() { const { children, toolbarItems, main, heading } = this.props; const { openView, test } = this.state; return ( <>
{heading}
{main && this.renderMainSelection(main)}
{toolbarItems.map(item => this.renderButton(item))}
{openView && this.renderOpenView(openView)}
{children}
); } }