Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified AlertIOS #5286

Closed
wants to merge 2 commits into from
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
3 changes: 1 addition & 2 deletions Examples/UIExplorer/AlertExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
var React = require('react-native');
var {
Alert,
Platform,
StyleSheet,
Text,
TouchableHighlight,
Expand Down Expand Up @@ -134,4 +133,4 @@ var styles = StyleSheet.create({
module.exports = {
AlertExample,
SimpleAlertExampleBlock,
}
};
96 changes: 26 additions & 70 deletions Examples/UIExplorer/AlertIOSExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,19 @@ exports.examples = [{
}
},
{
title: 'Alert Types',
title: 'Prompt Options',
render(): React.Component {
return <PromptOptions />;
}
},
{
title: 'Prompt Types',
render() {
return (
<View>
<TouchableHighlight
style={styles.wrapper}
onPress={() => AlertIOS.alert(
'Hello World',
null,
[
{text: 'OK', onPress: (text) => console.log('OK pressed'), type: 'default'}
]
)}>

<View style={styles.button}>
<Text>
{'default'}
</Text>
</View>

</TouchableHighlight>
<TouchableHighlight
style={styles.wrapper}
onPress={() => AlertIOS.alert(
'Plain Text Entry',
null,
[
{text: 'Submit', onPress: (text) => console.log('Text: ' + text), type: 'plain-text'},
{text: 'Cancel', onPress: () => console.log('Cancel'), style: 'cancel'}
]
)}>
onPress={() => AlertIOS.prompt('Plain Text Entry')}>

<View style={styles.button}>
<Text>
Expand All @@ -77,14 +59,7 @@ exports.examples = [{
</TouchableHighlight>
<TouchableHighlight
style={styles.wrapper}
onPress={() => AlertIOS.alert(
'Secure Text Entry',
null,
[
{text: 'Submit', onPress: (text) => console.log('Password: ' + text), type: 'secure-text'},
{text: 'Cancel', onPress: () => console.log('Cancel'), style: 'cancel'}
]
)}>
onPress={() => AlertIOS.prompt('Secure Text', null, null, 'secure-text')}>

<View style={styles.button}>
<Text>
Expand All @@ -95,14 +70,7 @@ exports.examples = [{
</TouchableHighlight>
<TouchableHighlight
style={styles.wrapper}
onPress={() => AlertIOS.alert(
'Login & Password',
null,
[
{text: 'Submit', onPress: (details) => console.log('Login: ' + details.login + '; Password: ' + details.password), type: 'login-password'},
{text: 'Cancel', onPress: () => console.log('Cancel'), style: 'cancel'}
]
)}>
onPress={() => AlertIOS.prompt('Login & Password', null, null, 'login-password')}>

<View style={styles.button}>
<Text>
Expand All @@ -114,32 +82,25 @@ exports.examples = [{
</View>
);
}
},
{
title: 'Prompt',
render(): React.Component {
return <PromptExample />;
}
}];

class PromptExample extends React.Component {
class PromptOptions extends React.Component {
constructor(props) {
super(props);

this.promptResponse = this.promptResponse.bind(this);
this.state = {
promptValue: undefined,
};
this.saveResponse = this.saveResponse.bind(this);

this.title = 'Type a value';
this.defaultValue = 'Default value';
this.buttons = [{
this.customButtons = [{
text: 'Custom OK',
onPress: this.promptResponse
onPress: this.saveResponse
}, {
text: 'Custom Cancel',
style: 'cancel',
}];

this.state = {
promptValue: undefined,
};
}

render() {
Expand All @@ -151,7 +112,7 @@ class PromptExample extends React.Component {

<TouchableHighlight
style={styles.wrapper}
onPress={this.prompt.bind(this, this.title, null, null, this.promptResponse)}>
onPress={() => AlertIOS.prompt('Type a value', null, this.saveResponse)}>

<View style={styles.button}>
<Text>
Expand All @@ -162,7 +123,7 @@ class PromptExample extends React.Component {

<TouchableHighlight
style={styles.wrapper}
onPress={this.prompt.bind(this, this.title, null, this.buttons, null)}>
onPress={() => AlertIOS.prompt('Type a value', null, this.customButtons)}>

<View style={styles.button}>
<Text>
Expand All @@ -173,36 +134,31 @@ class PromptExample extends React.Component {

<TouchableHighlight
style={styles.wrapper}
onPress={this.prompt.bind(this, this.title, this.defaultValue, null, this.promptResponse)}>
onPress={() => AlertIOS.prompt('Type a value', null, this.saveResponse, undefined, 'Default value')}>

<View style={styles.button}>
<Text>
prompt with title, default value & callback
prompt with title, callback & default value
</Text>
</View>
</TouchableHighlight>

<TouchableHighlight
style={styles.wrapper}
onPress={this.prompt.bind(this, this.title, this.defaultValue, this.buttons, null)}>
onPress={() => AlertIOS.prompt('Type a value', null, this.customButtons, 'login-password', 'admin@site.com')}>

<View style={styles.button}>
<Text>
prompt with title, default value & custom buttons
prompt with title, custom buttons, login/password & default value
</Text>
</View>
</TouchableHighlight>
</View>
);
}

prompt() {
// Flow's apply support is broken: #7035621
((AlertIOS.prompt: any).apply: any)(AlertIOS, arguments);
}

promptResponse(promptValue) {
this.setState({ promptValue });
saveResponse(promptValue) {
this.setState({ promptValue: JSON.stringify(promptValue) });
}
}

Expand Down
14 changes: 9 additions & 5 deletions Libraries/Utilities/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ type Buttons = Array<{
* ## iOS
*
* On iOS you can specify any number of buttons. Each button can optionally
* specify a style and you can also specify type of the alert. Refer to
* `AlertIOS` for details.
* specify a style, which is one of 'default', 'cancel' or 'destructive'.
*
* ## Android
*
*
* On Android at most three buttons can be specified. Android has a concept
* of a neutral, negative and a positive button:
*
Expand All @@ -68,10 +67,15 @@ class Alert {
title: ?string,
message?: ?string,
buttons?: Buttons,
type?: AlertType
type?: AlertType,
): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

This was for convenience on iOS. How can people pass the type / style with this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only reason to change the type is to switch from an alert to a prompt, and I think it's very unlikely that anyone was using a dialog that appears as a prompt on one platform and an alert on another. But I can add that arg back in with a deprecation warning.

if (Platform.OS === 'ios') {
AlertIOS.alert(title, message, buttons, type);
if (typeof type !== 'undefined') {
console.warn('Alert.alert() with a 4th "type" parameter is deprecated and will be removed. Use AlertIOS.prompt() instead.');
AlertIOS.alert(title, message, buttons, type);
return;
}
AlertIOS.alert(title, message, buttons);
} else if (Platform.OS === 'android') {
AlertAndroid.alert(title, message, buttons);
}
Expand Down
Loading