Skip to content

Commit ca5e3b1

Browse files
kaciebfacebook-github-bot
authored andcommitted
Fix sendAccessibilityEvent_unstable Example in RNTester
Summary: # First issue - incorrect ref In this example, `AccessibilityInfo.setAccessibilityFocus_unstable` is being called on the Button ref. This fails because Button is not a HostComponent and does not accept a forwarded ref. Since the button needs to be focused in order to click on it, I don't think the intention of this example actually makes sense. Since even if it worked, it would just reset the focus in the same place. Instead, I alter this to set accessibility focus on the preceding Text element, which makes it more clear that setAccessibilityFocus is working. # Second Issue - focus after closing Alert doesn't work I am not sure why this is the case, but removing the alert causes focus to work correctly. i'm guessing the set focus command is conflicting with Alert's default resetting focus behavior. # Minor Fix I also quickly cleaned this up to be a function component because class components make refs more confusing (to me). Changelog: [Genera] Fix sendAccessibilityEvent_unstable Example in RNTester Reviewed By: p-sun Differential Revision: D35725018 fbshipit-source-id: f5a1dbbcf2635f038c41db9ef2a0b31389d2c745
1 parent 8ae9c2c commit ca5e3b1

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -951,37 +951,24 @@ class AnnounceForAccessibility extends React.Component<{}> {
951951
}
952952
}
953953

954-
class SetAccessibilityFocusExample extends React.Component<{}> {
955-
render(): React.Node {
956-
const myRef: {current: React.ElementRef<any> | null} = createRef();
957-
958-
const onClose = () => {
959-
if (myRef && myRef.current) {
960-
AccessibilityInfo.sendAccessibilityEvent_unstable(
961-
myRef.current,
962-
'focus',
963-
);
964-
}
965-
};
954+
function SetAccessibilityFocusExample(props: {}): React.Node {
955+
const myRef = React.useRef<?React.ElementRef<typeof Text>>(null);
966956

967-
return (
968-
<View>
969-
<Text>SetAccessibilityFocus on native element</Text>
970-
<Button
971-
ref={myRef}
972-
title={'Click'}
973-
onPress={() => {
974-
Alert.alert(
975-
'Set Accessibility Focus',
976-
'Press okay to proceed',
977-
[{text: 'Okay', onPress: onClose}],
978-
{cancelable: true},
979-
);
980-
}}
981-
/>
982-
</View>
983-
);
984-
}
957+
const onPress = () => {
958+
if (myRef && myRef.current) {
959+
AccessibilityInfo.sendAccessibilityEvent_unstable(myRef.current, 'focus');
960+
}
961+
};
962+
963+
return (
964+
<View>
965+
<Text ref={myRef}>
966+
SetAccessibilityFocus on native element. This should get focus after
967+
clicking the button!
968+
</Text>
969+
<Button title={'Click'} onPress={onPress} />
970+
</View>
971+
);
985972
}
986973

987974
class EnabledExamples extends React.Component<{}> {

0 commit comments

Comments
 (0)