Skip to content

Commit e2bd7db

Browse files
wellmongefacebook-github-bot
authored andcommitted
Merge AlertIOS with Alert (#23318)
Summary: Itwas merged AlertIOS into Alert and removed type parameter from Alert.alert line 60 at Alert.js [AlertIOS] [Change and Replace] - Merge AlertIOS into Alert. Pull Request resolved: #23318 Reviewed By: mjesun Differential Revision: D14031421 Pulled By: cpojer fbshipit-source-id: 98db173adeb65aa90d309f8a583993bc0cddb6e1
1 parent 4ac65f5 commit e2bd7db

File tree

6 files changed

+144
-226
lines changed

6 files changed

+144
-226
lines changed

Libraries/Alert/Alert.js

+117-16
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
'use strict';
1212

13-
const AlertIOS = require('AlertIOS');
1413
const NativeModules = require('NativeModules');
14+
const RCTAlertManager = NativeModules.AlertManager;
1515
const Platform = require('Platform');
1616

17-
import type {AlertType, AlertButtonStyle} from 'AlertIOS';
18-
1917
export type Buttons = Array<{
2018
text?: string,
2119
onPress?: ?Function,
@@ -27,37 +25,140 @@ type Options = {
2725
onDismiss?: ?Function,
2826
};
2927

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+
3041
/**
3142
* Launches an alert dialog with the specified title and message.
3243
*
3344
* See http://facebook.github.io/react-native/docs/alert.html
3445
*/
3546
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-
*/
4147
static alert(
4248
title: ?string,
4349
message?: ?string,
4450
buttons?: Buttons,
4551
options?: Options,
46-
type?: AlertType,
4752
): void {
4853
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-
}
5654
AlertIOS.alert(title, message, buttons);
5755
} else if (Platform.OS === 'android') {
5856
AlertAndroid.alert(title, message, buttons, options);
5957
}
6058
}
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+
}
61162
}
62163

63164
/**

Libraries/Alert/AlertIOS.js

-181
This file was deleted.

Libraries/react-native/react-native-implementation.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ module.exports = {
198198
return require('Alert');
199199
},
200200
get AlertIOS() {
201-
return require('AlertIOS');
201+
warnOnce(
202+
'alert-ios',
203+
'AlertIOS is deprecated. Use the `Alert` module directly instead.',
204+
);
205+
return require('Alert');
202206
},
203207
get Animated() {
204208
return require('Animated');

0 commit comments

Comments
 (0)