mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: This PR updates the Alert API Examples in RNTester. It is part of the MLH Fellowship. ## Changelog - Added new use case for the Alert API. - Wrote each use case in a separate example block to allow searching - Wrote Detox tests for the Alert Screen Pull Request resolved: https://github.com/facebook/react-native/pull/29855 Test Plan: **Screen Recording**: https://i.imgur.com/9ubMmDC.mp4 - Run the RNTester app locally by following [these](https://github.com/MLH-Fellowship/react-native/tree/rntest-pkg/packages/rn-tester) instructions. - Search for `Alerts` and tap on it <img width="566" alt="Screenshot 2020-07-07 at 1 29 03 PM" src="https://user-images.githubusercontent.com/22813027/86742553-d5351380-c055-11ea-8cd5-af0484797b8f.png"> Reviewed By: cpojer Differential Revision: D23643369 Pulled By: rickhanlonii fbshipit-source-id: 008801b857a3cda2d9125b33cba6251692e433dd
268 lines
6.8 KiB
JavaScript
268 lines
6.8 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import React, {useState} from 'react';
|
|
import {Alert, StyleSheet, Text, TouchableHighlight, View} from 'react-native';
|
|
|
|
// Shows log on the screen
|
|
const Log = ({message}) =>
|
|
message ? (
|
|
<View style={styles.logContainer}>
|
|
<Text>
|
|
<Text style={styles.bold}>Log</Text>: {message}
|
|
</Text>
|
|
</View>
|
|
) : null;
|
|
|
|
/**
|
|
* Simple alert examples.
|
|
*/
|
|
|
|
const AlertWithDefaultButton = () => {
|
|
const alertMessage = 'An external USB drive has been detected!';
|
|
|
|
return (
|
|
<View>
|
|
<TouchableHighlight
|
|
testID="alert-with-default-button"
|
|
style={styles.wrapper}
|
|
onPress={() => Alert.alert('Alert', alertMessage)}>
|
|
<View style={styles.button}>
|
|
<Text>Tap to view alert</Text>
|
|
</View>
|
|
</TouchableHighlight>
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const AlertWithTwoButtons = () => {
|
|
const [message, setMessage] = useState('');
|
|
|
|
const alertMessage = 'Your subscription has expired!';
|
|
|
|
return (
|
|
<View>
|
|
<TouchableHighlight
|
|
style={styles.wrapper}
|
|
onPress={() =>
|
|
Alert.alert('Action Required!', alertMessage, [
|
|
{text: 'Ignore', onPress: () => setMessage('Ignore Pressed!')},
|
|
{text: 'Renew', onPress: () => setMessage('Renew Pressed!')},
|
|
])
|
|
}>
|
|
<View style={styles.button}>
|
|
<Text>Tap to view alert</Text>
|
|
</View>
|
|
</TouchableHighlight>
|
|
<Log message={message} />
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const AlertWithThreeButtons = () => {
|
|
const [message, setMessage] = useState('');
|
|
|
|
const alertMessage = 'Do you want to save your changes?';
|
|
|
|
return (
|
|
<View>
|
|
<TouchableHighlight
|
|
testID="alert-with-three-buttons"
|
|
style={styles.wrapper}
|
|
onPress={() =>
|
|
Alert.alert('Unsaved Changes!', alertMessage, [
|
|
{text: 'Cancel', onPress: () => setMessage('Cancel Pressed!')},
|
|
{text: 'No', onPress: () => setMessage('No Pressed!')},
|
|
{text: 'Yes', onPress: () => setMessage('Yes Pressed!')},
|
|
])
|
|
}>
|
|
<View style={styles.button}>
|
|
<Text>Tap to view alert</Text>
|
|
</View>
|
|
</TouchableHighlight>
|
|
<Log message={message} />
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const AlertWithManyButtons = () => {
|
|
const [message, setMessage] = useState('');
|
|
|
|
const alertMessage =
|
|
'Credibly reintermediate next-generation potentialities after goal-oriented ' +
|
|
'catalysts for change. Dynamically revolutionize.';
|
|
|
|
return (
|
|
<View>
|
|
<TouchableHighlight
|
|
style={styles.wrapper}
|
|
onPress={() =>
|
|
Alert.alert(
|
|
'Foo Title',
|
|
alertMessage,
|
|
'..............'.split('').map((dot, index) => ({
|
|
text: 'Button ' + index,
|
|
onPress: () => setMessage(`Button ${index} Pressed!`),
|
|
})),
|
|
)
|
|
}>
|
|
<View style={styles.button}>
|
|
<Text>Tap to view alert</Text>
|
|
</View>
|
|
</TouchableHighlight>
|
|
<Log message={message} />
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const AlertWithCancelableTrue = () => {
|
|
const [message, setMessage] = useState('');
|
|
|
|
const alertMessage = 'Tapping outside this dialog will dismiss this alert.';
|
|
|
|
return (
|
|
<View>
|
|
<TouchableHighlight
|
|
style={styles.wrapper}
|
|
onPress={() =>
|
|
Alert.alert(
|
|
'Alert Title',
|
|
alertMessage,
|
|
[{text: 'OK', onPress: () => setMessage('OK Pressed!')}],
|
|
{
|
|
cancelable: true,
|
|
onDismiss: () =>
|
|
setMessage(
|
|
'This alert was dismissed by tapping outside of the alert dialog.',
|
|
),
|
|
},
|
|
)
|
|
}>
|
|
<View style={styles.button}>
|
|
<Text>Tap to view alert</Text>
|
|
</View>
|
|
</TouchableHighlight>
|
|
<Log message={message} />
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const AlertWithStyles = () => {
|
|
const [message, setMessage] = useState('');
|
|
|
|
const alertMessage = 'Look at the button styles!';
|
|
|
|
return (
|
|
<View>
|
|
<TouchableHighlight
|
|
style={styles.wrapper}
|
|
onPress={() =>
|
|
Alert.alert('Styled Buttons!', alertMessage, [
|
|
{
|
|
text: 'Default',
|
|
onPress: () => setMessage('Default Pressed!'),
|
|
style: 'default',
|
|
},
|
|
{
|
|
text: 'Cancel',
|
|
onPress: () => setMessage('Cancel Pressed!'),
|
|
style: 'cancel',
|
|
},
|
|
{
|
|
text: 'Destructive',
|
|
onPress: () => setMessage('Destructive Pressed!'),
|
|
style: 'destructive',
|
|
},
|
|
])
|
|
}>
|
|
<View style={styles.button}>
|
|
<Text>Tap to view alert</Text>
|
|
</View>
|
|
</TouchableHighlight>
|
|
<Log message={message} />
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const styles = StyleSheet.create({
|
|
wrapper: {
|
|
borderRadius: 5,
|
|
marginBottom: 5,
|
|
},
|
|
button: {
|
|
backgroundColor: '#eeeeee',
|
|
padding: 10,
|
|
},
|
|
logContainer: {
|
|
paddingVertical: 8,
|
|
paddingHorizontal: 5,
|
|
},
|
|
bold: {
|
|
fontWeight: 'bold',
|
|
},
|
|
});
|
|
|
|
exports.title = 'Alerts';
|
|
exports.description =
|
|
'Alerts display a concise and informative message ' +
|
|
'and prompt the user to make a decision.';
|
|
exports.documentationURL = 'https://reactnative.dev/docs/alert';
|
|
exports.examples = [
|
|
{
|
|
title: 'Alert with default Button',
|
|
description:
|
|
"It can be used to show some information to user that doesn't require an action.",
|
|
render(): React.Node {
|
|
return <AlertWithDefaultButton />;
|
|
},
|
|
},
|
|
{
|
|
title: 'Alert with two Buttons',
|
|
description: 'It can be used when an action is required from the user.',
|
|
render(): React.Node {
|
|
return <AlertWithTwoButtons />;
|
|
},
|
|
},
|
|
{
|
|
title: 'Alert with three Buttons',
|
|
description: 'It can be used when there are three possible actions',
|
|
render(): React.Node {
|
|
return <AlertWithThreeButtons />;
|
|
},
|
|
},
|
|
{
|
|
title: 'Alert with many Buttons',
|
|
platform: 'ios',
|
|
description: 'It can be used when more than three actions are required.',
|
|
render(): React.Node {
|
|
return <AlertWithManyButtons />;
|
|
},
|
|
},
|
|
{
|
|
title: 'Alert with cancelable={true}',
|
|
platform: 'android',
|
|
description:
|
|
'By passing cancelable={false} prop to alerts on Android, they can be dismissed by tapping outside of the alert box.',
|
|
render(): React.Node {
|
|
return <AlertWithCancelableTrue />;
|
|
},
|
|
},
|
|
{
|
|
title: 'Alert with styles',
|
|
platform: 'ios',
|
|
description:
|
|
"Alert buttons can be styled. There are three button styles - 'default' | 'cancel' | 'destructive'.",
|
|
render(): React.Node {
|
|
return <AlertWithStyles />;
|
|
},
|
|
},
|
|
];
|