Skip to content

Commit 695683d

Browse files
committed
Add drag and drop support
Refs: #6368
1 parent a2c16d1 commit 695683d

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

packages/components/src/components/input-file/shadow.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,35 @@ export class KolInputFile implements InputFileAPI, FocusableElement {
320320
this.controller.componentWillLoad();
321321
}
322322

323+
public componentDidLoad(): void {
324+
this._touched = this._touched === true;
325+
this.controller.componentWillLoad();
326+
const container = this.inputRef?.parentElement?.parentElement;
327+
container?.addEventListener('dragover', this.onDragOver);
328+
container?.addEventListener('dragleave', this.onDragLeave);
329+
container?.addEventListener('drop', this.onDrop);
330+
}
331+
332+
private onDragOver = (event: DragEvent): void => {
333+
event.preventDefault();
334+
this.inputRef?.parentElement?.parentElement?.classList.add('kol-input-container--is-dragover');
335+
};
336+
337+
private onDragLeave = (): void => {
338+
this.inputRef?.parentElement?.parentElement?.classList.remove('kol-input-container--is-dragover');
339+
};
340+
341+
private onDrop = (event: DragEvent): void => {
342+
event.preventDefault();
343+
this.inputRef?.parentElement?.parentElement?.classList.remove('kol-input-container--is-dragover');
344+
if (event.dataTransfer?.files.length) {
345+
const files = event.dataTransfer.files;
346+
this.filename = Array.from(files)
347+
.map((file) => file.name)
348+
.join(', ');
349+
this.controller.setFormAssociatedValue(files);
350+
}
351+
};
323352
private onChange = (event: Event): void => {
324353
if (this.inputRef instanceof HTMLInputElement && this.inputRef.type === 'file') {
325354
const value = this.inputRef.files;

packages/themes/default/src/components/input-file.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
&__filename {
1212
color: var(--color-subtle);
1313
}
14+
15+
&--is-dragover {
16+
border-color: var(--color-primary) !important;
17+
}
1418
}
1519

1620
.kol-button {

packages/themes/ecl/src/ecl-ec/components/input-file.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
white-space: nowrap;
1010
}
1111
}
12+
13+
.kol-input-container {
14+
&--is-dragover {
15+
border-color: var(--color-blue) !important;
16+
}
17+
}
1218
}

packages/themes/ecl/src/ecl-eu/components/input-file.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@
1010
white-space: nowrap;
1111
}
1212
}
13+
14+
.kol-input-container {
15+
&--is-dragover {
16+
border-color: var(--color-blue) !important;
17+
}
18+
}
1319
}

0 commit comments

Comments
 (0)