/**
* 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';
const React = require('react');
const {
AccessibilityInfo,
Button,
Text,
View,
TouchableOpacity,
Alert,
UIManager,
findNodeHandle,
Platform,
} = require('react-native');
const RNTesterBlock = require('./RNTesterBlock');
class AccessibilityExample extends React.Component {
render() {
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.
{/* 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. 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"
accessibilityStates={['disabled']}
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
);
}
}
class CheckboxExample extends React.Component {
state = {
checkboxState: 'checked',
};
_onCheckboxPress = () => {
const checkboxState =
this.state.checkboxState === 'checked' ? 'unchecked' : 'checked';
this.setState({
checkboxState: checkboxState,
});
if (Platform.OS === 'android') {
UIManager.sendAccessibilityEvent(
findNodeHandle(this),
UIManager.AccessibilityEventTypes.typeViewClicked,
);
}
};
render() {
return (
Checkbox example
);
}
}
class SwitchExample extends React.Component {
state = {
switchState: 'checked',
};
_onSwitchToggle = () => {
const switchState =
this.state.switchState === 'checked' ? 'unchecked' : 'checked';
this.setState({
switchState: switchState,
});
if (Platform.OS === 'android') {
UIManager.sendAccessibilityEvent(
findNodeHandle(this),
UIManager.AccessibilityEventTypes.typeViewClicked,
);
}
};
render() {
return (
Switch example
);
}
}
class SelectionExample extends React.Component {
constructor(props) {
super(props);
this.selectableElement = React.createRef();
}
state = {
isSelected: true,
isEnabled: false,
};
render() {
let accessibilityStates = [];
let accessibilityHint = 'click me to select';
if (this.state.isSelected) {
accessibilityStates.push('selected');
accessibilityHint = 'click me to unselect';
}
if (!this.state.isEnabled) {
accessibilityStates.push('disabled');
accessibilityHint = 'use the button on the right to enable selection';
}
let buttonTitle = this.state.isEnabled
? 'Disable selection'
: 'Enable selection';
return (
{
this.setState({
isSelected: !this.state.isSelected,
});
if (Platform.OS === 'android') {
UIManager.sendAccessibilityEvent(
findNodeHandle(this.selectableElement.current),
UIManager.AccessibilityEventTypes.typeViewClicked,
);
}
}}
accessibilityLabel="element 19"
accessibilityStates={accessibilityStates}
accessibilityHint={accessibilityHint}>
Selectable element example
);
}
}
class ExpandableElementExample extends React.Component {
state = {
expandState: 'collapsed',
};
_onElementPress = () => {
const expandState =
this.state.expandState === 'collapsed' ? 'expanded' : 'collapsed';
this.setState({
expandState: expandState,
});
if (Platform.OS === 'android') {
UIManager.sendAccessibilityEvent(
findNodeHandle(this),
UIManager.AccessibilityEventTypes.typeViewClicked,
);
}
};
render() {
return (
Expandable element example
);
}
}
class AccessibilityRoleAndStateExample extends React.Component<{}> {
render() {
return (
Alert exampleCombobox exampleMenu exampleMenu bar exampleMenu item exampleProgress bar exampleRadio button exampleRadio group exampleScrollbar exampleSpin button exampleTab exampleTab list exampleTimer exampleToolbar exampleState busy example
);
}
}
class ScreenReaderStatusExample extends React.Component<{}> {
state = {
screenReaderEnabled: false,
};
componentDidMount() {
AccessibilityInfo.addEventListener(
'change',
this._handleScreenReaderToggled,
);
AccessibilityInfo.fetch().done(isEnabled => {
this.setState({
screenReaderEnabled: isEnabled,
});
});
}
componentWillUnmount() {
AccessibilityInfo.removeEventListener(
'change',
this._handleScreenReaderToggled,
);
}
_handleScreenReaderToggled = isEnabled => {
this.setState({
screenReaderEnabled: isEnabled,
});
};
render() {
return (
The screen reader is{' '}
{this.state.screenReaderEnabled ? 'enabled' : 'disabled'}.
);
}
}
class AnnounceForAccessibility extends React.Component<{}> {
_handleOnPress = () =>
AccessibilityInfo.announceForAccessibility('Announcement Test');
render() {
return (
);
}
}
exports.title = 'Accessibility';
exports.description = 'Examples of using Accessibility APIs.';
exports.examples = [
{
title: 'Accessibility elements',
render(): React.Element {
return ;
},
},
{
title: 'New accessibility roles and states',
render(): React.Element {
return ;
},
},
{
title: 'Check if the screen reader is enabled',
render(): React.Element {
return ;
},
},
{
title: 'Check if the screen reader announces',
render(): React.Element {
return ;
},
},
];