You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleModal.tsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import Modal, { ModalProps } from './Modal';
  22. export interface ChildrenProps {
  23. onCloseClick: (event?: React.SyntheticEvent<HTMLElement>) => void;
  24. onFormSubmit: (event: React.SyntheticEvent<HTMLFormElement>) => void;
  25. onSubmitClick: (event?: React.SyntheticEvent<HTMLElement>) => void;
  26. submitting: boolean;
  27. }
  28. interface Props extends ModalProps {
  29. children: (props: ChildrenProps) => React.ReactNode;
  30. header: string;
  31. onClose: () => void;
  32. onSubmit: () => void | Promise<void | Response>;
  33. }
  34. interface State {
  35. submitting: boolean;
  36. }
  37. export default class SimpleModal extends React.Component<Props, State> {
  38. mounted = false;
  39. state: State = { submitting: false };
  40. componentDidMount() {
  41. this.mounted = true;
  42. }
  43. componentWillUnmount() {
  44. this.mounted = false;
  45. }
  46. stopSubmitting = () => {
  47. if (this.mounted) {
  48. this.setState({ submitting: false });
  49. }
  50. };
  51. handleCloseClick = (event?: React.SyntheticEvent<HTMLElement>) => {
  52. if (event) {
  53. event.preventDefault();
  54. event.currentTarget.blur();
  55. }
  56. this.props.onClose();
  57. };
  58. handleFormSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
  59. event.preventDefault();
  60. this.submit();
  61. };
  62. handleSubmitClick = (event?: React.SyntheticEvent<HTMLElement>) => {
  63. if (event) {
  64. event.preventDefault();
  65. event.currentTarget.blur();
  66. }
  67. this.submit();
  68. };
  69. submit = () => {
  70. const result = this.props.onSubmit();
  71. if (result) {
  72. this.setState({ submitting: true });
  73. result.then(this.stopSubmitting, this.stopSubmitting);
  74. }
  75. };
  76. render() {
  77. const { children, header, onClose, onSubmit, ...modalProps } = this.props;
  78. return (
  79. <Modal contentLabel={header} onRequestClose={onClose} {...modalProps}>
  80. {children({
  81. onCloseClick: this.handleCloseClick,
  82. onFormSubmit: this.handleFormSubmit,
  83. onSubmitClick: this.handleSubmitClick,
  84. submitting: this.state.submitting
  85. })}
  86. </Modal>
  87. );
  88. }
  89. }