From c596bb720da233090a87eed2ef0f1849ef387ecc Mon Sep 17 00:00:00 2001 From: tanghui Date: Sat, 23 Dec 2023 22:36:41 +0800 Subject: [PATCH 1/4] fix: fix onChange fired without files issue --- src/utils/commonUtils.ts | 11 +++++++---- tests/index.test.tsx | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 15847d8..47bd70b 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -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. @@ -52,7 +54,7 @@ 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, { @@ -60,9 +62,10 @@ export function resolveOnChange< 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); 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); }); }); From 006990ec34c28ffd49af0ef636964cb6aa14d1c0 Mon Sep 17 00:00:00 2001 From: tanghui Date: Sat, 23 Dec 2023 22:38:28 +0800 Subject: [PATCH 2/4] chore: remove debugger --- src/utils/commonUtils.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 47bd70b..20d830d 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -25,8 +25,6 @@ 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. From 8a7cb9f47bfb1587b7e73da2ee027f1a6f6a03fb Mon Sep 17 00:00:00 2001 From: tanghui Date: Sat, 23 Dec 2023 22:55:56 +0800 Subject: [PATCH 3/4] chore: comment more --- src/utils/commonUtils.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 20d830d..880d600 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 @@ -53,6 +56,9 @@ export function resolveOnChange< // Trigger by composition event, this means we need force change the input value 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, { From e8777c8744e74bbc216b833e45b914579fed2bb2 Mon Sep 17 00:00:00 2001 From: tanghui Date: Sun, 24 Dec 2023 11:42:01 +0800 Subject: [PATCH 4/4] chore: code style --- src/utils/commonUtils.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 880d600..30a0e84 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -55,7 +55,9 @@ export function resolveOnChange< } // Trigger by composition event, this means we need force change the input value - if (target.type != 'file' && 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 @@ -65,11 +67,7 @@ export function resolveOnChange< target: { value: currentTarget }, currentTarget: { value: currentTarget }, }); - // https://github.com/ant-design/ant-design/issues/45737 - // 'file' check move to above - // if (currentTarget.type !== 'file') { currentTarget.value = targetValue; - // } onChange(event as React.ChangeEvent); return; }