Skip to content

Commit

Permalink
Deprecate AlertMacOS in favor of Alert
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Dec 14, 2020
1 parent a5723c1 commit 27ebe50
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
68 changes: 62 additions & 6 deletions Libraries/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
...
};

Expand All @@ -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;
Expand Down Expand Up @@ -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;
6 changes: 6 additions & 0 deletions Libraries/Alert/AlertMacOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'use strict';

import type {AlertType, AlertButtonStyle} from './Alert';
import warnOnce from '../Utilities/warnOnce';

var RCTAlertManager = require('../BatchedBridge/NativeModules').AlertManager;

Expand Down Expand Up @@ -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') {
Expand Down
6 changes: 6 additions & 0 deletions Libraries/Alert/NativeAlertManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 27ebe50

Please sign in to comment.