Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ConfirmModal.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 { translate } from '../../helpers/l10n';
  22. import DeferredSpinner from '../ui/DeferredSpinner';
  23. import { ResetButtonLink, SubmitButton } from './buttons';
  24. import ClickEventBoundary from './ClickEventBoundary';
  25. import { ModalProps } from './Modal';
  26. import SimpleModal, { ChildrenProps } from './SimpleModal';
  27. export interface ConfirmModalProps<T> extends ModalProps {
  28. cancelButtonText?: string;
  29. confirmButtonText: string;
  30. confirmData?: T;
  31. confirmDisable?: boolean;
  32. isDestructive?: boolean;
  33. onConfirm: (data?: T) => void | Promise<void | Response>;
  34. }
  35. interface Props<T> extends ConfirmModalProps<T> {
  36. header: string;
  37. headerDescription?: React.ReactNode;
  38. onClose: () => void;
  39. }
  40. export default class ConfirmModal<T = string> extends React.PureComponent<Props<T>> {
  41. mounted = false;
  42. componentDidMount() {
  43. this.mounted = true;
  44. }
  45. componentWillUnmount() {
  46. this.mounted = false;
  47. }
  48. handleSubmit = () => {
  49. const result = this.props.onConfirm(this.props.confirmData);
  50. if (result) {
  51. return result.then(this.props.onClose, () => {
  52. /* noop */
  53. });
  54. }
  55. this.props.onClose();
  56. return undefined;
  57. };
  58. renderModalContent = ({ onCloseClick, onFormSubmit, submitting }: ChildrenProps) => {
  59. const {
  60. children,
  61. confirmButtonText,
  62. confirmDisable,
  63. header,
  64. headerDescription,
  65. isDestructive,
  66. cancelButtonText = translate('cancel')
  67. } = this.props;
  68. return (
  69. <ClickEventBoundary>
  70. <form onSubmit={onFormSubmit}>
  71. <header className="modal-head">
  72. <h2>{header}</h2>
  73. {headerDescription}
  74. </header>
  75. <div className="modal-body">{children}</div>
  76. <footer className="modal-foot">
  77. <DeferredSpinner className="spacer-right" loading={submitting} />
  78. <SubmitButton
  79. autoFocus={true}
  80. className={isDestructive ? 'button-red' : undefined}
  81. disabled={submitting || confirmDisable}>
  82. {confirmButtonText}
  83. </SubmitButton>
  84. <ResetButtonLink disabled={submitting} onClick={onCloseClick}>
  85. {cancelButtonText}
  86. </ResetButtonLink>
  87. </footer>
  88. </form>
  89. </ClickEventBoundary>
  90. );
  91. };
  92. render() {
  93. const { header, onClose, noBackdrop, size } = this.props;
  94. const modalProps = { header, onClose, noBackdrop, size };
  95. return (
  96. <SimpleModal onSubmit={this.handleSubmit} {...modalProps}>
  97. {this.renderModalContent}
  98. </SimpleModal>
  99. );
  100. }
  101. }