-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathConfirmContent.tsx
257 lines (222 loc) · 8.91 KB
/
ConfirmContent.tsx
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
import type {ReactNode} from 'react';
import React from 'react';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type IconAsset from '@src/types/utils/IconAsset';
import Button from './Button';
import Header from './Header';
import Icon from './Icon';
import {Close} from './Icon/Expensicons';
import ImageSVG from './ImageSVG';
import {PressableWithoutFeedback} from './Pressable';
import Text from './Text';
import Tooltip from './Tooltip';
type ConfirmContentProps = {
/** Title of the modal */
title: string;
/** A callback to call when the form has been submitted */
onConfirm: () => void;
/** A callback to call when the form has been closed */
onCancel?: () => void;
/** Confirm button text */
confirmText?: string;
/** Cancel button text */
cancelText?: string;
/** Modal content text/element */
prompt?: string | ReactNode;
/** Whether we should use the success button color */
success?: boolean;
/** Whether we should use the danger button color. Use if the action is destructive */
danger?: boolean;
/** Whether we should disable the confirm button when offline */
shouldDisableConfirmButtonWhenOffline?: boolean;
/** Whether we should show the cancel button */
shouldShowCancelButton?: boolean;
/** Icon to display above the title */
iconSource?: IconAsset;
/** Fill color for the Icon */
iconFill?: string | false;
/** Icon width */
iconWidth?: number;
/** Icon height */
iconHeight?: number;
/** Should the icon be centered? */
shouldCenterIcon?: boolean;
/** Whether to center the icon / text content */
shouldCenterContent?: boolean;
/** Whether to show the dismiss icon */
shouldShowDismissIcon?: boolean;
/** Whether to stack the buttons */
shouldStackButtons?: boolean;
/** Whether to reverse the order of the stacked buttons */
shouldReverseStackedButtons?: boolean;
/** Styles for title */
titleStyles?: StyleProp<TextStyle>;
/** Styles for title container */
titleContainerStyles?: StyleProp<ViewStyle>;
/** Styles for prompt */
promptStyles?: StyleProp<TextStyle>;
/** Styles for view */
contentStyles?: StyleProp<ViewStyle>;
/** Styles for icon */
iconAdditionalStyles?: StyleProp<ViewStyle>;
/** Image to display with content */
image?: IconAsset;
/** Styles for the image */
imageStyles?: StyleProp<ViewStyle>;
/** Whether the modal is visible */
isVisible: boolean;
/** Whether the confirm button is loading */
isConfirmLoading?: boolean;
};
function ConfirmContent({
title,
onConfirm,
onCancel = () => {},
confirmText = '',
cancelText = '',
prompt = '',
success = true,
danger = false,
shouldDisableConfirmButtonWhenOffline = false,
shouldShowCancelButton = false,
iconSource,
iconFill,
shouldCenterContent = false,
shouldStackButtons = true,
titleStyles,
promptStyles,
contentStyles,
iconAdditionalStyles,
iconWidth = variables.appModalAppIconSize,
iconHeight = variables.appModalAppIconSize,
shouldCenterIcon = false,
shouldShowDismissIcon = false,
image,
imageStyles,
titleContainerStyles,
shouldReverseStackedButtons = false,
isVisible,
isConfirmLoading,
}: ConfirmContentProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const theme = useTheme();
const {isOffline} = useNetwork();
const isCentered = shouldCenterContent;
return (
<>
{!!image && (
<View style={imageStyles}>
<ImageSVG
contentFit="contain"
src={image}
height={CONST.CONFIRM_CONTENT_SVG_SIZE.HEIGHT}
width={CONST.CONFIRM_CONTENT_SVG_SIZE.WIDTH}
style={styles.alignSelfCenter}
/>
</View>
)}
<View style={[styles.m5, contentStyles]}>
{shouldShowDismissIcon && (
<View style={styles.alignItemsEnd}>
<Tooltip text={translate('common.close')}>
<PressableWithoutFeedback
onPress={onCancel}
role={CONST.ROLE.BUTTON}
accessibilityLabel={translate('common.close')}
>
<Icon
fill={theme.icon}
src={Close}
/>
</PressableWithoutFeedback>
</Tooltip>
</View>
)}
<View style={isCentered ? [styles.alignItemsCenter, styles.mb6] : []}>
{!!iconSource && (
<View style={[shouldCenterIcon ? styles.justifyContentCenter : null, styles.flexRow, styles.mb3]}>
<Icon
src={iconSource}
fill={iconFill === false ? undefined : iconFill ?? theme.icon}
width={iconWidth}
height={iconHeight}
additionalStyles={iconAdditionalStyles}
/>
</View>
)}
<View style={[styles.flexRow, isCentered ? {} : styles.mb4, titleContainerStyles]}>
<Header
title={title}
textStyles={titleStyles}
/>
</View>
{typeof prompt === 'string' ? <Text style={[promptStyles, isCentered ? styles.textAlignCenter : {}]}>{prompt}</Text> : prompt}
</View>
{shouldStackButtons ? (
<>
{shouldShowCancelButton && shouldReverseStackedButtons && (
<Button
style={[styles.mt4, styles.noSelect]}
onPress={onCancel}
large
text={cancelText || translate('common.no')}
/>
)}
<Button
success={success}
danger={danger}
style={shouldReverseStackedButtons ? styles.mt3 : styles.mt4}
onPress={onConfirm}
pressOnEnter
isPressOnEnterActive={isVisible}
large
text={confirmText || translate('common.yes')}
accessibilityLabel={confirmText || translate('common.yes')}
isDisabled={isOffline && shouldDisableConfirmButtonWhenOffline}
isLoading={isConfirmLoading}
/>
{shouldShowCancelButton && !shouldReverseStackedButtons && (
<Button
style={[styles.mt3, styles.noSelect]}
onPress={onCancel}
large
text={cancelText || translate('common.no')}
/>
)}
</>
) : (
<View style={[styles.flexRow, styles.gap4]}>
{shouldShowCancelButton && (
<Button
style={[styles.noSelect, styles.flex1]}
onPress={onCancel}
text={cancelText || translate('common.no')}
/>
)}
<Button
success={success}
danger={danger}
style={[styles.flex1]}
onPress={onConfirm}
pressOnEnter
isPressOnEnterActive={isVisible}
text={confirmText || translate('common.yes')}
isDisabled={isOffline && shouldDisableConfirmButtonWhenOffline}
isLoading={isConfirmLoading}
/>
</View>
)}
</View>
</>
);
}
ConfirmContent.displayName = 'ConfirmContent';
export default ConfirmContent;