Skip to content

Commit

Permalink
Update types exposed by Alert and the module structure (#49157)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [Internal]

Differential Revision: D69044715
  • Loading branch information
j-piasecki authored and facebook-github-bot committed Feb 5, 2025
1 parent c482241 commit 17a3d1a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 130 deletions.
62 changes: 0 additions & 62 deletions packages/react-native/Libraries/Alert/Alert.flow.js

This file was deleted.

56 changes: 51 additions & 5 deletions packages/react-native/Libraries/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,61 @@
*/

import type {DialogOptions} from '../NativeModules/specs/NativeDialogManagerAndroid';
import type {AlertOptions, AlertType, Buttons} from './Alert.flow';

import Platform from '../Utilities/Platform';
import RCTAlertManager from './RCTAlertManager';

export type * from './Alert.flow';
/**
* @platform ios
*/
export type AlertType =
| 'default'
| 'plain-text'
| 'secure-text'
| 'login-password';

/**
* @platform ios
*/
export type AlertButtonStyle = 'default' | 'cancel' | 'destructive';

export type AlertButton = {
text?: string,
onPress?: ?((value?: string) => any) | ?Function,
isPreferred?: boolean,
style?: AlertButtonStyle,
...
};

export type AlertButtons = Array<AlertButton>;

export type AlertOptions = {
/** @platform android */
cancelable?: ?boolean,
userInterfaceStyle?: 'unspecified' | 'light' | 'dark',
/** @platform android */
onDismiss?: ?() => void,
...
};

/**
* Launches an alert dialog with the specified title and message.
*
* Optionally provide a list of buttons. Tapping any button will fire the
* respective onPress callback and dismiss the alert. By default, the only
* button will be an 'OK' button.
*
* This is an API that works both on iOS and Android and can show static
* alerts. On iOS, you can show an alert that prompts the user to enter
* some information.
*
* See https://reactnative.dev/docs/alert
*/
class Alert {
static alert(
title: ?string,
message?: ?string,
buttons?: Buttons,
buttons?: AlertButtons,
options?: AlertOptions,
): void {
if (Platform.OS === 'ios') {
Expand Down Expand Up @@ -53,7 +96,7 @@ class Alert {
// At most three buttons (neutral, negative, positive). Ignore rest.
// The text 'OK' should be probably localized. iOS Alert does that in native.
const defaultPositiveText = 'OK';
const validButtons: Buttons = buttons
const validButtons: AlertButtons = buttons
? buttons.slice(0, 3)
: [{text: defaultPositiveText}];
const buttonPositive = validButtons.pop();
Expand Down Expand Up @@ -93,10 +136,13 @@ class Alert {
}
}

/**
* @platform ios
*/
static prompt(
title: ?string,
message?: ?string,
callbackOrButtons?: ?(((text: string) => void) | Buttons),
callbackOrButtons?: ?(((text: string) => void) | AlertButtons),
type?: ?AlertType = 'plain-text',
defaultValue?: string,
keyboardType?: string,
Expand Down
20 changes: 0 additions & 20 deletions packages/react-native/Libraries/Alert/RCTAlertManager.flow.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ declare export default typeof NativeActionSheetManager;
"
`;

exports[`public API should not change unintentionally Libraries/Alert/Alert.flow.js 1`] = `
exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1`] = `
"export type AlertType =
| \\"default\\"
| \\"plain-text\\"
Expand All @@ -67,7 +67,7 @@ export type AlertButton = {
style?: AlertButtonStyle,
...
};
export type Buttons = Array<AlertButton>;
export type AlertButtons = Array<AlertButton>;
export type AlertOptions = {
cancelable?: ?boolean,
userInterfaceStyle?: \\"unspecified\\" | \\"light\\" | \\"dark\\",
Expand All @@ -78,36 +78,13 @@ declare class Alert {
static alert(
title: ?string,
message?: ?string,
buttons?: Buttons,
buttons?: AlertButtons,
options?: AlertOptions
): void;
static prompt(
title: ?string,
message?: ?string,
callbackOrButtons?: ?(((text: string) => void) | Buttons),
type?: ?AlertType,
defaultValue?: string,
keyboardType?: string,
options?: AlertOptions
): void;
}
declare export default typeof Alert;
"
`;

exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1`] = `
"export type * from \\"./Alert.flow\\";
declare class Alert {
static alert(
title: ?string,
message?: ?string,
buttons?: Buttons,
options?: AlertOptions
): void;
static prompt(
title: ?string,
message?: ?string,
callbackOrButtons?: ?(((text: string) => void) | Buttons),
callbackOrButtons?: ?(((text: string) => void) | AlertButtons),
type?: ?AlertType,
defaultValue?: string,
keyboardType?: string,
Expand All @@ -124,17 +101,6 @@ declare export default typeof NativeAlertManager;
"
`;

exports[`public API should not change unintentionally Libraries/Alert/RCTAlertManager.flow.js 1`] = `
"declare const RCTAlertManager: {
alertWithArgs(
args: Args,
callback: (id: number, value: string) => void
): void,
};
declare export default typeof RCTAlertManager;
"
`;

exports[`public API should not change unintentionally Libraries/Alert/RCTAlertManager.js.flow 1`] = `
"declare export default {
alertWithArgs(
Expand Down
10 changes: 5 additions & 5 deletions scripts/build/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const path = require('path');
const {parseArgs} = require('util');

const TYPES_DIR = 'types_generated';
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**';
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures,flowtests}__/**';

const SOURCE_PATTERNS = [
// Start with Animated only
'react-native/Libraries/Alert/**/*.js',
'react-native/Libraries/Alert/**/*.{js,flow}',
'react-native/Libraries/ActionSheetIOS/**/*.js',
'react-native/Libraries/TurboModule/RCTExport.js',
'react-native/Libraries/Types/RootTagTypes.js',
Expand Down Expand Up @@ -115,7 +115,7 @@ function getBuildPath(file /*: string */) /*: string */ {
packageDir,
file
.replace(packageDir, TYPES_DIR)
.replace(/\.flow\.js$/, '.js')
.replace(/\.js\.flow$/, '.js')
.replace(/\.js$/, '.d.ts'),
);
}
Expand All @@ -126,15 +126,15 @@ function ignoreShadowedFiles(files /*: Array<string> */) /*: Array<string> */ {

// Find all flow definition files that shadow other files
for (const file of files) {
if (/\.flow\.js$/.test(file)) {
if (/\.js\.flow$/.test(file)) {
shadowedPrefixes[file.substring(0, file.length - 8)] = true;
}
}

// Filter out all files shadowed by flow definition files
for (const file of files) {
const prefix = file.split('.')[0];
if (/\.flow\.js$/.test(file) || !shadowedPrefixes[prefix]) {
if (/\.js\.flow$/.test(file) || !shadowedPrefixes[prefix]) {
result.push(file);
}
}
Expand Down

0 comments on commit 17a3d1a

Please sign in to comment.