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

fix: fix input-file dragged file receiving required error #411

Merged
merged 6 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 39 additions & 10 deletions src/lib/elements/forms/inputFile.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import { Trim } from '$lib/components';
import { humanFileSize } from '$lib/helpers/sizeConvertion';
import { onMount } from 'svelte';
import { Helper } from '.';

export let label: string = null;
export let files: FileList;
export let list = new DataTransfer();

export let allowedFileExtensions: string[] = [];
export let maxSize: number = null;
export let required = false;
Expand All @@ -14,16 +15,35 @@
let input: HTMLInputElement;
let hovering = false;

function setFiles(value: FileList) {
if (!value) return;

const hasInvalidExt = Array.from(value).some((file) => {
const fileExtension = file.name.split('.').pop();
return !allowedFileExtensions.includes(fileExtension);
});
if (hasInvalidExt) {
error = 'Invalid file extension';
return;
}

files = value;
input.files = value;
}

function resetFiles() {
setFiles(new DataTransfer().files);
}

function dropHandler(ev: DragEvent) {
ev.dataTransfer.dropEffect = 'move';
hovering = false;
if (ev.dataTransfer.items) {
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
if (ev.dataTransfer.items[i].kind === 'file') {
list.items.clear();
list.items.add(ev.dataTransfer.items[i].getAsFile());
files = list.files;
}
if (!ev.dataTransfer.items) return;
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
if (ev.dataTransfer.items[i].kind === 'file') {
const dataTransfer = new DataTransfer();
dataTransfer.items.add(ev.dataTransfer.items[i].getAsFile());
setFiles(dataTransfer.files);
}
}
}
Expand All @@ -47,10 +67,19 @@
}

$: fileArray = files?.length ? Array.from(files) : [];

onMount(() => {
setFiles(files);
});

const handleChange = (event: Event) => {
const target = event.currentTarget as HTMLInputElement;
setFiles(target.files);
};
</script>

<input
bind:files
on:change={handleChange}
bind:this={input}
accept={allowedFileExtensions.map((n) => `.${n}`).join(',')}
type="file"
Expand Down Expand Up @@ -123,7 +152,7 @@
{fileSize.value + fileSize.unit}
</span>
<button
on:click={() => (files = null)}
on:click|preventDefault={resetFiles}
type="button"
class="button is-text is-only-icon u-margin-inline-start-auto"
aria-label="remove file"
Expand Down