grafana/public/app/features/dashboard/dashgrid/EditorTabBody.tsx

134 lines
3.2 KiB
TypeScript
Raw Normal View History

// 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;
2018-11-21 13:32:04 -06:00
heading: string;
2018-12-13 00:44:58 -06:00
renderToolbar?: () => JSX.Element;
2018-12-11 23:52:08 -06:00
toolbarItems?: EditorToolBarView[];
}
export interface EditorToolBarView {
2018-12-13 14:24:41 -06:00
title?: string;
heading?: string;
2018-12-11 23:52:08 -06:00
imgSrc?: string;
icon?: string;
disabled?: boolean;
onClick?: () => void;
2018-12-12 01:33:49 -06:00
render: (closeFunction?: any) => JSX.Element | JSX.Element[];
}
interface State {
2018-12-11 23:52:08 -06:00
openView?: EditorToolBarView;
isOpen: boolean;
2018-11-22 05:22:59 -06:00
fadeIn: boolean;
}
export class EditorTabBody extends PureComponent<Props, State> {
2018-12-11 23:52:08 -06:00
static defaultProps = {
toolbarItems: [],
};
constructor(props) {
super(props);
this.state = {
2018-12-11 23:52:08 -06:00
openView: null,
2018-11-22 05:22:59 -06:00
fadeIn: false,
2018-12-11 23:52:08 -06:00
isOpen: false,
};
}
2018-11-22 05:19:52 -06:00
componentDidMount() {
2018-11-22 05:22:59 -06:00
this.setState({ fadeIn: true });
2018-11-22 05:19:52 -06:00
}
2018-12-11 23:52:08 -06:00
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 (
<div className="nav-buttons" key={view.title + view.icon}>
<button className="btn navbar-button" onClick={onClick} disabled={view.disabled}>
{view.icon && <i className={view.icon} />} {view.title}
</button>
</div>
);
}
renderOpenView(view: EditorToolBarView) {
return (
<PanelOptionSection title={view.title || view.heading} onClose={this.onCloseOpenView}>
{view.render()}
</PanelOptionSection>
2018-12-11 23:52:08 -06:00
);
}
render() {
2018-12-11 23:52:08 -06:00
const { children, renderToolbar, heading, toolbarItems } = this.props;
const { openView, fadeIn, isOpen } = this.state;
2018-11-22 05:22:59 -06:00
return (
2018-11-22 04:41:33 -06:00
<>
2018-11-21 13:32:04 -06:00
<div className="toolbar">
<div className="toolbar__heading">{heading}</div>
2018-12-11 10:03:38 -06:00
{renderToolbar && renderToolbar()}
2018-12-13 00:44:58 -06:00
{toolbarItems.length > 0 && (
<>
<div className="gf-form--grow" />
{toolbarItems.map(item => this.renderButton(item))}
</>
)}
2018-11-21 13:32:04 -06:00
</div>
2018-11-09 08:49:55 -06:00
<div className="panel-editor__scroll">
2018-11-12 10:54:44 -06:00
<CustomScrollbar autoHide={false}>
2018-11-22 05:19:52 -06:00
<div className="panel-editor__content">
2018-12-13 06:05:43 -06:00
<FadeIn in={isOpen} duration={200} unmountOnExit={true}>
{openView && this.renderOpenView(openView)}
</FadeIn>
2018-11-22 05:22:59 -06:00
<FadeIn in={fadeIn} duration={50}>
2018-11-22 05:19:52 -06:00
{children}
</FadeIn>
</div>
2018-11-09 08:49:55 -06:00
</CustomScrollbar>
</div>
2018-11-22 04:41:33 -06:00
</>
2018-11-09 08:49:55 -06:00
);
}
}