EditorTabBody.tsx 3.2 KB

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