/** * 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. * * @flow strict-local * @format */ import type {RNTesterModule} from '../../types/RNTesterTypes'; import RNTesterText from '../../components/RNTesterText'; import * as React from 'react'; import {useState} from 'react'; import {Alert, Pressable, StyleSheet, Text, View} from 'react-native'; // Shows log on the screen const Log = ({message}: {message: string}) => message ? ( Log: {message} ) : null; /** * Simple alert examples. */ const AlertWithDefaultButton = () => { const alertMessage = 'An external USB drive has been detected!'; return ( Alert.alert('Alert', alertMessage)}> Tap to view alert ); }; const AlertWithTwoButtons = () => { const [message, setMessage] = useState(''); const alertMessage = 'Your subscription has expired!'; return ( Alert.alert('Action Required!', alertMessage, [ {text: 'Ignore', onPress: () => setMessage('Ignore Pressed!')}, {text: 'Renew', onPress: () => setMessage('Renew Pressed!')}, ]) }> Tap to view alert ); }; const AlertWithThreeButtons = () => { const [message, setMessage] = useState(''); const alertMessage = 'Do you want to save your changes?'; return ( Alert.alert('Unsaved Changes!', alertMessage, [ {text: 'Cancel', onPress: () => setMessage('Cancel Pressed!')}, {text: 'No', onPress: () => setMessage('No Pressed!')}, {text: 'Yes', onPress: () => setMessage('Yes Pressed!')}, ]) }> Tap to view alert ); }; const AlertWithManyButtons = () => { const [message, setMessage] = useState(''); const alertMessage = 'Credibly reintermediate next-generation potentialities after goal-oriented ' + 'catalysts for change. Dynamically revolutionize.'; return ( Alert.alert( 'Foo Title', alertMessage, '..............'.split('').map((dot, index) => ({ text: 'Button ' + index, onPress: () => setMessage(`Button ${index} Pressed!`), })), ) }> Tap to view alert ); }; const AlertWithCancelableTrue = () => { const [message, setMessage] = useState(''); const alertMessage = 'Tapping outside this dialog will dismiss this alert.'; return ( 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.', ), }, ) }> Tap to view alert ); }; const AlertWithStyles = () => { const [message, setMessage] = useState(''); const alertMessage = 'Look at the button styles!'; return ( 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', }, ]) }> Tap to view alert ); }; const AlertWithStylesPreferred = () => { const [message, setMessage] = useState(''); const alertMessage = "The Preferred button is styled with 'preferred', so it is emphasized over the cancel button."; return ( Alert.alert('Foo Title', alertMessage, [ { text: 'Preferred', isPreferred: true, onPress: () => setMessage('Preferred Pressed!'), }, { text: 'Cancel', style: 'cancel', onPress: () => setMessage('Cancel Pressed!'), }, ]) }> Tap to view alert ); }; const PromptOptions = () => { const [promptValue, setPromptValue] = useState< string | {login: string, password: string}, >(''); const customButtons = [ { text: 'Custom OK', onPress: setPromptValue, }, { text: 'Custom Cancel', style: 'cancel', }, ]; return ( Prompt value: {JSON.stringify(promptValue, null, 2)} Alert.prompt('Type a value', null, setPromptValue)}> prompt with title & callback Alert.prompt('Type a value', null, customButtons)}> prompt with title & custom buttons Alert.prompt( 'Type a phone number', null, null, 'plain-text', undefined, 'phone-pad', ) }> prompt with title & custom keyboard Alert.prompt( 'Type a value', null, setPromptValue, undefined, 'Default value', ) }> prompt with title, callback & default value Alert.prompt( 'Type a value', null, /* $FlowFixMe[incompatible-type] Natural Inference rollout. See * https://fburl.com/workplace/6291gfvu */ customButtons, 'login-password', 'admin@site.com', ) }> prompt with title, custom buttons, login/password & default value ); }; const PromptTypes = () => { return ( Alert.prompt('Plain Text Entry')}> plain-text Alert.prompt('Secure Text', null, null, 'secure-text')}> secure-text Alert.prompt('Login & Password', null, null, 'login-password') }> login-password ); }; const styles = StyleSheet.create({ wrapper: { borderRadius: 5, marginBottom: 5, }, button: { backgroundColor: '#eeeeee', padding: 10, }, logContainer: { paddingVertical: 8, paddingHorizontal: 5, }, bold: { fontWeight: 'bold', }, promptValue: { marginBottom: 10, }, }); export const 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 ; }, }, { title: 'Alert with two Buttons', description: 'It can be used when an action is required from the user.', render(): React.Node { return ; }, }, { title: 'Alert with three Buttons', description: 'It can be used when there are three possible actions', render(): React.Node { return ; }, }, { title: 'Alert with many Buttons', platform: 'ios', description: 'It can be used when more than three actions are required.', render(): React.Node { return ; }, }, { 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 ; }, }, { title: 'Alert with styles', platform: 'ios', description: "Alert buttons can be styled. There are three button styles - 'default' | 'cancel' | 'destructive'.", render(): React.Node { return ; }, }, { title: 'Alert with styles + preferred', platform: 'ios', description: "Alert buttons with 'isPreferred' will be emphasized, even over cancel buttons", render(): React.Node { return ; }, }, { title: 'Prompt Options', platform: 'ios', render(): React.Node { return ; }, }, { title: 'Prompt Types', platform: 'ios', render(): React.Node { return ; }, }, ]; export default ({ framework: 'React', title: 'Alerts', category: 'UI', documentationURL: 'https://reactnative.dev/docs/alert', description: 'Alerts display a concise and informative message and prompt the user to make a decision.', /* $FlowFixMe[incompatible-type] Natural Inference rollout. See * https://fburl.com/workplace/6291gfvu */ examples, }: RNTesterModule);