-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathConfirmContent.js
169 lines (144 loc) · 5.91 KB
/
ConfirmContent.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
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
import PropTypes from 'prop-types';
import React from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@styles/useThemeStyles';
import variables from '@styles/variables';
import Button from './Button';
import Header from './Header';
import Icon from './Icon';
import Text from './Text';
const propTypes = {
/** Title of the modal */
title: PropTypes.string.isRequired,
/** A callback to call when the form has been submitted */
onConfirm: PropTypes.func.isRequired,
/** A callback to call when the form has been closed */
onCancel: PropTypes.func,
/** Confirm button text */
confirmText: PropTypes.string,
/** Cancel button text */
cancelText: PropTypes.string,
/** Modal content text/element */
prompt: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
/** Whether we should use the success button color */
success: PropTypes.bool,
/** Whether we should use the danger button color. Use if the action is destructive */
danger: PropTypes.bool,
/** Whether we should disable the confirm button when offline */
shouldDisableConfirmButtonWhenOffline: PropTypes.bool,
/** Whether we should show the cancel button */
shouldShowCancelButton: PropTypes.bool,
/** Icon to display above the title */
iconSource: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/** Whether to center the icon / text content */
shouldCenterContent: PropTypes.bool,
/** Whether to stack the buttons */
shouldStackButtons: PropTypes.bool,
/** Styles for title */
// eslint-disable-next-line react/forbid-prop-types
titleStyles: PropTypes.arrayOf(PropTypes.object),
/** Styles for prompt */
// eslint-disable-next-line react/forbid-prop-types
promptStyles: PropTypes.arrayOf(PropTypes.object),
/** Styles for view */
// eslint-disable-next-line react/forbid-prop-types
contentStyles: PropTypes.arrayOf(PropTypes.object),
/** Styles for icon */
// eslint-disable-next-line react/forbid-prop-types
iconAdditionalStyles: PropTypes.arrayOf(PropTypes.object),
};
const defaultProps = {
confirmText: '',
cancelText: '',
prompt: '',
success: true,
danger: false,
onCancel: () => {},
shouldDisableConfirmButtonWhenOffline: false,
shouldShowCancelButton: true,
contentStyles: [],
iconSource: null,
shouldCenterContent: false,
shouldStackButtons: true,
titleStyles: [],
promptStyles: [],
iconAdditionalStyles: [],
};
function ConfirmContent(props) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const isCentered = props.shouldCenterContent;
return (
<View style={[styles.m5, ...props.contentStyles]}>
<View style={isCentered ? [styles.alignItemsCenter, styles.mb6] : []}>
{!_.isEmpty(props.iconSource) ||
(_.isFunction(props.iconSource) && (
<View style={[styles.flexRow, styles.mb3]}>
<Icon
src={props.iconSource}
width={variables.appModalAppIconSize}
height={variables.appModalAppIconSize}
additionalStyles={[...props.iconAdditionalStyles]}
/>
</View>
))}
<View style={[styles.flexRow, isCentered ? {} : styles.mb4]}>
<Header
title={props.title}
textStyles={[...props.titleStyles]}
/>
</View>
{_.isString(props.prompt) ? <Text style={[...props.promptStyles, isCentered ? styles.textAlignCenter : {}]}>{props.prompt}</Text> : props.prompt}
</View>
{props.shouldStackButtons ? (
<>
<Button
success={props.success}
danger={props.danger}
style={[styles.mt4]}
onPress={props.onConfirm}
pressOnEnter
text={props.confirmText || translate('common.yes')}
isDisabled={isOffline && props.shouldDisableConfirmButtonWhenOffline}
/>
{props.shouldShowCancelButton && (
<Button
style={[styles.mt3, styles.noSelect]}
onPress={props.onCancel}
text={props.cancelText || translate('common.no')}
/>
)}
</>
) : (
<View style={[styles.flexRow, styles.gap4]}>
{props.shouldShowCancelButton && (
<Button
style={[styles.noSelect, styles.flex1]}
onPress={props.onCancel}
text={props.cancelText || translate('common.no')}
medium
/>
)}
<Button
success={props.success}
danger={props.danger}
style={[styles.flex1]}
onPress={props.onConfirm}
pressOnEnter
text={props.confirmText || translate('common.yes')}
isDisabled={isOffline && props.shouldDisableConfirmButtonWhenOffline}
medium
/>
</View>
)}
</View>
);
}
ConfirmContent.propTypes = propTypes;
ConfirmContent.defaultProps = defaultProps;
ConfirmContent.displayName = 'ConfirmContent';
export default ConfirmContent;