From 122cc8ba8ab7c5bbcc042f5221f5c0c53ec99c19 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Mon, 27 May 2019 22:09:41 -0700 Subject: [PATCH] Add spec for AlertManager (#24906) Summary: Part of #24875 ## Changelog [General] [Added] - Add TurboModule spec for AlertManager Pull Request resolved: https://github.com/facebook/react-native/pull/24906 Reviewed By: lunaleaps Differential Revision: D15471065 Pulled By: fkgozali fbshipit-source-id: bb22e6454b1f748987f3a8cd957bfd4e027493a5 --- Libraries/Alert/Alert.js | 40 ++++---------------- Libraries/Alert/NativeAlertManager.js | 51 ++++++++++++++++++++++++++ Libraries/Alert/RCTAlertManager.ios.js | 15 +++++++- 3 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 Libraries/Alert/NativeAlertManager.js diff --git a/Libraries/Alert/Alert.js b/Libraries/Alert/Alert.js index 125a488e0b2..63f5b31d4fe 100644 --- a/Libraries/Alert/Alert.js +++ b/Libraries/Alert/Alert.js @@ -10,37 +10,12 @@ 'use strict'; -import NativeModules from '../BatchedBridge/NativeModules'; import Platform from '../Utilities/Platform'; -import DialogManagerAndroid, { +import NativeDialogManagerAndroid, { 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. @@ -57,10 +32,10 @@ class Alert { if (Platform.OS === 'ios') { Alert.prompt(title, message, buttons, 'default'); } else if (Platform.OS === 'android') { - if (!DialogManagerAndroid) { + if (!NativeDialogManagerAndroid) { return; } - const constants = DialogManagerAndroid.getConstants(); + const constants = NativeDialogManagerAndroid.getConstants(); const config: DialogOptions = { title: title || '', @@ -105,7 +80,7 @@ class Alert { } }; const onError = errorMessage => console.warn(errorMessage); - DialogManagerAndroid.showAlert(config, onError, onAction); + NativeDialogManagerAndroid.showAlert(config, onError, onAction); } } @@ -131,7 +106,7 @@ class Alert { { title: title || '', type: 'plain-text', - defaultValue: message, + defaultValue: message || '', }, (id, value) => { callback(value); @@ -175,6 +150,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 00000000000..0a540cac727 --- /dev/null +++ b/Libraries/Alert/NativeAlertManager.js @@ -0,0 +1,51 @@ +/** + * 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. + * + * @flow + * @format + */ + +'use strict'; + +import type {TurboModule} from 'RCTExport'; +import * as TurboModuleRegistry from 'TurboModuleRegistry'; + +export type Buttons = Array<{ + text?: string, + onPress?: ?Function, + 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; + +export 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 TurboModuleRegistry.get('AlertManager'); diff --git a/Libraries/Alert/RCTAlertManager.ios.js b/Libraries/Alert/RCTAlertManager.ios.js index 24696735898..8ededddf4b4 100644 --- a/Libraries/Alert/RCTAlertManager.ios.js +++ b/Libraries/Alert/RCTAlertManager.ios.js @@ -10,6 +10,17 @@ 'use strict'; -const RCTAlertManager = require('../BatchedBridge/NativeModules').AlertManager; +import NativeAlertManager from './NativeAlertManager'; +import type {Args} from './NativeAlertManager'; -module.exports = RCTAlertManager; +module.exports = { + alertWithArgs( + args: Args, + callback: (id: number, value: string) => void, + ): void { + if (NativeAlertManager == null) { + return; + } + NativeAlertManager.alertWithArgs(args, callback); + }, +};