Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(FileInput): prevent file selection while busy (#9167)

zakelstormc829c09caf30
diff --git a/.changeset/neat-trees-judge.md b/.changeset/neat-trees-judge.md
new file mode 100644
index 0000000000..8f351e9c5e
--- /dev/null
+++ b/.changeset/neat-trees-judge.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Fix FileInput to prevent file picker activation and file selection while busy. Clear drag-over highlighting when disabled or busy, and expose the busy trigger as disabled to assistive technologies.
+@korkt-kim
diff --git a/packages/core/src/FileInput/FileInput.test.tsx b/packages/core/src/FileInput/FileInput.test.tsx
index 54ad7ae80e..54a27ff782 100644
--- a/packages/core/src/FileInput/FileInput.test.tsx
+++ b/packages/core/src/FileInput/FileInput.test.tsx
@@ -974,6 +974,168 @@ describe('FileInput', () => {
});
});
+ describe('busy interaction', () => {
+ it('keeps a loading trigger focusable without opening the picker on click, Enter or Space', async () => {
+ const user = userEvent.setup();
+ render(
+ <FileInput label="Upload" value={null} onChange={() => {}} isLoading />,
+ );
+ const trigger = screen.getByRole('button', {name: 'Upload'});
+ const click = vi.spyOn(fileInputEl(), 'click');
+
+ await user.click(screen.getByText('Choose file'));
+ await user.tab();
+ expect(trigger).toHaveFocus();
+ await user.keyboard('{Enter} ');
+ fireEvent.click(trigger);
+
+ expect(click).not.toHaveBeenCalled();
+ expect(trigger).toHaveFocus();
+ expect(trigger).toHaveAttribute('aria-busy', 'true');
+ expect(trigger).toHaveAttribute('aria-disabled', 'true');
+ click.mockRestore();
+ });
+
+ it('ignores dropped files while loading without starting another Action', async () => {
+ const onChange = vi.fn();
+ const changeAction = vi.fn(async () => {});
+ render(
+ <FileInput
+ label="Upload"
+ value={null}
+ onChange={onChange}
+ changeAction={changeAction}
+ mode="dropzone"
+ isLoading
+ />,
+ );
+ const trigger = screen.getByRole('button', {name: 'Upload'});
+ const drop = createEvent.drop(trigger, {
+ dataTransfer: {files: [createFile('dropped.txt', 100)]},
+ });
+ await act(async () => fireEvent(trigger, drop));
+
+ expect(drop.defaultPrevented).toBe(true);
+ expect(onChange).not.toHaveBeenCalled();
+ expect(changeAction).not.toHaveBeenCalled();
+ });
+
+ it('cancels native input activation while loading and preserves the idle caller handler', () => {
+ const onClick = vi.fn();
+ const props = {label: 'Upload', value: null, onChange: vi.fn(), onClick};
+ const {rerender} = render(<FileInput {...props} isLoading />);
+ const input = fileInputEl();
+
+ expect(fireEvent.click(input)).toBe(false);
+ expect(onClick).not.toHaveBeenCalled();
+
+ rerender(<FileInput {...props} />);
+ expect(fireEvent.click(input)).toBe(true);
+ expect(onClick).toHaveBeenCalledTimes(1);
+ });
+
+ it('ignores a native picker result delivered after loading starts and accepts it once idle', () => {
+ const onChange = vi.fn();
+ const props = {label: 'Upload', value: null, onChange};
+ const {rerender} = render(<FileInput {...props} />);
+ const input = fileInputEl();
+ const file = createFile('report.txt', 100);
+ fireEvent.click(screen.getByRole('button', {name: 'Upload'}));
+ rerender(<FileInput {...props} isLoading />);
+
+ fireEvent.change(input, {target: {files: [file]}});
+ expect(onChange).not.toHaveBeenCalled();
+
+ rerender(<FileInput {...props} />);
+ fireEvent.change(input, {target: {files: [file]}});
+ expect(onChange).toHaveBeenCalledExactlyOnceWith(file);
+ });
+
+ it('discards drag feedback when loading starts and ignores new drags until idle', () => {
+ const props = {
+ label: 'Upload',
+ value: null,
+ onChange: vi.fn(),
+ mode: 'dropzone' as const,
+ };
+ const {rerender} = render(<FileInput {...props} isLoading />);
+ const trigger = screen.getByRole('button', {name: 'Upload'});
+ const enter = createEvent.dragEnter(trigger);
+ const over = createEvent.dragOver(trigger);
+ fireEvent(trigger, enter);
+ fireEvent(trigger, over);
+ expect(enter.defaultPrevented).toBe(true);
+ expect(over.defaultPrevented).toBe(true);
+
+ rerender(<FileInput {...props} />);
+ expect(screen.getByText('Choose file')).toBeInTheDocument();
+ expect(screen.queryByText('Drop files here')).not.toBeInTheDocument();
+
+ fireEvent.dragEnter(trigger);
+ expect(screen.getByText('Drop files here')).toBeInTheDocument();
+ rerender(<FileInput {...props} isLoading />);
+ rerender(<FileInput {...props} />);
+ expect(screen.getByText('Choose file')).toBeInTheDocument();
+ expect(screen.queryByText('Drop files here')).not.toBeInTheDocument();
+
+ fireEvent.dragOver(trigger);
+ expect(screen.getByText('Drop files here')).toBeInTheDocument();
+ });
+
+ it('blocks a second pick while changeAction is pending and allows picking again after settlement', async () => {
+ let finish: () => void = () => {};
+ const pending = new Promise<void>(resolve => {
+ finish = resolve;
+ });
+ const onChange = vi.fn();
+ const changeAction = vi.fn(async () => pending);
+ render(
+ <FileInput
+ label="Upload"
+ value={null}
+ onChange={onChange}
+ changeAction={changeAction}
+ mode="dropzone"
+ />,
+ );
+ const trigger = screen.getByRole('button', {name: 'Upload'});
+ const input = fileInputEl();
+ const click = vi.spyOn(input, 'click');
+ trigger.focus();
+ fireEvent.change(input, {
+ target: {files: [createFile('first.txt', 100)]},
+ });
+
+ try {
+ expect(trigger).toHaveAttribute('aria-busy', 'true');
+ fireEvent.click(trigger);
+ fireEvent.keyDown(trigger, {key: 'Enter'});
+ fireEvent.keyDown(trigger, {key: ' '});
+ fireEvent.drop(trigger, {
+ dataTransfer: {files: [createFile('second.txt', 100)]},
+ });
+
+ expect(click).not.toHaveBeenCalled();
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(changeAction).toHaveBeenCalledTimes(1);
+ expect(trigger).toHaveFocus();
+ expect(trigger).toHaveAttribute('aria-disabled', 'true');
+ } finally {
+ await act(async () => finish());
+ click.mockRestore();
+ }
+
+ expect(trigger).not.toHaveAttribute('aria-busy');
+ expect(trigger).not.toHaveAttribute('aria-disabled', 'true');
+ fireEvent.change(input, {
+ target: {files: [createFile('after.txt', 100)]},
+ });
+ await act(async () => {});
+ expect(onChange).toHaveBeenCalledTimes(2);
+ expect(changeAction).toHaveBeenCalledTimes(2);
+ });
+ });
+
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.tsx
index 65e9038aa1..0ece83b998 100644
--- a/packages/core/src/FileInput/FileInput.tsx
+++ b/packages/core/src/FileInput/FileInput.tsx
@@ -238,6 +238,10 @@ const styles = stylex.create({
':focus-within': colorVars['--color-accent'],
},
},
+ busy: {
+ cursor: 'default',
+ boxShadow: 'none',
+ },
hiddenInput: {
position: 'absolute',
width: 1,
@@ -500,6 +504,10 @@ export function FileInput({
const [, startTransition] = useTransition();
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
const isBusy = isLoading || optimisticValue !== value;
+ const isSelectionDisabled = isDisabled || isBusy;
+ if (isDragOver && isSelectionDisabled) {
+ setIsDragOver(false);
+ }
// A pick is one user action, already announced as "file selected"; a busy
// period it starts (changeAction, or a caller setting isLoading from
// onChange) adds no second message (spec:AST-086 FR6). The mark lasts until
@@ -574,7 +582,7 @@ export function FileInput({
const handleFiles = useCallback(
(fileList: File[]) => {
- if (isDisabled) {
+ if (isSelectionDisabled) {
return;
}
@@ -625,7 +633,7 @@ export function FileInput({
},
[
accept,
- isDisabled,
+ isSelectionDisabled,
isMultiple,
maxFiles,
maxSize,
@@ -681,22 +689,24 @@ export function FileInput({
);
const handleClick = useCallback(() => {
- if (!isDisabled) {
+ if (!isSelectionDisabled) {
inputRef.current?.click();
}
- }, [isDisabled]);
+ }, [isSelectionDisabled]);
// Explicit keyboard activation so Enter/Space open the picker even where a
- // synthetic keydown does not raise a native click. The isDisabled guard
- // blocks activation while focusable-disabled.
+ // synthetic keydown does not raise a native click. Disabled and busy triggers
+ // also cancel the default activation while remaining focusable.
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
- if ((e.key === 'Enter' || e.key === ' ') && !isDisabled) {
+ if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
- inputRef.current?.click();
+ if (!isSelectionDisabled) {
+ inputRef.current?.click();
+ }
}
},
- [isDisabled],
+ [isSelectionDisabled],
);
// Make the surface clickable without giving it an interactive role. Clicks on
@@ -706,29 +716,29 @@ export function FileInput({
useClickableContainer({
containerRef,
onClick: handleClick,
- disabled: isDisabled,
+ disabled: isSelectionDisabled,
});
const handleDragEnter = useCallback(
(e: DragEvent) => {
e.preventDefault();
e.stopPropagation();
- if (!isDisabled && mode === 'dropzone') {
+ if (!isSelectionDisabled && mode === 'dropzone') {
setIsDragOver(true);
}
},
- [isDisabled, mode],
+ [isSelectionDisabled, mode],
);
const handleDragOver = useCallback(
(e: DragEvent) => {
e.preventDefault();
e.stopPropagation();
- if (!isDisabled && mode === 'dropzone') {
+ if (!isSelectionDisabled && mode === 'dropzone') {
setIsDragOver(true);
}
},
- [isDisabled, mode],
+ [isSelectionDisabled, mode],
);
const handleDragLeave = useCallback((e: DragEvent) => {
@@ -751,7 +761,7 @@ export function FileInput({
e.preventDefault();
e.stopPropagation();
setIsDragOver(false);
- if (isDisabled || mode !== 'dropzone') {
+ if (isSelectionDisabled || mode !== 'dropzone') {
return;
}
const fileList = Array.from(e.dataTransfer.files);
@@ -759,7 +769,7 @@ export function FileInput({
handleFiles(fileList);
}
},
- [isDisabled, mode, handleFiles],
+ [isSelectionDisabled, mode, handleFiles],
);
const hasFiles =
@@ -889,17 +899,21 @@ export function FileInput({
// keyboard focus. Handlers are gated internally by isEnabled.
disabledMessageTooltip.ref(el);
}}
- onClick={!isDisabled ? onContainerClick : undefined}
- onMouseUp={!isDisabled ? onContainerMouseUp : undefined}
+ onClick={!isSelectionDisabled ? onContainerClick : undefined}
+ onMouseUp={!isSelectionDisabled ? onContainerMouseUp : undefined}
{...dragDropProps}
{...mergeProps(
themeProps('file-input', {mode, status: status?.type ?? null}),
stylex.props(
isDropzone ? styles.dropzone : styles.compact,
- isDropzone && !isDisabled && styles.dropzoneHover,
- isDropzone && isDragOver && styles.dropzoneActive,
+ isDropzone && !isSelectionDisabled && styles.dropzoneHover,
+ isDropzone &&
+ !isSelectionDisabled &&
+ isDragOver &&
+ styles.dropzoneActive,
isDropzone && isDisabled && styles.dropzoneDisabled,
!isDropzone && isDisabled && styles.compactDisabled,
+ isBusy && styles.busy,
status && statusBorderStyles[status.type],
controlXstyle,
),
@@ -919,7 +933,7 @@ export function FileInput({
type="button"
// A disabled trigger keeps its Tab stop (spec:AST-085 FR1).
tabIndex={0}
- aria-disabled={isDisabled ? 'true' : undefined}
+ aria-disabled={isSelectionDisabled ? 'true' : undefined}
onClick={handleClick}
onKeyDown={handleKeyDown}
// aria-label suppresses child content from the accessible name, so
@@ -947,8 +961,10 @@ export function FileInput({
// submitted and `required` does not block submission (FR3).
form={isDisabled ? '' : undefined}
// A label or description click activates this input directly; cancel
- // it so a disabled input never opens the picker (FR2).
- onClick={isDisabled ? event => event.preventDefault() : rest.onClick}
+ // it so a disabled or busy input never opens the picker (FR2).
+ onClick={
+ isSelectionDisabled ? event => event.preventDefault() : rest.onClick
+ }
onChange={handleInputChange}
// The visually-hidden native input is never focused (tabIndex={-1});
// its describedby/required/invalid live on the trigger button.