-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.android.js
72 lines (65 loc) · 2.16 KB
/
index.android.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
import {
NativeModules
} from 'react-native';
const PromptAndroid = NativeModules.PromptAndroid;
export default function prompt(title, message, callbackOrButtons, options) {
const defaultButtons = [
{
text: 'Cancel',
},
{
text: 'OK',
onPress: callbackOrButtons
}
];
let buttons = typeof callbackOrButtons === 'function'
? defaultButtons
: callbackOrButtons;
let config = {
title: title || '',
message: message || '',
};
if (options) {
config = {
...config,
cancelable: options.cancelable !== false,
type: options.type || 'default',
style: options.style || 'default',
defaultValue: options.defaultValue || '',
placeholder: options.placeholder || ''
};
}
// At most three buttons (neutral, negative, positive). Ignore rest.
// The text 'OK' should be probably localized. iOS Alert does that in native.
const validButtons = buttons ? buttons.slice(0, 3) : [{text: 'OK'}];
const buttonPositive = validButtons.pop();
const buttonNegative = validButtons.pop();
const buttonNeutral = validButtons.pop();
if (buttonNeutral) {
config = {...config, buttonNeutral: buttonNeutral.text || '' };
}
if (buttonNegative) {
config = {...config, buttonNegative: buttonNegative.text || '' };
}
if (buttonPositive) {
config = {
...config,
buttonPositive: buttonPositive.text || ''
};
}
PromptAndroid.promptWithArgs(
config,
(action, buttonKey, input) => {
if (action !== PromptAndroid.buttonClicked) {
return;
}
if (buttonKey === PromptAndroid.buttonNeutral) {
buttonNeutral.onPress && buttonNeutral.onPress(input);
} else if (buttonKey === PromptAndroid.buttonNegative) {
buttonNegative.onPress && buttonNegative.onPress();
} else if (buttonKey === PromptAndroid.buttonPositive) {
buttonPositive.onPress && buttonPositive.onPress(input);
}
}
);
}