Skip to content

Expose inputRef and and add required prop to allow html5 validations fixes #93 #102

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

Merged
merged 15 commits into from
Feb 10, 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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ yarn add svelte-file-dropzone
| containerClasses | custom container classes | "" |
| containerStyles | custom inline container styles | "" |
| disableDefaultStyles | don't apply default styles to container | false |
| inputElement | reference to inputElement | undefined |
| required | html5 required attribute added to input | false |

### Events

Expand Down
33 changes: 21 additions & 12 deletions src/components/Dropzone.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
export let containerStyles = "";
export let disableDefaultStyles = false;
export let name = "";
export let inputElement;
export let required = false;
const dispatch = createEventDispatcher();

//state
Expand All @@ -44,11 +46,10 @@
isDragReject: false,
draggedFiles: [],
acceptedFiles: [],
fileRejections: []
fileRejections: [],
};

let rootRef;
let inputRef;

function resetState() {
state.isFileDialogActive = false;
Expand All @@ -60,10 +61,10 @@

// Fn for opening the file dialog programmatically
function openFileDialog() {
if (inputRef) {
inputRef.value = null; // TODO check if null needs to be set
if (inputElement) {
inputElement.value = null; // TODO check if null needs to be set
state.isFileDialogActive = true;
inputRef.click();
inputElement.click();
}
}

Expand Down Expand Up @@ -211,6 +212,11 @@
acceptedFiles.splice(0);
}

// Files dropped keep input in sync
if (event.dataTransfer) {
inputElement.files = event.dataTransfer.files;
}

state.acceptedFiles = acceptedFiles;
state.fileRejections = fileRejections;

Expand Down Expand Up @@ -281,8 +287,8 @@
// Execute the timeout only if the file dialog is opened in the browser
if (state.isFileDialogActive) {
setTimeout(() => {
if (inputRef) {
const { files } = inputRef;
if (inputElement) {
const { files } = inputElement;

if (!files.length) {
state.isFileDialogActive = false;
Expand All @@ -295,7 +301,7 @@

onDestroy(() => {
// This is critical for canceling the timeout behaviour on `onWindowFocus()`
inputRef = null;
inputElement = null;
});

function onInputElementClick(event) {
Expand Down Expand Up @@ -339,18 +345,21 @@
on:dragenter={composeDragHandler(onDragEnterCb)}
on:dragover={composeDragHandler(onDragOverCb)}
on:dragleave={composeDragHandler(onDragLeaveCb)}
on:drop={composeDragHandler(onDropCb)}>
on:drop={composeDragHandler(onDropCb)}
>
<input
{accept}
{multiple}
{required}
type="file"
name={name}
{name}
autocomplete="off"
tabindex="-1"
on:change={onDropCb}
on:click={onInputElementClick}
bind:this={inputRef}
style="display: none;" />
bind:this={inputElement}
style="display: none;"
/>
<slot>
<p>Drag 'n' drop some files here, or click to select files</p>
</slot>
Expand Down