Repository navigation
Expand file tree
/
Copy pathAlert.web.js
More file actions
120 lines (110 loc) · 2.66 KB
/
Copy pathAlert.web.js
File metadata and controls
120 lines (110 loc) · 2.66 KB
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
/**
* Copyright (c) 2015-present, Alibaba Group Holding Limited.
* All rights reserved.
*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* @providesModule ReactAlert
*/
'use strict';
const DEFAULT_BUTTON_TEXT = 'OK';
const DEFAULT_BUTTON = {
text: DEFAULT_BUTTON_TEXT,
onPress: null,
};
const noop = function() {};
/**
* Launches an alert dialog with the specified title and message.
*
* Optionally provide a list of buttons. Tapping any button will fire the
* respective onPress callback and dismiss the alert. By default, the only
* button will be an 'OK' button
*
* The last button in the list will be considered the 'Primary' button and
* it will appear bold.
*
* ```
* AlertIOS.alert(
* 'Foo Title',
* 'My Alert Msg',
* [
* {text: 'Foo', onPress: () => console.log('Foo Pressed!')},
* {text: 'Bar', onPress: () => console.log('Bar Pressed!')},
* ]
* )
* ```
*/
class AlertIOS {
static alert(
title: ?string,
message?: ?string,
buttons?: Array<{
text: ?string;
onPress?: ?Function;
}>,
type?: ?string
): void {
let callbacks = [];
let buttonsSpec = [];
title = title || '';
message = message || '';
buttons = buttons || [DEFAULT_BUTTON];
type = type || '';
buttons.forEach((btn, index) => {
callbacks[index] = btn.onPress;
let btnDef = {};
btnDef[index] = btn.text || DEFAULT_BUTTON_TEXT;
buttonsSpec.push(btnDef);
});
const confirmCallback = callbacks.pop() || noop;
const cancelCallback = callbacks.pop() || noop;
if (buttons.length === 1) {
alert(title);
confirmCallback();
} else if (buttons.length === 2) {
if (confirm(title)) {
confirmCallback();
} else {
cancelCallback();
}
} else {
throw new Error('max two buttons supported: [negativeActionBtn, positiveActionBtn]');
}
}
static prompt(
title: string,
value?: string,
buttons?: Array<{
text: ?string;
onPress?: ?Function;
}>,
callback?: ?Function
): void {
if (arguments.length === 2) {
if (typeof value === 'object') {
buttons = value;
value = undefined;
} else if (typeof value === 'function') {
callback = value;
value = undefined;
}
} else if (arguments.length === 3 && typeof buttons === 'function') {
callback = buttons;
buttons = undefined;
}
if (!buttons) {
buttons = [{
text: 'Cancel',
}, {
text: 'OK',
onPress: callback
}];
}
let ret = prompt(title);
if (ret && callback) {
callback();
}
}
}
export default AlertIOS;