11
11
*/
12
12
'use strict' ;
13
13
14
- var AlertIOS = require ( 'AlertIOS' ) ;
15
- var Platform = require ( 'Platform' ) ;
16
- var DialogModuleAndroid = require ( 'NativeModules' ) . DialogManagerAndroid ;
17
- var DialogModuleWindows = require ( 'NativeModules' ) . DialogManagerWindows ;
14
+ const AlertIOS = require ( 'AlertIOS' ) ;
15
+ const NativeModules = require ( 'NativeModules' ) ;
16
+ const Platform = require ( 'Platform' ) ;
18
17
19
18
import type { AlertType , AlertButtonStyle } from 'AlertIOS' ;
20
19
21
20
type Buttons = Array < {
22
- text ?: string ;
23
- onPress ?: ?Function ;
24
- style ?: AlertButtonStyle ;
21
+ text ?: string ,
22
+ onPress ?: ?Function ,
23
+ style ?: AlertButtonStyle ,
25
24
} > ;
26
25
26
+ type Options = {
27
+ cancelable ?: ?boolean ,
28
+ onDismiss ?: ?Function ,
29
+ } ;
30
+
27
31
/**
28
32
* Launches an alert dialog with the specified title and message.
29
33
*
@@ -49,6 +53,15 @@ type Buttons = Array<{
49
53
* - Two buttons mean 'negative', 'positive' (such as 'Cancel', 'OK')
50
54
* - Three buttons mean 'neutral', 'negative', 'positive' (such as 'Later', 'Cancel', 'OK')
51
55
*
56
+ * By default alerts on Android can be dismissed by tapping outside of the alert
57
+ * box. This event can be handled by providing an optional `options` parameter,
58
+ * with an `onDismiss` callback property `{ onDismiss: () => {} }`.
59
+ *
60
+ * Alternatively, the dismissing behavior can be disabled altogether by providing
61
+ * an optional `options` parameter with the `cancelable` property set to `false`
62
+ * i.e. `{ cancelable: false }`
63
+ *
64
+ * Example usage:
52
65
* ```
53
66
* // Works on both iOS and Android
54
67
* Alert.alert(
@@ -58,7 +71,8 @@ type Buttons = Array<{
58
71
* {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
59
72
* {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
60
73
* {text: 'OK', onPress: () => console.log('OK Pressed')},
61
- * ]
74
+ * ],
75
+ * { cancelable: false }
62
76
* )
63
77
* ```
64
78
* ## Windows
@@ -86,17 +100,18 @@ class Alert {
86
100
title : ?string ,
87
101
message ?: ?string ,
88
102
buttons ?: Buttons ,
103
+ options ?: Options ,
89
104
type ?: AlertType ,
90
105
) : void {
91
106
if ( Platform . OS === 'ios' ) {
92
107
if ( typeof type !== 'undefined' ) {
93
- console . warn ( 'Alert.alert() with a 4th "type" parameter is deprecated and will be removed. Use AlertIOS.prompt() instead.' ) ;
108
+ console . warn ( 'Alert.alert() with a 5th "type" parameter is deprecated and will be removed. Use AlertIOS.prompt() instead.' ) ;
94
109
AlertIOS . alert ( title , message , buttons , type ) ;
95
110
return ;
96
111
}
97
112
AlertIOS . alert ( title , message , buttons ) ;
98
113
} else if ( Platform . OS === 'android' ) {
99
- AlertAndroid . alert ( title , message , buttons ) ;
114
+ AlertAndroid . alert ( title , message , buttons , options ) ;
100
115
} else if ( Platform . OS === 'windows' ) {
101
116
AlertWindows . alert ( title , message , buttons ) ;
102
117
}
@@ -112,39 +127,45 @@ class AlertAndroid {
112
127
title : ?string ,
113
128
message ?: ?string ,
114
129
buttons ?: Buttons ,
130
+ options ?: Options ,
115
131
) : void {
116
132
var config = {
117
133
title : title || '' ,
118
134
message : message || '' ,
119
135
} ;
136
+
137
+ if ( options ) {
138
+ config = { ...config , cancelable : options . cancelable } ;
139
+ }
120
140
// At most three buttons (neutral, negative, positive). Ignore rest.
121
141
// The text 'OK' should be probably localized. iOS Alert does that in native.
122
142
var validButtons : Buttons = buttons ? buttons . slice ( 0 , 3 ) : [ { text : 'OK' } ] ;
123
143
var buttonPositive = validButtons . pop ( ) ;
124
144
var buttonNegative = validButtons . pop ( ) ;
125
145
var buttonNeutral = validButtons . pop ( ) ;
126
146
if ( buttonNeutral ) {
127
- config = { ...config , buttonNeutral : buttonNeutral . text || '' }
147
+ config = { ...config , buttonNeutral : buttonNeutral . text || '' } ;
128
148
}
129
149
if ( buttonNegative ) {
130
- config = { ...config , buttonNegative : buttonNegative . text || '' }
150
+ config = { ...config , buttonNegative : buttonNegative . text || '' } ;
131
151
}
132
152
if ( buttonPositive ) {
133
- config = { ...config , buttonPositive : buttonPositive . text || '' }
153
+ config = { ...config , buttonPositive : buttonPositive . text || '' } ;
134
154
}
135
- DialogModuleAndroid . showAlert (
155
+ NativeModules . DialogManagerAndroid . showAlert (
136
156
config ,
137
- ( errorMessage ) => console . warn ( message ) ,
157
+ ( errorMessage ) => console . warn ( errorMessage ) ,
138
158
( action , buttonKey ) => {
139
- if ( action !== DialogModuleAndroid . buttonClicked ) {
140
- return ;
141
- }
142
- if ( buttonKey === DialogModuleAndroid . buttonNeutral ) {
143
- buttonNeutral . onPress && buttonNeutral . onPress ( ) ;
144
- } else if ( buttonKey === DialogModuleAndroid . buttonNegative ) {
145
- buttonNegative . onPress && buttonNegative . onPress ( ) ;
146
- } else if ( buttonKey === DialogModuleAndroid . buttonPositive ) {
147
- buttonPositive . onPress && buttonPositive . onPress ( ) ;
159
+ if ( action === NativeModules . DialogManagerAndroid . buttonClicked ) {
160
+ if ( buttonKey === NativeModules . DialogManagerAndroid . buttonNeutral ) {
161
+ buttonNeutral . onPress && buttonNeutral . onPress ( ) ;
162
+ } else if ( buttonKey === NativeModules . DialogManagerAndroid . buttonNegative ) {
163
+ buttonNegative . onPress && buttonNegative . onPress ( ) ;
164
+ } else if ( buttonKey === NativeModules . DialogManagerAndroid . buttonPositive ) {
165
+ buttonPositive . onPress && buttonPositive . onPress ( ) ;
166
+ }
167
+ } else if ( action === NativeModules . DialogManagerAndroid . dismissed ) {
168
+ options && options . onDismiss && options . onDismiss ( ) ;
148
169
}
149
170
}
150
171
) ;
@@ -176,16 +197,16 @@ class AlertWindows {
176
197
if ( buttonPositive ) {
177
198
config = { ...config , buttonPositive : buttonPositive . text || '' }
178
199
}
179
- DialogModuleWindows . showAlert (
200
+ NativeModules . DialogManagerWindows . showAlert (
180
201
config ,
181
202
( errorMessage ) => console . warn ( message ) ,
182
203
( action , buttonKey ) => {
183
- if ( action !== DialogModuleWindows . buttonClicked ) {
204
+ if ( action !== NativeModules . DialogManagerWindows . buttonClicked ) {
184
205
return ;
185
206
}
186
- if ( buttonKey === DialogModuleWindows . buttonNegative ) {
207
+ if ( buttonKey === NativeModules . DialogManagerWindows . buttonNegative ) {
187
208
buttonNegative . onPress && buttonNegative . onPress ( ) ;
188
- } else if ( buttonKey === DialogModuleWindows . buttonPositive ) {
209
+ } else if ( buttonKey === NativeModules . DialogManagerWindows . buttonPositive ) {
189
210
buttonPositive . onPress && buttonPositive . onPress ( ) ;
190
211
}
191
212
}
0 commit comments