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,37 +25,140 @@ 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
}
60
58
}
59
+
60
+ static prompt (
61
+ title : ?string ,
62
+ message ?: ?string ,
63
+ callbackOrButtons ?: ?( ( ( text : string ) => void ) | Buttons ) ,
64
+ type ?: ?AlertType = 'plain-text' ,
65
+ defaultValue ?: string ,
66
+ keyboardType ?: string ,
67
+ ) : void {
68
+ if ( Platform . OS === 'ios' ) {
69
+ AlertIOS . prompt (
70
+ title ,
71
+ message ,
72
+ callbackOrButtons ,
73
+ type ,
74
+ defaultValue ,
75
+ keyboardType ,
76
+ ) ;
77
+ }
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Wrapper around the iOS native module.
83
+ */
84
+ class AlertIOS {
85
+ static alert (
86
+ title : ?string ,
87
+ message ?: ?string ,
88
+ callbackOrButtons ?: ?( ( ( ) => void ) | Buttons ) ,
89
+ ) : void {
90
+ this . prompt ( title , message , callbackOrButtons , 'default' ) ;
91
+ }
92
+
93
+ static prompt (
94
+ title : ?string ,
95
+ message ?: ?string ,
96
+ callbackOrButtons ?: ?( ( ( text : string ) => void ) | Buttons ) ,
97
+ type ?: ?AlertType = 'plain-text' ,
98
+ defaultValue ?: string ,
99
+ keyboardType ?: string ,
100
+ ) : void {
101
+ if ( typeof type === 'function' ) {
102
+ console . warn (
103
+ 'You passed a callback function as the "type" argument to Alert.prompt(). React Native is ' +
104
+ 'assuming you want to use the deprecated Alert.prompt(title, defaultValue, buttons, callback) ' +
105
+ 'signature. The current signature is Alert.prompt(title, message, callbackOrButtons, type, defaultValue, ' +
106
+ 'keyboardType) and the old syntax will be removed in a future version.' ,
107
+ ) ;
108
+
109
+ const callback = type ;
110
+ RCTAlertManager . alertWithArgs (
111
+ {
112
+ title : title || '' ,
113
+ type : 'plain-text' ,
114
+ defaultValue : message ,
115
+ } ,
116
+ ( id , value ) => {
117
+ callback ( value ) ;
118
+ } ,
119
+ ) ;
120
+ return ;
121
+ }
122
+
123
+ let callbacks = [ ] ;
124
+ const buttons = [ ] ;
125
+ let cancelButtonKey ;
126
+ let destructiveButtonKey ;
127
+ if ( typeof callbackOrButtons === 'function ') {
128
+ callbacks = [ callbackOrButtons ] ;
129
+ } else if ( Array . isArray ( callbackOrButtons ) ) {
130
+ callbackOrButtons . forEach ( ( btn , index ) => {
131
+ callbacks [ index ] = btn . onPress ;
132
+ if ( btn . style === 'cancel' ) {
133
+ cancelButtonKey = String ( index ) ;
134
+ } else if ( btn . style === 'destructive' ) {
135
+ destructiveButtonKey = String ( index ) ;
136
+ }
137
+ if ( btn . text || index < ( callbackOrButtons || [ ] ) . length - 1 ) {
138
+ const btnDef = { } ;
139
+ btnDef [ index ] = btn . text || '' ;
140
+ buttons . push ( btnDef ) ;
141
+ }
142
+ } ) ;
143
+ }
144
+
145
+ RCTAlertManager . alertWithArgs (
146
+ {
147
+ title : title || '' ,
148
+ message : message || undefined ,
149
+ buttons,
150
+ type : type || undefined ,
151
+ defaultValue,
152
+ cancelButtonKey,
153
+ destructiveButtonKey,
154
+ keyboardType,
155
+ } ,
156
+ ( id , value ) => {
157
+ const cb = callbacks [ id ] ;
158
+ cb && cb ( value ) ;
159
+ } ,
160
+ ) ;
161
+ }
61
162
}
62
163
63
164
/**
0 commit comments