Skip to content

Commit

Permalink
fix: fix onChange fired without files issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo837 committed Dec 23, 2023
1 parent 6f55ae3 commit c596bb7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/utils/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function resolveOnChange<
}
let event = e;

debugger;

if (e.type === 'click') {
// Clone a new target for event.
// Avoid the following usage, the setQuery method gets the original value.
Expand Down Expand Up @@ -52,17 +54,18 @@ export function resolveOnChange<
}

// Trigger by composition event, this means we need force change the input value
if (targetValue !== undefined) {
if (target.type != 'file' && targetValue !== undefined) {
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;
}
// 'file' check move to above
// if (currentTarget.type !== 'file') {
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 c596bb7

Please sign in to comment.