From 439759a1dae78202d5c22320d03d34c6ce9e3740 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 14 Apr 2023 15:15:26 +0100 Subject: [PATCH] Relate field validation tooltip via aria-describedby (#10522) * Relate field validation tooltip via aria-describedby * Iterate --- src/components/views/elements/Field.tsx | 49 ++++++++++++++----------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/components/views/elements/Field.tsx b/src/components/views/elements/Field.tsx index 96151a2e965..a453ffbee54 100644 --- a/src/components/views/elements/Field.tsx +++ b/src/components/views/elements/Field.tsx @@ -251,6 +251,34 @@ export default class Field extends React.PureComponent { this.inputRef = inputRef || React.createRef(); + // Handle displaying feedback on validity + let fieldTooltip: JSX.Element | undefined; + if (tooltipContent || this.state.feedback) { + const tooltipId = `${this.id}_tooltip`; + const visible = (this.state.focused && forceTooltipVisible) || this.state.feedbackVisible; + if (visible) { + inputProps["aria-describedby"] = tooltipId; + } + + let role: React.AriaRole; + if (tooltipContent) { + role = "tooltip"; + } else { + role = this.state.valid ? "status" : "alert"; + } + + fieldTooltip = ( + + ); + } + inputProps.placeholder = inputProps.placeholder ?? inputProps.label; inputProps.id = this.id; // this overwrites the id from props @@ -287,27 +315,6 @@ export default class Field extends React.PureComponent { mx_Field_invalid: hasValidationFlag ? !forceValidity : onValidate && this.state.valid === false, }); - // Handle displaying feedback on validity - let fieldTooltip: JSX.Element | undefined; - if (tooltipContent || this.state.feedback) { - let role: React.AriaRole; - if (tooltipContent) { - role = "tooltip"; - } else { - role = this.state.valid ? "status" : "alert"; - } - - fieldTooltip = ( - - ); - } - return (
{prefixContainer}