-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
90 lines (79 loc) · 2.05 KB
/
index.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
/* @flow */
/**
* index.js
*
* @author SimMan (liwei0990#gmail.com)
* @Time at 2016-12-04 12:04:33
* Copyright 2011-2016 RNKit.io, Inc.
*/
'use strict';
import {
NativeModules,
processColor,
Platform,
NativeEventEmitter
} from 'react-native';
const { RNKitAlertView } = NativeModules;
const nativeEventEmitter = new NativeEventEmitter(RNKitAlertView);
type AlertType = $Enum<{
'default': string,
'plain-text': string
}>;
type AlertButtonStyle = $Enum<{
'default': string,
'cancel': string,
'destructive': string,
}>;
type ButtonsArray = Array<{
text?: string,
onPress?: ?Function,
style?: AlertButtonStyle,
}>;
class Alert {
static alert(
title: ?string,
message?: ?string,
callbackOrButtons?: ?(() => void) | ButtonsArray,
type?: AlertType,
placeholder?: string,
doneButtonKey?: number,
): void {
var callbacks = [];
var buttons = [];
if (typeof callbackOrButtons === 'function') {
callbacks = [callbackOrButtons];
}
else if (callbackOrButtons instanceof Array) {
callbackOrButtons.forEach((btn, index) => {
callbacks[index] = btn.onPress;
if (btn.text || index < (callbackOrButtons || []).length - 1) {
var btnDef = {};
btnDef['text'] = btn.text || '';
btnDef['index'] = String(index);
btnDef['style'] = btn.style || 'default';
buttons.push(btnDef);
}
});
}
this.listener && this.listener.remove();
this.listener = nativeEventEmitter.addListener('AlertViewEvent', event => {
if (event.text.length && Platform.OS !== 'ios') {
this.listener && this.listener.remove();
}
var cb = callbacks[doneButtonKey];
cb && cb(event.text);
// console.log('=========>' + event.text);
});
RNKitAlertView.alertWithArgs({
title: title || undefined,
message: message || undefined,
buttons,
type: type || undefined,
placeholder
}, (id, value) => {
var cb = callbacks[id];
cb && cb(value);
});
}
};
export default Alert;