forked from react-native-dialogs/react-native-dialogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogAndroid.js
82 lines (67 loc) · 1.97 KB
/
DialogAndroid.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
/**
* @providesModule DialogAndroid
*/
'use strict';
var { NativeModules } = require('react-native');
var callbackNames = [
'onPositive',
'onNegative',
'onNeutral',
'onAny',
'itemsCallback',
'itemsCallbackSingleChoice',
'itemsCallbackMultiChoice',
'showListener',
'cancelListener',
'dismissListener',
];
class DialogAndroid {
constructor() {
this.options = {};
}
set(obj) {
Object.assign(this.options, obj);
}
show() {
var finalOptions = Object.assign({}, this.options);
var callbacks = {
error: (err, op) => console.error(err, op),
}
// Remove callbacks from the options, and store them separately
callbackNames.forEach(cb => {
if (cb in finalOptions) {
callbacks[cb] = finalOptions[cb];
finalOptions[cb] = true;
}
});
// Handle special case of input separately
if ('input' in finalOptions) {
finalOptions.input = Object.assign({}, finalOptions.input);
var inputCallback = finalOptions.input.callback || (x => console.log(x));
finalOptions.input.callback = true;
callbacks['input'] = inputCallback;
}
// Parse the result form multiple choice dialog
if ('itemsCallbackMultiChoice' in callbacks) {
var originalCallback = callbacks.itemsCallbackMultiChoice;
callbacks.itemsCallbackMultiChoice = selected => {
var indices = selected.split(',').map(x => parseInt(x));
var elements = indices.map(ind => (finalOptions.items || [])[ind]);
if(indices.length === 1 && isNaN(indices[0])){
indices=[] // the case of empty selection
elements=[]
}
originalCallback(indices, elements);
}
}
var callbackFunc = (cb, ...rest) => callbacks[cb](...rest);
NativeModules.DialogAndroid.show(finalOptions, callbackFunc);
}
dismiss() {
NativeModules.DialogAndroid.dismiss();
}
list(options, cb){
NativeModules.DialogAndroid.list(options, cb)
}
}
module.exports = DialogAndroid;