-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathComposedModal.jsx
296 lines (279 loc) · 9.62 KB
/
ComposedModal.jsx
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import {
ComposedModal as CarbonComposedModal,
ModalBody,
ModalFooter,
ModalHeader,
Loading,
InlineNotification,
} from '@carbon/react';
import PropTypes from 'prop-types';
import React, { Fragment, useEffect, useLayoutEffect, useRef, useState } from 'react';
import classnames from 'classnames';
import warning from 'warning';
import { v4 as uuidv4 } from 'uuid';
import { settings } from '../../constants/Settings';
import { scrollErrorIntoView } from '../../utils/componentUtilityFunctions';
import Button from '../Button';
import deprecate from '../../internal/deprecate';
import useSizeObserver from '../../hooks/useSizeObserver';
const { iotPrefix, prefix } = settings;
export const ComposedModalPropTypes = {
/** Header Props
* label: goes on top of the dialog
* title: Heading of the dialog
* helpText, additional information will stay at the top of the screen when scrolling dialog content
*/
header: PropTypes.shape({
label: PropTypes.node,
title: PropTypes.node,
helpText: PropTypes.node,
}),
/** ability to add translation string to close icon */
iconDescription: PropTypes.string,
/** Content to render inside Modal */
children: PropTypes.node,
/** Footer Props
* Either supply your own footer element or supply an object with button labels and submit handlers and we will make a footer with two buttons for you
*/
footer: PropTypes.oneOfType([
PropTypes.element.isRequired,
PropTypes.shape({
primaryButtonLabel: PropTypes.node,
secondaryButtonLabel: PropTypes.node,
/** should the primary button be hidden (i.e. only show Cancel) */
isPrimaryButtonHidden: PropTypes.bool,
/** should the primary button be disabled */
isPrimaryButtonDisabled: PropTypes.bool,
}),
]),
/** NEW PROP: Type of dialog, affects colors, styles of dialog */
type: PropTypes.oneOf(['warn', 'normal']),
/** NEW PROP: Whether this particular dialog needs to be very large */
isLarge: PropTypes.bool,
/** NEW PROP: Whether this particular dialog needs to be full width */
isFullScreen: PropTypes.bool,
/** Should the dialog be open or not */
open: PropTypes.bool, // eslint-disable-line react/boolean-prop-naming
/** Close the dialog */
onClose: PropTypes.func.isRequired,
/** Is data currently being sent to the backend */
sendingData: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
/** Is my data actively loading? */
isFetchingData: PropTypes.bool,
/** Form Error Details */
error: PropTypes.string,
/** Clear the currently shown error, triggered if the user closes the ErrorNotification */
onClearError: PropTypes.func,
/** Did the form submission fail */
submitFailed: PropTypes.bool, // eslint-disable-line react/boolean-prop-naming
/** Is the form currently invalid */
invalid: PropTypes.bool, // eslint-disable-line react/boolean-prop-naming
/** Callback to submit the dialog/form */
onSubmit: PropTypes.func,
/** Hide the footer */
passiveModal: PropTypes.bool,
// TODO: remove deprecated testID in v3.
// eslint-disable-next-line react/require-default-props
testID: deprecate(
PropTypes.string,
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
/** Id that can be used for testing */
testId: PropTypes.string,
/**
* Specify a CSS selector that matches the DOM element that should be
* focused when the Modal opens
*/
selectorPrimaryFocus: PropTypes.string,
};
const defaultProps = {
open: true,
error: null,
isFetchingData: false,
sendingData: null,
onClearError: null,
type: null,
footer: null,
isFullScreen: false,
isLarge: false,
submitFailed: false,
onSubmit: null,
invalid: false,
children: null,
header: {},
iconDescription: 'Close',
passiveModal: false,
testId: 'ComposedModal',
selectorPrimaryFocus: '.cds--modal-header',
};
/**
* Renders a carbon modal dialog. This dialog adds these additional features on top of the base carbon dialog:
* adds header.helpText prop to explain dialog
* adds type prop for warning and error type dialogs
* adds isFullScreen prop to have the modal appear in full width using class styling
* adds isLarge prop for large and small class styling dialogs
* adds isFetchingData props for loading state
* adds error and dataError prop to display notification about error at bottom of dialog
* if submitFailed prop, it will find and scroll the failing carbon element into view
* shows spinner on primary dialog button if sendingData prop is true
*
* We also prevent the dialog from closing if you click outside it.
*
*/
const ComposedModal = ({
header,
error,
sendingData,
children,
footer,
open,
className,
type,
onClose,
isFetchingData,
isFullScreen,
isLarge,
onSubmit,
iconDescription,
onClearError,
submitFailed,
invalid,
passiveModal,
// TODO: remove deprecated testID in v3.
testID,
testId,
selectorPrimaryFocus,
...props
}) => {
useEffect(() => {
if (__DEV__ && passiveModal && (footer || onSubmit)) {
warning(
false,
'You have set passiveModal to true, but also passed a footer or onSubmit handler. Your footer will not be rendered.'
);
}
}, [footer, onSubmit, passiveModal]);
useEffect(() => {
if (invalid && submitFailed) {
scrollErrorIntoView(true);
}
}, [invalid, submitFailed]);
const handleClearError = () => {
if (onClearError) {
onClearError();
}
};
const [modalExtrasHeight, setModalExtrasHeight] = useState(0);
/** TODO: This is needed to keep the ComposedModal from closing if you click outside it this supports our dialogs on top of dialogs issue */
const doNotClose = () => false;
const modalRef = useRef(null);
const modalBodyRef = useRef(null);
const [modalSize] = useSizeObserver({ ref: modalRef });
const [modalBodySize] = useSizeObserver({ ref: modalBodyRef });
useLayoutEffect(() => {
if (modalRef.current && modalBodyRef.current) {
const headerHeight =
modalRef.current.querySelector(`.${prefix}--modal-header`)?.getBoundingClientRect()
?.height ?? 0;
const footerHeight =
modalRef.current.querySelector(`.${prefix}--modal-footer`)?.getBoundingClientRect()
?.height ?? 0;
setModalExtrasHeight(Math.floor(headerHeight + footerHeight));
}
}, [modalSize.height, modalBodySize.height]);
const { label, title, helpText } = header;
// First check for dataErrors as they are worse than form errors
return isFetchingData ? (
<Loading />
) : (
<CarbonComposedModal
{...props}
selectorPrimaryFocus={selectorPrimaryFocus}
forwardedRef={modalRef}
// TODO: remove deprecated testID in v3.
data-testid={testID || testId}
open={open}
onClose={doNotClose}
data-floating-menu-container // TODO: Can remove once this issue is fixed: https://github.com/carbon-design-system/carbon/issues/6662
className={classnames(
className,
{
[`${iotPrefix}--composed-modal--large`]: isLarge,
[`${iotPrefix}--composed-modal--full-screen`]: isFullScreen,
},
`${iotPrefix}--composed-modal`
)}
style={modalExtrasHeight > 0 ? { '--modal-extras-height': modalExtrasHeight } : undefined}
>
<ModalHeader
label={label}
title={title}
closeModal={onClose}
buttonOnClick={onClose}
iconDescription={iconDescription}
>
{helpText ? <p className={`${prefix}--modal-content__text`}>{helpText}</p> : null}
</ModalHeader>
{children ? (
<ModalBody
ref={modalBodyRef}
className={classnames({
// Prevent double scrollbars
[`${iotPrefix}--composed-modal__body--small-margin-bottom`]: error,
})}
style={{ maxHeight: `calc(100vh - ${modalExtrasHeight}px)` }}
>
{children}
</ModalBody>
) : null}
{error ? (
<InlineNotification
key={uuidv4()} // regenerate the inline notification after they click on close button
lowContrast
title={error}
subtitle=""
kind="error"
onCloseButtonClick={handleClearError}
className={`${iotPrefix}--composed-modal--inline-notification`}
data-testid={`${testID || testId}-notification`}
/>
) : null}
{!passiveModal ? (
<ModalFooter className={`${iotPrefix}--composed-modal-footer`}>
{React.isValidElement(footer) ? (
footer
) : (
<Fragment>
<Button
kind="secondary"
onClick={onClose}
testId={`${testID || testId}-modal-secondary-button`}
>
{(footer && footer.secondaryButtonLabel) || 'Cancel'}
</Button>
{!footer?.isPrimaryButtonHidden ? (
<Button
disabled={footer?.isPrimaryButtonDisabled}
kind={type === 'warn' ? 'danger' : 'primary'}
loading={
(typeof sendingData === 'boolean' && sendingData) ||
typeof sendingData === 'string'
}
onClick={onSubmit}
testId={`${testID || testId}-modal-${
type === 'warn' ? 'danger' : 'primary'
}-button`}
>
{(footer && footer.primaryButtonLabel) || 'Save'}
</Button>
) : null}
</Fragment>
)}
</ModalFooter>
) : null}
</CarbonComposedModal>
);
};
ComposedModal.propTypes = ComposedModalPropTypes;
ComposedModal.defaultProps = defaultProps;
export default ComposedModal;