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

130 lines
3.4 KiB
TypeScript
Raw Normal View History

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;
2018-11-21 13:32:04 -06:00
heading: string;
2018-11-15 00:33:59 -06:00
main?: EditorToolBarView;
toolbarItems: EditorToolBarView[];
}
export interface EditorToolBarView {
title: string;
2018-11-09 08:49:55 -06:00
imgSrc?: string;
icon?: string;
2018-11-20 07:17:03 -06:00
disabled?: boolean;
onClick?: () => void;
render: (closeFunction: any) => JSX.Element | JSX.Element[];
}
interface State {
openView?: EditorToolBarView;
2018-11-22 05:19:52 -06:00
test: boolean;
}
export class EditorTabBody extends PureComponent<Props, State> {
constructor(props) {
super(props);
this.state = {
openView: null,
2018-11-22 05:19:52 -06:00
test: false,
};
}
2018-11-22 05:19:52 -06:00
componentDidMount() {
this.setState({ test: true });
}
onToggleToolBarView = (item: EditorToolBarView) => {
this.setState({
openView: item === this.state.openView ? null : item,
});
2018-11-09 08:49:55 -06:00
};
onCloseOpenView = () => {
this.setState({ openView: null });
2018-11-09 08:49:55 -06:00
};
2018-11-20 07:17:03 -06:00
static getDerivedStateFromProps(props, state) {
if (state.openView) {
const activeToolbarItem = props.toolbarItems.find(
item => item.title === state.openView.title && item.icon === state.openView.icon
);
2018-11-20 07:17:03 -06:00
if (activeToolbarItem) {
return {
...state,
openView: activeToolbarItem,
};
}
}
return state;
}
2018-11-09 08:49:55 -06:00
renderMainSelection(view: EditorToolBarView) {
return (
2018-11-20 10:11:47 -06:00
<div className="toolbar__main" onClick={() => this.onToggleToolBarView(view)} key={view.title + view.icon}>
2018-11-10 10:27:25 -06:00
<img className="toolbar__main-image" src={view.imgSrc} />
<div className="toolbar__main-name">{view.title}</div>
<i className="fa fa-caret-down" />
</div>
);
}
2018-11-09 08:49:55 -06:00
renderButton(view: EditorToolBarView) {
2018-11-20 07:17:03 -06:00
const onClick = () => {
if (view.onClick) {
view.onClick();
}
this.onToggleToolBarView(view);
};
2018-11-09 08:49:55 -06:00
return (
2018-11-20 10:11:47 -06:00
<div className="nav-buttons" key={view.title + view.icon}>
2018-11-20 07:17:03 -06:00
<button className="btn navbar-button" onClick={onClick} disabled={view.disabled}>
{view.icon && <i className={view.icon} />} {view.title}
2018-11-09 08:49:55 -06:00
</button>
</div>
);
}
renderOpenView(view: EditorToolBarView) {
return (
2018-11-10 10:27:25 -06:00
<div className="toolbar-subview">
<button className="toolbar-subview__close" onClick={this.onCloseOpenView}>
<i className="fa fa-chevron-up" />
</button>
{view.render(this.onCloseOpenView)}
</div>
);
}
render() {
2018-11-21 13:32:04 -06:00
const { children, toolbarItems, main, heading } = this.props;
2018-11-22 05:19:52 -06:00
const { openView, test } = this.state;
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>
{main && this.renderMainSelection(main)}
<div className="gf-form--grow" />
{toolbarItems.map(item => this.renderButton(item))}
</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:13:28 -06:00
<FadeIn in={openView !== null} duration={200}>
<div className="panel-editor__toolbar-view">{openView && this.renderOpenView(openView)}</div>
</FadeIn>
2018-11-22 05:19:52 -06:00
<div className="panel-editor__content">
<FadeIn in={test} duration={50}>
{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
);
}
}