Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix onChange fired without files issue #58

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/utils/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export function resolveOnChange<
// }}
// />

// A bug report filed on WebKit's Bugzilla tracker, dating back to 2009, specifically addresses the issue of cloneNode() not copying files of <input type="file"> elements.
// As of the last update, this bug was still marked as "NEW," indicating that it might not have been resolved yet​​.
// https://bugs.webkit.org/show_bug.cgi?id=28123
const currentTarget = target.cloneNode(true) as E;

// click clear icon
Expand All @@ -52,17 +55,19 @@ export function resolveOnChange<
}

// Trigger by composition event, this means we need force change the input value
if (targetValue !== undefined) {
// https://github.com/ant-design/ant-design/issues/45737
// https://github.com/ant-design/ant-design/issues/46598
if (target.type !== 'file' && targetValue !== undefined) {
// A bug report filed on WebKit's Bugzilla tracker, dating back to 2009, specifically addresses the issue of cloneNode() not copying files of <input type="file"> elements.
// As of the last update, this bug was still marked as "NEW," indicating that it might not have been resolved yet​​.
// https://bugs.webkit.org/show_bug.cgi?id=28123
const currentTarget = target.cloneNode(true) as E;

event = Object.create(e, {
target: { value: currentTarget },
currentTarget: { value: currentTarget },
});
// https://github.com/ant-design/ant-design/issues/45737
if (currentTarget.type !== 'file') {
currentTarget.value = targetValue;
}
currentTarget.value = targetValue;
onChange(event as React.ChangeEvent<E>);
return;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ describe('Input', () => {
fireEvent.change(inputEl, {
target: { files: [file] },
});

expect(onChange).toHaveBeenCalled();
expect(onChange.mock.calls[0][0].target.files[0]).toBe(file);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this test case without fix can make the test fail.

});
});

Expand Down
Loading