Skip to content

Commit

Permalink
Add flow definitions for Alert and align them with TypeScript (facebo…
Browse files Browse the repository at this point in the history
…ok#49006)

Summary:
Pull Request resolved: facebook#49006

Changelog: [Internal]

Reviewed By: huntie

Differential Revision: D68774525

fbshipit-source-id: 509f26b0f5f0d309502681f3228ae519689d480a
  • Loading branch information
j-piasecki authored and facebook-github-bot committed Jan 29, 2025
1 parent 8058aab commit 5d7feda
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 35 deletions.
62 changes: 62 additions & 0 deletions packages/react-native/Libraries/Alert/Alert.flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

export type AlertType =
| 'default'
| 'plain-text'
| 'secure-text'
| 'login-password';

export type AlertButtonStyle = 'default' | 'cancel' | 'destructive';

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

export type Buttons = 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.
*
* See https://reactnative.dev/docs/alert
*/
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),
type?: ?AlertType,
defaultValue?: string,
keyboardType?: string,
options?: AlertOptions,
): void;
}

export default Alert;
34 changes: 4 additions & 30 deletions packages/react-native/Libraries/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,19 @@
*/

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 AlertType =
| 'default'
| 'plain-text'
| 'secure-text'
| 'login-password';
export type AlertButtonStyle = 'default' | 'cancel' | 'destructive';
export type AlertButton = {
text?: string,
onPress?: ?Function,
isPreferred?: boolean,
style?: AlertButtonStyle,
...
};
export type Buttons = Array<AlertButton>;
export type * from './Alert.flow';

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

/**
* Launches an alert dialog with the specified title and message.
*
* See https://reactnative.dev/docs/alert
*/
class Alert {
static alert(
title: ?string,
message?: ?string,
buttons?: Buttons,
options?: Options,
options?: AlertOptions,
): void {
if (Platform.OS === 'ios') {
Alert.prompt(
Expand Down Expand Up @@ -126,7 +100,7 @@ class Alert {
type?: ?AlertType = 'plain-text',
defaultValue?: string,
keyboardType?: string,
options?: Options,
options?: AlertOptions,
): void {
if (Platform.OS === 'ios') {
let callbacks: Array<?any> = [];
Expand Down
20 changes: 20 additions & 0 deletions packages/react-native/Libraries/Alert/RCTAlertManager.flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

import type {Args} from './NativeAlertManager';

declare const RCTAlertManager: {
alertWithArgs(
args: Args,
callback: (id: number, value: string) => void,
): void,
};

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

exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1`] = `
exports[`public API should not change unintentionally Libraries/Alert/Alert.flow.js 1`] = `
"export type AlertType =
| \\"default\\"
| \\"plain-text\\"
Expand All @@ -44,13 +44,13 @@ exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1
export type AlertButtonStyle = \\"default\\" | \\"cancel\\" | \\"destructive\\";
export type AlertButton = {
text?: string,
onPress?: ?Function,
onPress?: ?((value?: string) => any) | ?Function,
isPreferred?: boolean,
style?: AlertButtonStyle,
...
};
export type Buttons = Array<AlertButton>;
type Options = {
export type AlertOptions = {
cancelable?: ?boolean,
userInterfaceStyle?: \\"unspecified\\" | \\"light\\" | \\"dark\\",
onDismiss?: ?() => void,
Expand All @@ -61,7 +61,7 @@ declare class Alert {
title: ?string,
message?: ?string,
buttons?: Buttons,
options?: Options
options?: AlertOptions
): void;
static prompt(
title: ?string,
Expand All @@ -70,7 +70,30 @@ declare class Alert {
type?: ?AlertType,
defaultValue?: string,
keyboardType?: string,
options?: Options
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),
type?: ?AlertType,
defaultValue?: string,
keyboardType?: string,
options?: AlertOptions
): void;
}
declare export default typeof Alert;
Expand All @@ -83,6 +106,17 @@ 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

0 comments on commit 5d7feda

Please sign in to comment.