/
ConfirmContent.tsx
190 lines (165 loc) · 6.43 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
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 useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import colors from '@styles/theme/colors';
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 ImageSVG from './ImageSVG';
import Text from './Text';
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;
/** Whether to center the icon / text content */
shouldCenterContent?: boolean;
/** Whether to stack the buttons */
shouldStackButtons?: boolean;
/** Styles for title */
titleStyles?: StyleProp<TextStyle>;
/** 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;
};
function ConfirmContent({
title,
onConfirm,
onCancel = () => {},
confirmText = '',
cancelText = '',
prompt = '',
success = true,
danger = false,
shouldDisableConfirmButtonWhenOffline = false,
shouldShowCancelButton = false,
iconSource,
shouldCenterContent = false,
shouldStackButtons = true,
titleStyles,
promptStyles,
contentStyles,
iconAdditionalStyles,
image,
}: ConfirmContentProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const theme = useTheme();
const {isOffline} = useNetwork();
const StyleUtils = useStyleUtils();
const isCentered = shouldCenterContent;
return (
<>
{!!image && (
<View style={[StyleUtils.getBackgroundColorStyle(colors.pink800)]}>
<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]}>
<View style={isCentered ? [styles.alignItemsCenter, styles.mb6] : []}>
{typeof iconSource === 'function' && (
<View style={[styles.flexRow, styles.mb3]}>
<Icon
src={iconSource}
fill={theme.icon}
width={variables.appModalAppIconSize}
height={variables.appModalAppIconSize}
additionalStyles={iconAdditionalStyles}
/>
</View>
)}
<View style={[styles.flexRow, isCentered ? {} : styles.mb4]}>
<Header
title={title}
textStyles={titleStyles}
/>
</View>
{typeof prompt === 'string' ? <Text style={[promptStyles, isCentered ? styles.textAlignCenter : {}]}>{prompt}</Text> : prompt}
</View>
{shouldStackButtons ? (
<>
<Button
success={success}
danger={danger}
style={[styles.mt4]}
onPress={onConfirm}
pressOnEnter
large
text={confirmText || translate('common.yes')}
isDisabled={isOffline && shouldDisableConfirmButtonWhenOffline}
/>
{shouldShowCancelButton && (
<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')}
medium
/>
)}
<Button
success={success}
danger={danger}
style={[styles.flex1]}
onPress={onConfirm}
pressOnEnter
text={confirmText || translate('common.yes')}
isDisabled={isOffline && shouldDisableConfirmButtonWhenOffline}
medium
/>
</View>
)}
</View>
</>
);
}
ConfirmContent.displayName = 'ConfirmContent';
export default ConfirmContent;