EditorTabBody.tsx 3.2 KB

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