Skip to content

Commit 7d5f748

Browse files
authored
refactor(ui5-file-uploader): remove form element and cleanup (#12789)
* File input can reset by assigning `new DataTransfer().files` directly, so there's no need to create an extra `<form>` element. * An empty file input already returns `null`, so you don't need to create a temporary input element just to retrieve its default value.
1 parent 5f8bf37 commit 7d5f748

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

packages/main/src/FileUploader.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,6 @@ class FileUploader extends UI5Element implements IFormInputElement {
304304
@property({ type: Boolean, noAttribute: true })
305305
_tokenizerOpen = false;
306306

307-
static emptyInput: HTMLInputElement;
308-
309307
@i18n("@ui5/webcomponents")
310308
static i18nBundle: I18nBundle;
311309

@@ -488,18 +486,14 @@ class FileUploader extends UI5Element implements IFormInputElement {
488486
this._clearFileSelection();
489487
}
490488

491-
_onFormSubmit(e: SubmitEvent) {
492-
e.preventDefault();
493-
}
494-
495489
_openFileBrowser() {
496490
this._input.click();
497491
}
498492

499493
_clearFileSelection() {
500494
this._selectedFilesNames = [];
501495
this.value = "";
502-
this._form?.reset();
496+
this._input.files = new DataTransfer().files;
503497
this.fireDecoratorEvent("change", {
504498
files: this.files,
505499
});
@@ -515,7 +509,7 @@ class FileUploader extends UI5Element implements IFormInputElement {
515509
return this._input.files;
516510
}
517511

518-
return FileUploader._emptyFilesList;
512+
return null;
519513
}
520514

521515
onAfterRendering() {
@@ -621,18 +615,6 @@ class FileUploader extends UI5Element implements IFormInputElement {
621615
}
622616
}
623617

624-
/**
625-
* in case when the component is not placed in the DOM, return empty FileList, like native input would do
626-
* @private
627-
*/
628-
static get _emptyFilesList() {
629-
if (!this.emptyInput) {
630-
this.emptyInput = document.createElement("input");
631-
this.emptyInput.type = "file";
632-
}
633-
return this.emptyInput.files;
634-
}
635-
636618
get accInfo(): InputAccInfo {
637619
return {
638620
"ariaRoledescription": FileUploader.i18nBundle.getText(FILEUPLOADER_ROLE_DESCRIPTION),

packages/main/src/FileUploaderTemplate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function FileUploaderTemplate(this: FileUploader) {
2020
onDragOver={this._ondrag}
2121
onDrop={this._ondrop}
2222
>
23-
<form class="ui5-file-uploader-form" onSubmit={this._onFormSubmit}>
23+
<div class="ui5-file-uploader-form">
2424
<input
2525
type="file"
2626
class="ui5-file-uploader-native-input"
@@ -39,7 +39,7 @@ export default function FileUploaderTemplate(this: FileUploader) {
3939
onChange={this._onChange}
4040
data-sap-focus-ref
4141
/>
42-
</form>
42+
</div>
4343

4444
{this.hideInput ? (
4545
<slot></slot>

0 commit comments

Comments
 (0)