diff --git a/Libraries/Alert/Alert.js b/Libraries/Alert/Alert.js index 1e03d1d136df3a..3eef5b2c1556e0 100644 --- a/Libraries/Alert/Alert.js +++ b/Libraries/Alert/Alert.js @@ -15,32 +15,8 @@ import Platform from '../Utilities/Platform'; import DialogManagerAndroid, { type DialogOptions, } from '../NativeModules/specs/NativeDialogManagerAndroid'; - -const RCTAlertManager = NativeModules.AlertManager; - -export type Buttons = Array<{ - text?: string, - onPress?: ?Function, - style?: AlertButtonStyle, -}>; - -type Options = { - cancelable?: ?boolean, - onDismiss?: ?Function, -}; - -type AlertType = $Keys<{ - default: string, - 'plain-text': string, - 'secure-text': string, - 'login-password': string, -}>; - -export type AlertButtonStyle = $Keys<{ - default: string, - cancel: string, - destructive: string, -}>; +import RCTAlertManager from './RCTAlertManager'; +import {type Buttons, type Options, type AlertType} from './NativeAlertManager'; /** * Launches an alert dialog with the specified title and message. @@ -126,11 +102,14 @@ class Alert { ); const callback = type; + if (!RCTAlertManager) { + return; + } RCTAlertManager.alertWithArgs( { title: title || '', type: 'plain-text', - defaultValue: message, + defaultValue: message || '', }, (id, value) => { callback(value); @@ -161,6 +140,9 @@ class Alert { }); } + if (!RCTAlertManager) { + return; + } RCTAlertManager.alertWithArgs( { title: title || '', @@ -174,6 +156,7 @@ class Alert { }, (id, value) => { const cb = callbacks[id]; + // $FlowFixMe cb && cb(value); }, ); diff --git a/Libraries/Alert/NativeAlertManager.js b/Libraries/Alert/NativeAlertManager.js new file mode 100644 index 00000000000000..dc22fe5412de96 --- /dev/null +++ b/Libraries/Alert/NativeAlertManager.js @@ -0,0 +1,51 @@ +/** + * Copyright 2004-present Facebook. All Rights Reserved. + * + * @flow strict-local + * @format + */ + +'use strict'; + +import type {TurboModule} from 'RCTExport'; +import * as TurboModuleRegistry from 'TurboModuleRegistry'; +import Platform from '../Utilities/Platform'; + +export type Buttons = Array<{ + text?: string, + onPress?: ?() => void, + style?: AlertButtonStyle, +}>; + +export type Options = { + cancelable?: ?boolean, + onDismiss?: ?() => void, +}; + +/* 'default' | plain-text' | 'secure-text' | 'login-password' */ +export type AlertType = string; + +/* 'default' | 'cancel' | 'destructive' */ +export type AlertButtonStyle = string; + +type Args = {| + title: string, + message?: string, + buttons?: Buttons, + type?: string, + defaultValue?: string, + cancelButtonKey?: string, + destructiveButtonKey?: string, + keyboardType?: string, +|}; + +export interface Spec extends TurboModule { + +alertWithArgs: ( + args: Args, + callback: (id: number, value: string) => void, + ) => void; +} + +export default (Platform.OS === 'ios' + ? TurboModuleRegistry.getEnforcing('AlertManager') + : undefined); diff --git a/Libraries/Alert/RCTAlertManager.ios.js b/Libraries/Alert/RCTAlertManager.ios.js index 246967358982b9..53bda2a5984764 100644 --- a/Libraries/Alert/RCTAlertManager.ios.js +++ b/Libraries/Alert/RCTAlertManager.ios.js @@ -10,6 +10,6 @@ 'use strict'; -const RCTAlertManager = require('../BatchedBridge/NativeModules').AlertManager; +import RCTAlertManager from './NativeAlertManager'; module.exports = RCTAlertManager;