-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSweetAlert.android.js
executable file
·86 lines (77 loc) · 2.78 KB
/
SweetAlert.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
import {NativeModules, Platform} from 'react-native'
const resolveAssetSource = require('resolveAssetSource');
const {SweetAlert} = NativeModules
export type Buttons = Array<{
text?: string,
onPress?: ?Function,
}>;
type Options = {
cancelable?: ?boolean,
onDismiss?: ?Function,
};
export default class SweetAlertRN {
/**
* SweetAlert.show("标题", "消息",
[{
text: "取消", onPress: () => {
console.info("cancel");
}
}, {
text: "确定", onPress: () => {
console.info("confirm");
}
}], {
cancelable: true, onDismiss: (e) => {
console.info("dismiss");
}
});
*
* @param title
* @param message
* @param buttons
* @param options
*/
static show(title: ?string,
message?: ?string,
buttons?: Buttons,
options?: Options,): void {
console.info(SweetAlert);
console.info("SweetAlertRN show", title, message, buttons, options);
let config = {
title: title || '',
message: message || '',
type: 0,
image: undefined,
};
if (options) {
config = {...config, cancelable: options.cancelable};
}
// 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, 2)
: [{text: 'OK'}];
const buttonPositive = validButtons.pop();
const buttonNegative = validButtons.pop();
if (buttonNegative) {
config = {...config, buttonNegative: buttonNegative.text || ''};
}
if (buttonPositive) {
config = {...config, buttonPositive: buttonPositive.text || ''};
}
SweetAlert.showAlert(
config,
(action, buttonKey) => {
if (action === SweetAlert.buttonClicked) {// "buttonClicked"
if (buttonKey === SweetAlert.buttonNegative) { // 0
buttonNegative.onPress && buttonNegative.onPress();
} else if (buttonKey === SweetAlert.buttonPositive) {// 1
buttonPositive.onPress && buttonPositive.onPress();
}
} else if (action === SweetAlert.dismissed) { // "dismissed"
options && options.onDismiss && options.onDismiss();
}
},
);
}
}