Skip to content

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

TypeScript

fix(FileInput, Slider): convey required under FormLayout defaultOptionality (#9053)

Cindy Zhangafcea15520cb
diff --git a/.changeset/fileinput-slider-form-required.md b/.changeset/fileinput-slider-form-required.md
new file mode 100644
index 0000000000..7fbd0b9d11
--- /dev/null
+++ b/.changeset/fileinput-slider-form-required.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Inside `<FormLayout defaultOptionality="required">`, FileInput and Slider now tell screen readers they are required even without their own `isRequired`, like the other fields. `isOptional` still opts a field out.
+
+@cixzhang
diff --git a/packages/core/src/FileInput/FileInput.test.tsx b/packages/core/src/FileInput/FileInput.test.tsx
index 29d73c9149..1e97fe8c4a 100644
--- a/packages/core/src/FileInput/FileInput.test.tsx
+++ b/packages/core/src/FileInput/FileInput.test.tsx
@@ -20,6 +20,7 @@ import {
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {FileInput} from './FileInput';
+import {FormLayout} from '../FormLayout';
import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';
import {InternationalizationProvider} from '../i18n';
import {Theme} from '../theme/Theme';
@@ -204,6 +205,26 @@ describe('FileInput', () => {
expect(trigger).not.toHaveAttribute('aria-required');
});
+ it('conveys required state under FormLayout defaultOptionality="required" unless isOptional (FormLayout FR5)', () => {
+ render(
+ <FormLayout defaultOptionality="required">
+ <FileInput label="Resume" value={null} onChange={() => {}} />
+ <FileInput
+ label="Cover letter"
+ isOptional
+ value={null}
+ onChange={() => {}}
+ />
+ </FormLayout>,
+ );
+ expect(
+ screen.getByRole('button', {name: 'Resume'}),
+ ).toHaveAccessibleDescription(/Required/);
+ expect(
+ screen.getByRole('button', {name: /Cover letter/}),
+ ).not.toHaveAccessibleDescription(/Required/);
+ });
+
it('combines required with the description in the accessible description', () => {
render(
<FileInput
diff --git a/packages/core/src/FileInput/FileInput.tsx b/packages/core/src/FileInput/FileInput.tsx
index 59bda7e73b..25a8c59502 100644
--- a/packages/core/src/FileInput/FileInput.tsx
+++ b/packages/core/src/FileInput/FileInput.tsx
@@ -68,6 +68,7 @@ import {useTranslator} from '../i18n';
import type {TranslatorFn} from '../i18n';
import {useMergedRefs} from '../hooks/useMergedRefs';
+import {useResolvedRequired} from '../hooks/useResolvedRequired';
function formatFileSize(bytes: number, t: TranslatorFn): string {
if (bytes < 1024) {
return t('@astryx.fileInput.fileSize.bytes', {size: bytes});
@@ -544,7 +545,9 @@ export function FileInput({
// at a visually hidden "Required" span — mirroring the Field label's visible
// indicator (where isOptional takes precedence) and the pattern Slider uses
// for its thumbs.
- const conveysRequired = isRequired && !isOptional;
+ // A surrounding FormLayout's defaultOptionality="required" makes an unmarked
+ // field required too (FormLayout FR5).
+ const conveysRequired = useResolvedRequired({isRequired, isOptional});
const ariaDescribedBy =
[
diff --git a/packages/core/src/FormLayout/FormLayout.spec.md b/packages/core/src/FormLayout/FormLayout.spec.md
index b2d01cc9f3..b2d752f836 100644
--- a/packages/core/src/FormLayout/FormLayout.spec.md
+++ b/packages/core/src/FormLayout/FormLayout.spec.md
@@ -63,12 +63,11 @@ This table is a review projection; the body below is authoritative.
## Current-state impact
-- **Required state lost for four controls (FR5, AR2).** Under
+- **Required state lost for two controls (FR5, AR2).** Under
`defaultOptionality="required"` the visible "Required" marker is hidden,
- but Typeahead and Tokenizer set no `aria-required`, and FileInput and
- Slider build their required description from `isRequired` alone. Screen
- reader users hear those fields as not required, while sighted users see
- the form-wide convention.
+ but Typeahead and Tokenizer set no `aria-required`. Screen reader users
+ hear those fields as not required, while sighted users see the form-wide
+ convention.
## Ownership boundary
@@ -101,7 +100,7 @@ This table is a review projection; the body below is authoritative.
| FR2 | `horizontal` places every direct child in one `1fr` grid column in the document's logical inline order, with a `--spacing-4` gap and no component-owned wrap rule. | Released source, tests, and stories |
| FR3 | `horizontal-labels` is a two-column grid (`auto 1fr`) with a `--spacing-3` row gap and `--spacing-4` column gap, items aligned to the start, so Field labels sit on the inline-start side of their controls; at a viewport width of 480px or below it becomes one flex column with a `--spacing-4` gap. OQ1 holds whether the collapsed label and control need a tighter pair gap. | Released source, Field composition, and tests |
| FR4 | FormLayout provides its direction and `defaultOptionality` to descendants in the same layer; a nested FormLayout replaces both for its own descendants, and content in a layer opened inside a FormLayout starts without them (`spec:AST-038` FR7, FR8). | Context source and tests |
-| FR5 | `defaultOptionality` suppresses only the visible marker that restates the form default; descendant controls still expose the resolved `aria-required`, while native `required` stays tied to an explicit field `isRequired`. Unset keeps each field's own indicators. Implementation gap: under `defaultOptionality="required"`, Typeahead and Tokenizer set no `aria-required`, and FileInput and Slider describe required state from `isRequired` alone, so those fields are announced as not required while the marker is hidden (Current-state impact). | Tests; `family:input-fields` ownership |
+| FR5 | `defaultOptionality` suppresses only the visible marker that restates the form default; descendant controls still expose the resolved `aria-required`, while native `required` stays tied to an explicit field `isRequired`. Unset keeps each field's own indicators. Implementation gap: under `defaultOptionality="required"`, Typeahead and Tokenizer set no `aria-required`, so those fields are announced as not required while the marker is hidden (Current-state impact). | Tests; `family:input-fields` ownership |
| FR6 | The root is a `div`. `ref`, DOM, data, ARIA, and event props, `xstyle`, class name, and inline style reach or compose on the root without replacing `data-direction` or the `form-layout` target. | `architecture:public-component-api/INV5–INV8`; source and tests |
### Allowed variation
@@ -135,7 +134,7 @@ This table is a review projection; the body below is authoritative.
## Accessibility contract
- **AR1 — Layout remains nonsemantic.** FormLayout renders a `<div>` and does not imply `<form>`, group, label, or submission semantics; callers provide those when their composition needs them.
-- **AR2 — Required state remains perceivable.** A form-wide optionality convention may remove redundant visible markers, but descendant controls preserve the resolved required state for assistive technology. Implementation gap: Typeahead, Tokenizer, FileInput, and Slider do not (FR5).
+- **AR2 — Required state remains perceivable.** A form-wide optionality convention may remove redundant visible markers, but descendant controls preserve the resolved required state for assistive technology. Implementation gap: Typeahead and Tokenizer do not (FR5).
- **AR3 — Logical order is preserved.** Every direction retains DOM order and logical inline flow; FormLayout does not create a separate keyboard order.
## Design relationships
@@ -191,7 +190,7 @@ The root target reflects the closed `direction` axis. It owns the outer arrangem
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------- |
| FR1–FR3 | `FormLayout.test.tsx` direction snapshots; `FormLayout.stories.tsx` play checks; stable visual baseline `core-formlayout--vertical` | Vertical, horizontal, horizontal-labels wide and narrow | A changed style class set fails the snapshots; a narrow horizontal-labels control narrower than the layout, or a wide label not before its control, fails the play checks; a changed vertical layout comes back changed for review. The story play guard also runs the AuditMatrix play right-to-left at 320px and the default width, where mirrored horizontal columns are asserted. Gap: no right-to-left check asserts that a horizontal-labels label sits on the inline-start side of its control. | `audit:FormLayout/behavior` |
| FR4, ORD1 | `FormLayout.test.tsx` context and nesting tests; `Layer/layerScopedContext.test.tsx` | Default, explicit, nested; content in a fixed Layer and an inline Dialog | A lost default or value, lost shadowing by an inner layout's explicit values, or content inside a layer that still reads the outer `defaultOptionality` fails. Gap: no test covers an inner layout with unset props (which supplies its own defaults) or `direction` across a layer boundary. | `audit:FormLayout/behavior` |
-| FR5, ORD3, AR2 | `FormLayout.test.tsx` optionality and `aria-required` tests | Required, optional, unset defaults; explicit exceptions | A redundant marker, a missing `aria-required`, or a native `required` from the default fails. Gap: no test covers Typeahead, Tokenizer, FileInput, or Slider under a `required` default, which do not expose it. | `audit:FormLayout/accessibility` |
+| FR5, ORD3, AR2 | `FormLayout.test.tsx` optionality and `aria-required` tests | Required, optional, unset defaults; explicit exceptions | A redundant marker, a missing `aria-required`, or a native `required` from the default fails. FileInput and Slider tests cover a `required` default. Gap: no test covers Typeahead or Tokenizer under a `required` default, which do not expose it. | `audit:FormLayout/accessibility` |
| FR6, ORD2 | `FormLayout.test.tsx` ref, passthrough, and owned-metadata tests; `themingTargets.test.ts` | Ref, attributes, colliding `data-direction` | A ref on a non-`div`, a dropped `id` or `role`, or a replaced `data-direction` fails. Gap: no test covers event props or the styling-input merge order. | `audit:FormLayout/api` |
| AR1, AR3 | `FormLayout.test.tsx` div and passthrough tests; `FormLayout.stories.tsx` play checks | Plain root; caller `role` | A non-`div` root or a dropped caller `role` fails. Horizontal columns that do not mirror right-to-left fail the AuditMatrix play under the story play guard. Gap: no check fails if FormLayout adds a role of its own. | `audit:FormLayout/accessibility` |
| PR1 | Source review | Every direction | Gap: no automated check covers context identity or added resources. | `audit:FormLayout/behavior` |
diff --git a/packages/core/src/Slider/Slider.test.tsx b/packages/core/src/Slider/Slider.test.tsx
index 8fac0f7856..4d1caeb88d 100644
--- a/packages/core/src/Slider/Slider.test.tsx
+++ b/packages/core/src/Slider/Slider.test.tsx
@@ -337,6 +337,18 @@ describe('Slider', () => {
expect(slider).not.toHaveAttribute('aria-required');
});
+ it('conveys required state under FormLayout defaultOptionality="required" unless isOptional (FormLayout FR5)', () => {
+ render(
+ <FormLayout defaultOptionality="required">
+ <Slider label="Volume" value={50} />
+ <Slider label="Balance" value={50} isOptional />
+ </FormLayout>,
+ );
+ const [volume, balance] = screen.getAllByRole('slider');
+ expect(volume).toHaveAccessibleDescription(/Required/);
+ expect(balance).not.toHaveAccessibleDescription(/Required/);
+ });
+
it('conveys required state on both thumbs of a range slider', () => {
render(
<Slider
diff --git a/packages/core/src/Slider/Slider.tsx b/packages/core/src/Slider/Slider.tsx
index 15aae8e28a..916672579c 100644
--- a/packages/core/src/Slider/Slider.tsx
+++ b/packages/core/src/Slider/Slider.tsx
@@ -58,6 +58,7 @@ import type {SizeValue} from '../utils/types';
import {themeProps} from '../utils/themeProps';
import {useMergedRefs} from '../hooks/useMergedRefs';
+import {useResolvedRequired} from '../hooks/useResolvedRequired';
// =============================================================================
// Types
// =============================================================================
@@ -591,7 +592,9 @@ export function Slider({ref, ...props}: SliderProps) {
// role="slider" in WAI-ARIA 1.2, so the thumb instead points its
// aria-describedby at a visually hidden "Required" span — mirroring the
// Field label's visible indicator (where isOptional takes precedence).
- const conveysRequired = isRequired && !isOptional;
+ // A surrounding FormLayout's defaultOptionality="required" makes an unmarked
+ // field required too (FormLayout FR5).
+ const conveysRequired = useResolvedRequired({isRequired, isOptional});
// Build aria-describedby
const describedByParts: string[] = [];