An open source design system that's fully customizable and agent ready
TypeScript
fix(DateRangeInput): disabled and busy fields and disabled presets stay focusable (#7866)
Cindy Zhang5af3293b15ba
diff --git a/.changeset/date-range-input-focusable-disabled.md b/.changeset/date-range-input-focusable-disabled.mdnew file mode 100644index 0000000000..55d4bea3bb--- /dev/null+++ b/.changeset/date-range-input-focusable-disabled.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] DateRangeInput: a disabled or busy field and a disabled preset no longer render native `disabled`. The trigger, calendar button and presets keep their place in the focus order and expose `aria-disabled`, while opening and selecting stay blocked, and a focused disabled field shows its focus ring. This meets the focusable-disabled requirement in spec:AST-085 (FR1, FR2, FR6) and DateRangeInput AR3. Tests that query them with `toBeDisabled()` or `:disabled` should check `aria-disabled="true"` instead.++@cixzhangdiff --git a/apps/storybook/stories/DateRangeInput.stories.tsx b/apps/storybook/stories/DateRangeInput.stories.tsxindex 3f2f8f8bc9..af598649ee 100644--- a/apps/storybook/stories/DateRangeInput.stories.tsx+++ b/apps/storybook/stories/DateRangeInput.stories.tsx@@ -61,7 +61,7 @@ const meta: Meta<typeof DateRangeInput> = {disabledMessage: {control: 'text',description:- 'Explains why the input is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the field focusable via aria-disabled (activation stays blocked). Use this instead of wrapping a disabled DateRangeInput in Tooltip.',+ 'Explains why the input is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and describes the trigger (activation stays blocked). Use this instead of wrapping a disabled DateRangeInput in Tooltip.',},size: {control: 'radio',@@ -232,9 +232,9 @@ export const Disabled: Story = {// Disabled with an explanation tooltip. Hover or keyboard-focus the field to// see why it's disabled — the reason is announced to assistive tech via-// aria-describedby, and the field stays focusable (activation is still-// blocked). Use disabledMessage instead of wrapping a disabled DateRangeInput in Tooltip:-// disabled controls swallow the pointer events a Tooltip wrapper needs.+// aria-describedby on the trigger (activation is still blocked). Use+// disabledMessage instead of wrapping a disabled DateRangeInput in Tooltip, so+// the reason is tied to the field.export const DisabledWithMessage: Story = {render: args => {const [value, setValue] = useState<DateRange | null>(null);diff --git a/packages/core/src/DateRangeInput/DateRangeInput.doc.mjs b/packages/core/src/DateRangeInput/DateRangeInput.doc.mjsindex 4c308ea96d..e31644e455 100644--- a/packages/core/src/DateRangeInput/DateRangeInput.doc.mjs+++ b/packages/core/src/DateRangeInput/DateRangeInput.doc.mjs@@ -45,14 +45,15 @@ export const docs = {{name: 'isDisabled',type: 'boolean',- description: 'Disable the trigger and picker.',+ description:+ 'Disable the trigger and picker. The trigger stays focusable and exposes `aria-disabled`; opening is blocked.',default: 'false',},{name: 'disabledMessage',type: 'string',description:- 'Explains why the input is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the field focusable via aria-disabled (activation stays blocked). Use this instead of wrapping a disabled DateRangeInput in Tooltip. Disabled controls swallow the hover events an external Tooltip needs.',+ 'Explains why the input is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and describes the trigger (activation stays blocked). Use this instead of wrapping a disabled DateRangeInput in Tooltip.',},{name: 'value',@@ -240,7 +241,7 @@ export const docs = {{guidance: false,description:- 'Wrap a disabled DateRangeInput in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',+ 'Wrap a disabled DateRangeInput in Tooltip to explain why it is disabled. Use the disabledMessage prop instead, which ties the reason to the field.',},],anatomy: [@@ -344,7 +345,7 @@ export const docsDense = {{guidance: false,description:- 'Wrap a disabled DateRangeInput in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',+ 'Wrap a disabled DateRangeInput in Tooltip to explain why it is disabled. Use the disabledMessage prop instead, which ties the reason to the field.',},],},@@ -356,7 +357,7 @@ export const docsDense = {isRequired: 'mark field required',isDisabled: 'disable trigger+picker',disabledMessage:- 'reason shown in a tooltip on hover/focus when disabled; keeps trigger focusable via aria-disabled',+ 'reason shown in a tooltip on hover/focus when disabled; describes the trigger',value:'selected range {start, end} or null; import DateRange type from @astryxdesign/core/DateRangeInput (do not redeclare)',onChange: 'callback on range change; null on clear',diff --git a/packages/core/src/DateRangeInput/DateRangeInput.test.tsx b/packages/core/src/DateRangeInput/DateRangeInput.test.tsxindex 59ae56d796..7dd5f57567 100644--- a/packages/core/src/DateRangeInput/DateRangeInput.test.tsx+++ b/packages/core/src/DateRangeInput/DateRangeInput.test.tsx@@ -23,6 +23,8 @@ function generateThemeTestCSS(theme: Parameters<typeof generateThemeCSS>[0]) {// made every role+name query compute ~85 accessible names through jsdom's// slow getComputedStyle (~450ms per query). See fastRoleQueries.ts.import {getButton, queryButton} from '../__tests__/fastRoleQueries';+import * as stylex from '@stylexjs/stylex';+import {colorVars} from '../theme/tokens.stylex';import {DateRangeInput} from './DateRangeInput';import type {DateRange} from './DateRangeInput';import {Icon} from '../Icon';@@ -155,7 +157,46 @@ describe('DateRangeInput', () => {/>,);const trigger = getButton(/Range/);- expect(trigger).toBeDisabled();+ expect(trigger).not.toHaveAttribute('disabled');+ expect(trigger).toHaveAttribute('aria-disabled', 'true');+ trigger.focus();+ expect(trigger).toHaveFocus();+ });++ it('restores the focus ring on a focused disabled field (AST-085 FR6)', () => {+ // The same declarations as the field's ring style compile to the same+ // atomic classes.+ const ringClasses = stylex+ .props(+ stylex.create({+ ring: {+ borderColor: {+ default: colorVars['--color-border-emphasized'],+ ':focus-within': colorVars['--color-accent'],+ },+ boxShadow: {+ default: 'none',+ ':focus-within': `inset 0px 0px 0px 2px ${colorVars['--color-accent-muted']}`,+ },+ },+ }).ring,+ )+ .className!.split(' ')+ // Keep the atomic classes, not the dev-only debug name.+ .filter(name => !name.includes('__'));+ render(+ <DateRangeInput+ label="Range"+ isDisabled+ value={null}+ onChange={() => {}}+ />,+ );+ // The shared disabled style alone drops these :focus-within declarations.+ const wrapper = getButton(/Range/).parentElement!;+ for (const name of ringClasses) {+ expect(wrapper).toHaveClass(name);+ }});it('is not disabled by default', () => {@@ -272,7 +313,8 @@ describe('DateRangeInput', () => {onChange={() => {}}/>,);- expect(getButton('Open calendar')).toBeDisabled();+ expect(getButton('Open calendar')).not.toHaveAttribute('disabled');+ expect(getButton('Open calendar')).toHaveAttribute('aria-disabled', 'true');});it('renders with size="lg"', () => {@@ -526,7 +568,8 @@ describe('DateRangeInput', () => {);const preset = getButton('Last 7 days');- expect(preset).toBeDisabled();+ expect(preset).not.toHaveAttribute('disabled');+ expect(preset).toHaveAttribute('aria-disabled', 'true');fireEvent.click(preset);expect(handleChange).not.toHaveBeenCalled();});@@ -544,7 +587,8 @@ describe('DateRangeInput', () => {);const preset = getButton('Last 7 days');- expect(preset).toBeDisabled();+ expect(preset).not.toHaveAttribute('disabled');+ expect(preset).toHaveAttribute('aria-disabled', 'true');fireEvent.click(preset);expect(handleChange).not.toHaveBeenCalled();});@@ -562,7 +606,8 @@ describe('DateRangeInput', () => {);const preset = getButton('Last 7 days');- expect(preset).toBeDisabled();+ expect(preset).not.toHaveAttribute('disabled');+ expect(preset).toHaveAttribute('aria-disabled', 'true');fireEvent.click(preset);expect(handleChange).not.toHaveBeenCalled();});@@ -590,7 +635,7 @@ describe('DateRangeInput', () => {const getRangeCallsBeforeClick = getRange.mock.calls.length;const preset = getButton('Allowed range');- expect(preset).not.toBeDisabled();+ expect(preset).not.toHaveAttribute('aria-disabled');fireEvent.click(preset);expect(handleChange).toHaveBeenCalledWith(range);expect(getRangeCallsAtChange).toBe(getRangeCallsBeforeClick);@@ -613,7 +658,10 @@ describe('DateRangeInput', () => {// accessibility tree; use hidden: true to find it.const h = {hidden: true} as const;- function renderDisabled(props?: {disabledMessage?: string}) {+ function renderDisabled(props?: {+ disabledMessage?: string;+ onClick?: () => void;+ }) {return render(<DateRangeInputlabel="Range"@@ -699,11 +747,22 @@ describe('DateRangeInput', () => {expect(trigger).toHaveAttribute('aria-expanded', 'false');});- it('remains natively disabled when disabled without a reason', () => {- renderDisabled();+ it('blocks activation and a caller onClick when disabled without a reason (AST-085 FR1, FR2)', async () => {+ const user = userEvent.setup();+ const onClick = vi.fn();+ renderDisabled({onClick});const trigger = getButton(/Range:/);- expect(trigger).toBeDisabled();- expect(trigger).not.toHaveAttribute('aria-disabled');+ expect(trigger).not.toHaveAttribute('disabled');+ await user.click(trigger);+ await user.keyboard('{Enter}');+ expect(trigger).toHaveAttribute('aria-expanded', 'false');+ expect(onClick).not.toHaveBeenCalled();+ });++ it('blocks the calendar button while disabled (AST-085 FR2)', () => {+ renderDisabled();+ fireEvent.click(getButton('Open calendar'));+ expect(getButton(/Range:/)).toHaveAttribute('aria-expanded', 'false');});});});@@ -1030,9 +1089,10 @@ describe('DateRangeInput range-span forwarding', () => {// The 3-day preset fits the 7-day cap; the 30-day preset can't be committed.const withinCap = getButton('Last 3 days');const overCap = getButton('Last 30 days');- expect(withinCap).not.toBeDisabled();+ expect(withinCap).not.toHaveAttribute('aria-disabled');expect(withinCap).not.toHaveAttribute('data-disabled');- expect(overCap).toBeDisabled();+ expect(overCap).not.toHaveAttribute('disabled');+ expect(overCap).toHaveAttribute('aria-disabled', 'true');expect(overCap).toHaveClass('astryx-date-range-input-preset');expect(overCap).toHaveAttribute('data-disabled', 'disabled');diff --git a/packages/core/src/DateRangeInput/DateRangeInput.tsx b/packages/core/src/DateRangeInput/DateRangeInput.tsxindex aad3fbf478..127968684b 100644--- a/packages/core/src/DateRangeInput/DateRangeInput.tsx+++ b/packages/core/src/DateRangeInput/DateRangeInput.tsx@@ -97,6 +97,18 @@ export type {} from '../Field';const styles = stylex.create({+ // The shared disabled style drops the focus ring; restore it while a+ // control inside the field holds focus (spec:AST-085 FR6). Defaults repeat it.+ disabledFocusRing: {+ borderColor: {+ default: colorVars['--color-border-emphasized'],+ ':focus-within': colorVars['--color-accent'],+ },+ boxShadow: {+ default: 'none',+ ':focus-within': `inset 0px 0px 0px 2px ${colorVars['--color-accent-muted']}`,+ },+ },trigger: {display: 'block',flex: 1,@@ -309,12 +321,12 @@ export interface DateRangeInputProps extends Omit</*** Explains why the input is disabled. When set together with* `isDisabled`, the input shows a tooltip with this text on hover and- * keyboard focus, and the trigger stays focusable (via `aria-disabled`)- * so the reason is discoverable by keyboard and assistive technology.- * Activation stays blocked.+ * keyboard focus, and the trigger is described by it, so the reason is+ * discoverable by keyboard and assistive technology. Activation stays+ * blocked.*- * Use this instead of wrapping a disabled input in `Tooltip` — disabled- * controls don't emit the pointer events an external tooltip needs.+ * Use this instead of wrapping a disabled input in `Tooltip`, so the+ * reason is tied to the field.** @example* ```@@ -546,12 +558,12 @@ export function DateRangeInput({const isBusy = isLoading || optimisticValue !== value;const isEffectivelyDisabled = isDisabled || isBusy;- // Disabled-reason tooltip. Disabled controls swallow pointer events, so the- // tooltip listeners attach to the trigger container (which already exists)- // and the trigger button stays perceivable via aria-disabled instead of the- // disabled attribute. Activation is blocked by the isEffectivelyDisabled- // guard in handleToggle. Only the persistent isDisabled state (not the- // transient busy state) surfaces a reason.+ // A disabled or busy field never renders native `disabled` (spec:AST-085+ // FR1): its buttons stay focusable via aria-disabled, and activation is+ // blocked by the isEffectivelyDisabled guard in handleToggle. The+ // disabled-reason tooltip's listeners attach to the trigger container. Only+ // the persistent isDisabled state (not the transient busy state) surfaces a+ // reason.const showsDisabledMessage = isDisabled && !!disabledMessage;const disabledMessageTooltip = useTooltip({placement: 'above',@@ -695,6 +707,7 @@ export function DateRangeInput({inputWrapperStyles.base,sizeStyles[size],isEffectivelyDisabled && inputWrapperStyles.disabled,+ isEffectivelyDisabled && styles.disabledFocusRing,status && inputStatusBorderStyles[status.type],status &&!isEffectivelyDisabled &&@@ -708,7 +721,7 @@ export function DateRangeInput({<buttontype="button"onClick={handleToggle}- disabled={isEffectivelyDisabled}+ aria-disabled={isEffectivelyDisabled || undefined}aria-label={popover.isOpen? t('@astryx.dateInput.toggleCalendarClose')@@ -734,12 +747,14 @@ export function DateRangeInput({ref={triggerButtonRef}id={id}type="button"- onClick={chainEventHandlers(handleToggle, onClickProp)}- // With a disabledMessage the trigger keeps focusability via- // aria-disabled so the reason is focus-discoverable; activation is- // still blocked by the isEffectivelyDisabled guard in handleToggle.- disabled={isEffectivelyDisabled && !showsDisabledMessage}- aria-disabled={showsDisabledMessage ? 'true' : undefined}+ // The browser still dispatches a click on an aria-disabled button,+ // so a caller's onClick is skipped too (spec:AST-085 FR2).+ onClick={+ isEffectivelyDisabled+ ? undefined+ : chainEventHandlers(handleToggle, onClickProp)+ }+ aria-disabled={isEffectivelyDisabled ? 'true' : undefined}// A caller aria-label replaces the trigger's name; a caller// aria-labelledby is read after it (label and selected range).aria-label={ariaLabelProp ?? triggerAriaLabel}@@ -807,8 +822,13 @@ export function DateRangeInput({// marked with aria-current (not aria-selected, a listbox// concept that contradicted the Tab interaction) (forms-5).aria-current={isActive ? 'true' : undefined}- disabled={isPresetDisabled}- onClick={() => handleRangeSelect(presetRange)}+ // Focusable while disabled (AR3, spec:AST-085 FR1).+ aria-disabled={isPresetDisabled || undefined}+ onClick={() => {+ if (!isPresetDisabled) {+ handleRangeSelect(presetRange);+ }+ }}{...mergeProps(themeProps('date-range-input-preset', {selected: isActive ? 'selected' : null,