Skip to content

Commit bae1bb6

Browse files
authored
Make Button respect accessibilityRole passed in props (#2358)
e.g. I'd like to be able to use things like menuitem or similar, which indicates a button-like control in a list of similar controls. Testing: * I see the expected role now with tools, e.g. Accessibility Insights.
1 parent f8ba933 commit bae1bb6

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

apps/E2E/src/Button/consts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const BUTTON_ACCESSIBILITY_LABEL = 'E2E testing Button accessibility labe
1717

1818
/* Test Button 2 */
1919
export const BUTTON_NO_A11Y_LABEL_COMPONENT = 'Button_No_A11y_label_Component';
20-
export const BUTTON_TEST_COMPONENT_LABEL = 'Test Button2 - No Accessibility Label'; // A component on each specific test page
20+
export const BUTTON_TEST_COMPONENT_LABEL = 'Test Button2 - No Accessibility Label + Custom Role'; // A component on each specific test page
2121

2222
/* Test Button 3 */
2323
export const BUTTON_PRESS_TEST_COMPONENT = 'Button_Press_Test_Component';

apps/fluent-tester/src/TestComponents/Button/E2EButtonTest.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const E2EButtonExperimentalTest: React.FunctionComponent = () => {
5151
<Button testID={BUTTON_TEST_COMPONENT} onClick={onClick} accessibilityLabel={BUTTON_ACCESSIBILITY_LABEL}>
5252
This is a button for E2E testing
5353
</Button>
54-
<Button testID={BUTTON_NO_A11Y_LABEL_COMPONENT} onClick={onClick}>
54+
<Button testID={BUTTON_NO_A11Y_LABEL_COMPONENT} onClick={onClick} accessibilityRole="menuitem">
5555
{BUTTON_TEST_COMPONENT_LABEL}
5656
</Button>
5757
<Button
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Make Button respect accessibilityRole passed in props",
4+
"packageName": "@fluentui-react-native/button",
5+
"email": "email not defined",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Rename a test button to reflect new actual usage",
4+
"packageName": "@fluentui-react-native/e2e-testing",
5+
"email": "email not defined",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Test case for Button with accessibilityRole",
4+
"packageName": "@fluentui-react-native/tester",
5+
"email": "email not defined",
6+
"dependentChangeType": "patch"
7+
}

packages/components/Button/src/useButton.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ButtonProps, ButtonInfo } from './Button.types';
44

55
export const useButton = (props: ButtonProps): ButtonInfo => {
66
const defaultComponentRef = React.useRef(null);
7-
const { onClick, componentRef = defaultComponentRef, disabled, loading, enableFocusRing, focusable, ...rest } = props;
7+
const { onClick, accessibilityRole, componentRef = defaultComponentRef, disabled, loading, enableFocusRing, focusable, ...rest } = props;
88
const isDisabled = !!disabled || !!loading;
99
// GH #1336: Set focusRef to null if button is disabled to prevent getting keyboard focus.
1010
const focusRef = isDisabled ? null : componentRef;
@@ -24,7 +24,7 @@ export const useButton = (props: ButtonProps): ButtonInfo => {
2424
*/
2525
...(isDisabled && { disabled: isDisabled }),
2626
accessible: true,
27-
accessibilityRole: 'button',
27+
accessibilityRole: accessibilityRole || 'button',
2828
onAccessibilityTap: props.onAccessibilityTap || (!hasTogglePattern ? props.onClick : undefined),
2929
accessibilityLabel: props.accessibilityLabel,
3030
enableFocusRing: enableFocusRing ?? true,

0 commit comments

Comments
 (0)