diff --git a/Libraries/Alert/Alert.js b/Libraries/Alert/Alert.js index 613d40c530f215..cfc0262bc55e03 100644 --- a/Libraries/Alert/Alert.js +++ b/Libraries/Alert/Alert.js @@ -10,7 +10,7 @@ 'use strict'; -import AlertMacOS from './AlertMacOS'; // TODO(macOS ISS#2323203) +import {type DefaultInputsArray} from './AlertMacOS'; // TODO(macOS ISS#2323203) import Platform from '../Utilities/Platform'; import NativeDialogManagerAndroid, { type DialogOptions, @@ -33,6 +33,10 @@ export type Buttons = Array<{ type Options = { cancelable?: ?boolean, onDismiss?: ?() => void, + // [TODO(macOS ISS#2323203) + modal?: ?boolean, + critical?: ?boolean, + // ]TODO(macOS ISS#2323203) ... }; @@ -48,11 +52,20 @@ class Alert { buttons?: Buttons, options?: Options, ): void { - if ( - Platform.OS === 'ios' || - Platform.OS === 'macos' /* TODO(macOS ISS#2323203) */ - ) { + if (Platform.OS === 'ios') { Alert.prompt(title, message, buttons, 'default'); + // [TODO(macOS ISS#2323203) + } else if (Platform.OS === 'macos') { + promptMacOS( + title, + message, + buttons, + 'default', + undefined, + options?.modal, + options?.critical, + ); + // ]TODO(macOS ISS#2323203) } else if (Platform.OS === 'android') { if (!NativeDialogManagerAndroid) { return; @@ -156,10 +169,53 @@ class Alert { // [TODO(macOS ISS#2323203) } else if (Platform.OS === 'macos') { const defaultInputs = [{default: defaultValue}]; - AlertMacOS.prompt(title, message, callbackOrButtons, type, defaultInputs); + promptMacOS(title, message, callbackOrButtons, type, defaultInputs); } // ]TODO(macOS ISS#2323203) } } +// [TODO(macOS ISS#2323203) +function promptMacOS( + title: ?string, + message?: ?string, + callbackOrButtons?: ?((text: string) => void) | Buttons, + type?: ?AlertType = 'plain-text', + defaultInputs?: DefaultInputsArray, + modal?: ?boolean, + critical?: ?boolean, +): void { + let callbacks = []; + const buttons = []; + if (typeof callbackOrButtons === 'function') { + callbacks = [callbackOrButtons]; + } else if (callbackOrButtons instanceof Array) { + callbackOrButtons.forEach((btn, index) => { + callbacks[index] = btn.onPress; + if (btn.text || index < (callbackOrButtons || []).length - 1) { + const btnDef = {}; + btnDef[index] = btn.text || ''; + buttons.push(btnDef); + } + }); + } + + RCTAlertManager.alertWithArgs( + { + title: title || undefined, + message: message || undefined, + buttons, + type: type || undefined, + defaultInputs, + modal: modal || undefined, + critical: critical || undefined, + }, + (id, value) => { + const cb = callbacks[id]; + cb && cb(value); + }, + ); +} +// ]TODO(macOS ISS#2323203) + module.exports = Alert; diff --git a/Libraries/Alert/AlertMacOS.js b/Libraries/Alert/AlertMacOS.js index f0bbf3c50357be..40642b4a2a4cfa 100644 --- a/Libraries/Alert/AlertMacOS.js +++ b/Libraries/Alert/AlertMacOS.js @@ -14,6 +14,7 @@ 'use strict'; import type {AlertType, AlertButtonStyle} from './Alert'; +import warnOnce from '../Utilities/warnOnce'; var RCTAlertManager = require('../BatchedBridge/NativeModules').AlertManager; @@ -176,6 +177,11 @@ class AlertMacOS { modal?: ?boolean, critical?: ?boolean, ): void { + warnOnce( + 'deprecated-AlertMacOS', + '"AlertMacOS" module has been deprecated in favor of "Alert" and will be removed in a future version of react-native-macos', + ); + var callbacks = []; var buttons = []; if (typeof callbackOrButtons === 'function') { diff --git a/Libraries/Alert/NativeAlertManager.js b/Libraries/Alert/NativeAlertManager.js index 452be85c5b112f..e9f5347b82b85b 100644 --- a/Libraries/Alert/NativeAlertManager.js +++ b/Libraries/Alert/NativeAlertManager.js @@ -12,6 +12,7 @@ import type {TurboModule} from '../TurboModule/RCTExport'; import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; +import type {DefaultInputsArray} from './AlertMacOS'; // TODO(macOS ISS#2323203) export type Args = {| title?: string, @@ -22,6 +23,11 @@ export type Args = {| cancelButtonKey?: string, destructiveButtonKey?: string, keyboardType?: string, + // [TODO(macOS ISS#2323203) + defaultInputs?: DefaultInputsArray, + modal?: ?boolean, + critical?: ?boolean, + // ]TODO(macOS ISS#2323203) |}; export interface Spec extends TurboModule {