-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathFormAlertWithSubmitButton.tsx
146 lines (122 loc) · 4.98 KB
/
FormAlertWithSubmitButton.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
import type {Ref} from 'react';
import React from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import useSafePaddingBottomStyle from '@hooks/useSafePaddingBottomStyle';
import useThemeStyles from '@hooks/useThemeStyles';
import getPlatform from '@libs/getPlatform';
import CONST from '@src/CONST';
import Button from './Button';
import FormAlertWrapper from './FormAlertWrapper';
type FormAlertWithSubmitButtonProps = {
/** Error message to display above button */
message?: string;
/** Whether the button is disabled */
isDisabled?: boolean;
/** Whether message is in html format */
isMessageHtml?: boolean;
/** Styles for container element */
containerStyles?: StyleProp<ViewStyle>;
/** Is the button in a loading state */
isLoading?: boolean;
/** Callback fired when the "fix the errors" link is pressed */
onFixTheErrorsLinkPressed?: () => void;
/** Submit function */
onSubmit: () => void;
/** Should the button be enabled when offline */
enabledWhenOffline?: boolean;
/** Disable press on enter for submit button */
disablePressOnEnter?: boolean;
/** Whether the form submit action is dangerous */
isSubmitActionDangerous?: boolean;
/** Custom content to display in the footer after submit button */
footerContent?: React.ReactNode;
/** Styles for the button */
buttonStyles?: StyleProp<ViewStyle>;
/** Whether to show the alert text */
isAlertVisible?: boolean;
/** React ref being forwarded to the submit button */
buttonRef?: Ref<View>;
/** Text for the button */
buttonText: string;
/** Whether to use a smaller submit button size */
useSmallerSubmitButtonSize?: boolean;
/** Style for the error message for submit button */
errorMessageStyle?: StyleProp<ViewStyle>;
/** The priority to assign the enter key event listener to buttons. 0 is the highest priority. */
enterKeyEventListenerPriority?: number;
};
function FormAlertWithSubmitButton({
message = '',
isDisabled = false,
isMessageHtml = false,
containerStyles,
isLoading = false,
onFixTheErrorsLinkPressed = () => {},
enabledWhenOffline = false,
disablePressOnEnter = false,
isSubmitActionDangerous = false,
footerContent,
buttonRef,
buttonStyles,
buttonText,
isAlertVisible = false,
onSubmit,
useSmallerSubmitButtonSize = false,
errorMessageStyle,
enterKeyEventListenerPriority = 0,
}: FormAlertWithSubmitButtonProps) {
const styles = useThemeStyles();
const style = [!footerContent ? {} : styles.mb3, buttonStyles];
const safePaddingBottomStyle = useSafePaddingBottomStyle();
// Disable pressOnEnter for Android Native to avoid issues with the Samsung keyboard,
// where pressing Enter saves the form instead of adding a new line in multiline input.
// More details: https://github.com/Expensify/App/issues/46644
const isAndroidNative = getPlatform() === CONST.PLATFORM.ANDROID;
const pressOnEnter = isAndroidNative ? false : !disablePressOnEnter;
return (
<FormAlertWrapper
containerStyles={[styles.justifyContentEnd, safePaddingBottomStyle, containerStyles]}
isAlertVisible={isAlertVisible}
isMessageHtml={isMessageHtml}
message={message}
onFixTheErrorsLinkPressed={onFixTheErrorsLinkPressed}
errorMessageStyle={errorMessageStyle}
>
{(isOffline: boolean | undefined) => (
<View>
{isOffline && !enabledWhenOffline ? (
<Button
success
isDisabled
text={buttonText}
style={style}
danger={isSubmitActionDangerous}
medium={useSmallerSubmitButtonSize}
large={!useSmallerSubmitButtonSize}
/>
) : (
<Button
ref={buttonRef}
success
pressOnEnter={pressOnEnter}
enterKeyEventListenerPriority={enterKeyEventListenerPriority}
text={buttonText}
style={style}
onPress={onSubmit}
isDisabled={isDisabled}
isLoading={isLoading}
danger={isSubmitActionDangerous}
medium={useSmallerSubmitButtonSize}
large={!useSmallerSubmitButtonSize}
/>
)}
{footerContent}
</View>
)}
</FormAlertWrapper>
);
}
FormAlertWithSubmitButton.displayName = 'FormAlertWithSubmitButton';
export default FormAlertWithSubmitButton;
export type {FormAlertWithSubmitButtonProps};