mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
b58e176af0
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/28058 I'm taking the first step towards supporting iOS 13 UIScene APIs and modernizing React Native not to assume an app only has a single window. See discussion here: https://github.com/facebook/react-native/issues/25181#issuecomment-505612941 The approach I'm taking is to take advantage of `RootTagContext` and passing it to NativeModules so that they can identify correctly which window they refer to. Here I'm just laying groundwork. - [x] `Alert` and `ActionSheetIOS` take an optional `rootTag` argument that will cause them to appear on the correct window - [x] `StatusBar` methods also have `rootTag` argument added, but it's not fully hooked up on the native side — this turns out to require some more work, see: https://github.com/facebook/react-native/issues/25181#issuecomment-506690818 - [x] `setNetworkActivityIndicatorVisible` is deprecated in iOS 13 - [x] `RCTPerfMonitor`, `RCTProfile` no longer assume `UIApplicationDelegate` has a `window` property (no longer the best practice) — they now just render on the key window Next steps: Add VC-based status bar management (if I get the OK on https://github.com/facebook/react-native/issues/25181#issuecomment-506690818 ), add multiple window demo to RNTester, deprecate Dimensions in favor of a layout context, consider adding hook-based APIs for native modules such as Alert that automatically know which rootTag to pass ## Changelog [Internal] [Changed] - Modernize Modal to use RootTagContext [iOS] [Changed] - `Alert`, `ActionSheetIOS`, `StatusBar` methods now take an optional `surface` argument (for future iPadOS 13 support) [iOS] [Changed] - RCTPresentedViewController now takes a nullable `window` arg [Internal] [Changed] - Do not assume `UIApplicationDelegate` has a `window` property Pull Request resolved: https://github.com/facebook/react-native/pull/25425 Test Plan: - Open RNTester and: - go to Modal and check if it still works - Alert → see if works - ACtionSheetIOS → see if it works - StatusBar → see if it works - Share → see if it works Reviewed By: PeteTheHeat Differential Revision: D16957751 Pulled By: hramos fbshipit-source-id: ae2a4478e2e7f8d2be3022c9c4861561ec244a26
167 lines
4.7 KiB
JavaScript
167 lines
4.7 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import Platform from '../Utilities/Platform';
|
|
import NativeDialogManagerAndroid, {
|
|
type DialogOptions,
|
|
} from '../NativeModules/specs/NativeDialogManagerAndroid';
|
|
import RCTAlertManager from './RCTAlertManager';
|
|
import ReactNative from '../Renderer/shims/ReactNative';
|
|
|
|
export type AlertType =
|
|
| 'default'
|
|
| 'plain-text'
|
|
| 'secure-text'
|
|
| 'login-password';
|
|
export type AlertButtonStyle = 'default' | 'cancel' | 'destructive';
|
|
export type Buttons = Array<{
|
|
text?: string,
|
|
onPress?: ?Function,
|
|
style?: AlertButtonStyle,
|
|
...
|
|
}>;
|
|
|
|
type Options = {
|
|
cancelable?: ?boolean,
|
|
onDismiss?: ?() => void,
|
|
surface?: mixed,
|
|
...
|
|
};
|
|
|
|
type PromptOptions = {
|
|
surface?: mixed,
|
|
};
|
|
|
|
/**
|
|
* Launches an alert dialog with the specified title and message.
|
|
*
|
|
* See https://reactnative.dev/docs/alert.html
|
|
*/
|
|
class Alert {
|
|
static alert(
|
|
title: ?string,
|
|
message?: ?string,
|
|
buttons?: Buttons,
|
|
options?: Options = {},
|
|
): void {
|
|
if (Platform.OS === 'ios') {
|
|
Alert.prompt(title, message, buttons, 'default', undefined, undefined, {
|
|
surface: options.surface,
|
|
});
|
|
} else if (Platform.OS === 'android') {
|
|
if (!NativeDialogManagerAndroid) {
|
|
return;
|
|
}
|
|
const constants = NativeDialogManagerAndroid.getConstants();
|
|
|
|
const config: DialogOptions = {
|
|
title: title || '',
|
|
message: message || '',
|
|
cancelable: false,
|
|
};
|
|
|
|
if (options.cancelable) {
|
|
config.cancelable = options.cancelable;
|
|
}
|
|
// At most three buttons (neutral, negative, positive). Ignore rest.
|
|
// The text 'OK' should be probably localized. iOS Alert does that in native.
|
|
const defaultPositiveText = 'OK';
|
|
const validButtons: Buttons = buttons
|
|
? buttons.slice(0, 3)
|
|
: [{text: defaultPositiveText}];
|
|
const buttonPositive = validButtons.pop();
|
|
const buttonNegative = validButtons.pop();
|
|
const buttonNeutral = validButtons.pop();
|
|
|
|
if (buttonNeutral) {
|
|
config.buttonNeutral = buttonNeutral.text || '';
|
|
}
|
|
if (buttonNegative) {
|
|
config.buttonNegative = buttonNegative.text || '';
|
|
}
|
|
if (buttonPositive) {
|
|
config.buttonPositive = buttonPositive.text || defaultPositiveText;
|
|
}
|
|
|
|
const onAction = (action, buttonKey) => {
|
|
if (action === constants.buttonClicked) {
|
|
if (buttonKey === constants.buttonNeutral) {
|
|
buttonNeutral.onPress && buttonNeutral.onPress();
|
|
} else if (buttonKey === constants.buttonNegative) {
|
|
buttonNegative.onPress && buttonNegative.onPress();
|
|
} else if (buttonKey === constants.buttonPositive) {
|
|
buttonPositive.onPress && buttonPositive.onPress();
|
|
}
|
|
} else if (action === constants.dismissed) {
|
|
options.onDismiss && options.onDismiss();
|
|
}
|
|
};
|
|
const onError = errorMessage => console.warn(errorMessage);
|
|
NativeDialogManagerAndroid.showAlert(config, onError, onAction);
|
|
}
|
|
}
|
|
|
|
static prompt(
|
|
title: ?string,
|
|
message?: ?string,
|
|
callbackOrButtons?: ?(((text: string) => void) | Buttons),
|
|
type?: ?AlertType = 'plain-text',
|
|
defaultValue?: string,
|
|
keyboardType?: string,
|
|
options?: PromptOptions = {surface: undefined},
|
|
): void {
|
|
if (Platform.OS === 'ios') {
|
|
let callbacks = [];
|
|
const buttons = [];
|
|
let cancelButtonKey;
|
|
let destructiveButtonKey;
|
|
if (typeof callbackOrButtons === 'function') {
|
|
callbacks = [callbackOrButtons];
|
|
} else if (Array.isArray(callbackOrButtons)) {
|
|
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) {
|
|
const btnDef = {};
|
|
btnDef[index] = btn.text || '';
|
|
buttons.push(btnDef);
|
|
}
|
|
});
|
|
}
|
|
|
|
RCTAlertManager.alertWithArgs(
|
|
{
|
|
title: title || '',
|
|
message: message || undefined,
|
|
buttons,
|
|
type: type || undefined,
|
|
defaultValue,
|
|
cancelButtonKey,
|
|
destructiveButtonKey,
|
|
keyboardType,
|
|
reactTag: ReactNative.findNodeHandle(options.surface) ?? -1,
|
|
},
|
|
(id, value) => {
|
|
const cb = callbacks[id];
|
|
cb && cb(value);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Alert;
|