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-05-11 23:40:04 +00:00
|
|
|
var invariant = require('invariant');
|
2015-03-14 00:48:31 +00:00
|
|
|
|
|
|
|
var DEFAULT_BUTTON_TEXT = 'OK';
|
|
|
|
var DEFAULT_BUTTON = {
|
|
|
|
text: DEFAULT_BUTTON_TEXT,
|
|
|
|
onPress: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-04-04 17:14:29 +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
|
|
|
|
*
|
|
|
|
* The last button in the list will be considered the 'Primary' button and
|
|
|
|
* it will appear bold.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* AlertIOS.alert(
|
|
|
|
* 'Foo Title',
|
|
|
|
* 'My Alert Msg',
|
|
|
|
* [
|
|
|
|
* {text: 'Foo', onPress: () => console.log('Foo Pressed!')},
|
|
|
|
* {text: 'Bar', onPress: () => console.log('Bar Pressed!')},
|
|
|
|
* ]
|
2015-04-21 23:09:56 +00:00
|
|
|
* )
|
2015-04-04 17:14:29 +00:00
|
|
|
* ```
|
2015-03-14 00:48:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class AlertIOS {
|
|
|
|
static alert(
|
|
|
|
title: ?string,
|
2015-03-23 18:36:57 +00:00
|
|
|
message?: ?string,
|
2015-03-23 07:46:11 +00:00
|
|
|
buttons?: Array<{
|
2015-03-14 00:48:31 +00:00
|
|
|
text: ?string;
|
2015-05-11 23:40:04 +00:00
|
|
|
onPress?: ?Function;
|
|
|
|
}>,
|
|
|
|
type?: ?string
|
2015-03-14 00:48:31 +00:00
|
|
|
): void {
|
|
|
|
var callbacks = [];
|
|
|
|
var buttonsSpec = [];
|
|
|
|
title = title || '';
|
|
|
|
message = message || '';
|
|
|
|
buttons = buttons || [DEFAULT_BUTTON];
|
2015-05-11 23:40:04 +00:00
|
|
|
type = type || '';
|
|
|
|
|
2015-03-14 00:48:31 +00:00
|
|
|
buttons.forEach((btn, index) => {
|
|
|
|
callbacks[index] = btn.onPress;
|
|
|
|
var btnDef = {};
|
|
|
|
btnDef[index] = btn.text || DEFAULT_BUTTON_TEXT;
|
|
|
|
buttonsSpec.push(btnDef);
|
|
|
|
});
|
|
|
|
RCTAlertManager.alertWithArgs({
|
|
|
|
title,
|
|
|
|
message,
|
|
|
|
buttons: buttonsSpec,
|
2015-05-11 23:40:04 +00:00
|
|
|
type,
|
|
|
|
}, (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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:40:04 +00:00
|
|
|
static prompt(
|
|
|
|
title: string,
|
|
|
|
value?: string,
|
|
|
|
buttons?: Array<{
|
|
|
|
text: ?string;
|
|
|
|
onPress?: ?Function;
|
|
|
|
}>,
|
|
|
|
callback?: ?Function
|
|
|
|
): void {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
buttons = value;
|
|
|
|
value = undefined;
|
|
|
|
} else if (typeof value === 'function') {
|
|
|
|
callback = value;
|
|
|
|
value = undefined;
|
|
|
|
}
|
|
|
|
} else if (arguments.length === 3 && typeof buttons === 'function') {
|
|
|
|
callback = buttons;
|
|
|
|
buttons = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
invariant(
|
|
|
|
!(callback && buttons) && (callback || buttons),
|
|
|
|
'Must provide either a button list or a callback, but not both'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!buttons) {
|
|
|
|
buttons = [{
|
|
|
|
text: 'Cancel',
|
|
|
|
}, {
|
|
|
|
text: 'OK',
|
|
|
|
onPress: callback
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
this.alert(title, value, buttons, 'plain-text');
|
|
|
|
}
|
2015-03-14 00:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AlertIOS;
|