Skip to content

Commit 6ab7744

Browse files
authored
fix(FileUploader): prevent double file dialog on Enter key in Chrome (#19950)
1 parent 5c89a5a commit 6ab7744

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/react/src/components/FileUploader/FileUploaderButton.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ function FileUploaderButton({
148148
}
149149

150150
function onKeyDown(event) {
151-
if (matches(event, [keys.Enter, keys.Space]) && inputNode.current) {
152-
inputNode.current.value = '';
153-
inputNode.current.click();
151+
if (matches(event, [keys.Enter, keys.Space])) {
152+
event.preventDefault();
153+
if (inputNode.current) {
154+
inputNode.current.value = '';
155+
inputNode.current.click();
156+
}
154157
}
155158
}
156159

packages/react/src/components/FileUploader/__tests__/FileUploaderButton-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ describe('FileUploaderButton', () => {
5050
expect(onChange).toHaveBeenCalledTimes(1);
5151
});
5252

53+
it('should not trigger multiple file dialogs when using keyboard Enter', async () => {
54+
const onChange = jest.fn();
55+
56+
render(<FileUploaderButton onChange={onChange} labelText="Add file" />);
57+
58+
const button = screen.getByRole('button', { name: /add file/i });
59+
const input = screen.getByLabelText(/add file/i);
60+
61+
// Simulate keyboard interaction
62+
button.focus();
63+
await userEvent.keyboard('{Enter}');
64+
65+
// Simulate file upload (since real dialog can't be tested)
66+
const file = new File(['test'], 'test.png', { type: 'image/png' });
67+
await userEvent.upload(input, file);
68+
69+
expect(onChange).toHaveBeenCalledTimes(1);
70+
});
71+
5372
it('should not support multiple files by default', () => {
5473
const { container } = render(<FileUploaderButton />);
5574
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access

0 commit comments

Comments
 (0)