/** * 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 */ 'use strict'; import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; import type {EventSubscription, GestureResponderEvent} from 'react-native'; import RNTesterBlock from '../../components/RNTesterBlock'; import RNTesterText from '../../components/RNTesterText'; import checkImageSource from './check.png'; import mixedCheckboxImageSource from './mixed.png'; import uncheckImageSource from './uncheck.png'; import React, {createRef, useEffect, useRef, useState} from 'react'; import { AccessibilityInfo, Alert, Button, Image, ImageBackground, Platform, Pressable, ScrollView, StyleSheet, Switch, Text, TextInput, TouchableNativeFeedback, TouchableOpacity, TouchableWithoutFeedback, View, } from 'react-native'; const styles = StyleSheet.create({ sectionContainer: { rowGap: 20, }, default: { borderWidth: StyleSheet.hairlineWidth, borderColor: '#0f0f0f', flex: 1, fontSize: 13, padding: 4, }, touchable: { backgroundColor: 'blue', borderColor: 'red', borderWidth: 1, borderRadius: 10, padding: 10, borderStyle: 'solid', }, image: { width: 20, height: 20, resizeMode: 'contain', marginRight: 10, }, disabledImage: { width: 120, height: 120, }, containerAlignCenter: { display: 'flex', flexDirection: 'column', justifyContent: 'space-between', }, button: { padding: 8, borderWidth: 1, borderColor: 'blue', }, smallRedSquare: { backgroundColor: 'red', height: 40, width: 40, }, container: { flex: 1, }, ImageBackground: { flex: 1, justifyContent: 'center', }, text: { color: 'white', fontSize: 20, lineHeight: 84, fontWeight: 'bold', textAlign: 'center', backgroundColor: '#000000c0', }, scrollView: { height: 50, }, boxSize: { width: 50, height: 50, }, smallBoxSize: { width: 25, height: 25, }, bigBoxSize: { width: 100, height: 100, }, link: { color: 'blue', }, }); class AccessibilityExample extends React.Component<{}> { render(): React.Node { return ( Text's accessibilityLabel is the raw text itself unless it is set explicitly. This text component's accessibilityLabel is set explicitly. This is text one. This is text two. This is text one. This is text two. This is text one. This is text two. This view's children are hidden from the accessibility tree {/* Android screen readers will say the accessibility hint instead of the text since the view doesn't have a label. */} This is text one. This is text two. This is text one. This is text two. This is a title. This is a title. Alert.alert('Link has been clicked!')} accessibilityRole="link"> Click me Alert.alert('Button has been pressed!')} accessibilityRole="button"> Click me Alert.alert('Button has been pressed!')} accessibilityRole="button" accessibilityState={{disabled: true}} disabled={true}> I am disabled. Clicking me will not trigger any action. Alert.alert('Disabled Button has been pressed!')} accessibilityLabel={'You are pressing Disabled TouchableOpacity'} accessibilityState={{disabled: true}}> I am disabled. Clicking me will not trigger any action. This view is selected and disabled. Accessible view with label, hint, role, and state Accessible view with label, hint, role, and state Mail Address First Name Enable Notifications ); } } class AutomaticContentGrouping extends React.Component<{}> { render(): React.Node { return ( Text number 1 with a role Text number 2 { switch (event.nativeEvent.actionName) { case 'cut': Alert.alert('Alert', 'cut action success'); break; case 'copy': Alert.alert('Alert', 'copy action success'); break; case 'paste': Alert.alert('Alert', 'paste action success'); break; } }} accessibilityRole="button"> Text number 1 Text number 2 Text number 3 Text number 1 Text number 1 console.warn('onPress child')} accessible={false} accessibilityLabel="this is my label" accessibilityRole="image" accessibilityState={{disabled: true}} accessibilityValue={{ text: 'this is the accessibility value', }}> Text number 3 Text number 2 Text number 3 Text number 4 console.warn('onPress child')} accessible={true} accessibilityRole="button"> ); } } class CheckboxExample extends React.Component< {}, { checkboxState: boolean | 'mixed', }, > { state: {checkboxState: boolean | 'mixed'} = { checkboxState: true, }; _onCheckboxPress = () => { let checkboxState: boolean | string = false; if (this.state.checkboxState === false) { checkboxState = 'mixed'; } else if (this.state.checkboxState === 'mixed') { checkboxState = true; } else { checkboxState = false; } this.setState({ checkboxState, }); }; render(): React.Node { return ( Checkbox example ); } } class SwitchExample extends React.Component< {}, { switchState: boolean, }, > { state: {switchState: boolean} = { switchState: true, }; _onSwitchToggle = () => { const switchState = !this.state.switchState; this.setState({ switchState, }); }; render(): React.Node { return ( Switch example ); } } class SelectionExample extends React.Component< {}, { isSelected: boolean, isEnabled: boolean, }, > { constructor(props: {}) { super(props); this.selectableElement = createRef(); } selectableElement: { current: React.ElementRef | null, }; state: {isEnabled: boolean, isSelected: boolean} = { isSelected: true, isEnabled: false, }; render(): React.Node { const {isSelected, isEnabled} = this.state; let accessibilityHint = 'click me to select'; if (isSelected) { accessibilityHint = 'click me to unselect'; } if (!isEnabled) { accessibilityHint = 'use the button on the right to enable selection'; } const buttonTitle = isEnabled ? 'Disable selection' : 'Enable selection'; const touchableHint = ` (touching the TouchableOpacity will ${ isSelected ? 'disable' : 'enable' } accessibilityState.selected)`; return ( { if (isEnabled) { this.setState({ isSelected: !isSelected, }); } else { console.warn('selection is disabled, please enable selection.'); } }} accessibilityLabel="element 19" accessibilityState={{ selected: isSelected, disabled: !isEnabled, }} style={styles.touchable} accessibilityHint={accessibilityHint}> {`Selectable TouchableOpacity Example ${touchableHint}`}