EditorTabBody.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Components
  4. import { CustomScrollbar } from '@grafana/ui';
  5. import { FadeIn } from 'app/core/components/Animations/FadeIn';
  6. import { PanelOptionSection } from './PanelOptionSection';
  7. interface Props {
  8. children: JSX.Element;
  9. heading: string;
  10. renderToolbar?: () => JSX.Element;
  11. toolbarItems?: EditorToolbarView[];
  12. }
  13. export interface EditorToolbarView {
  14. title?: string;
  15. heading?: string;
  16. icon?: string;
  17. disabled?: boolean;
  18. onClick?: () => void;
  19. render?: () => JSX.Element;
  20. action?: () => void;
  21. btnType?: 'danger';
  22. }
  23. interface State {
  24. openView?: EditorToolbarView;
  25. isOpen: boolean;
  26. fadeIn: boolean;
  27. }
  28. export class EditorTabBody extends PureComponent<Props, State> {
  29. static defaultProps = {
  30. toolbarItems: [],
  31. };
  32. constructor(props) {
  33. super(props);
  34. this.state = {
  35. openView: null,
  36. fadeIn: false,
  37. isOpen: false,
  38. };
  39. }
  40. componentDidMount() {
  41. this.setState({ fadeIn: true });
  42. }
  43. onToggleToolBarView = (item: EditorToolbarView) => {
  44. this.setState({
  45. openView: item,
  46. isOpen: this.state.openView !== item || !this.state.isOpen,
  47. });
  48. };
  49. onCloseOpenView = () => {
  50. this.setState({ isOpen: false });
  51. };
  52. static getDerivedStateFromProps(props, state) {
  53. if (state.openView) {
  54. const activeToolbarItem = props.toolbarItems.find(
  55. item => item.title === state.openView.title && item.icon === state.openView.icon
  56. );
  57. if (activeToolbarItem) {
  58. return {
  59. ...state,
  60. openView: activeToolbarItem,
  61. };
  62. }
  63. }
  64. return state;
  65. }
  66. renderButton(view: EditorToolbarView) {
  67. const onClick = () => {
  68. if (view.onClick) {
  69. view.onClick();
  70. }
  71. if (view.render) {
  72. this.onToggleToolBarView(view);
  73. }
  74. };
  75. return (
  76. <div className="nav-buttons" key={view.title + view.icon}>
  77. <button className="btn navbar-button" onClick={onClick} disabled={view.disabled}>
  78. {view.icon && <i className={view.icon} />} {view.title}
  79. </button>
  80. </div>
  81. );
  82. }
  83. renderOpenView(view: EditorToolbarView) {
  84. return (
  85. <PanelOptionSection title={view.title || view.heading} onClose={this.onCloseOpenView}>
  86. {view.render()}
  87. </PanelOptionSection>
  88. );
  89. }
  90. render() {
  91. const { children, renderToolbar, heading, toolbarItems } = this.props;
  92. const { openView, fadeIn, isOpen } = this.state;
  93. return (
  94. <>
  95. <div className="toolbar">
  96. <div className="toolbar__heading">{heading}</div>
  97. {renderToolbar && renderToolbar()}
  98. {toolbarItems.length > 0 && (
  99. <>
  100. <div className="gf-form--grow" />
  101. {toolbarItems.map(item => this.renderButton(item))}
  102. </>
  103. )}
  104. </div>
  105. <div className="panel-editor__scroll">
  106. <CustomScrollbar autoHide={false}>
  107. <div className="panel-editor__content">
  108. <FadeIn in={isOpen} duration={200} unmountOnExit={true}>
  109. {openView && this.renderOpenView(openView)}
  110. </FadeIn>
  111. <FadeIn in={fadeIn} duration={50}>
  112. {children}
  113. </FadeIn>
  114. </div>
  115. </CustomScrollbar>
  116. </div>
  117. </>
  118. );
  119. }
  120. }