From 964064924f2c7e2747f6d4444f713fd0ff46908d Mon Sep 17 00:00:00 2001 From: Amumu Date: Sun, 24 Dec 2023 14:38:19 +0800 Subject: [PATCH] fix: fix onChange fired without files issue (#58) * fix: fix onChange fired without files issue * chore: remove debugger * chore: comment more * chore: code style --- src/utils/commonUtils.ts | 15 ++++++++++----- tests/index.test.tsx | 3 +++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 15847d8..30a0e84 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -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 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 @@ -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 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); return; } diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 2a88538..bb5c757 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -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); }); });