forked from shimohq/react-native-prompt-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
140 lines (129 loc) · 3.42 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
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
import {
NativeModules
} from 'react-native';
const PromptAndroid = NativeModules.PromptAndroid;
export type PromptType = $Enum<{
/**
* Default alert with no inputs
*/
'default': string,
/**
* Plain text input alert
*/
'plain-text': string,
/**
* Secure text input alert
*/
'secure-text': string,
/**
* Numeric input alert
*/
'numeric': string,
/**
* Email address input alert
*/
'email-address': string,
/**
* Phone pad input alert
*/
'phone-pad': string,
}>;
export type PromptStyle = $Enum<{
/**
* Default alert dialog style
*/
'default': string,
/**
* Shimo alert dialog style
*/
'shimo': string,
}>;
type Options = {
cancelable?: ?boolean;
type?: ?PromptType;
defaultValue?: ?String;
placeholder?: ?String;
style?: ?PromptStyle;
};
/**
* Array or buttons
* @typedef {Array} ButtonsArray
* @property {string=} text Button label
* @property {Function=} onPress Callback function when button pressed
*/
type ButtonsArray = Array<{
/**
* Button label
*/
text?: string,
/**
* Callback function when button pressed
*/
onPress?: ?Function,
}>;
export default function prompt(
title: ?string,
message?: ?string,
callbackOrButtons?: ?((text: string) => void) | ButtonsArray,
options?: Options
): void {
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 ? 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);
}
}
);
}