-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alert.tsx
189 lines (177 loc) · 4.55 KB
/
Alert.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
import * as React from 'react';
import { AlertButton, Text, View } from 'react-native';
import { useHeaderContext } from '../contexts/HeaderContext';
import { useThemeContext } from '../contexts/ThemeContext';
import createStyleSheet, {
createStyleSheetP,
} from '../styles/createStyleSheet';
import type { ButtonStateColor } from '../types';
import Button from './Button';
import DialogBox from './DialogBox';
import Modal from './Modal';
export type AlertItem = {
title: string | undefined;
message?: string | undefined;
buttons?: AlertButton[] | undefined;
};
type AlertProps = {
visible: boolean;
onHide: () => void;
onDismiss?: () => void;
} & AlertItem;
export default function Alert({
onDismiss,
visible,
onHide,
title,
message,
buttons = [{ text: 'OK' }],
}: AlertProps): JSX.Element {
if (buttons.length > 2) {
throw new Error('Supports a maximum of two buttons.');
}
const { defaultStatusBarTranslucent: statusBarTranslucent } =
useHeaderContext();
const { colors, fonts } = useThemeContext();
const disableBackgroundClose = true;
const transparent = true;
const contentFC = () => {
if (message !== undefined) {
return (
<View style={styles.titleContainer1}>
<Text
numberOfLines={1}
style={[{ color: colors.card.title }, fonts.title]}
>
{title}
</Text>
<View style={styles.messageContainer}>
<Text style={[{ color: 'rgba(108, 113, 146, 1)' }, fonts.subtitle]}>
{message}
</Text>
</View>
</View>
);
} else {
return (
<View style={styles.titleContainer2}>
<Text
numberOfLines={1}
style={[
{
color: colors.card.title,
},
fonts.title,
]}
>
{title}
</Text>
</View>
);
}
};
const buttonStyle1 = useStyleSheet(0, buttons.length).button;
const buttonStyle2 = useStyleSheet(1, buttons.length).button;
const buttonColor1: Partial<ButtonStateColor> = {
enabled: {
background: 'rgba(242, 242, 242, 1)',
content: 'rgba(0, 0, 0, 1)',
},
pressed: {
background: 'rgba(230, 230, 230, 1)',
content: 'rgba(0, 0, 0, 1)',
},
};
return (
<Modal
onRequestClose={onHide}
onDismiss={onDismiss}
statusBarTranslucent={statusBarTranslucent}
visible={visible}
backgroundStyle={{
alignItems: 'center',
justifyContent: 'center',
}}
disableBackgroundClose={disableBackgroundClose}
transparent={transparent}
backdropColor={colors.backdrop}
>
<DialogBox style={styles.container}>
{contentFC()}
{/* <Divider /> */}
<View style={styles.buttonContainer}>
{buttons.map(({ text, onPress }, index) => {
return (
<Button
key={text + index.toString()}
style={
buttons.length === 2
? index === 0
? buttonStyle1
: buttonStyle2
: buttonStyle2
}
color={
buttons.length === 2
? index === 0
? buttonColor1
: undefined
: undefined
}
onPress={() => {
try {
onPress?.();
} finally {
onHide();
}
}}
>
{text}
</Button>
);
})}
</View>
</DialogBox>
</Modal>
);
}
const styles = createStyleSheet({
container: {
paddingTop: 20,
},
titleContainer1: {
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 30,
},
titleContainer2: {
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 30,
},
messageContainer: {
paddingTop: 16,
marginBottom: 0,
},
buttonContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-evenly',
paddingVertical: 20,
},
});
const useStyleSheet = (index: number, count: number) => {
const styles = createStyleSheetP(
(params: { index: number; count: number }) => {
return {
button: {
width: params.count === 1 ? '90%' : '40%',
borderRadius: 22,
height: 36,
},
};
},
{ index, count }
);
return styles;
};