Skip to content

Commit

Permalink
fix: fix onChange fired without files issue (#58)
Browse files Browse the repository at this point in the history
* fix: fix onChange fired without files issue

* chore: remove debugger

* chore: comment more

* chore: code style
  • Loading branch information
yoyo837 authored Dec 24, 2023
1 parent 6f55ae3 commit 9640649
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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);
});
});

Expand Down

0 comments on commit 9640649

Please sign in to comment.