Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DialogContainer.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { ModalTransition } from '@atlaskit/modal-dialog';
  2. import React, { Component, ComponentType } from 'react';
  3. import { IState } from '../../../../app/types';
  4. import KeyboardShortcutsDialog from '../../../../keyboard-shortcuts/components/web/KeyboardShortcutsDialog';
  5. import { ReactionEmojiProps } from '../../../../reactions/constants';
  6. import { connect } from '../../../redux/functions';
  7. import DialogTransition from './DialogTransition';
  8. interface Props {
  9. /**
  10. * The component to render.
  11. */
  12. _component: ComponentType;
  13. /**
  14. * The props to pass to the component that will be rendered.
  15. */
  16. _componentProps: Object;
  17. /**
  18. * Array of reactions to be displayed.
  19. */
  20. _reactionsQueue: Array<ReactionEmojiProps>;
  21. /**
  22. * True if the UI is in a compact state where we don't show dialogs.
  23. */
  24. _reducedUI: boolean;
  25. }
  26. // This function is necessary while the transition from @atlaskit dialog to our component is ongoing.
  27. const isNewDialog = (component: any) => {
  28. const list = [ KeyboardShortcutsDialog ];
  29. return Boolean(list.find(comp => comp === component));
  30. };
  31. // Needed for the transition to our component.
  32. type State = {
  33. isNewDialog: boolean;
  34. };
  35. /**
  36. * Implements a DialogContainer responsible for showing all dialogs. Necessary
  37. * for supporting @atlaskit's modal animations.
  38. *
  39. */
  40. class DialogContainer extends Component<Props, State> {
  41. /**
  42. * Initializes a new {@code DialogContainer} instance.
  43. *
  44. * @param {Props} props - The React {@code Component} props to initialize
  45. * the new {@code DialogContainer} instance with.
  46. */
  47. constructor(props: Props) {
  48. super(props);
  49. this.state = {
  50. isNewDialog: false
  51. };
  52. }
  53. /**
  54. * Check which Dialog container to render.
  55. * Needed during transition from atlaskit.
  56. *
  57. * @inheritdoc
  58. * @returns {void}
  59. */
  60. componentDidUpdate(prevProps: Props) {
  61. if (this.props._component && prevProps._component !== this.props._component) {
  62. // eslint-disable-next-line react/no-did-update-set-state
  63. this.setState({
  64. isNewDialog: isNewDialog(this.props._component)
  65. });
  66. }
  67. }
  68. /**
  69. * Returns the dialog to be displayed.
  70. *
  71. * @private
  72. * @returns {ReactElement|null}
  73. */
  74. _renderDialogContent() {
  75. const {
  76. _component: component,
  77. _reducedUI: reducedUI
  78. } = this.props;
  79. return (
  80. component && !reducedUI
  81. ? React.createElement(component, this.props._componentProps)
  82. : null);
  83. }
  84. /**
  85. * Implements React's {@link Component#render()}.
  86. *
  87. * @inheritdoc
  88. * @returns {ReactElement}
  89. */
  90. render() {
  91. return this.state.isNewDialog ? (
  92. <DialogTransition>
  93. {this._renderDialogContent()}
  94. </DialogTransition>
  95. ) : (
  96. <ModalTransition>
  97. { this._renderDialogContent() }
  98. </ModalTransition>
  99. );
  100. }
  101. }
  102. /**
  103. * Maps (parts of) the redux state to the associated
  104. * {@code AbstractDialogContainer}'s props.
  105. *
  106. * @param {Object} state - The redux state.
  107. * @private
  108. * @returns {Props}
  109. */
  110. function mapStateToProps(state: IState) {
  111. const stateFeaturesBaseDialog = state['features/base/dialog'];
  112. const { reducedUI } = state['features/base/responsive-ui'];
  113. return {
  114. _component: stateFeaturesBaseDialog.component,
  115. _componentProps: stateFeaturesBaseDialog.componentProps,
  116. _reducedUI: reducedUI
  117. };
  118. }
  119. export default connect(mapStateToProps)(DialogContainer);