Skip to content

Commit 17a3d1a

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Update types exposed by Alert and the module structure (#49157)
Summary: Changelog: [Internal] Differential Revision: D69044715
1 parent c482241 commit 17a3d1a

File tree

5 files changed

+60
-130
lines changed

5 files changed

+60
-130
lines changed

packages/react-native/Libraries/Alert/Alert.flow.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

packages/react-native/Libraries/Alert/Alert.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,61 @@
99
*/
1010

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

1413
import Platform from '../Utilities/Platform';
1514
import RCTAlertManager from './RCTAlertManager';
1615

17-
export type * from './Alert.flow';
16+
/**
17+
* @platform ios
18+
*/
19+
export type AlertType =
20+
| 'default'
21+
| 'plain-text'
22+
| 'secure-text'
23+
| 'login-password';
24+
25+
/**
26+
* @platform ios
27+
*/
28+
export type AlertButtonStyle = 'default' | 'cancel' | 'destructive';
29+
30+
export type AlertButton = {
31+
text?: string,
32+
onPress?: ?((value?: string) => any) | ?Function,
33+
isPreferred?: boolean,
34+
style?: AlertButtonStyle,
35+
...
36+
};
37+
38+
export type AlertButtons = Array<AlertButton>;
1839

40+
export type AlertOptions = {
41+
/** @platform android */
42+
cancelable?: ?boolean,
43+
userInterfaceStyle?: 'unspecified' | 'light' | 'dark',
44+
/** @platform android */
45+
onDismiss?: ?() => void,
46+
...
47+
};
48+
49+
/**
50+
* Launches an alert dialog with the specified title and message.
51+
*
52+
* Optionally provide a list of buttons. Tapping any button will fire the
53+
* respective onPress callback and dismiss the alert. By default, the only
54+
* button will be an 'OK' button.
55+
*
56+
* This is an API that works both on iOS and Android and can show static
57+
* alerts. On iOS, you can show an alert that prompts the user to enter
58+
* some information.
59+
*
60+
* See https://reactnative.dev/docs/alert
61+
*/
1962
class Alert {
2063
static alert(
2164
title: ?string,
2265
message?: ?string,
23-
buttons?: Buttons,
66+
buttons?: AlertButtons,
2467
options?: AlertOptions,
2568
): void {
2669
if (Platform.OS === 'ios') {
@@ -53,7 +96,7 @@ class Alert {
5396
// At most three buttons (neutral, negative, positive). Ignore rest.
5497
// The text 'OK' should be probably localized. iOS Alert does that in native.
5598
const defaultPositiveText = 'OK';
56-
const validButtons: Buttons = buttons
99+
const validButtons: AlertButtons = buttons
57100
? buttons.slice(0, 3)
58101
: [{text: defaultPositiveText}];
59102
const buttonPositive = validButtons.pop();
@@ -93,10 +136,13 @@ class Alert {
93136
}
94137
}
95138

139+
/**
140+
* @platform ios
141+
*/
96142
static prompt(
97143
title: ?string,
98144
message?: ?string,
99-
callbackOrButtons?: ?(((text: string) => void) | Buttons),
145+
callbackOrButtons?: ?(((text: string) => void) | AlertButtons),
100146
type?: ?AlertType = 'plain-text',
101147
defaultValue?: string,
102148
keyboardType?: string,

packages/react-native/Libraries/Alert/RCTAlertManager.flow.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ declare export default typeof NativeActionSheetManager;
5353
"
5454
`;
5555

56-
exports[`public API should not change unintentionally Libraries/Alert/Alert.flow.js 1`] = `
56+
exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1`] = `
5757
"export type AlertType =
5858
| \\"default\\"
5959
| \\"plain-text\\"
@@ -67,7 +67,7 @@ export type AlertButton = {
6767
style?: AlertButtonStyle,
6868
...
6969
};
70-
export type Buttons = Array<AlertButton>;
70+
export type AlertButtons = Array<AlertButton>;
7171
export type AlertOptions = {
7272
cancelable?: ?boolean,
7373
userInterfaceStyle?: \\"unspecified\\" | \\"light\\" | \\"dark\\",
@@ -78,36 +78,13 @@ declare class Alert {
7878
static alert(
7979
title: ?string,
8080
message?: ?string,
81-
buttons?: Buttons,
81+
buttons?: AlertButtons,
8282
options?: AlertOptions
8383
): void;
8484
static prompt(
8585
title: ?string,
8686
message?: ?string,
87-
callbackOrButtons?: ?(((text: string) => void) | Buttons),
88-
type?: ?AlertType,
89-
defaultValue?: string,
90-
keyboardType?: string,
91-
options?: AlertOptions
92-
): void;
93-
}
94-
declare export default typeof Alert;
95-
"
96-
`;
97-
98-
exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1`] = `
99-
"export type * from \\"./Alert.flow\\";
100-
declare class Alert {
101-
static alert(
102-
title: ?string,
103-
message?: ?string,
104-
buttons?: Buttons,
105-
options?: AlertOptions
106-
): void;
107-
static prompt(
108-
title: ?string,
109-
message?: ?string,
110-
callbackOrButtons?: ?(((text: string) => void) | Buttons),
87+
callbackOrButtons?: ?(((text: string) => void) | AlertButtons),
11188
type?: ?AlertType,
11289
defaultValue?: string,
11390
keyboardType?: string,
@@ -124,17 +101,6 @@ declare export default typeof NativeAlertManager;
124101
"
125102
`;
126103

127-
exports[`public API should not change unintentionally Libraries/Alert/RCTAlertManager.flow.js 1`] = `
128-
"declare const RCTAlertManager: {
129-
alertWithArgs(
130-
args: Args,
131-
callback: (id: number, value: string) => void
132-
): void,
133-
};
134-
declare export default typeof RCTAlertManager;
135-
"
136-
`;
137-
138104
exports[`public API should not change unintentionally Libraries/Alert/RCTAlertManager.js.flow 1`] = `
139105
"declare export default {
140106
alertWithArgs(

scripts/build/build-types.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const path = require('path');
2121
const {parseArgs} = require('util');
2222

2323
const TYPES_DIR = 'types_generated';
24-
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**';
24+
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures,flowtests}__/**';
2525

2626
const SOURCE_PATTERNS = [
2727
// Start with Animated only
28-
'react-native/Libraries/Alert/**/*.js',
28+
'react-native/Libraries/Alert/**/*.{js,flow}',
2929
'react-native/Libraries/ActionSheetIOS/**/*.js',
3030
'react-native/Libraries/TurboModule/RCTExport.js',
3131
'react-native/Libraries/Types/RootTagTypes.js',
@@ -115,7 +115,7 @@ function getBuildPath(file /*: string */) /*: string */ {
115115
packageDir,
116116
file
117117
.replace(packageDir, TYPES_DIR)
118-
.replace(/\.flow\.js$/, '.js')
118+
.replace(/\.js\.flow$/, '.js')
119119
.replace(/\.js$/, '.d.ts'),
120120
);
121121
}
@@ -126,15 +126,15 @@ function ignoreShadowedFiles(files /*: Array<string> */) /*: Array<string> */ {
126126

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

134134
// Filter out all files shadowed by flow definition files
135135
for (const file of files) {
136136
const prefix = file.split('.')[0];
137-
if (/\.flow\.js$/.test(file) || !shadowedPrefixes[prefix]) {
137+
if (/\.js\.flow$/.test(file) || !shadowedPrefixes[prefix]) {
138138
result.push(file);
139139
}
140140
}

0 commit comments

Comments
 (0)