-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionMenu.tsx
144 lines (138 loc) · 3.67 KB
/
ActionMenu.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
import React from 'react';
import {
StyleProp,
Text,
TouchableOpacity,
View,
ViewStyle,
} from 'react-native';
import { useHeaderContext } from '../contexts/HeaderContext';
import { useThemeContext } from '../contexts/ThemeContext';
import createStyleSheet from '../styles/createStyleSheet';
import DialogBox from './DialogBox';
import Loading from './Loading';
import Modal from './Modal';
export type ActionMenuItem = {
title?: string | undefined;
menuItems: {
title: string;
onPress?: (() => Promise<void>) | (() => void) | undefined;
onError?: () => void | undefined;
}[];
};
type ActionMenuProps = {
visible: boolean;
onHide: () => void;
onError?: (error: unknown) => void | undefined;
onDismiss?: () => void | undefined;
style?: StyleProp<ViewStyle> | undefined;
} & ActionMenuItem;
export default function ActionMenu({
visible,
onHide,
onError,
onDismiss,
style,
title,
menuItems,
}: ActionMenuProps): JSX.Element {
const { colors, fonts } = useThemeContext();
const { defaultStatusBarTranslucent } = useHeaderContext();
const [pending, setPending] = React.useState(false);
const transparent = true;
const _onHide = () => {
if (!pending) onHide();
};
return (
<Modal
type="fade"
onRequestClose={_onHide}
onDismiss={onDismiss}
statusBarTranslucent={defaultStatusBarTranslucent}
visible={visible}
backgroundStyle={[
{ alignItems: 'center', justifyContent: 'center' },
style,
]}
transparent={transparent}
>
<DialogBox style={{ backgroundColor: colors.card.background }}>
{title && (
<View style={styles.title}>
<Text
numberOfLines={1}
style={[
{
maxWidth: pending ? '86%' : '100%',
color: colors.primary,
},
fonts.title,
]}
>
{title}
</Text>
{pending && (
<Loading
size={20}
color={colors.primary}
style={{ width: '10%', marginLeft: '4%' }}
/>
)}
</View>
)}
<View style={styles.buttonContainer}>
{menuItems.map((item, index) => {
return (
<TouchableOpacity
activeOpacity={0.55}
key={item.title + index.toString()}
style={styles.button}
disabled={pending}
onPress={async () => {
setPending(true);
try {
await item.onPress?.();
} catch (e: unknown) {
const errorHandler = onError ?? item.onError;
errorHandler?.(e);
if (!errorHandler)
console.error('ActionMenu onPress error', e);
} finally {
onHide();
setPending(false);
}
}}
>
<Text
numberOfLines={1}
style={[{ color: colors.primary }, fonts.subtitle]}
>
{item.title}
</Text>
</TouchableOpacity>
);
})}
</View>
</DialogBox>
</Modal>
);
}
const styles = createStyleSheet({
title: {
flexDirection: 'row',
paddingHorizontal: 24,
paddingVertical: 20,
alignItems: 'center',
},
buttonContainer: {
marginTop: 4,
marginBottom: 8,
},
button: {
paddingHorizontal: 24,
paddingVertical: 12,
},
container: {
backgroundColor: 'rgba(235, 235, 235, 1)',
},
});