Skip to content

Commit

Permalink
fix(react-field): Add screenreader announcement for `validationMessag…
Browse files Browse the repository at this point in the history
…e` if `validationState="warning"` (#33175)
  • Loading branch information
emmayjiang authored Oct 31, 2024
1 parent fbd3cfd commit c262869
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(react-field): Add screenreader announcement for validationMessage if validationState=\"warning\"",
"packageName": "@fluentui/react-field",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('Field', () => {
it.each([
[undefined, 'alert'], // defaults to error
['error', 'alert'],
['warning', null],
['warning', 'alert'],
['success', null],
['none', null],
] as const)('if validationState is %s, sets role to %s on the validationMessage', (validationState, role) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export type FieldProps = Omit<ComponentProps<FieldSlots>, 'children'> & {
* announced by screen readers. Additionally, the control inside the field has `aria-invalid` set, which adds a
* red border to some field components (such as `Input`).
* * success: The validation message has a green checkmark icon and gray text.
* * warning: The validation message has a yellow exclamation icon and gray text.
* * warning: The validation message has a yellow exclamation icon and gray text, with `role="alert"` so it is
* announced by screen readers.
* * none: The validation message has no icon and gray text.
*
* @default error when validationMessage is set; none otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export const useField_unstable = (props: FieldProps, ref: React.Ref<HTMLDivEleme
elementType: Label,
});
const validationMessage = slot.optional(props.validationMessage, {
defaultProps: { id: baseId + '__validationMessage', role: validationState === 'error' ? 'alert' : undefined },
defaultProps: {
id: baseId + '__validationMessage',
role: validationState === 'error' || validationState === 'warning' ? 'alert' : undefined,
},
elementType: 'div',
});
const hint = slot.optional(props.hint, { defaultProps: { id: baseId + '__hint' }, elementType: 'div' });
Expand All @@ -51,6 +54,7 @@ export const useField_unstable = (props: FieldProps, ref: React.Ref<HTMLDivEleme
defaultProps: { children: defaultIcon },
elementType: 'span',
});

return {
children,
generatedControlId,
Expand Down

0 comments on commit c262869

Please sign in to comment.