An open source design system that's fully customizable and agent ready
TypeScript
fix(FileInput): dispatch changeAction and update optimistic value on clear and selection (#6427)
Manohar Paturi2cc4ec2a72f4
diff --git a/.changeset/file-input-clear-change-action.md b/.changeset/file-input-clear-change-action.mdnew file mode 100644index 0000000000..c87998fdd1--- /dev/null+++ b/.changeset/file-input-clear-change-action.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] FileInput: invoke changeAction and update optimistic state on clear and file selection++@ManoharPaturidiff --git a/packages/core/src/FileInput/FileInput.test.tsx b/packages/core/src/FileInput/FileInput.test.tsxindex aa6bdbba25..3c1e601871 100644--- a/packages/core/src/FileInput/FileInput.test.tsx+++ b/packages/core/src/FileInput/FileInput.test.tsx@@ -16,6 +16,7 @@ import {fireEvent,createEvent,waitFor,+ act,} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {FileInput} from './FileInput';@@ -626,6 +627,68 @@ describe('FileInput', () => {expect(handleChange).toHaveBeenCalledWith(null);});+ it('calls onChange and changeAction with null when clear is clicked', async () => {+ const user = userEvent.setup();+ const order: string[] = [];+ const handleChange = vi.fn(() => {+ order.push('onChange');+ });+ const changeAction = vi.fn(async () => {+ order.push('changeAction');+ });+ const file = createFile('test.txt', 100);+ render(+ <FileInput+ label="Upload"+ value={file}+ onChange={handleChange}+ changeAction={changeAction}+ />,+ );+ await user.click(screen.getByRole('button', {name: 'Clear Upload'}));+ expect(handleChange).toHaveBeenCalledWith(null);+ expect(changeAction).toHaveBeenCalledWith(null);+ expect(order).toEqual(['onChange', 'changeAction']);+ });++ it('presents optimistic cleared state and busy indicator while clear changeAction is pending', async () => {+ const user = userEvent.setup();+ let resolveAction: () => void = () => {};+ const changeAction = vi.fn(+ async () =>+ new Promise<void>(resolve => {+ resolveAction = resolve;+ }),+ );+ const file = createFile('test.txt', 100);+ render(+ <FileInput+ label="Upload"+ value={file}+ onChange={() => {}}+ changeAction={changeAction}+ />,+ );+ const trigger = screen.getByRole('button', {+ name: 'Upload, test.txt',+ });+ expect(trigger).not.toHaveAttribute('aria-busy');+ expect(+ screen.getByRole('button', {name: 'Clear Upload'}),+ ).toBeInTheDocument();++ await user.click(screen.getByRole('button', {name: 'Clear Upload'}));++ expect(trigger).toHaveAttribute('aria-busy', 'true');+ expect(+ screen.queryByRole('button', {name: 'Clear Upload'}),+ ).not.toBeInTheDocument();++ await act(async () => {+ resolveAction();+ });+ });+it('does not show clear button during loading', () => {const file = createFile('test.txt', 100);render(@@ -637,6 +700,66 @@ describe('FileInput', () => {});});+ describe('changeAction and optimistic updates', () => {+ it('calls changeAction with selected file in a transition', async () => {+ const order: string[] = [];+ const handleChange = vi.fn(() => {+ order.push('onChange');+ });+ const changeAction = vi.fn(async () => {+ order.push('changeAction');+ });+ render(+ <FileInput+ label="Upload"+ value={null}+ onChange={handleChange}+ changeAction={changeAction}+ />,+ );+ const input = fileInputEl();+ const file = createFile('resume.pdf', 200, 'application/pdf');+ await act(async () => {+ fireEvent.change(input, {target: {files: [file]}});+ });++ expect(handleChange).toHaveBeenCalledWith(file);+ expect(changeAction).toHaveBeenCalledWith(file);+ expect(order).toEqual(['onChange', 'changeAction']);+ });++ it('presents optimistic file name and aria-busy while changeAction is pending', async () => {+ let resolveAction: () => void = () => {};+ const changeAction = vi.fn(+ async () =>+ new Promise<void>(resolve => {+ resolveAction = resolve;+ }),+ );+ render(+ <FileInput+ label="Upload"+ value={null}+ onChange={() => {}}+ changeAction={changeAction}+ />,+ );+ const trigger = screen.getByRole('button', {name: 'Upload'});+ expect(trigger).not.toHaveAttribute('aria-busy');++ const input = fileInputEl();+ const file = createFile('report.pdf', 300, 'application/pdf');+ fireEvent.change(input, {target: {files: [file]}});++ expect(trigger).toHaveAttribute('aria-busy', 'true');+ expect(screen.getByText('report.pdf')).toBeInTheDocument();++ await act(async () => {+ resolveAction();+ });+ });+ });+describe('drag and drop', () => {it('calls onChange when files are dropped in dropzone mode', () => {const handleChange = vi.fn();diff --git a/packages/core/src/FileInput/FileInput.tsx b/packages/core/src/FileInput/FileInput.tsxindex e7b641c59d..58b33ff910 100644--- a/packages/core/src/FileInput/FileInput.tsx+++ b/packages/core/src/FileInput/FileInput.tsx@@ -21,6 +21,7 @@ import {useCallback,useRef,useState,+ useOptimistic,useTransition,type DragEvent,} from 'react';@@ -463,6 +464,8 @@ export function FileInput({const [isDragOver, setIsDragOver] = useState(false);const [validationError, setValidationError] = useState<string | null>(null);const [, startTransition] = useTransition();+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);+ const isBusy = isLoading || optimisticValue !== value;// Announce successful file selection to screen readers via a persistent// live region (forms-17). Validation errors are announced (assertively) by@@ -562,6 +565,7 @@ export function FileInput({if (changeAction) {startTransition(async () => {+ setOptimisticValue(result);await changeAction(result);});}@@ -575,6 +579,7 @@ export function FileInput({onChange,changeAction,startTransition,+ setOptimisticValue,announce,t,],@@ -596,6 +601,12 @@ export function FileInput({e.stopPropagation();setValidationError(null);onChange(null);+ if (changeAction) {+ startTransition(async () => {+ setOptimisticValue(null);+ await changeAction(null);+ });+ }if (inputRef.current) {inputRef.current.value = '';const targetInput = inputRef.current;@@ -610,7 +621,7 @@ export function FileInput({}}},- [onChange],+ [onChange, changeAction, startTransition, setOptimisticValue],);const handleClick = useCallback(() => {@@ -696,15 +707,16 @@ export function FileInput({);const hasFiles =- value != null && (Array.isArray(value) ? value.length > 0 : true);+ optimisticValue != null &&+ (Array.isArray(optimisticValue) ? optimisticValue.length > 0 : true);const fileNames = hasFiles- ? Array.isArray(value)- ? value.map(f => f.name).join(', ')- : (value?.name ?? '')+ ? Array.isArray(optimisticValue)+ ? optimisticValue.map(f => f.name).join(', ')+ : (optimisticValue?.name ?? ''): null;const renderDropzoneContent = () => {- if (isLoading) {+ if (isBusy) {return <Spinner size="md" />;}if (hasFiles) {@@ -730,7 +742,7 @@ export function FileInput({};const renderCompactContent = () => {- if (isLoading) {+ if (isBusy) {return (<><span {...stylex.props(styles.fileNameText)}>@@ -849,7 +861,7 @@ export function FileInput({? t('@astryx.fileInput.triggerWithFiles', {label, fileNames}): label}- aria-busy={isLoading || undefined}+ aria-busy={isBusy || undefined}aria-describedby={ariaDescribedBy}aria-invalid={status?.type === 'error' ? 'true' : undefined}/>@@ -870,7 +882,7 @@ export function FileInput({{...stylex.props(styles.hiddenInput)}/>{isDropzone ? renderDropzoneContent() : renderCompactContent()}- {hasFiles && !isDisabled && !isLoading && (+ {hasFiles && !isDisabled && !isBusy && (<InputClearButtonlabel={t('@astryx.fileInput.clearLabel', {label})}onClick={handleClear}