Skip to content

Commit 4c1b771

Browse files
authored
Merge pull request #102 from zwergius/master
Expose inputRef and and add required prop to allow html5 validations fixes #93
2 parents ca21a95 + 92cc08c commit 4c1b771

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ yarn add svelte-file-dropzone
6363
| containerClasses | custom container classes | "" |
6464
| containerStyles | custom inline container styles | "" |
6565
| disableDefaultStyles | don't apply default styles to container | false |
66+
| inputElement | reference to inputElement | undefined |
67+
| required | html5 required attribute added to input | false |
6668

6769
### Events
6870

src/components/Dropzone.svelte

+21-12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
export let containerStyles = "";
3333
export let disableDefaultStyles = false;
3434
export let name = "";
35+
export let inputElement;
36+
export let required = false;
3537
const dispatch = createEventDispatcher();
3638
3739
//state
@@ -44,11 +46,10 @@
4446
isDragReject: false,
4547
draggedFiles: [],
4648
acceptedFiles: [],
47-
fileRejections: []
49+
fileRejections: [],
4850
};
4951
5052
let rootRef;
51-
let inputRef;
5253
5354
function resetState() {
5455
state.isFileDialogActive = false;
@@ -60,10 +61,10 @@
6061
6162
// Fn for opening the file dialog programmatically
6263
function openFileDialog() {
63-
if (inputRef) {
64-
inputRef.value = null; // TODO check if null needs to be set
64+
if (inputElement) {
65+
inputElement.value = null; // TODO check if null needs to be set
6566
state.isFileDialogActive = true;
66-
inputRef.click();
67+
inputElement.click();
6768
}
6869
}
6970
@@ -211,6 +212,11 @@
211212
acceptedFiles.splice(0);
212213
}
213214
215+
// Files dropped keep input in sync
216+
if (event.dataTransfer) {
217+
inputElement.files = event.dataTransfer.files;
218+
}
219+
214220
state.acceptedFiles = acceptedFiles;
215221
state.fileRejections = fileRejections;
216222
@@ -281,8 +287,8 @@
281287
// Execute the timeout only if the file dialog is opened in the browser
282288
if (state.isFileDialogActive) {
283289
setTimeout(() => {
284-
if (inputRef) {
285-
const { files } = inputRef;
290+
if (inputElement) {
291+
const { files } = inputElement;
286292
287293
if (!files.length) {
288294
state.isFileDialogActive = false;
@@ -295,7 +301,7 @@
295301
296302
onDestroy(() => {
297303
// This is critical for canceling the timeout behaviour on `onWindowFocus()`
298-
inputRef = null;
304+
inputElement = null;
299305
});
300306
301307
function onInputElementClick(event) {
@@ -339,18 +345,21 @@
339345
on:dragenter={composeDragHandler(onDragEnterCb)}
340346
on:dragover={composeDragHandler(onDragOverCb)}
341347
on:dragleave={composeDragHandler(onDragLeaveCb)}
342-
on:drop={composeDragHandler(onDropCb)}>
348+
on:drop={composeDragHandler(onDropCb)}
349+
>
343350
<input
344351
{accept}
345352
{multiple}
353+
{required}
346354
type="file"
347-
name={name}
355+
{name}
348356
autocomplete="off"
349357
tabindex="-1"
350358
on:change={onDropCb}
351359
on:click={onInputElementClick}
352-
bind:this={inputRef}
353-
style="display: none;" />
360+
bind:this={inputElement}
361+
style="display: none;"
362+
/>
354363
<slot>
355364
<p>Drag 'n' drop some files here, or click to select files</p>
356365
</slot>

0 commit comments

Comments
 (0)