EditorTabBody.tsx 3.5 KB

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