Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions Libraries/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,8 @@ import Platform from '../Utilities/Platform';
import DialogManagerAndroid, {
type DialogOptions,
} from '../NativeModules/specs/NativeDialogManagerAndroid';

const RCTAlertManager = NativeModules.AlertManager;

export type Buttons = Array<{
text?: string,
onPress?: ?Function,
style?: AlertButtonStyle,
}>;

type Options = {
cancelable?: ?boolean,
onDismiss?: ?Function,
};

type AlertType = $Keys<{
default: string,
'plain-text': string,
'secure-text': string,
'login-password': string,
}>;

export type AlertButtonStyle = $Keys<{
default: string,
cancel: string,
destructive: string,
}>;
import RCTAlertManager from './RCTAlertManager';
import {type Buttons, type Options, type AlertType} from './NativeAlertManager';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor and no need to update it, but you also can use

import type { Buttons, Options, AlertType } from './NativeAlertManager';


/**
* Launches an alert dialog with the specified title and message.
Expand Down Expand Up @@ -126,11 +102,14 @@ class Alert {
);

const callback = type;
if (!RCTAlertManager) {
return;
}
RCTAlertManager.alertWithArgs(
{
title: title || '',
type: 'plain-text',
defaultValue: message,
defaultValue: message || '',
},
(id, value) => {
callback(value);
Expand Down Expand Up @@ -161,6 +140,9 @@ class Alert {
});
}

if (!RCTAlertManager) {
return;
}
RCTAlertManager.alertWithArgs(
{
title: title || '',
Expand All @@ -174,6 +156,7 @@ class Alert {
},
(id, value) => {
const cb = callbacks[id];
// $FlowFixMe
cb && cb(value);
},
);
Expand Down
51 changes: 51 additions & 0 deletions Libraries/Alert/NativeAlertManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @flow strict-local
* @format
*/

'use strict';

import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';
import Platform from '../Utilities/Platform';

export type Buttons = Array<{
text?: string,
onPress?: ?() => void,
style?: AlertButtonStyle,
}>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also doesn't support enums. 😔


export type Options = {
cancelable?: ?boolean,
onDismiss?: ?() => void,
};

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

/* 'default' | 'cancel' | 'destructive' */
export type AlertButtonStyle = string;

type Args = {|
Comment thread
sasurau4 marked this conversation as resolved.
title: string,
message?: string,
buttons?: Buttons,
type?: string,
defaultValue?: string,
cancelButtonKey?: string,
destructiveButtonKey?: string,
keyboardType?: string,
|};

export interface Spec extends TurboModule {
+alertWithArgs: (
args: Args,
callback: (id: number, value: string) => void,
) => void;
}

export default (Platform.OS === 'ios'
? TurboModuleRegistry.getEnforcing<Spec>('AlertManager')
: undefined);
2 changes: 1 addition & 1 deletion Libraries/Alert/RCTAlertManager.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

'use strict';

const RCTAlertManager = require('../BatchedBridge/NativeModules').AlertManager;
import RCTAlertManager from './NativeAlertManager';

module.exports = RCTAlertManager;