EditorTabBody.tsx 3.4 KB

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