1
1
/**
2
- * Copyright (c) 2015-present, Facebook, Inc.
2
+ * Copyright (c) Facebook, Inc. and its affiliates .
3
3
*
4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
10
10
11
11
'use strict' ;
12
12
13
- const AlertIOS = require ( 'AlertIOS' ) ;
14
13
const NativeModules = require ( 'NativeModules' ) ;
14
+ const RCTAlertManager = NativeModules . AlertManager ;
15
15
const Platform = require ( 'Platform' ) ;
16
16
17
- import type { AlertType , AlertButtonStyle } from 'AlertIOS' ;
18
-
19
17
export type Buttons = Array < {
20
18
text ?: string ,
21
19
onPress ?: ?Function ,
@@ -27,39 +25,142 @@ type Options = {
27
25
onDismiss ?: ?Function ,
28
26
} ;
29
27
28
+ type AlertType = $Enum < {
29
+ default : string ,
30
+ 'plain-text' : string ,
31
+ 'secure-text' : string ,
32
+ 'login-password' : string ,
33
+ } > ;
34
+
35
+ export type AlertButtonStyle = $Enum < {
36
+ default : string ,
37
+ cancel : string ,
38
+ destructive : string ,
39
+ } > ;
40
+
30
41
/**
31
42
* Launches an alert dialog with the specified title and message.
32
43
*
33
44
* See http://facebook.github.io/react-native/docs/alert.html
34
45
*/
35
46
class Alert {
36
- /**
37
- * Launches an alert dialog with the specified title and message.
38
- *
39
- * See http://facebook.github.io/react-native/docs/alert.html#alert
40
- */
41
47
static alert (
42
48
title : ?string ,
43
49
message ?: ?string ,
44
50
buttons ?: Buttons ,
45
51
options ?: Options ,
46
- type ?: AlertType ,
47
52
) : void {
48
53
if ( Platform . OS === 'ios' ) {
49
- if ( typeof type !== 'undefined' ) {
50
- console . warn (
51
- 'Alert.alert() with a 5th "type" parameter is deprecated and will be removed. Use AlertIOS.prompt() instead.' ,
52
- ) ;
53
- AlertIOS . alert ( title , message , buttons , type ) ;
54
- return ;
55
- }
56
54
AlertIOS . alert ( title , message , buttons ) ;
57
55
} else if ( Platform . OS === 'android' ) {
58
56
AlertAndroid . alert ( title , message , buttons , options ) ;
59
57
} else if ( Platform . OS === 'desktop' ) {
60
58
AlertDesktop . alert ( title , message , buttons , options ) ;
61
59
}
62
60
}
61
+
62
+ static prompt (
63
+ title : ?string ,
64
+ message ?: ?string ,
65
+ callbackOrButtons ?: ?( ( ( text : string ) => void ) | Buttons ) ,
66
+ type ?: ?AlertType = 'plain-text' ,
67
+ defaultValue ?: string ,
68
+ keyboardType ?: string ,
69
+ ) : void {
70
+ if ( Platform . OS === 'ios' ) {
71
+ AlertIOS . prompt (
72
+ title ,
73
+ message ,
74
+ callbackOrButtons ,
75
+ type ,
76
+ defaultValue ,
77
+ keyboardType ,
78
+ ) ;
79
+ }
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Wrapper around the iOS native module.
85
+ */
86
+ class AlertIOS {
87
+ static alert (
88
+ title : ?string ,
89
+ message ?: ?string ,
90
+ callbackOrButtons ?: ?( ( ( ) => void ) | Buttons ) ,
91
+ ) : void {
92
+ this . prompt ( title , message , callbackOrButtons , 'default' ) ;
93
+ }
94
+
95
+ static prompt (
96
+ title : ?string ,
97
+ message ?: ?string ,
98
+ callbackOrButtons ?: ?( ( ( text : string ) => void ) | Buttons ) ,
99
+ type ?: ?AlertType = 'plain-text' ,
100
+ defaultValue ?: string ,
101
+ keyboardType ?: string ,
102
+ ) : void {
103
+ if ( typeof type === 'function' ) {
104
+ console . warn (
105
+ 'You passed a callback function as the "type" argument to Alert.prompt(). React Native is ' +
106
+ 'assuming you want to use the deprecated Alert.prompt(title, defaultValue, buttons, callback) ' +
107
+ 'signature. The current signature is Alert.prompt(title, message, callbackOrButtons, type, defaultValue, ' +
108
+ 'keyboardType) and the old syntax will be removed in a future version.' ,
109
+ ) ;
110
+
111
+ const callback = type ;
112
+ RCTAlertManager . alertWithArgs (
113
+ {
114
+ title : title || '' ,
115
+ type : 'plain-text' ,
116
+ defaultValue : message ,
117
+ } ,
118
+ ( id , value ) => {
119
+ callback ( value ) ;
120
+ } ,
121
+ ) ;
122
+ return ;
123
+ }
124
+
125
+ let callbacks = [ ] ;
126
+ const buttons = [ ] ;
127
+ let cancelButtonKey ;
128
+ let destructiveButtonKey ;
129
+ if ( typeof callbackOrButtons === 'function ') {
130
+ callbacks = [ callbackOrButtons ] ;
131
+ } else if ( Array . isArray ( callbackOrButtons ) ) {
132
+ callbackOrButtons . forEach ( ( btn , index ) => {
133
+ callbacks [ index ] = btn . onPress ;
134
+ if ( btn . style === 'cancel' ) {
135
+ cancelButtonKey = String ( index ) ;
136
+ } else if ( btn . style === 'destructive' ) {
137
+ destructiveButtonKey = String ( index ) ;
138
+ }
139
+ if ( btn . text || index < ( callbackOrButtons || [ ] ) . length - 1 ) {
140
+ const btnDef = { } ;
141
+ btnDef [ index ] = btn . text || '' ;
142
+ buttons . push ( btnDef ) ;
143
+ }
144
+ } ) ;
145
+ }
146
+
147
+ RCTAlertManager . alertWithArgs (
148
+ {
149
+ title : title || '' ,
150
+ message : message || undefined ,
151
+ buttons,
152
+ type : type || undefined ,
153
+ defaultValue,
154
+ cancelButtonKey,
155
+ destructiveButtonKey,
156
+ keyboardType,
157
+ } ,
158
+ ( id , value ) => {
159
+ const cb = callbacks [ id ] ;
160
+ cb && cb ( value ) ;
161
+ } ,
162
+ ) ;
163
+ }
63
164
}
64
165
65
166
/**
@@ -116,7 +217,7 @@ class AlertAndroid {
116
217
} else if ( action === NativeModules . DialogManagerAndroid . dismissed ) {
117
218
options && options . onDismiss && options . onDismiss ( ) ;
118
219
}
119
- }
220
+ } ,
120
221
) ;
121
222
}
122
223
}
0 commit comments