Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move macOS JS module overrides to their own files #1961

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = {
'^ReactNativeInternalFeatureFlags$':
'<rootDir>/packages/react-native/jest/ReactNativeInternalFeatureFlagsMock.js',
'^react-native(.*)': '<rootDir>/packages/react-native$1', // [macOS]
'@react-native/virtualized-lists': '@react-native-mac/virtualized-lists', // [macOS]
},
moduleFileExtensions: ['fb.js'].concat(defaults.moduleFileExtensions),
unmockedModulePathPatterns: [
Expand Down
118 changes: 0 additions & 118 deletions packages/react-native/Libraries/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,11 @@ export type Buttons = Array<{
style?: AlertButtonStyle,
...
}>;
// [macOS
export type DefaultInputsArray = Array<{
default?: string,
placeholder?: string,
style?: AlertButtonStyle,
}>;
// macOS]

type Options = {
cancelable?: ?boolean,
userInterfaceStyle?: 'unspecified' | 'light' | 'dark',
onDismiss?: ?() => void,
// [macOS
modal?: ?boolean,
critical?: ?boolean,
// macOS]
...
};

Expand All @@ -67,18 +56,6 @@ class Alert {
undefined,
options,
);
// [macOS
} else if (Platform.OS === 'macos') {
Alert.promptMacOS(
title,
message,
buttons,
'default',
undefined,
options?.modal,
options?.critical,
);
// macOS]
} else if (Platform.OS === 'android') {
const NativeDialogManagerAndroid =
require('../NativeModules/specs/NativeDialogManagerAndroid').default;
Expand Down Expand Up @@ -190,103 +167,8 @@ class Alert {
cb && cb(value);
},
);
// [macOS
} else if (Platform.OS === 'macos') {
const defaultInputs = [{default: defaultValue}];
Alert.promptMacOS(title, message, callbackOrButtons, type, defaultInputs);
}
// macOS]
}

// [macOS
/**
* Create and display a prompt to enter some text.
* @static
* @method promptMacOS
* @param title The dialog's title.
* @param message An optional message that appears above the text
* input.
* @param callbackOrButtons This optional argument should
* be either a single-argument function or an array of buttons. If passed
* a function, it will be called with the prompt's value when the user
* taps 'OK'.
*
* If passed an array of button configurations, each button should include
* a `text` key, as well as optional `onPress` key (see
* example).
* @param type This configures the text input. One of 'plain-text',
* 'secure-text' or 'login-password'.
* @param defaultInputs This optional argument should be an array of couple
* default value - placeholder for the input fields.
* @param modal The alert can be optionally run as an app-modal dialog, instead
* of the default presentation as a sheet.
* @param critical This optional argument should be used when it's needed to
* warn the user about severe consequences of an impending event
* (such as deleting a file).
*
* @example <caption>Example with custom buttons</caption>
*
* AlertMacOS.promptMacOS(
* 'Enter password',
* 'Enter your password to claim your $1.5B in lottery winnings',
* [
* {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
* {text: 'OK', onPress: password => console.log('OK Pressed, password: ' + password)},
* ],
* 'secure-text'
* );
*
* @example <caption>Example with the default button and a custom callback</caption>
*
* AlertMacOS.prompt(
* 'Update username',
* null,
* text => console.log("Your username is "+text),
* null,
* 'default'
* );
*/
static promptMacOS(
title: ?string,
message?: ?string,
callbackOrButtons?: ?((text: string) => void) | Buttons,
type?: ?AlertType = 'plain-text',
defaultInputs?: DefaultInputsArray,
modal?: ?boolean,
critical?: ?boolean,
): void {
let callbacks: Array<?any> = [];
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: {[number]: string} = {};
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);
},
);
}
// macOS]
}

module.exports = Alert;