Skip to content

Commit 7938a0d

Browse files
Fedo Hagge-KubatKolja
authored andcommitted
fix(a11y): WCAG 3.3.1 — deduplicate error ids and live-regions, widen ModalFormField traversal
Address Bito + CodeAnt review findings on PR #39245: ControlHeader / NumberControl / TextControl - ControlHeader accepts an external errorId so wrappers share one DOM id per control. Previously both ControlHeader and the input wrapper rendered id="${name}-error" + role="alert", producing duplicate ids and duplicate screen-reader announcements for the same validation error. - NumberControl now computes a stable input id via useId when name is missing, so id="undefined" never hits the DOM. - The visible inline red error below the input keeps the text but drops its id and role="alert" — the live-region inside ControlHeader is the single source of truth for assistive tech. ModalFormField - Injecting aria-invalid / aria-describedby on the first child missed FormItem-wrapped inputs, leaving the real interactive element unlinked to the error. The traversal now hops through a single-child wrapper to decorate the wrapped input. SSHTunnelForm - Required-field check treated whitespace-only input as valid, letting users bypass validation with a space. The check now trims before the emptiness test. Already addressed in prior commit 3f2a60f on this branch: - AlertReportModal name/owners error guards against currentAlert loading - ControlHeader useId fallback for missing name Not changed: - RoleNameField — Ant Design FormItem with rules={[{required:true}]} already sets aria-invalid on the input via FieldContext when validation fails; the explicit render-prop pattern the bot suggested would nest FormItems for no behavior gain.
1 parent f5aa012 commit 7938a0d

5 files changed

Lines changed: 51 additions & 19 deletions

File tree

‎superset-frontend/src/components/Modal/ModalFormField.tsx‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,33 @@ export function ModalFormField({
132132
const errorId = error ? `${uniqueId}-error` : undefined;
133133

134134
// Clone the first child element to inject aria-invalid and aria-describedby
135+
// on the real input. Many call sites wrap the input in a FormItem/Row so
136+
// the ARIA attrs must hop through wrappers to reach the actual interactive
137+
// element, otherwise screen readers never learn about the error.
138+
const applyAria = (element: React.ReactElement<any>): React.ReactElement<any> => {
139+
const ariaProps = {
140+
'aria-invalid': true,
141+
'aria-describedby': errorId,
142+
};
143+
const wrappedChild = element.props?.children;
144+
if (
145+
isValidElement(wrappedChild) &&
146+
Children.count(wrappedChild) === 1
147+
) {
148+
return cloneElement(element, {
149+
children: cloneElement(
150+
wrappedChild as React.ReactElement<any>,
151+
ariaProps,
152+
),
153+
});
154+
}
155+
return cloneElement(element, ariaProps);
156+
};
157+
135158
const enhancedChildren = error
136159
? Children.map(children, (child, index) => {
137160
if (index === 0 && isValidElement(child)) {
138-
return cloneElement(child as React.ReactElement<any>, {
139-
'aria-invalid': true,
140-
'aria-describedby': errorId,
141-
});
161+
return applyAria(child as React.ReactElement<any>);
142162
}
143163
return child;
144164
})

‎superset-frontend/src/explore/components/ControlHeader.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export type ControlHeaderProps = {
3737
tooltipOnClick?: () => void;
3838
warning?: string;
3939
danger?: string;
40+
// WCAG 3.3.1: error container ID shared with the input's aria-describedby.
41+
// Passed in by wrappers like NumberControl/TextControl so there is exactly
42+
// one live-region and one DOM id per control (no duplicate ids/alerts).
43+
errorId?: string;
4044
// Allow extra props from control spread patterns (e.g. {...this.props})
4145
[key: string]: unknown;
4246
};
@@ -70,10 +74,11 @@ const ControlHeader: FC<ControlHeaderProps> = ({
7074
tooltipOnClick = () => {},
7175
warning,
7276
danger,
77+
errorId: providedErrorId,
7378
}) => {
7479
const theme = useTheme();
7580
const uniqueId = useId();
76-
const errorId = `${name || uniqueId}-error`;
81+
const errorId = providedErrorId ?? `${name || uniqueId}-error`;
7782

7883
if (!label) {
7984
return null;

‎superset-frontend/src/explore/components/controls/NumberControl/index.tsx‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import { useRef } from 'react';
19+
import { useId, useRef } from 'react';
2020
import { styled } from '@apache-superset/core/theme';
2121
import { InputNumber } from '@superset-ui/core/components/Input';
2222
import ControlHeader, { ControlHeaderProps } from '../../ControlHeader';
@@ -62,6 +62,8 @@ export default function NumberControl({
6262
...rest
6363
}: NumberControlProps) {
6464
const pendingValueRef = useRef<NumberValueType>(value);
65+
const uniqueId = useId();
66+
const inputId = rest.name || uniqueId;
6567

6668
const handleChange = (val: string | number | null) => {
6769
pendingValueRef.current = parseValue(val);
@@ -78,13 +80,15 @@ export default function NumberControl({
7880

7981
const hasErrors =
8082
rest.validationErrors && rest.validationErrors.length > 0;
81-
const errorId = hasErrors
82-
? `${rest.name || 'number-control'}-error`
83-
: undefined;
83+
// WCAG 3.3.1: the visually hidden live-region inside ControlHeader carries
84+
// the id and role="alert"; this wrapper only needs to point the input at
85+
// that same id via aria-describedby. Sharing one id per control avoids
86+
// duplicate DOM ids and duplicate screen-reader announcements.
87+
const errorId = hasErrors ? `${inputId}-error` : undefined;
8488

8589
return (
8690
<FullWidthDiv>
87-
<ControlHeader {...rest} />
91+
<ControlHeader {...rest} errorId={errorId} />
8892
<FullWidthInputNumber
8993
min={min}
9094
max={max}
@@ -98,12 +102,10 @@ export default function NumberControl({
98102
aria-label={rest.label}
99103
aria-invalid={hasErrors || undefined}
100104
aria-describedby={errorId}
101-
id={rest.name}
105+
id={inputId}
102106
/>
103107
{hasErrors && (
104108
<span
105-
id={errorId}
106-
role="alert"
107109
style={{ color: 'red', fontSize: '12px', display: 'block', marginTop: '4px' }}
108110
>
109111
{rest.validationErrors!.join('. ')}

‎superset-frontend/src/explore/components/controls/TextControl/index.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ export default class TextControl<
106106
}
107107
const hasErrors =
108108
this.props.validationErrors && this.props.validationErrors.length > 0;
109-
const errorId = hasErrors ? `${this.props.controlId || this.props.name || 'text'}-error` : undefined;
109+
const inputId = this.props.controlId || this.props.name;
110+
// WCAG 3.3.1: share a single error container id with ControlHeader so
111+
// the input's aria-describedby resolves to one live-region, not two.
112+
const errorId = hasErrors && inputId ? `${inputId}-error` : undefined;
110113
return (
111114
<div>
112-
<ControlHeader {...this.props} />
115+
<ControlHeader {...this.props} errorId={errorId} />
113116
<Input
114117
type="text"
115118
data-test="inline-name"
@@ -121,12 +124,10 @@ export default class TextControl<
121124
aria-label={this.props.label}
122125
aria-invalid={hasErrors || undefined}
123126
aria-describedby={errorId}
124-
id={this.props.controlId || this.props.name}
127+
id={inputId}
125128
/>
126129
{hasErrors && (
127130
<span
128-
id={errorId}
129-
role="alert"
130131
style={{ color: 'red', fontSize: '12px', display: 'block', marginTop: '4px' }}
131132
>
132133
{this.props.validationErrors!.join('. ')}

‎superset-frontend/src/features/databases/DatabaseModal/SSHTunnelForm.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ const SSHTunnelForm = ({
6666
const markBlurred = (field: string) =>
6767
setBlurred(prev => ({ ...prev, [field]: true }));
6868
const fieldError = (field: string, value: string | number | undefined) =>
69-
blurred[field] && !value;
69+
// WCAG 3.3.1: treat whitespace-only strings as empty so users cannot
70+
// bypass required-field validation with a space character.
71+
blurred[field] &&
72+
(value === undefined ||
73+
(typeof value === 'string' && value.trim().length === 0));
7074

7175
return (
7276
<Form>

0 commit comments

Comments
 (0)