An open source design system that's fully customizable and agent ready
TypeScript
fix(DateRangeInput): move focus to the trigger only when the clear button is activated by keyboard (#7507)
홍규진cc17af5011d6
diff --git a/.changeset/daterangeinput-clear-returns-focus.md b/.changeset/daterangeinput-clear-returns-focus.mdnew file mode 100644index 0000000000..c4cd3b83b6--- /dev/null+++ b/.changeset/daterangeinput-clear-returns-focus.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `DateRangeInput`: activating the clear button with the keyboard now returns focus to the range trigger. Previously the button unmounted once the range was cleared and focus fell to `<body>`, so a keyboard user had to Tab back in from the top of the page. Pointer clears are unchanged.++@kyu-rongdiff --git a/packages/core/src/DateRangeInput/DateRangeInput.test.tsx b/packages/core/src/DateRangeInput/DateRangeInput.test.tsxindex 7b86625cd8..59ae56d796 100644--- a/packages/core/src/DateRangeInput/DateRangeInput.test.tsx+++ b/packages/core/src/DateRangeInput/DateRangeInput.test.tsx@@ -320,6 +320,55 @@ describe('DateRangeInput', () => {).toHaveLength(0);});+ it('returns focus to the trigger when the clear button is activated by keyboard', async () => {+ const user = userEvent.setup();+ function ControlledDateRangeInput() {+ const [controlledValue, setControlledValue] =+ useState<DateRange | null>({+ start: '2026-01-01',+ end: '2026-01-03',+ });+ return (+ <DateRangeInput+ label="Range"+ value={controlledValue}+ onChange={setControlledValue}+ />+ );+ }+ render(<ControlledDateRangeInput />);++ getButton('Clear Range').focus();+ await user.keyboard('{Enter}');++ expect(queryButton('Clear Range')).not.toBeInTheDocument();+ expect(getButton(/^Range:/)).toHaveFocus();+ });++ it('does not move focus to the trigger when the clear button is clicked', async () => {+ const user = userEvent.setup();+ function ControlledDateRangeInput() {+ const [controlledValue, setControlledValue] =+ useState<DateRange | null>({+ start: '2026-01-01',+ end: '2026-01-03',+ });+ return (+ <DateRangeInput+ label="Range"+ value={controlledValue}+ onChange={setControlledValue}+ />+ );+ }+ render(<ControlledDateRangeInput />);++ await user.click(getButton('Clear Range'));++ expect(queryButton('Clear Range')).not.toBeInTheDocument();+ expect(getButton(/^Range:/)).not.toHaveFocus();+ });+it('shows clear button when hasClear is true and value exists', () => {const range: DateRange = {start: '2026-03-15',diff --git a/packages/core/src/DateRangeInput/DateRangeInput.tsx b/packages/core/src/DateRangeInput/DateRangeInput.tsxindex 43aeda46c8..aad3fbf478 100644--- a/packages/core/src/DateRangeInput/DateRangeInput.tsx+++ b/packages/core/src/DateRangeInput/DateRangeInput.tsx@@ -16,7 +16,14 @@* - /packages/cli/assets/templates/blocks/components/DateRangeInput/ (showcase blocks)*/-import {useId, useCallback, useMemo, useOptimistic, useTransition} from 'react';+import {+ useId,+ useCallback,+ useMemo,+ useOptimistic,+ useRef,+ useTransition,+} from 'react';import * as stylex from '@stylexjs/stylex';import {plainDateFromISO,@@ -68,6 +75,7 @@ import {import type {SizeValue} from '../utils/types';import {useSize} from '../SizeContext/SizeContext';import {useInputStatusIcon} from '../hooks/useInputStatusIcon';+import {useMergedRefs} from '../hooks/useMergedRefs';import {useResolvedRequired} from '../hooks/useResolvedRequired';import {themeProps} from '../utils/themeProps';import {focusOutlineStyles} from '../utils/focusOutline.stylex';@@ -624,10 +632,20 @@ export function DateRangeInput({[fireChange, popover],);+ const triggerRef = useRef<HTMLButtonElement>(null);+ const triggerButtonRef = useMergedRefs<HTMLButtonElement>(triggerRef, ref);+const handleClear = useCallback((e: React.MouseEvent) => {e.stopPropagation();fireChange(null);+ // A keyboard activation (detail 0) leaves focus on the clear button,+ // which unmounts once the range is null and would drop focus to <body>;+ // hand it to the trigger. A pointer press never focuses the button, so+ // there is no focus to repair.+ if (e.detail === 0) {+ triggerRef.current?.focus();+ }},[fireChange],);@@ -713,7 +731,7 @@ export function DateRangeInput({</button><button{...controlProps}- ref={ref}+ ref={triggerButtonRef}id={id}type="button"onClick={chainEventHandlers(handleToggle, onClickProp)}