An open source design system that's fully customizable and agent ready
TypeScript
fix(FileInput): return focus to the trigger, not the aria-hidden native input, after a clear (#8799)
홍규진aaf573b10807
diff --git a/.changeset/fileinput-clear-focus-trigger.md b/.changeset/fileinput-clear-focus-trigger.mdnew file mode 100644index 0000000000..2218b1d896--- /dev/null+++ b/.changeset/fileinput-clear-focus-trigger.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] FileInput: clearing the selection returns focus to the file-selection trigger. Before, both keyboard and pointer activation of the clear button focused the visually hidden native `<input type="file">`, which is `aria-hidden` and out of the tab order, so a screen reader announced nothing after the clear. Now focus lands on the trigger, which announces the field label. No API changes.++@kyu-rongdiff --git a/packages/core/src/FileInput/FileInput.doc.mjs b/packages/core/src/FileInput/FileInput.doc.mjsindex 18ddb6db22..b5d032ee68 100644--- a/packages/core/src/FileInput/FileInput.doc.mjs+++ b/packages/core/src/FileInput/FileInput.doc.mjs@@ -196,7 +196,7 @@ export const docs = {{name: 'Upload icon', required: false, description: 'An upload-to-tray icon in the drop zone hinting at the upload action.'},{name: 'Placeholder', required: false, description: 'Hint text shown when no files are selected.'},{name: 'File name display', required: false, description: 'Shows the name(s) of selected files.'},- {name: 'Clear button', required: false, description: 'A close button that removes selected files and returns focus to the input.'},+ {name: 'Clear button', required: false, description: 'A close button that removes selected files and returns focus to the file-selection trigger.'},{name: 'Spinner', required: false, description: 'Loading indicator that appears during async upload actions.'},{name: 'Status message', required: false, description: 'Validation feedback showing error, warning, or success with a message.'},],diff --git a/packages/core/src/FileInput/FileInput.test.tsx b/packages/core/src/FileInput/FileInput.test.tsxindex de82a66326..29d73c9149 100644--- a/packages/core/src/FileInput/FileInput.test.tsx+++ b/packages/core/src/FileInput/FileInput.test.tsx@@ -728,6 +728,30 @@ describe('FileInput', () => {expect(handleChange).toHaveBeenCalledWith(null);});+ it('returns focus to the trigger, not the aria-hidden native input, after a keyboard clear', () => {+ const file = createFile('test.txt', 100);+ render(<FileInput label="Upload" value={file} onChange={() => {}} />);+ fireEvent.click(screen.getByRole('button', {name: 'Clear Upload'}), {+ detail: 0,+ });+ expect(screen.getByRole('button', {name: /^Upload/})).toHaveFocus();+ });++ it('returns focus to the trigger, not the aria-hidden native input, after a pointer clear', async () => {+ const raf = vi+ .spyOn(window, 'requestAnimationFrame')+ .mockImplementation(cb => {+ cb(0);+ return 0;+ });+ const user = userEvent.setup();+ const file = createFile('test.txt', 100);+ render(<FileInput label="Upload" value={file} onChange={() => {}} />);+ await user.click(screen.getByRole('button', {name: 'Clear Upload'}));+ expect(screen.getByRole('button', {name: /^Upload/})).toHaveFocus();+ raf.mockRestore();+ });+it('calls onChange and changeAction with null when clear is clicked', async () => {const user = userEvent.setup();const order: string[] = [];diff --git a/packages/core/src/FileInput/FileInput.tsx b/packages/core/src/FileInput/FileInput.tsxindex 1ece331f28..59bda7e73b 100644--- a/packages/core/src/FileInput/FileInput.tsx+++ b/packages/core/src/FileInput/FileInput.tsx@@ -487,6 +487,7 @@ export function FileInput({const statusMessageID = useId();const requiredID = useId();const inputRef = useRef<HTMLInputElement | null>(null);+ const triggerRef = useRef<HTMLButtonElement | null>(null);const containerRef = useRef<HTMLDivElement | null>(null);const [isDragOver, setIsDragOver] = useState(false);const [validationError, setValidationError] = useState<string | null>(null);@@ -651,14 +652,16 @@ export function FileInput({}if (inputRef.current) {inputRef.current.value = '';- const targetInput = inputRef.current;+ // Focus returns to the trigger button, the control a person operates;+ // the native input is aria-hidden and outside the tab order.+ const trigger = triggerRef.current;if (e.detail === 0) {- targetInput.focus();+ trigger?.focus();} else {// Defer focus restoration past the button's unmount task so iOS Safari// and touch browsers don't jump the page scroll to 0 on tap.requestAnimationFrame(() => {- targetInput.focus({preventScroll: true});+ trigger?.focus({preventScroll: true});});}}@@ -884,6 +887,7 @@ export function FileInput({keyboard activation still fires here. */}<VisuallyHidden><button+ ref={triggerRef}type="button"// A disabled trigger keeps its Tab stop (spec:AST-085 FR1).tabIndex={0}