diff --git a/packages/react-native/Libraries/Alert/Alert.flow.js b/packages/react-native/Libraries/Alert/Alert.flow.js new file mode 100644 index 00000000000000..97c18ef07c4cf3 --- /dev/null +++ b/packages/react-native/Libraries/Alert/Alert.flow.js @@ -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; + +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; diff --git a/packages/react-native/Libraries/Alert/Alert.js b/packages/react-native/Libraries/Alert/Alert.js index 79c7e11b105956..be65d0d2b3f3f4 100644 --- a/packages/react-native/Libraries/Alert/Alert.js +++ b/packages/react-native/Libraries/Alert/Alert.js @@ -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; +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( @@ -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 = []; diff --git a/packages/react-native/Libraries/Alert/RCTAlertManager.flow.js b/packages/react-native/Libraries/Alert/RCTAlertManager.flow.js new file mode 100644 index 00000000000000..a2fb268a3d1902 --- /dev/null +++ b/packages/react-native/Libraries/Alert/RCTAlertManager.flow.js @@ -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; diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 0926e84b4fe9fa..831996bcd259f9 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -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\\" @@ -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; -type Options = { +export type AlertOptions = { cancelable?: ?boolean, userInterfaceStyle?: \\"unspecified\\" | \\"light\\" | \\"dark\\", onDismiss?: ?() => void, @@ -61,7 +61,7 @@ declare class Alert { title: ?string, message?: ?string, buttons?: Buttons, - options?: Options + options?: AlertOptions ): void; static prompt( title: ?string, @@ -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; @@ -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(