Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.handleInputChange() - use .currentTarget; clear the input using '' #5381

Merged
merged 2 commits into from
Oct 15, 2024

Conversation

lakesare
Copy link
Collaborator

@lakesare lakesare commented Aug 1, 2024

This PR:

  • created a proper demo of the bug event.target.value = '' is preventing, added that demo to the comments
  • properly type the text input event: we go from ChangeEvent to TargetedEvent<HTMLInputElement, Event>
  • everything else (target => currentTarget, null => '') is a consequence of the correct typing

New version is tested in:

  • Firefox
  • Chrome
  • Safari

Testing consists of:

  1. make sure uploading a duplicate file always triggers onBeforeFileAdded
  2. make sure currentTarget.files actually has files

Copy link
Contributor

github-actions bot commented Aug 1, 2024

Diff output files
diff --git a/packages/@uppy/dashboard/lib/Dashboard.js b/packages/@uppy/dashboard/lib/Dashboard.js
index a4e8014..d666201 100644
--- a/packages/@uppy/dashboard/lib/Dashboard.js
+++ b/packages/@uppy/dashboard/lib/Dashboard.js
@@ -494,7 +494,7 @@ export default class Dashboard extends UIPlugin {
     };
     this.handleInputChange = event => {
       event.preventDefault();
-      const files = toArray(event.target.files);
+      const files = toArray(event.currentTarget.files || []);
       if (files.length > 0) {
         this.uppy.log("[Dashboard] Files selected through input");
         this.addFiles(files);
diff --git a/packages/@uppy/dashboard/lib/components/AddFiles.js b/packages/@uppy/dashboard/lib/components/AddFiles.js
index 8a0edf9..8e4ee54 100644
--- a/packages/@uppy/dashboard/lib/components/AddFiles.js
+++ b/packages/@uppy/dashboard/lib/components/AddFiles.js
@@ -16,7 +16,7 @@ class AddFiles extends Component {
     };
     this.onFileInputChange = event => {
       this.props.handleInputChange(event);
-      event.target.value = null;
+      event.currentTarget.value = "";
     };
     this.renderHiddenInput = (isFolder, refCallback) => {
       var _this$props$allowedFi;
diff --git a/packages/@uppy/drag-drop/lib/DragDrop.js b/packages/@uppy/drag-drop/lib/DragDrop.js
index 5f2d2c3..2515b3b 100644
--- a/packages/@uppy/drag-drop/lib/DragDrop.js
+++ b/packages/@uppy/drag-drop/lib/DragDrop.js
@@ -36,12 +36,12 @@ export default class DragDrop extends UIPlugin {
       }
     };
     this.onInputChange = event => {
-      const files = toArray(event.target.files);
+      const files = toArray(event.currentTarget.files || []);
       if (files.length > 0) {
         this.uppy.log("[DragDrop] Files selected through input");
         this.addFiles(files);
       }
-      event.target.value = null;
+      event.currentTarget.value = "";
     };
     this.handleDragOver = event => {
       var _this$opts$onDragOver, _this$opts;
diff --git a/packages/@uppy/file-input/lib/FileInput.js b/packages/@uppy/file-input/lib/FileInput.js
index 13cfa50..e859b0c 100644
--- a/packages/@uppy/file-input/lib/FileInput.js
+++ b/packages/@uppy/file-input/lib/FileInput.js
@@ -40,9 +40,9 @@ export default class FileInput extends UIPlugin {
   }
   handleInputChange(event) {
     this.uppy.log("[FileInput] Something selected through input...");
-    const files = toArray(event.target.files);
+    const files = toArray(event.currentTarget.files || []);
     this.addFiles(files);
-    event.target.value = null;
+    event.currentTarget.value = "";
   }
   handleClick() {
     this.input.click();

@lakesare lakesare force-pushed the lakesare/event-target-value branch from 536bfc9 to 2ee1645 Compare August 2, 2024 08:32
@lakesare lakesare changed the title Clear file input value using an '' instead of null .handleInputChange() - use .currentTarget; clear the input using '' Aug 2, 2024
@lakesare lakesare marked this pull request as ready for review August 2, 2024 09:01
event.preventDefault()
const files = toArray((event.target as HTMLInputElement).files!)
const files = toArray(event.currentTarget.files || [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const files = toArray(event.currentTarget.files || [])
const files = toArray(event.currentTarget.files ?? [])

private onInputChange = (event: ChangeEvent) => {
const files = toArray((event.target as HTMLInputElement).files!)
private onInputChange = (event: TargetedEvent<HTMLInputElement, Event>) => {
const files = toArray(event.currentTarget.files || [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const files = toArray(event.currentTarget.files || [])
const files = toArray(event.currentTarget.files ?? [])

this.uppy.log('[FileInput] Something selected through input...')
const files = toArray((event.target as HTMLFileInputElement).files)
const files = toArray(event.currentTarget.files || [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const files = toArray(event.currentTarget.files || [])
const files = toArray(event.currentTarget.files ?? [])

@mifi
Copy link
Contributor

mifi commented Aug 4, 2024

I assume you checked that using currentTarget in place of target is safe (e.g. they seem to differ with regards to bubbling)

@Murderlon Murderlon merged commit 9a4b8ef into main Oct 15, 2024
16 of 17 checks passed
@Murderlon Murderlon deleted the lakesare/event-target-value branch October 15, 2024 08:09
Murderlon added a commit that referenced this pull request Oct 15, 2024
* main: (65 commits)
  `.handleInputChange()` - use `.currentTarget`; clear the input using `''` (#5381)
  build(deps): bump @blakeembrey/template from 1.1.0 to 1.2.0 (#5448)
  Update packages/@uppy/locales/src/fr_FR.ts (#5472)
  @uppy/svelte: use SvelteKit as the build tool (#5484)
  @uppy/xhr-upload: add response to upload-error callback (#5486)
  tus: Avoid duplicate `upload-error` event (#5485)
  Fix redis emitter (#5474)
  build(deps): bump docker/build-push-action from 6.8.0 to 6.9.0 (#5483)
  Release: [email protected] (#5479)
  @uppy/transloadit: fix multiple upload batches & run again (#5478)
  build(deps): bump docker/build-push-action from 6.7.0 to 6.8.0 (#5477)
  build(deps): bump vite from 5.2.11 to 5.4.8 (#5471)
  build(deps-dev): bump rollup from 4.18.0 to 4.22.4 (#5470)
  build(deps): bump vite from 5.2.11 to 5.4.6 (#5466)
  Release: [email protected] (#5467)
  @uppy/tus: fix retry check for status code 400 (#5461)
  meta: fix AwsS3 endpoint option in private/dev
  build(deps): bump body-parser from 1.20.2 to 1.20.3 (#5462)
  build(deps-dev): bump vite from 5.3.1 to 5.3.6 (#5459)
  @uppy/tus: set response from tus-js-client (#5456)
  ...
github-actions bot added a commit that referenced this pull request Oct 15, 2024
| Package          | Version | Package          | Version |
| ---------------- | ------- | ---------------- | ------- |
| @uppy/companion  |   5.1.2 | @uppy/svelte     |   4.1.0 |
| @uppy/core       |   4.2.2 | @uppy/tus        |   4.1.2 |
| @uppy/dashboard  |   4.1.1 | @uppy/utils      |   6.0.3 |
| @uppy/drag-drop  |   4.0.3 | @uppy/xhr-upload |   4.2.1 |
| @uppy/file-input |   4.0.2 | uppy             |   4.5.0 |
| @uppy/locales    |   4.2.0 |                  |         |

- @uppy/dashboard: Dashboard - convert some files to typescript  (Evgenia Karunus / #5367)
- @uppy/dashboard,@uppy/drag-drop,@uppy/file-input:  `.handleInputChange()` - use `.currentTarget`; clear the input using `''` (Evgenia Karunus / #5381)
- meta: build(deps): bump @blakeembrey/template from 1.1.0 to 1.2.0 (dependabot[bot] / #5448)
- @uppy/locales: Update packages/@uppy/locales/src/fr_FR.ts (Zéfyx / #5472)
- @uppy/svelte: use SvelteKit as the build tool (Merlijn Vos / #5484)
- @uppy/xhr-upload: add response to upload-error callback (Caleb Hardin / #5486)
- @uppy/tus: tus: Avoid duplicate `upload-error` event (Marius / #5485)
- @uppy/companion: Fix redis emitter (Mikael Finstad / #5474)
- meta: build(deps): bump docker/build-push-action from 6.8.0 to 6.9.0 (dependabot[bot] / #5483)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants