|
9 | 9 | * OF ANY KIND, either express or implied. See the License for the specific language
|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 |
| - |
| 12 | +import {announce} from '@react-aria/live-announcer'; |
13 | 13 | import {classNames, SlotProvider, useStyleProps} from '@react-spectrum/utils';
|
14 | 14 | import {Flex} from '@react-spectrum/layout';
|
| 15 | +import {getActiveElement, mergeProps, useId} from '@react-aria/utils'; |
15 | 16 | import {HelpText} from './HelpText';
|
16 | 17 | import {Label} from './Label';
|
17 | 18 | import {LabelPosition, RefObject} from '@react-types/shared';
|
18 | 19 | import labelStyles from '@adobe/spectrum-css-temp/components/fieldlabel/vars.css';
|
19 |
| -import {mergeProps, useId} from '@react-aria/utils'; |
20 | 20 | import React, {ReactNode, Ref} from 'react';
|
21 | 21 | import {SpectrumFieldProps} from '@react-types/label';
|
22 | 22 | import {useFormProps} from '@react-spectrum/form';
|
@@ -64,9 +64,19 @@ export const Field = React.forwardRef(function Field(props: SpectrumFieldProps,
|
64 | 64 | } else {
|
65 | 65 | errorMessageString = errorMessage;
|
66 | 66 | }
|
67 |
| - let hasHelpText = !!description || errorMessageString && (isInvalid || validationState === 'invalid'); |
| 67 | + let hasErrorMessage = !!errorMessageString && (isInvalid || validationState === 'invalid'); |
| 68 | + let hasHelpText = !!description || hasErrorMessage; |
68 | 69 | let contextualHelpId = useId();
|
69 | 70 |
|
| 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 | + |
70 | 80 | let fallbackLabelPropsId = useId();
|
71 | 81 | if (label && contextualHelp && !labelProps.id) {
|
72 | 82 | labelProps.id = fallbackLabelPropsId;
|
|
0 commit comments