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

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