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.

ValidationModal.tsx 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 Modal, { ModalProps } from './Modal';
  25. import ValidationForm, { ChildrenProps } from './ValidationForm';
  26. interface Props<V> extends ModalProps {
  27. children: (props: ChildrenProps<V>) => React.ReactNode;
  28. confirmButtonText: string;
  29. header: string;
  30. initialValues: V;
  31. isDestructive?: boolean;
  32. isInitialValid?: boolean;
  33. onClose: () => void;
  34. onSubmit: (data: V) => Promise<void>;
  35. validate: (data: V) => { [P in keyof V]?: string };
  36. }
  37. export default class ValidationModal<V> extends React.PureComponent<Props<V>> {
  38. handleSubmit = (data: V) => {
  39. return this.props.onSubmit(data).then(() => {
  40. this.props.onClose();
  41. });
  42. };
  43. render() {
  44. return (
  45. <Modal
  46. contentLabel={this.props.header}
  47. noBackdrop={this.props.noBackdrop}
  48. onRequestClose={this.props.onClose}
  49. size={this.props.size}>
  50. <ValidationForm
  51. initialValues={this.props.initialValues}
  52. isInitialValid={this.props.isInitialValid}
  53. onSubmit={this.handleSubmit}
  54. validate={this.props.validate}>
  55. {(props) => (
  56. <>
  57. <header className="modal-head">
  58. <h2>{this.props.header}</h2>
  59. </header>
  60. <div className="modal-body">{this.props.children(props)}</div>
  61. <footer className="modal-foot">
  62. <DeferredSpinner className="spacer-right" loading={props.isSubmitting} />
  63. <SubmitButton
  64. className={this.props.isDestructive ? 'button-red' : undefined}
  65. disabled={props.isSubmitting || !props.isValid || !props.dirty}>
  66. {this.props.confirmButtonText}
  67. </SubmitButton>
  68. <ResetButtonLink disabled={props.isSubmitting} onClick={this.props.onClose}>
  69. {translate('cancel')}
  70. </ResetButtonLink>
  71. </footer>
  72. </>
  73. )}
  74. </ValidationForm>
  75. </Modal>
  76. );
  77. }
  78. }