10
10
11
11
'use strict' ;
12
12
13
- const NativeModules = require ( '../BatchedBridge/NativeModules' ) ;
13
+ import NativeModules from '../BatchedBridge/NativeModules' ;
14
+ import Platform from '../Utilities/Platform' ;
15
+ import DialogManagerAndroid , {
16
+ type DialogOptions ,
17
+ } from '../NativeModules/specs/NativeDialogManagerAndroid' ;
18
+
14
19
const RCTAlertManager = NativeModules . AlertManager ;
15
- const Platform = require ( '../Utilities/Platform' ) ;
16
20
17
21
export type Buttons = Array < {
18
22
text ?: string ,
@@ -53,14 +57,19 @@ class Alert {
53
57
if ( Platform . OS === 'ios' ) {
54
58
Alert . prompt ( title , message , buttons , 'default' ) ;
55
59
} else if ( Platform . OS === 'android' ) {
56
- let config = {
60
+ if ( ! DialogManagerAndroid ) {
61
+ return ;
62
+ }
63
+ const constants = DialogManagerAndroid . getConstants ( ) ;
64
+
65
+ const config : DialogOptions = {
57
66
title : title || '' ,
58
67
message : message || '' ,
59
68
cancelable : false ,
60
69
} ;
61
70
62
- if ( options ) {
63
- config = { ... config , cancelable : options . cancelable } ;
71
+ if ( options && options . cancelable ) {
72
+ config . cancelable = options . cancelable ;
64
73
}
65
74
// At most three buttons (neutral, negative, positive). Ignore rest.
66
75
// The text 'OK' should be probably localized. iOS Alert does that in native.
@@ -70,38 +79,32 @@ class Alert {
70
79
const buttonPositive = validButtons . pop ( ) ;
71
80
const buttonNegative = validButtons . pop ( ) ;
72
81
const buttonNeutral = validButtons . pop ( ) ;
82
+
73
83
if ( buttonNeutral ) {
74
- config = { ... config , buttonNeutral : buttonNeutral . text || '' } ;
84
+ config . buttonNeutral = buttonNeutral . text || '' ;
75
85
}
76
86
if ( buttonNegative ) {
77
- config = { ... config , buttonNegative : buttonNegative . text || '' } ;
87
+ config . buttonNegative = buttonNegative . text || '' ;
78
88
}
79
89
if ( buttonPositive ) {
80
- config = { ... config , buttonPositive : buttonPositive . text || '' } ;
90
+ config . buttonPositive = buttonPositive . text || '' ;
81
91
}
82
- NativeModules . DialogManagerAndroid . showAlert (
83
- config ,
84
- errorMessage => console . warn ( errorMessage ) ,
85
- ( action , buttonKey ) => {
86
- if ( action === NativeModules . DialogManagerAndroid . buttonClicked ) {
87
- if (
88
- buttonKey === NativeModules . DialogManagerAndroid . buttonNeutral
89
- ) {
90
- buttonNeutral . onPress && buttonNeutral . onPress ( ) ;
91
- } else if (
92
- buttonKey === NativeModules . DialogManagerAndroid . buttonNegative
93
- ) {
94
- buttonNegative . onPress && buttonNegative . onPress ( ) ;
95
- } else if (
96
- buttonKey === NativeModules . DialogManagerAndroid . buttonPositive
97
- ) {
98
- buttonPositive . onPress && buttonPositive . onPress ( ) ;
99
- }
100
- } else if ( action === NativeModules . DialogManagerAndroid . dismissed ) {
101
- options && options . onDismiss && options . onDismiss ( ) ;
92
+
93
+ const onAction = ( action , buttonKey ) => {
94
+ if ( action === constants . buttonClicked ) {
95
+ if ( buttonKey === constants . buttonNeutral ) {
96
+ buttonNeutral . onPress && buttonNeutral . onPress ( ) ;
97
+ } else if ( buttonKey === constants . buttonNegative ) {
98
+ buttonNegative . onPress && buttonNegative . onPress ( ) ;
99
+ } else if ( buttonKey === constants . buttonPositive ) {
100
+ buttonPositive . onPress && buttonPositive . onPress ( ) ;
102
101
}
103
- } ,
104
- ) ;
102
+ } else if ( action === constants . dismissed ) {
103
+ options && options . onDismiss && options . onDismiss ( ) ;
104
+ }
105
+ } ;
106
+ const onError = errorMessage => console . warn ( errorMessage ) ;
107
+ DialogManagerAndroid . showAlert ( config , onError , onAction ) ;
105
108
}
106
109
}
107
110
0 commit comments