-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
DialogManager.tsx
150 lines (133 loc) · 3.71 KB
/
DialogManager.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
import * as React from 'react';
import { createContext, useEffect, useMemo, useRef, useState } from 'react';
import { Alert, Platform, StyleSheet } from 'react-native';
import { Button, Dialog, Portal, Text } from 'react-native-paper';
import Modal from './Modal';
import { _ } from '@joplin/lib/locale';
import shim from '@joplin/lib/shim';
import makeShowMessageBox from '../utils/makeShowMessageBox';
export interface PromptButton {
text: string;
onPress?: ()=> void;
style?: 'cancel'|'default'|'destructive';
}
interface PromptOptions {
cancelable?: boolean;
}
export interface DialogControl {
prompt(title: string, message: string, buttons?: PromptButton[], options?: PromptOptions): void;
}
export const DialogContext = createContext<DialogControl>(null);
interface Props {
children: React.ReactNode;
}
interface PromptDialogData {
key: string;
title: string;
message: string;
buttons: PromptButton[];
onDismiss: (()=> void)|null;
}
const styles = StyleSheet.create({
dialogContainer: {
maxWidth: 400,
minWidth: '50%',
alignSelf: 'center',
},
modalContainer: {
marginTop: 'auto',
marginBottom: 'auto',
},
});
const DialogManager: React.FC<Props> = props => {
const [dialogModels, setPromptDialogs] = useState<PromptDialogData[]>([]);
const nextDialogIdRef = useRef(0);
const dialogControl: DialogControl = useMemo(() => {
const defaultButtons = [{ text: _('OK') }];
return {
prompt: (title: string, message: string, buttons: PromptButton[] = defaultButtons, options?: PromptOptions) => {
if (Platform.OS !== 'web') {
// Alert.alert provides a more native style on iOS.
Alert.alert(title, message, buttons, options);
// Alert.alert doesn't work on web.
} else {
const onDismiss = () => {
setPromptDialogs(dialogs => dialogs.filter(d => d !== dialog));
};
const cancelable = options?.cancelable ?? true;
const dialog: PromptDialogData = {
key: `dialog-${nextDialogIdRef.current++}`,
title,
message,
buttons: buttons.map(button => ({
...button,
onPress: () => {
onDismiss();
button.onPress?.();
},
})),
onDismiss: cancelable ? onDismiss : null,
};
setPromptDialogs(dialogs => {
return [
...dialogs,
dialog,
];
});
}
},
};
}, []);
const dialogControlRef = useRef(dialogControl);
dialogControlRef.current = dialogControl;
useEffect(() => {
shim.showMessageBox = makeShowMessageBox(dialogControlRef);
return () => {
dialogControlRef.current = null;
};
}, []);
const dialogComponents: React.ReactNode[] = [];
for (const dialog of dialogModels) {
const buttons = dialog.buttons.map((button, index) => {
return (
<Button key={`${index}-${button.text}`} onPress={button.onPress}>{button.text}</Button>
);
});
dialogComponents.push(
<Dialog
testID={'prompt-dialog'}
style={styles.dialogContainer}
key={dialog.key}
visible={true}
onDismiss={dialog.onDismiss}
>
<Dialog.Title>{dialog.title}</Dialog.Title>
<Dialog.Content>
<Text variant='bodyMedium'>{dialog.message}</Text>
</Dialog.Content>
<Dialog.Actions>
{buttons}
</Dialog.Actions>
</Dialog>,
);
}
// Web: Use a <Modal> wrapper for better keyboard focus handling.
return <>
<DialogContext.Provider value={dialogControl}>
{props.children}
</DialogContext.Provider>
<Portal>
<Modal
visible={!!dialogComponents.length}
containerStyle={styles.modalContainer}
animationType='none'
backgroundColor='rgba(0, 0, 0, 0.1)'
transparent={true}
onRequestClose={dialogModels[dialogComponents.length - 1]?.onDismiss}
>
{dialogComponents}
</Modal>
</Portal>
</>;
};
export default DialogManager;