2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-23 23:28:42 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-23 23:28:42 +03:00
|
|
|
*/
|
2015-02-20 07:10:52 +03:00
|
|
|
|
|
|
|
#import "RCTAlertManager.h"
|
|
|
|
|
2019-10-15 19:12:56 +03:00
|
|
|
#import <FBReactNativeSpec/FBReactNativeSpec.h>
|
|
|
|
#import <RCTTypeSafety/RCTConvertHelpers.h>
|
|
|
|
#import <React/RCTAssert.h>
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
|
|
|
|
#import "CoreModulesPlugins.h"
|
2015-02-20 07:10:52 +03:00
|
|
|
|
2015-12-01 05:44:06 +03:00
|
|
|
@implementation RCTConvert (UIAlertViewStyle)
|
|
|
|
|
2016-09-27 16:19:45 +03:00
|
|
|
RCT_ENUM_CONVERTER(RCTAlertViewStyle, (@{
|
|
|
|
@"default": @(RCTAlertViewStyleDefault),
|
|
|
|
@"secure-text": @(RCTAlertViewStyleSecureTextInput),
|
|
|
|
@"plain-text": @(RCTAlertViewStylePlainTextInput),
|
|
|
|
@"login-password": @(RCTAlertViewStyleLoginAndPasswordInput),
|
|
|
|
}), RCTAlertViewStyleDefault, integerValue)
|
2015-12-01 05:44:06 +03:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2019-10-15 19:12:56 +03:00
|
|
|
@interface RCTAlertManager() <NativeAlertManagerSpec>
|
2015-02-20 07:10:52 +03:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTAlertManager
|
|
|
|
{
|
2016-10-26 03:17:19 +03:00
|
|
|
NSHashTable *_alertControllers;
|
2015-02-20 07:10:52 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:42:43 +03:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-04-20 22:06:02 +03:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2015-09-18 18:40:49 +03:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
2015-10-30 21:23:47 +03:00
|
|
|
for (UIAlertController *alertController in _alertControllers) {
|
|
|
|
[alertController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
2015-09-18 18:40:49 +03:00
|
|
|
}
|
|
|
|
|
2015-02-20 07:10:52 +03:00
|
|
|
/**
|
|
|
|
* @param {NSDictionary} args Dictionary of the form
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
* @"message": @"<Alert message>",
|
|
|
|
* @"buttons": @[
|
|
|
|
* @{@"<key1>": @"<title1>"},
|
2015-12-01 05:44:06 +03:00
|
|
|
* @{@"<key2>": @"<title2>"},
|
|
|
|
* ],
|
|
|
|
* @"cancelButtonKey": @"<key2>",
|
2015-02-20 07:10:52 +03:00
|
|
|
* }
|
|
|
|
* The key from the `buttons` dictionary is passed back in the callback on click.
|
2015-12-01 05:44:06 +03:00
|
|
|
* Buttons are displayed in the order they are specified.
|
2015-02-20 07:10:52 +03:00
|
|
|
*/
|
2019-10-15 19:12:56 +03:00
|
|
|
RCT_EXPORT_METHOD(alertWithArgs:(JS::NativeAlertManager::Args &)args
|
2015-04-08 18:52:48 +03:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-02-20 07:10:52 +03:00
|
|
|
{
|
2019-10-15 19:12:56 +03:00
|
|
|
NSString *title = [RCTConvert NSString:args.title()];
|
|
|
|
NSString *message = [RCTConvert NSString:args.message()];
|
|
|
|
RCTAlertViewStyle type = [RCTConvert RCTAlertViewStyle:args.type()];
|
|
|
|
NSArray<NSDictionary *> *buttons = [RCTConvert NSDictionaryArray:RCTConvertOptionalVecToArray(args.buttons(), ^id(id<NSObject> element) { return element; })];
|
|
|
|
NSString *defaultValue = [RCTConvert NSString:args.defaultValue()];
|
|
|
|
NSString *cancelButtonKey = [RCTConvert NSString:args.cancelButtonKey()];
|
|
|
|
NSString *destructiveButtonKey = [RCTConvert NSString:args.destructiveButtonKey()];
|
|
|
|
UIKeyboardType keyboardType = [RCTConvert UIKeyboardType:args.keyboardType()];
|
2015-02-20 07:10:52 +03:00
|
|
|
|
|
|
|
if (!title && !message) {
|
2015-03-02 02:33:55 +03:00
|
|
|
RCTLogError(@"Must specify either an alert title, or message, or both");
|
2015-02-20 07:10:52 +03:00
|
|
|
return;
|
2015-12-01 05:44:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buttons.count == 0) {
|
2016-09-27 16:19:45 +03:00
|
|
|
if (type == RCTAlertViewStyleDefault) {
|
2015-12-01 05:44:06 +03:00
|
|
|
buttons = @[@{@"0": RCTUIKitLocalizedString(@"OK")}];
|
|
|
|
cancelButtonKey = @"0";
|
|
|
|
} else {
|
|
|
|
buttons = @[
|
|
|
|
@{@"0": RCTUIKitLocalizedString(@"OK")},
|
|
|
|
@{@"1": RCTUIKitLocalizedString(@"Cancel")},
|
|
|
|
];
|
|
|
|
cancelButtonKey = @"1";
|
|
|
|
}
|
2015-02-20 07:10:52 +03:00
|
|
|
}
|
2015-10-30 21:23:47 +03:00
|
|
|
|
2020-03-06 02:55:10 +03:00
|
|
|
UIViewController *presentingController = RCTPresentedViewController();
|
|
|
|
if (presentingController == nil) {
|
|
|
|
RCTLogError(@"Tried to display alert view but there is no application window. args: %@", @{
|
|
|
|
@"title": args.title() ?: [NSNull null],
|
|
|
|
@"message": args.message() ?: [NSNull null],
|
|
|
|
@"buttons": RCTConvertOptionalVecToArray(args.buttons(), ^id(id<NSObject> element) { return element; }) ?: [NSNull null],
|
|
|
|
@"type": args.type() ?: [NSNull null],
|
|
|
|
@"defaultValue": args.defaultValue() ?: [NSNull null],
|
|
|
|
@"cancelButtonKey": args.cancelButtonKey() ?: [NSNull null],
|
|
|
|
@"destructiveButtonKey": args.destructiveButtonKey() ?: [NSNull null],
|
|
|
|
@"keyboardType": args.keyboardType() ?: [NSNull null],
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 05:37:54 +03:00
|
|
|
UIAlertController *alertController = [UIAlertController
|
|
|
|
alertControllerWithTitle:title
|
|
|
|
message:nil
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
switch (type) {
|
2016-09-27 16:19:45 +03:00
|
|
|
case RCTAlertViewStylePlainTextInput: {
|
2016-09-02 05:37:54 +03:00
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.secureTextEntry = NO;
|
|
|
|
textField.text = defaultValue;
|
2017-01-20 01:29:39 +03:00
|
|
|
textField.keyboardType = keyboardType;
|
2016-09-02 05:37:54 +03:00
|
|
|
}];
|
|
|
|
break;
|
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 21:48:58 +03:00
|
|
|
}
|
2016-09-27 16:19:45 +03:00
|
|
|
case RCTAlertViewStyleSecureTextInput: {
|
2016-09-02 05:37:54 +03:00
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.placeholder = RCTUIKitLocalizedString(@"Password");
|
|
|
|
textField.secureTextEntry = YES;
|
|
|
|
textField.text = defaultValue;
|
2017-01-20 01:29:39 +03:00
|
|
|
textField.keyboardType = keyboardType;
|
2016-09-02 05:37:54 +03:00
|
|
|
}];
|
|
|
|
break;
|
2015-10-30 21:23:47 +03:00
|
|
|
}
|
2016-09-27 16:19:45 +03:00
|
|
|
case RCTAlertViewStyleLoginAndPasswordInput: {
|
2016-09-02 05:37:54 +03:00
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.placeholder = RCTUIKitLocalizedString(@"Login");
|
|
|
|
textField.text = defaultValue;
|
2017-01-20 01:29:39 +03:00
|
|
|
textField.keyboardType = keyboardType;
|
2016-09-02 05:37:54 +03:00
|
|
|
}];
|
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.placeholder = RCTUIKitLocalizedString(@"Password");
|
|
|
|
textField.secureTextEntry = YES;
|
|
|
|
}];
|
|
|
|
break;
|
2015-11-25 14:09:00 +03:00
|
|
|
}
|
2016-09-27 16:19:45 +03:00
|
|
|
case RCTAlertViewStyleDefault:
|
2016-09-02 05:37:54 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-11-04 01:45:46 +03:00
|
|
|
|
2016-09-02 05:37:54 +03:00
|
|
|
alertController.message = message;
|
2015-11-04 01:45:46 +03:00
|
|
|
|
2016-09-02 05:37:54 +03:00
|
|
|
for (NSDictionary<NSString *, id> *button in buttons) {
|
|
|
|
if (button.count != 1) {
|
|
|
|
RCTLogError(@"Button definitions should have exactly one key.");
|
2015-11-16 21:20:34 +03:00
|
|
|
}
|
2016-09-02 05:37:54 +03:00
|
|
|
NSString *buttonKey = button.allKeys.firstObject;
|
|
|
|
NSString *buttonTitle = [RCTConvert NSString:button[buttonKey]];
|
|
|
|
UIAlertActionStyle buttonStyle = UIAlertActionStyleDefault;
|
|
|
|
if ([buttonKey isEqualToString:cancelButtonKey]) {
|
|
|
|
buttonStyle = UIAlertActionStyleCancel;
|
|
|
|
} else if ([buttonKey isEqualToString:destructiveButtonKey]) {
|
|
|
|
buttonStyle = UIAlertActionStyleDestructive;
|
2015-04-18 20:43:20 +03:00
|
|
|
}
|
2016-10-26 03:17:19 +03:00
|
|
|
__weak UIAlertController *weakAlertController = alertController;
|
2016-09-02 05:37:54 +03:00
|
|
|
[alertController addAction:[UIAlertAction actionWithTitle:buttonTitle
|
|
|
|
style:buttonStyle
|
|
|
|
handler:^(__unused UIAlertAction *action) {
|
|
|
|
switch (type) {
|
2016-09-27 16:19:45 +03:00
|
|
|
case RCTAlertViewStylePlainTextInput:
|
|
|
|
case RCTAlertViewStyleSecureTextInput:
|
2016-10-26 03:17:19 +03:00
|
|
|
callback(@[buttonKey, [weakAlertController.textFields.firstObject text]]);
|
2016-09-02 05:37:54 +03:00
|
|
|
break;
|
2016-09-27 16:19:45 +03:00
|
|
|
case RCTAlertViewStyleLoginAndPasswordInput: {
|
2016-09-02 05:37:54 +03:00
|
|
|
NSDictionary<NSString *, NSString *> *loginCredentials = @{
|
2016-10-26 03:17:19 +03:00
|
|
|
@"login": [weakAlertController.textFields.firstObject text],
|
|
|
|
@"password": [weakAlertController.textFields.lastObject text]
|
2016-09-02 05:37:54 +03:00
|
|
|
};
|
|
|
|
callback(@[buttonKey, loginCredentials]);
|
|
|
|
break;
|
2015-10-30 21:23:47 +03:00
|
|
|
}
|
2016-09-27 16:19:45 +03:00
|
|
|
case RCTAlertViewStyleDefault:
|
2016-09-02 05:37:54 +03:00
|
|
|
callback(@[buttonKey]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}]];
|
|
|
|
}
|
2015-11-25 14:09:00 +03:00
|
|
|
|
2016-09-02 05:37:54 +03:00
|
|
|
if (!_alertControllers) {
|
2016-10-26 03:17:19 +03:00
|
|
|
_alertControllers = [NSHashTable weakObjectsHashTable];
|
2015-10-30 21:23:47 +03:00
|
|
|
}
|
2016-09-02 05:37:54 +03:00
|
|
|
[_alertControllers addObject:alertController];
|
|
|
|
|
Fixes alert view block first responder (#23240)
Summary:
Fixes #23076 , the reason is `blur()` is managed by `UIManager`, `UIManager` maintains all operations and execute them each `batchDidComplete`, which means every time `JS` finish callback native , but `Alert` module would call directly, this mess up the order of method call, for example like below, even `this.$input.blur()` is called before `Alert.alert()`, but in native side, `Alert.alert()` is called before `this.$input.blur()`.
```
<TextInput style={{ borderWidth: 1 }} ref={$input => this.$input = $input} />
<Button title="Show Alert" onPress={() => {
// // `blur` works if using without `Alert`
this.$input && this.$input.blur()
// // `blur` is not working
Alert.alert('show alert', 'desc', [
{ text: 'cancel', style: 'cancel' },
{ text: 'show', onPress: () => {
}},
])
}} />
```
[iOS] [Fixed] - Fixes alert view block first responder
After fix, example like below, `blur` can works.
```
import * as React from 'react';
import { TextInput, View, Alert, Button } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center' }}>
<TextInput style={{ borderWidth: 1 }} ref={$input => this.$input = $input} />
<Button title="Show Alert" onPress={() => {
this.$input && this.$input.blur()
Alert.alert('show alert', 'desc', [
{ text: 'cancel', style: 'cancel' },
{ text: 'show', onPress: () => {
}},
])
}} />
</View>
);
}
}
```
Pull Request resolved: https://github.com/facebook/react-native/pull/23240
Differential Revision: D13915920
Pulled By: cpojer
fbshipit-source-id: fe1916fcb5913e2b8128d045a6364c5e3d39c516
2019-02-01 14:48:02 +03:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[presentingController presentViewController:alertController animated:YES completion:nil];
|
|
|
|
});
|
2015-02-20 07:10:52 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 19:12:56 +03:00
|
|
|
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
|
|
|
|
{
|
|
|
|
return std::make_shared<facebook::react::NativeAlertManagerSpecJSI>(self, jsInvoker);
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:10:52 +03:00
|
|
|
@end
|
2019-10-15 19:12:56 +03:00
|
|
|
|
|
|
|
Class RCTAlertManagerCls(void) {
|
|
|
|
return RCTAlertManager.class;
|
|
|
|
}
|