2015-03-14 00:48:31 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +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.
|
2015-03-14 00:48:31 +00:00
|
|
|
*
|
|
|
|
* @providesModule AlertIOS
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-18 05:22:03 +00:00
|
|
|
var RCTAlertManager = require('NativeModules').AlertManager;
|
2015-03-14 00:48:31 +00:00
|
|
|
|
2015-12-17 19:09:22 +00:00
|
|
|
export type AlertType = $Enum<{
|
2015-12-01 02:44:06 +00:00
|
|
|
'default': string;
|
|
|
|
'plain-text': string;
|
|
|
|
'secure-text': string;
|
|
|
|
'login-password': string;
|
|
|
|
}>;
|
|
|
|
|
2015-12-17 19:09:22 +00:00
|
|
|
export type AlertButtonStyle = $Enum<{
|
2015-12-01 02:44:06 +00:00
|
|
|
'default': string;
|
|
|
|
'cancel': string;
|
|
|
|
'destructive': string;
|
|
|
|
}>;
|
2015-03-14 00:48:31 +00:00
|
|
|
|
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 ButtonsArray = Array<{
|
|
|
|
text?: string;
|
|
|
|
onPress?: ?Function;
|
|
|
|
style?: AlertButtonStyle;
|
|
|
|
}>;
|
|
|
|
|
2015-03-14 00:48:31 +00:00
|
|
|
/**
|
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
|
|
|
* The AlertsIOS utility provides two functions: `alert` and `prompt`. All
|
|
|
|
* functionality available through `AlertIOS.alert` is also available in the
|
|
|
|
* cross-platform `Alert.alert`, which we recommend you use if you don't need
|
|
|
|
* iOS-specific functionality.
|
2015-04-04 17:14:29 +00:00
|
|
|
*
|
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
|
|
|
* `AlertIOS.prompt` allows you to prompt the user for input inside of an
|
|
|
|
* alert popup.
|
2015-12-18 14:10:09 +00:00
|
|
|
*
|
2015-03-14 00:48:31 +00:00
|
|
|
*/
|
|
|
|
class AlertIOS {
|
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
|
|
|
/**
|
|
|
|
* Creates a popup to alert the user. See
|
2016-02-11 14:16:34 +00:00
|
|
|
* [Alert](docs/alert.html).
|
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
|
|
|
*
|
|
|
|
* - title: string -- The dialog's title.
|
|
|
|
* - message: string -- An optional message that appears above the text input.
|
|
|
|
* - callbackOrButtons -- This optional argument should be either a
|
|
|
|
* single-argument function or an array of buttons. If passed a function,
|
|
|
|
* it will be called when the user taps 'OK'.
|
|
|
|
*
|
|
|
|
* If passed an array of button configurations, each button should include
|
|
|
|
* a `text` key, as well as optional `onPress` and `style` keys.
|
|
|
|
* `style` should be one of 'default', 'cancel' or 'destructive'.
|
|
|
|
* - type -- *deprecated, do not use*
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* AlertIOS.alert(
|
|
|
|
* 'Sync Complete',
|
|
|
|
* 'All your data are belong to us.'
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*/
|
2015-03-14 00:48:31 +00:00
|
|
|
static alert(
|
|
|
|
title: ?string,
|
2015-03-23 18:36:57 +00:00
|
|
|
message?: ?string,
|
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
|
|
|
callbackOrButtons?: ?(() => void) | ButtonsArray,
|
|
|
|
type?: AlertType,
|
2015-03-14 00:48:31 +00:00
|
|
|
): void {
|
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('AlertIOS.alert() with a 4th "type" parameter is deprecated and will be removed. Use AlertIOS.prompt() instead.');
|
|
|
|
this.prompt(title, message, callbackOrButtons, type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.prompt(title, message, callbackOrButtons, 'default');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt the user to enter some text.
|
|
|
|
*
|
|
|
|
* - title: string -- The dialog's title.
|
|
|
|
* - message: string -- An optional message that appears above the text input.
|
|
|
|
* - callbackOrButtons -- This optional argument should be either a
|
|
|
|
* single-argument function or an array of buttons. If passed a function,
|
|
|
|
* it will be called with the prompt's value when the user taps 'OK'.
|
|
|
|
*
|
|
|
|
* If passed an array of button configurations, each button should include
|
|
|
|
* a `text` key, as well as optional `onPress` and `style` keys (see example).
|
|
|
|
* `style` should be one of 'default', 'cancel' or 'destructive'.
|
|
|
|
* - type: string -- This configures the text input. One of 'plain-text',
|
|
|
|
* 'secure-text' or 'login-password'.
|
|
|
|
* - defaultValue: string -- the default value for the text field.
|
|
|
|
*
|
|
|
|
* Example with custom buttons:
|
|
|
|
* ```
|
|
|
|
* AlertIOS.prompt(
|
|
|
|
* 'Enter password',
|
|
|
|
* 'Enter your password to claim your $1.5B in lottery winnings',
|
|
|
|
* [
|
|
|
|
* {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
|
|
|
|
* {text: 'OK', onPress: password => console.log('OK Pressed, password: ' + password)},
|
|
|
|
* ],
|
|
|
|
* 'secure-text'
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Example with the default button and a custom callback:
|
|
|
|
* ```
|
|
|
|
* AlertIOS.prompt(
|
|
|
|
* 'Update username',
|
|
|
|
* null,
|
|
|
|
* text => console.log("Your username is "+text),
|
|
|
|
* null,
|
|
|
|
* 'default'
|
|
|
|
* )
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
static prompt(
|
|
|
|
title: ?string,
|
|
|
|
message?: ?string,
|
|
|
|
callbackOrButtons?: ?((text: string) => void) | ButtonsArray,
|
|
|
|
type?: ?AlertType = 'plain-text',
|
|
|
|
defaultValue?: string,
|
|
|
|
): void {
|
|
|
|
if (typeof type === 'function') {
|
|
|
|
console.warn(
|
|
|
|
'You passed a callback function as the "type" argument to AlertIOS.prompt(). React Native is ' +
|
|
|
|
'assuming you want to use the deprecated AlertIOS.prompt(title, defaultValue, buttons, callback) ' +
|
|
|
|
'signature. The current signature is AlertIOS.prompt(title, message, callbackOrButtons, type, defaultValue) ' +
|
|
|
|
'and the old syntax will be removed in a future version.');
|
|
|
|
|
|
|
|
var callback = type;
|
|
|
|
var defaultValue = message;
|
|
|
|
RCTAlertManager.alertWithArgs({
|
|
|
|
title: title || undefined,
|
|
|
|
type: 'plain-text',
|
|
|
|
defaultValue,
|
|
|
|
}, (id, value) => {
|
|
|
|
callback(value);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:48:31 +00:00
|
|
|
var callbacks = [];
|
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
|
|
|
var buttons = [];
|
2015-12-01 02:44:06 +00:00
|
|
|
var cancelButtonKey;
|
|
|
|
var destructiveButtonKey;
|
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 callbackOrButtons === 'function') {
|
|
|
|
callbacks = [callbackOrButtons];
|
|
|
|
}
|
|
|
|
else if (callbackOrButtons instanceof Array) {
|
|
|
|
callbackOrButtons.forEach((btn, index) => {
|
|
|
|
callbacks[index] = btn.onPress;
|
|
|
|
if (btn.style === 'cancel') {
|
|
|
|
cancelButtonKey = String(index);
|
|
|
|
} else if (btn.style === 'destructive') {
|
|
|
|
destructiveButtonKey = String(index);
|
|
|
|
}
|
|
|
|
if (btn.text || index < (callbackOrButtons || []).length - 1) {
|
|
|
|
var btnDef = {};
|
|
|
|
btnDef[index] = btn.text || '';
|
|
|
|
buttons.push(btnDef);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:48:31 +00:00
|
|
|
RCTAlertManager.alertWithArgs({
|
2015-12-01 02:44:06 +00:00
|
|
|
title: title || undefined,
|
|
|
|
message: message || undefined,
|
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
|
|
|
buttons,
|
2015-12-01 02:44:06 +00:00
|
|
|
type: type || undefined,
|
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
|
|
|
defaultValue,
|
2015-12-01 02:44:06 +00:00
|
|
|
cancelButtonKey,
|
|
|
|
destructiveButtonKey,
|
2015-05-11 23:40:04 +00:00
|
|
|
}, (id, value) => {
|
2015-03-14 00:48:31 +00:00
|
|
|
var cb = callbacks[id];
|
2015-05-11 23:40:04 +00:00
|
|
|
cb && cb(value);
|
2015-03-14 00:48:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AlertIOS;
|