An open source design system that's fully customizable and agent ready
TypeScript
fix(core): include selected filenames in FileInput trigger name (#3989)
Baraa Hamodi674eb3c4ec15
diff --git a/.changeset/fileinput-trigger-name.md b/.changeset/fileinput-trigger-name.mdnew file mode 100644index 0000000000..f6b6be0975--- /dev/null+++ b/.changeset/fileinput-trigger-name.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] FileInput: the trigger's accessible name now includes the selected filenames, so screen-reader users can review what is attached when refocusing the control.++@bhamodidiff --git a/packages/core/locales/en.json b/packages/core/locales/en.jsonindex 1ad8670bc2..79cc55c319 100644--- a/packages/core/locales/en.json+++ b/packages/core/locales/en.json@@ -679,6 +679,10 @@"defaultMessage": "Clear {label}","description": "Screen-reader-only label on the X that removes the selected file from a FileInput. Example: `Clear Attachment`, `Clear Résumé`."},+ "@astryx.fileInput.triggerWithFiles": {+ "defaultMessage": "{label}, {fileNames}",+ "description": "Accessible name for a FileInput trigger that has files attached, composing the field label with the selected filenames. Example: `Attachments, report.pdf, notes.txt`."+ },"@astryx.lightbox.mediaViewer": {"defaultMessage": "Media viewer","description": "Screen-reader-only fallback name for the Lightbox dialog when the current media item has no alt text. Should rarely render — consumers should provide alt."diff --git a/packages/core/src/FileInput/FileInput.test.tsx b/packages/core/src/FileInput/FileInput.test.tsxindex ab719e3a51..192e6260ae 100644--- a/packages/core/src/FileInput/FileInput.test.tsx+++ b/packages/core/src/FileInput/FileInput.test.tsx@@ -641,6 +641,54 @@ describe('FileInput', () => {});});+ describe('trigger accessible name', () => {+ it('includes the selected file name in the trigger name', () => {+ const file = createFile('report.pdf', 1024, 'application/pdf');+ render(<FileInput label="Document" value={file} onChange={() => {}} />);+ expect(+ screen.getByRole('button', {name: 'Document, report.pdf'}),+ ).toBeInTheDocument();+ });++ it('includes all selected file names when multiple files are selected', () => {+ const files = [createFile('a.txt', 100), createFile('b.txt', 200)];+ render(+ <FileInput+ label="Files"+ value={files}+ onChange={() => {}}+ isMultiple+ />,+ );+ expect(+ screen.getByRole('button', {name: 'Files, a.txt, b.txt'}),+ ).toBeInTheDocument();+ });++ it('uses exactly the label when no files are selected', () => {+ render(<FileInput label="Document" value={null} onChange={() => {}} />);+ expect(screen.getByRole('button', {name: 'Document'})).toHaveAttribute(+ 'aria-label',+ 'Document',+ );+ });++ it('includes the selected file name in dropzone mode', () => {+ const file = createFile('doc.pdf', 100, 'application/pdf');+ render(+ <FileInput+ label="Upload"+ value={file}+ onChange={() => {}}+ mode="dropzone"+ />,+ );+ expect(+ screen.getByRole('button', {name: 'Upload, doc.pdf'}),+ ).toBeInTheDocument();+ });+ });+describe('data-testid', () => {it('passes data-testid to native input', () => {render(diff --git a/packages/core/src/FileInput/FileInput.tsx b/packages/core/src/FileInput/FileInput.tsxindex 7f982699b1..e50288c160 100644--- a/packages/core/src/FileInput/FileInput.tsx+++ b/packages/core/src/FileInput/FileInput.tsx@@ -745,7 +745,15 @@ export function FileInput({aria-disabled={showsDisabledMessage ? 'true' : undefined}onClick={handleClick}onKeyDown={handleKeyDown}- aria-label={label}+ // aria-label suppresses child content from the accessible name, so+ // compose the selected filenames into it — otherwise a screen-reader+ // user refocusing the control hears only the field label and cannot+ // tell what (if anything) is attached.+ aria-label={+ hasFiles && fileNames+ ? t('@astryx.fileInput.triggerWithFiles', {label, fileNames})+ : label+ }aria-busy={isLoading || undefined}// These describe the operable control, so they belong on the focusable// role="button" wrapper — not the hidden tabIndex={-1} file input the