EditorTabBody.tsx 3.2 KB

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