Skip to content

Commit 3484b2f

Browse files
committed
fix(#8328): Field announce error message for focused field
1 parent 9786f69 commit 3484b2f

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

packages/@react-spectrum/inlinealert/stories/InlineAlert.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ export const Dynamic = {
5555
render: (args) => <DynamicExample {...args} />
5656
};
5757

58+
export const DynamicWithAriaLivePolite = {
59+
render: (args) => <DynamicExample aria-live="polite" autoFocus={false} {...args} />
60+
};
61+
5862
function DynamicExample(args) {
5963
let [shown, setShown] = useState(false);
6064

6165
return (
6266
<>
6367
<Button variant="primary" onPress={() => setShown(!shown)}>{shown ? 'Hide Alert' : 'Show Alert'}</Button>
6468
{shown &&
65-
<InlineAlert {...args} autoFocus>
69+
<InlineAlert autoFocus {...args}>
6670
<Heading>{args.title}</Heading>
6771
<Content>{args.content}</Content>
6872
</InlineAlert>

packages/@react-spectrum/label/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"dependencies": {
4343
"@react-aria/i18n": "^3.12.9",
44+
"@react-aria/live-announcer": "^3.4.2",
4445
"@react-aria/utils": "^3.29.0",
4546
"@react-spectrum/form": "^3.7.15",
4647
"@react-spectrum/layout": "^3.6.15",

packages/@react-spectrum/label/src/Field.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
12+
import {announce} from '@react-aria/live-announcer';
1313
import {classNames, SlotProvider, useStyleProps} from '@react-spectrum/utils';
1414
import {Flex} from '@react-spectrum/layout';
15+
import {getActiveElement, mergeProps, useId} from '@react-aria/utils';
1516
import {HelpText} from './HelpText';
1617
import {Label} from './Label';
1718
import {LabelPosition, RefObject} from '@react-types/shared';
1819
import labelStyles from '@adobe/spectrum-css-temp/components/fieldlabel/vars.css';
19-
import {mergeProps, useId} from '@react-aria/utils';
2020
import React, {ReactNode, Ref} from 'react';
2121
import {SpectrumFieldProps} from '@react-types/label';
2222
import {useFormProps} from '@react-spectrum/form';
@@ -64,9 +64,19 @@ export const Field = React.forwardRef(function Field(props: SpectrumFieldProps,
6464
} else {
6565
errorMessageString = errorMessage;
6666
}
67-
let hasHelpText = !!description || errorMessageString && (isInvalid || validationState === 'invalid');
67+
let hasErrorMessage = !!errorMessageString && (isInvalid || validationState === 'invalid');
68+
let hasHelpText = !!description || hasErrorMessage;
6869
let contextualHelpId = useId();
6970

71+
React.useEffect(() => {
72+
if (hasErrorMessage &&
73+
(ref as RefObject<HTMLElement>)?.current?.contains(getActiveElement()) &&
74+
typeof errorMessageString === 'string' &&
75+
errorMessageString.length > 0) {
76+
announce(errorMessageString, 'polite');
77+
}
78+
}, [errorMessageString, hasErrorMessage, ref]);
79+
7080
let fallbackLabelPropsId = useId();
7181
if (label && contextualHelp && !labelProps.id) {
7282
labelProps.id = fallbackLabelPropsId;

0 commit comments

Comments
 (0)