Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(RadioList, Switch): required inputs take their invalid state from status (#9042)

Cindy Zhanga3eaa6d3b754
diff --git a/.changeset/required-native-invalid.md b/.changeset/required-native-invalid.md
new file mode 100644
index 0000000000..bd27777dfc
--- /dev/null
+++ b/.changeset/required-native-invalid.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] RadioList and Switch: a required field no longer reads as "invalid entry" to screen readers before the user has answered. Each required radio and a required switch take their invalid state from `status`.
+
+@cixzhang
diff --git a/packages/core/src/RadioList/RadioList.spec.md b/packages/core/src/RadioList/RadioList.spec.md
index 9c5afed717..2468684f03 100644
--- a/packages/core/src/RadioList/RadioList.spec.md
+++ b/packages/core/src/RadioList/RadioList.spec.md
@@ -120,7 +120,7 @@ Consumer migration instructions belong in consumer docs and release notes.
| FR3 | Selecting an enabled option calls `onChange(value)` once; RadioList holds no selection of its own. | Current tests |
| FR4 | With an empty `value`, keyboard focus entering the group lands on the first radio that is not natively disabled (in a group disabled with a reason, that is its first `aria-disabled` radio, keeping the enabled focus order under `spec:AST-085` FR1), or stays on the last one when entering backward. Focus moving between radios inside the group is not redirected. With a selection, the selected radio is the Tab stop. Arrow keys follow the browser's native radio behavior, which stops on `aria-disabled` radios. | WAI-ARIA radio group pattern; current tests |
| FR5 | `isDisabled` MUST disable every item: each radio stays focusable with `aria-disabled="true"`, is detached from the form so it does not submit, and refuses selection (`spec:AST-085` FR1–FR3, FR7). With `disabledMessage`, a reason tooltip also opens on group hover and keyboard focus, and the reason MUST describe each focusable radio (`spec:AST-085` FR5, FR7). Implementation gap: the reason describes only the radiogroup, so a focused radio is not described by it; and without a `disabledMessage`, each radio gets native `disabled` instead, which removes it from the tab order, and a radio focused when the group becomes disabled loses focus (`spec:AST-085` FR4). | `spec:AST-085` FR1–FR5, FR7; current tests |
-| FR6 | `status.type === 'error'` sets `aria-invalid` on the radiogroup. The radiogroup exposes the effective required state (including a form-level default) with `aria-required`, while each radio's native `required` follows only the explicit `isRequired`. | Current source and tests |
+| FR6 | `status.type === 'error'` sets `aria-invalid` on the radiogroup. The radiogroup exposes the effective required state (including a form-level default) with `aria-required`, while each radio's native `required` follows only the explicit `isRequired`; a natively required radio's `aria-invalid` follows the same status, so the browser's own invalid state never shows before an error. | Current source and tests |
| FR7 | The radiogroup lays options in a column with a small gap, or a row with a wider gap when horizontal, and passes `size` to every item. | Current source and tests |
| FR8 | Base props (`ref`, `id`, `aria-*`, `data-*`, `hidden`, `inert`, `dir`, DOM handlers) reach the radiogroup; a caller `onFocus` runs after FR4's entry handling. `container*` reaches the Field box where one renders (in a `horizontal-labels` FormLayout none renders, and `container*` has no effect and warns), and `control*` the radiogroup; root `xstyle`, `className`, and `style` are not accepted. | `family:input-fields` FR10; current tests |
| FR9 | The radiogroup carries `astryx-radio-list` and reflects `data-orientation` and `data-size`. | `architecture:component-theming-surface`; current tests |
diff --git a/packages/core/src/RadioList/RadioList.test.tsx b/packages/core/src/RadioList/RadioList.test.tsx
index 0c1956141b..1fdabb7f2e 100644
--- a/packages/core/src/RadioList/RadioList.test.tsx
+++ b/packages/core/src/RadioList/RadioList.test.tsx
@@ -165,6 +165,34 @@ describe('RadioList', () => {
expect(screen.getByText(/Required/)).toBeInTheDocument();
});
+ it('keeps required radios valid until the group shows an error', () => {
+ const {rerender} = render(
+ <RadioList label="Preference" value="" onChange={() => {}} isRequired>
+ <RadioListItem label="Option A" value="a" />
+ <RadioListItem label="Option B" value="b" />
+ </RadioList>,
+ );
+ // Native required alone makes Chromium report every radio invalid.
+ for (const radio of screen.getAllByRole('radio')) {
+ expect(radio).toBeRequired();
+ expect(radio).toHaveAttribute('aria-invalid', 'false');
+ }
+ rerender(
+ <RadioList
+ label="Preference"
+ value=""
+ onChange={() => {}}
+ isRequired
+ status={{type: 'error', message: 'Pick one'}}>
+ <RadioListItem label="Option A" value="a" />
+ <RadioListItem label="Option B" value="b" />
+ </RadioList>,
+ );
+ for (const radio of screen.getAllByRole('radio')) {
+ expect(radio).toHaveAttribute('aria-invalid', 'true');
+ }
+ });
+
it('shows Optional indicator when isOptional is true', () => {
render(
<RadioList label="Preference" value="" onChange={() => {}} isOptional>
diff --git a/packages/core/src/RadioList/RadioListItem.tsx b/packages/core/src/RadioList/RadioListItem.tsx
index 683f7dbee9..6c881b3d31 100644
--- a/packages/core/src/RadioList/RadioListItem.tsx
+++ b/packages/core/src/RadioList/RadioListItem.tsx
@@ -326,6 +326,12 @@ export function RadioListItem({
// from submission, matching a natively disabled control.
form={keepsFocusableForMessage ? '' : undefined}
required={context.isRequired}
+ // Chromium reports an unanswered native-required radio invalid before
+ // any interaction; the invalid state follows the group's status
+ // instead (RadioList FR6).
+ aria-invalid={
+ context.isRequired ? context.status?.type === 'error' : undefined
+ }
onChange={() => {
if (isDisabled) {
return;
diff --git a/packages/core/src/Switch/Switch.spec.md b/packages/core/src/Switch/Switch.spec.md
index c7d59a4c1c..685d1ca151 100644
--- a/packages/core/src/Switch/Switch.spec.md
+++ b/packages/core/src/Switch/Switch.spec.md
@@ -123,7 +123,7 @@ Consumer migration instructions belong in consumer docs and release notes.
| FR5 | A change calls `onChange(checked, event)`; unless that call prevents default, `changeAction(checked, event)` runs in a transition and the switch shows the proposed state while the Action is pending, and `value` once it settles. | Current tests |
| FR6 | While `isLoading`, or while the shown state differs from `value`, the input sets `aria-busy`, ignores pointer input, refuses changes, keeps focus, and the switch announces its busy start once per busy period through the shared channel. | `spec:AST-086` FR8; current tests |
| FR7 | Disabled: `aria-disabled="true"` on a focusable input, no `name`, detached from its form (so it neither submits nor blocks submission when required), no change, no caller `onClick` on the field, no hover or press paint, and a half-opacity track. With `disabledMessage`, a reason tooltip opens on row hover and keyboard focus and describes the input. Implementation gap: caller DOM handlers other than `onClick`, such as `onKeyDown`, still run on the field root while disabled (`spec:AST-085` FR2). | `spec:AST-085` FR1–FR3, FR5; current tests |
-| FR8 | Enabled, the input submits `on` under `htmlName` when on and nothing when off; `isRequired` sets native `required`, while `aria-required` follows the effective required state. | Current tests |
+| FR8 | Enabled, the input submits `on` under `htmlName` when on and nothing when off; `isRequired` sets native `required`, while `aria-required` follows the effective required state; `aria-invalid` follows `status` (`false` on a required switch without an error), so the browser's own invalid state never shows before an error. | Current tests |
| FR9 | Keyboard focus paints the shared focus outline on the track. Hovering the enabled row tints the track; pressing paints the pressed overlay on both, applying `design:user-states` DR1 and DR2; a touch press fades out from the row. Track color transitions are instant under reduced motion. | `architecture:interaction-modality` INV1; `design:user-states` DR1, DR2 for the pressed overlay; `design:reduced-motion`; current tests |
| FR10 | The thumb travels toward the inline end when on, mirrored under RTL, and moves without a transition under reduced motion (`design:reduced-motion` DR1). On coarse pointers the input's hit area is at least 24 by 24 CSS pixels (WCAG 2.5.8) and follows the inline direction. Under `spec:AST-094` FR42 a coarse-pointer target MUST reach 44 CSS pixels through an invisible hit area where neighbors allow. Implementation gap: the input's hit area stops at 24 by 24. | WCAG 2.5.8; current tests; `design:reduced-motion` DR1; `spec:AST-094` FR42 |
| FR11 | Under forced colors, on and off stay distinguishable (Highlight track when on), and the hover tint does not hide the thumb. | WCAG 1.4.11; current tests |
diff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsx
index bd0db9af3f..4e2c85a98f 100644
--- a/packages/core/src/Switch/Switch.test.tsx
+++ b/packages/core/src/Switch/Switch.test.tsx
@@ -429,7 +429,9 @@ describe('Switch', () => {
continue;
}
for (const node of record.addedNodes) {
- if (node.textContent) {written.push(node.textContent);}
+ if (node.textContent) {
+ written.push(node.textContent);
+ }
}
}
});
@@ -563,6 +565,24 @@ describe('Switch', () => {
expect(screen.getByRole('switch')).toBeRequired();
});
+ it('keeps a required switch valid until status shows an error', () => {
+ const {rerender} = render(
+ <Switch label="Accept" value={false} onChange={() => {}} isRequired />,
+ );
+ // Native required alone makes Chromium report the switch invalid.
+ expect(screen.getByRole('switch')).toHaveAttribute('aria-invalid', 'false');
+ rerender(
+ <Switch
+ label="Accept"
+ value={false}
+ onChange={() => {}}
+ isRequired
+ status={{type: 'error', message: 'Required'}}
+ />,
+ );
+ expect(screen.getByRole('switch')).toHaveAttribute('aria-invalid', 'true');
+ });
+
describe('disabledMessage', () => {
const h = {hidden: true} as const;
diff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsx
index d3249eb9b3..927a57dab3 100644
--- a/packages/core/src/Switch/Switch.tsx
+++ b/packages/core/src/Switch/Switch.tsx
@@ -679,7 +679,11 @@ export function Switch({
// than the field wrapper via `...rest`.
aria-labelledby={ariaLabelledByProp}
aria-describedby={ariaDescribedBy}
- aria-invalid={status?.type === 'error' ? true : undefined}
+ // Chromium reports an unchecked native-required checkbox invalid before
+ // any interaction; the invalid state follows status instead.
+ aria-invalid={
+ status?.type === 'error' ? true : isRequired ? false : undefined
+ }
aria-busy={isBusy || undefined}
{...stylex.props(
styles.input,