From a5723c16e5eba3363faf98f37482cd4b72bb5045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 2 Dec 2020 21:08:23 +0100 Subject: [PATCH 1/5] address feedback; fix iOS build --- React/Base/RCTUIKit.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/React/Base/RCTUIKit.h b/React/Base/RCTUIKit.h index ef25f3dd273967..0354432c95a853 100644 --- a/React/Base/RCTUIKit.h +++ b/React/Base/RCTUIKit.h @@ -172,6 +172,9 @@ UIKIT_STATIC_INLINE CGFloat UIFontLineHeight(UIFont *font) // UIInterface.h/NSUserInterfaceLayout.h #define UIUserInterfaceLayoutDirection NSUserInterfaceLayoutDirection +// UIAlertController.h/NSViewController.h +#define UIAlertController NSViewController + // // semantically equivalent enums // From 27ebe500f8c7f651154c51926030c4c8578c269c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 3 Dec 2020 00:06:14 +0100 Subject: [PATCH 2/5] Deprecate AlertMacOS in favor of Alert --- Libraries/Alert/Alert.js | 68 ++++++++++++++++++++++++--- Libraries/Alert/AlertMacOS.js | 6 +++ Libraries/Alert/NativeAlertManager.js | 6 +++ 3 files changed, 74 insertions(+), 6 deletions(-) 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 { From 3d87b36c97509bd6ed6e2461b97f7c7ebcfdf19d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 3 Dec 2020 13:23:50 +0100 Subject: [PATCH 3/5] move UIAlertController define to RCTAlertController.h --- React/Base/RCTUIKit.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/React/Base/RCTUIKit.h b/React/Base/RCTUIKit.h index 0354432c95a853..ef25f3dd273967 100644 --- a/React/Base/RCTUIKit.h +++ b/React/Base/RCTUIKit.h @@ -172,9 +172,6 @@ UIKIT_STATIC_INLINE CGFloat UIFontLineHeight(UIFont *font) // UIInterface.h/NSUserInterfaceLayout.h #define UIUserInterfaceLayoutDirection NSUserInterfaceLayoutDirection -// UIAlertController.h/NSViewController.h -#define UIAlertController NSViewController - // // semantically equivalent enums // From 1cb846edd25d31f462267df3c1d481f390d5fdca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 3 Dec 2020 15:16:58 +0100 Subject: [PATCH 4/5] define only on macos target --- RNTester/Podfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RNTester/Podfile.lock b/RNTester/Podfile.lock index 5cea5f4e0e0cc5..55b74c6d84afee 100644 --- a/RNTester/Podfile.lock +++ b/RNTester/Podfile.lock @@ -530,7 +530,7 @@ SPEC CHECKSUMS: FlipperKit: ab353d41aea8aae2ea6daaf813e67496642f3d7d glog: 1cb7c408c781ae8f35bbababe459b45e3dee4ec1 hermes: 12d049af0d8e8379c5b3b54ffb1919d670045bdc - libevent: ee9265726a1fc599dea382964fa304378affaa5f + libevent: d721abced7738e428c291489465953c9263d78f9 OpenSSL-Universal: 8b48cc0d10c1b2923617dfe5c178aa9ed2689355 RCT-Folly: 1347093ffe75e152d846f7e45a3ef901b60021aa RCTRequired: 5b6e3555bf3e4de31f5f9eec6765cb1e1c3f8588 @@ -563,4 +563,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 18ca7d3b0e7db79041574a8bb6200b9e1c2d5359 -COCOAPODS: 1.9.1 \ No newline at end of file +COCOAPODS: 1.9.1 From 8973d60e2aa50f70e73933eb9ec083dc67d63dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 14 Dec 2020 11:29:49 +0100 Subject: [PATCH 5/5] revert podfile.lock --- RNTester/Podfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RNTester/Podfile.lock b/RNTester/Podfile.lock index 55b74c6d84afee..5cea5f4e0e0cc5 100644 --- a/RNTester/Podfile.lock +++ b/RNTester/Podfile.lock @@ -530,7 +530,7 @@ SPEC CHECKSUMS: FlipperKit: ab353d41aea8aae2ea6daaf813e67496642f3d7d glog: 1cb7c408c781ae8f35bbababe459b45e3dee4ec1 hermes: 12d049af0d8e8379c5b3b54ffb1919d670045bdc - libevent: d721abced7738e428c291489465953c9263d78f9 + libevent: ee9265726a1fc599dea382964fa304378affaa5f OpenSSL-Universal: 8b48cc0d10c1b2923617dfe5c178aa9ed2689355 RCT-Folly: 1347093ffe75e152d846f7e45a3ef901b60021aa RCTRequired: 5b6e3555bf3e4de31f5f9eec6765cb1e1c3f8588 @@ -563,4 +563,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 18ca7d3b0e7db79041574a8bb6200b9e1c2d5359 -COCOAPODS: 1.9.1 +COCOAPODS: 1.9.1 \ No newline at end of file