Skip to content

Commit 5889cbe

Browse files
Huzaifa Khanfacebook-github-bot
Huzaifa Khan
authored andcommitted
Added talkback support for button accessibility: disabled prop (#31001)
Summary: Issue # #30934 .When using a screen reader disabled buttons do not announce that they are disabled. ## Changelog [Android] [Changed] - Passing accessibility state in button so it can announce disabled in talkback Pull Request resolved: #31001 Test Plan: I have added Button in Button Example with accessibiltyState prop that will announce button is disabled when testing with talkback. ## Ios test I am unable to run ios project on my machine. RNTesterPods.xcworkspace gives workspace integrity error :/ Reviewed By: kacieb Differential Revision: D26492483 Pulled By: lunaleaps fbshipit-source-id: c4bbe8ca896b0d303976591c300ccac67a96ac73
1 parent fdb2bb7 commit 5889cbe

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Libraries/Components/Button.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const Text = require('../Text/Text');
1818
const TouchableNativeFeedback = require('./Touchable/TouchableNativeFeedback');
1919
const TouchableOpacity = require('./Touchable/TouchableOpacity');
2020
const View = require('./View/View');
21-
2221
const invariant = require('invariant');
2322

23+
import type {AccessibilityState} from './View/ViewAccessibility';
2424
import type {PressEvent} from '../Types/CoreEventTypes';
2525
import type {ColorValue} from '../StyleSheet/StyleSheet';
2626

@@ -134,6 +134,11 @@ type ButtonProps = $ReadOnly<{|
134134
Used to locate this view in end-to-end tests.
135135
*/
136136
testID?: ?string,
137+
138+
/**
139+
* Accessibility props.
140+
*/
141+
accessibilityState?: ?AccessibilityState,
137142
|}>;
138143

139144
/**
@@ -261,7 +266,6 @@ class Button extends React.Component<ButtonProps> {
261266
nextFocusLeft,
262267
nextFocusRight,
263268
nextFocusUp,
264-
disabled,
265269
testID,
266270
} = this.props;
267271
const buttonStyles = [styles.button];
@@ -273,12 +277,22 @@ class Button extends React.Component<ButtonProps> {
273277
buttonStyles.push({backgroundColor: color});
274278
}
275279
}
276-
const accessibilityState = {};
280+
281+
const disabled =
282+
this.props.disabled != null
283+
? this.props.disabled
284+
: this.props.accessibilityState?.disabled;
285+
286+
const accessibilityState =
287+
disabled !== this.props.accessibilityState?.disabled
288+
? {...this.props.accessibilityState, disabled}
289+
: this.props.accessibilityState;
290+
277291
if (disabled) {
278292
buttonStyles.push(styles.buttonDisabled);
279293
textStyles.push(styles.textDisabled);
280-
accessibilityState.disabled = true;
281294
}
295+
282296
invariant(
283297
typeof title === 'string',
284298
'The title prop of a Button must be a string',
@@ -287,6 +301,7 @@ class Button extends React.Component<ButtonProps> {
287301
Platform.OS === 'android' ? title.toUpperCase() : title;
288302
const Touchable =
289303
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
304+
290305
return (
291306
<Touchable
292307
accessibilityLabel={accessibilityLabel}

packages/rn-tester/js/examples/Button/ButtonExample.js

+23
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@ exports.examples = [
174174
);
175175
},
176176
},
177+
{
178+
title: 'Button with accessibilityState={{disabled: true}}',
179+
description: ('Note: This prop will announce on TalkBack that the button is disabled. ' +
180+
'The "disabled" prop has higher precedence on the state of the component': string),
181+
render: function(): React.Node {
182+
return (
183+
<RNTesterThemeContext.Consumer>
184+
{theme => {
185+
return (
186+
<Button
187+
accessibilityState={{disabled: true}}
188+
onPress={() => onButtonPress('submitted')}
189+
testID="accessibilityState_button"
190+
color={theme.LinkColor}
191+
title="Submit Application"
192+
accessibilityLabel="Press to submit your application!"
193+
/>
194+
);
195+
}}
196+
</RNTesterThemeContext.Consumer>
197+
);
198+
},
199+
},
177200
];
178201

179202
const styles = StyleSheet.create({

0 commit comments

Comments
 (0)