-
Notifications
You must be signed in to change notification settings - Fork 37
/
ConfirmationModal.js
93 lines (85 loc) · 2.58 KB
/
ConfirmationModal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { useRef } from 'react';
import { FormattedMessage } from 'react-intl';
import PropTypes from 'prop-types';
import uniqueId from 'lodash/uniqueId';
import Button from '../Button';
import Modal from '../Modal';
import ModalFooter from '../ModalFooter';
import css from './ConfirmationModal.css';
const focusFooterPrimary = ref => ref.current.focus();
const propTypes = {
bodyTag: PropTypes.string,
buttonStyle: PropTypes.string,
cancelButtonStyle: PropTypes.string,
cancelLabel: PropTypes.node,
confirmLabel: PropTypes.node,
heading: PropTypes.node.isRequired,
id: PropTypes.string,
isConfirmButtonDisabled: PropTypes.bool,
message: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
onCancel: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired,
open: PropTypes.bool.isRequired,
};
const ConfirmationModal = ({
bodyTag: Element = 'p',
buttonStyle = 'primary',
cancelButtonStyle = 'default',
isConfirmButtonDisabled = false,
...rest
}) => {
const props = { Element, buttonStyle, cancelButtonStyle, isConfirmButtonDisabled, ...rest };
const footerPrimary = useRef(null);
const contentId = useRef(uniqueId('modal-content')).current;
const testId = props.id || uniqueId('confirmation-');
const cancelLabel = props.cancelLabel || <FormattedMessage id="stripes-components.cancel" />;
const confirmLabel = props.confirmLabel || <FormattedMessage id="stripes-components.submit" />;
const footer = (
<ModalFooter>
<Button
data-test-confirmation-modal-confirm-button
buttonStyle={props.buttonStyle}
id={`clickable-${testId}-confirm`}
onClick={props.onConfirm}
ref={footerPrimary}
disabled={isConfirmButtonDisabled}
>
{confirmLabel}
</Button>
<Button
data-test-confirmation-modal-cancel-button
buttonStyle={props.cancelButtonStyle}
id={`clickable-${testId}-cancel`}
onClick={props.onCancel}
>
{cancelLabel}
</Button>
</ModalFooter>
);
return (
<Modal
open={props.open}
onClose={props.onCancel}
onOpen={() => { focusFooterPrimary(footerPrimary); }}
id={testId}
label={props.heading}
aria-labelledby={contentId}
scope="module"
size="small"
footer={footer}
>
<Element
data-test-confirmation-modal-message
className={css.message}
id={contentId}
>
{props.message}
</Element>
</Modal>
);
};
ConfirmationModal.propTypes = propTypes;
export default ConfirmationModal;