2015-12-17 19:09:22 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule Alert
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var AlertIOS = require('AlertIOS');
|
|
|
|
var Platform = require('Platform');
|
|
|
|
var DialogModuleAndroid = require('NativeModules').DialogManagerAndroid;
|
|
|
|
|
|
|
|
import type { AlertType, AlertButtonStyle } from 'AlertIOS';
|
|
|
|
|
|
|
|
type Buttons = Array<{
|
2016-08-09 13:32:41 +00:00
|
|
|
text?: string,
|
|
|
|
onPress?: ?Function,
|
|
|
|
style?: AlertButtonStyle,
|
2015-12-17 19:09:22 +00:00
|
|
|
}>;
|
|
|
|
|
2016-08-09 13:08:44 +00:00
|
|
|
type Options = {
|
|
|
|
cancelable?: ?boolean;
|
|
|
|
};
|
|
|
|
|
2015-12-17 19:09:22 +00:00
|
|
|
/**
|
|
|
|
* Launches an alert dialog with the specified title and message.
|
|
|
|
*
|
|
|
|
* Optionally provide a list of buttons. Tapping any button will fire the
|
|
|
|
* respective onPress callback and dismiss the alert. By default, the only
|
|
|
|
* button will be an 'OK' button.
|
|
|
|
*
|
2015-12-18 14:10:09 +00:00
|
|
|
* This is an API that works both on iOS and Android and can show static
|
|
|
|
* alerts. To show an alert that prompts the user to enter some information,
|
|
|
|
* see `AlertIOS`; entering text in an alert is common on iOS only.
|
2015-12-17 19:09:22 +00:00
|
|
|
*
|
|
|
|
* ## iOS
|
|
|
|
*
|
|
|
|
* On iOS you can specify any number of buttons. Each button can optionally
|
Simplified AlertIOS
Summary:
Ok, so this started as fixing #5273 but ended up getting a little more complicated. :smile:
Currently, AlertIOS has the following API:
* `alert(title, message, buttons, type)`
* `prompt(title, defaultValue, buttons, callback)`
I've changed the API to look like the following:
* `alert(title, message, callbackOrButtons)`
* `prompt(title, message, callbackOrButtons, type, defaultValue)`
I know that breaking changes are a big deal, but I find the current alert API to be fairly inconsistent and unnecessarily confusing. I'll try to justify my changes one by one:
1. Currently `type` is an optional parameter of `alert`. However, the only reason to change the alert type from the default is in order to create one of the input dialogs (text, password or username/password). So we're in a weird state where if you want a normal text input, you use `prompt`, but if you want a password input you use `alert` with the 'secure-text' type. I've moved `type` to `prompt` so all text input is now done with `pro
Closes https://github.com/facebook/react-native/pull/5286
Reviewed By: svcscm
Differential Revision: D2850400
Pulled By: androidtrunkagent
fb-gh-sync-id: 2986cfa2266225df7e4dcd703fce1e322c12b816
2016-01-21 18:48:58 +00:00
|
|
|
* specify a style, which is one of 'default', 'cancel' or 'destructive'.
|
2015-12-17 19:09:22 +00:00
|
|
|
*
|
|
|
|
* ## Android
|
Simplified AlertIOS
Summary:
Ok, so this started as fixing #5273 but ended up getting a little more complicated. :smile:
Currently, AlertIOS has the following API:
* `alert(title, message, buttons, type)`
* `prompt(title, defaultValue, buttons, callback)`
I've changed the API to look like the following:
* `alert(title, message, callbackOrButtons)`
* `prompt(title, message, callbackOrButtons, type, defaultValue)`
I know that breaking changes are a big deal, but I find the current alert API to be fairly inconsistent and unnecessarily confusing. I'll try to justify my changes one by one:
1. Currently `type` is an optional parameter of `alert`. However, the only reason to change the alert type from the default is in order to create one of the input dialogs (text, password or username/password). So we're in a weird state where if you want a normal text input, you use `prompt`, but if you want a password input you use `alert` with the 'secure-text' type. I've moved `type` to `prompt` so all text input is now done with `pro
Closes https://github.com/facebook/react-native/pull/5286
Reviewed By: svcscm
Differential Revision: D2850400
Pulled By: androidtrunkagent
fb-gh-sync-id: 2986cfa2266225df7e4dcd703fce1e322c12b816
2016-01-21 18:48:58 +00:00
|
|
|
*
|
2015-12-17 19:09:22 +00:00
|
|
|
* On Android at most three buttons can be specified. Android has a concept
|
2015-12-18 14:10:09 +00:00
|
|
|
* of a neutral, negative and a positive button:
|
2015-12-17 19:09:22 +00:00
|
|
|
*
|
|
|
|
* - If you specify one button, it will be the 'positive' one (such as 'OK')
|
|
|
|
* - Two buttons mean 'negative', 'positive' (such as 'Cancel', 'OK')
|
|
|
|
* - Three buttons mean 'neutral', 'negative', 'positive' (such as 'Later', 'Cancel', 'OK')
|
|
|
|
*
|
|
|
|
* ```
|
2015-12-18 14:10:09 +00:00
|
|
|
* // Works on both iOS and Android
|
2015-12-17 19:09:22 +00:00
|
|
|
* Alert.alert(
|
|
|
|
* 'Alert Title',
|
|
|
|
* 'My Alert Msg',
|
|
|
|
* [
|
|
|
|
* {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
|
2015-12-18 14:10:09 +00:00
|
|
|
* {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
|
2015-12-17 19:09:22 +00:00
|
|
|
* {text: 'OK', onPress: () => console.log('OK Pressed')},
|
|
|
|
* ]
|
|
|
|
* )
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
class Alert {
|
|
|
|
|
|
|
|
static alert(
|
|
|
|
title: ?string,
|
|
|
|
message?: ?string,
|
|
|
|
buttons?: Buttons,
|
2016-08-09 13:08:44 +00:00
|
|
|
options?: Options,
|
Simplified AlertIOS
Summary:
Ok, so this started as fixing #5273 but ended up getting a little more complicated. :smile:
Currently, AlertIOS has the following API:
* `alert(title, message, buttons, type)`
* `prompt(title, defaultValue, buttons, callback)`
I've changed the API to look like the following:
* `alert(title, message, callbackOrButtons)`
* `prompt(title, message, callbackOrButtons, type, defaultValue)`
I know that breaking changes are a big deal, but I find the current alert API to be fairly inconsistent and unnecessarily confusing. I'll try to justify my changes one by one:
1. Currently `type` is an optional parameter of `alert`. However, the only reason to change the alert type from the default is in order to create one of the input dialogs (text, password or username/password). So we're in a weird state where if you want a normal text input, you use `prompt`, but if you want a password input you use `alert` with the 'secure-text' type. I've moved `type` to `prompt` so all text input is now done with `pro
Closes https://github.com/facebook/react-native/pull/5286
Reviewed By: svcscm
Differential Revision: D2850400
Pulled By: androidtrunkagent
fb-gh-sync-id: 2986cfa2266225df7e4dcd703fce1e322c12b816
2016-01-21 18:48:58 +00:00
|
|
|
type?: AlertType,
|
2015-12-17 19:09:22 +00:00
|
|
|
): void {
|
|
|
|
if (Platform.OS === 'ios') {
|
Simplified AlertIOS
Summary:
Ok, so this started as fixing #5273 but ended up getting a little more complicated. :smile:
Currently, AlertIOS has the following API:
* `alert(title, message, buttons, type)`
* `prompt(title, defaultValue, buttons, callback)`
I've changed the API to look like the following:
* `alert(title, message, callbackOrButtons)`
* `prompt(title, message, callbackOrButtons, type, defaultValue)`
I know that breaking changes are a big deal, but I find the current alert API to be fairly inconsistent and unnecessarily confusing. I'll try to justify my changes one by one:
1. Currently `type` is an optional parameter of `alert`. However, the only reason to change the alert type from the default is in order to create one of the input dialogs (text, password or username/password). So we're in a weird state where if you want a normal text input, you use `prompt`, but if you want a password input you use `alert` with the 'secure-text' type. I've moved `type` to `prompt` so all text input is now done with `pro
Closes https://github.com/facebook/react-native/pull/5286
Reviewed By: svcscm
Differential Revision: D2850400
Pulled By: androidtrunkagent
fb-gh-sync-id: 2986cfa2266225df7e4dcd703fce1e322c12b816
2016-01-21 18:48:58 +00:00
|
|
|
if (typeof type !== 'undefined') {
|
|
|
|
console.warn('Alert.alert() with a 4th "type" parameter is deprecated and will be removed. Use AlertIOS.prompt() instead.');
|
|
|
|
AlertIOS.alert(title, message, buttons, type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AlertIOS.alert(title, message, buttons);
|
2015-12-17 19:09:22 +00:00
|
|
|
} else if (Platform.OS === 'android') {
|
2016-08-09 13:08:44 +00:00
|
|
|
AlertAndroid.alert(title, message, buttons, options);
|
2015-12-17 19:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around the Android native module.
|
|
|
|
*/
|
|
|
|
class AlertAndroid {
|
|
|
|
|
|
|
|
static alert(
|
|
|
|
title: ?string,
|
|
|
|
message?: ?string,
|
|
|
|
buttons?: Buttons,
|
2016-08-09 13:08:44 +00:00
|
|
|
options?: Options,
|
2015-12-17 19:09:22 +00:00
|
|
|
): void {
|
|
|
|
var config = {
|
|
|
|
title: title || '',
|
|
|
|
message: message || '',
|
|
|
|
};
|
2016-08-09 13:08:44 +00:00
|
|
|
|
|
|
|
if (options) {
|
|
|
|
config = {...config, cancelable: options.cancelable};
|
|
|
|
}
|
2015-12-17 19:09:22 +00:00
|
|
|
// At most three buttons (neutral, negative, positive). Ignore rest.
|
|
|
|
// The text 'OK' should be probably localized. iOS Alert does that in native.
|
|
|
|
var validButtons: Buttons = buttons ? buttons.slice(0, 3) : [{text: 'OK'}];
|
|
|
|
var buttonPositive = validButtons.pop();
|
|
|
|
var buttonNegative = validButtons.pop();
|
|
|
|
var buttonNeutral = validButtons.pop();
|
|
|
|
if (buttonNeutral) {
|
2016-08-09 13:32:41 +00:00
|
|
|
config = {...config, buttonNeutral: buttonNeutral.text || '' };
|
2015-12-17 19:09:22 +00:00
|
|
|
}
|
|
|
|
if (buttonNegative) {
|
2016-08-09 13:32:41 +00:00
|
|
|
config = {...config, buttonNegative: buttonNegative.text || '' };
|
2015-12-17 19:09:22 +00:00
|
|
|
}
|
|
|
|
if (buttonPositive) {
|
2016-08-09 13:32:41 +00:00
|
|
|
config = {...config, buttonPositive: buttonPositive.text || '' };
|
2015-12-17 19:09:22 +00:00
|
|
|
}
|
|
|
|
DialogModuleAndroid.showAlert(
|
|
|
|
config,
|
2016-07-19 14:17:08 +00:00
|
|
|
(errorMessage) => console.warn(errorMessage),
|
2015-12-17 19:09:22 +00:00
|
|
|
(action, buttonKey) => {
|
|
|
|
if (action !== DialogModuleAndroid.buttonClicked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (buttonKey === DialogModuleAndroid.buttonNeutral) {
|
|
|
|
buttonNeutral.onPress && buttonNeutral.onPress();
|
|
|
|
} else if (buttonKey === DialogModuleAndroid.buttonNegative) {
|
|
|
|
buttonNegative.onPress && buttonNegative.onPress();
|
|
|
|
} else if (buttonKey === DialogModuleAndroid.buttonPositive) {
|
|
|
|
buttonPositive.onPress && buttonPositive.onPress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Alert;
|