Skip to content

Commit 9640649

Browse files
authored
fix: fix onChange fired without files issue (#58)
* fix: fix onChange fired without files issue * chore: remove debugger * chore: comment more * chore: code style
1 parent 6f55ae3 commit 9640649

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/utils/commonUtils.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export function resolveOnChange<
3838
// }}
3939
// />
4040

41+
// 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.
42+
// As of the last update, this bug was still marked as "NEW," indicating that it might not have been resolved yet​​.
43+
// https://bugs.webkit.org/show_bug.cgi?id=28123
4144
const currentTarget = target.cloneNode(true) as E;
4245

4346
// click clear icon
@@ -52,17 +55,19 @@ export function resolveOnChange<
5255
}
5356

5457
// Trigger by composition event, this means we need force change the input value
55-
if (targetValue !== undefined) {
58+
// https://github.com/ant-design/ant-design/issues/45737
59+
// https://github.com/ant-design/ant-design/issues/46598
60+
if (target.type !== 'file' && targetValue !== undefined) {
61+
// 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.
62+
// As of the last update, this bug was still marked as "NEW," indicating that it might not have been resolved yet​​.
63+
// https://bugs.webkit.org/show_bug.cgi?id=28123
5664
const currentTarget = target.cloneNode(true) as E;
5765

5866
event = Object.create(e, {
5967
target: { value: currentTarget },
6068
currentTarget: { value: currentTarget },
6169
});
62-
// https://github.com/ant-design/ant-design/issues/45737
63-
if (currentTarget.type !== 'file') {
64-
currentTarget.value = targetValue;
65-
}
70+
currentTarget.value = targetValue;
6671
onChange(event as React.ChangeEvent<E>);
6772
return;
6873
}

tests/index.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ describe('Input', () => {
9090
fireEvent.change(inputEl, {
9191
target: { files: [file] },
9292
});
93+
94+
expect(onChange).toHaveBeenCalled();
95+
expect(onChange.mock.calls[0][0].target.files[0]).toBe(file);
9396
});
9497
});
9598

0 commit comments

Comments
 (0)