Skip to content

Commit 30bd0f1

Browse files
committed
Expose inputRef and and add required prop to allow html5 validations
1 parent 8cb257c commit 30bd0f1

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

src/components/Dropzone.svelte

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
isIeOrEdge,
1010
isPropagationStopped,
1111
onDocumentDragOver,
12-
TOO_MANY_FILES_REJECTION
12+
TOO_MANY_FILES_REJECTION,
1313
} from "./../utils/index";
1414
import { onMount, onDestroy, createEventDispatcher } from "svelte";
1515
@@ -33,8 +33,11 @@
3333
export let containerStyles = "";
3434
export let disableDefaultStyles = false;
3535
export let name = "";
36+
export let inputRef;
37+
export let required = false;
3638
const dispatch = createEventDispatcher();
3739
40+
console.log("yeeehaaaa here is the local UPDATED version!");
3841
//state
3942
4043
let state = {
@@ -45,11 +48,10 @@
4548
isDragReject: false,
4649
draggedFiles: [],
4750
acceptedFiles: [],
48-
fileRejections: []
51+
fileRejections: [],
4952
};
5053
5154
let rootRef;
52-
let inputRef;
5355
5456
function resetState() {
5557
state.isFileDialogActive = false;
@@ -112,7 +114,7 @@
112114
dragTargetsRef = [...dragTargetsRef, event.target];
113115
114116
if (isEvtWithFiles(event)) {
115-
Promise.resolve(getFilesFromEvent(event)).then(draggedFiles => {
117+
Promise.resolve(getFilesFromEvent(event)).then((draggedFiles) => {
116118
if (isPropagationStopped(event) && !noDragEventsBubbling) {
117119
return;
118120
}
@@ -121,7 +123,7 @@
121123
state.isDragActive = true;
122124
123125
dispatch("dragenter", {
124-
dragEvent: event
126+
dragEvent: event,
125127
});
126128
});
127129
}
@@ -139,7 +141,7 @@
139141
140142
if (isEvtWithFiles(event)) {
141143
dispatch("dragover", {
142-
dragEvent: event
144+
dragEvent: event,
143145
});
144146
}
145147
@@ -152,7 +154,7 @@
152154
153155
// Only deactivate once the dropzone and all children have been left
154156
const targets = dragTargetsRef.filter(
155-
target => rootRef && rootRef.contains(target)
157+
(target) => rootRef && rootRef.contains(target)
156158
);
157159
// Make sure to remove a target present multiple times only once
158160
// (Firefox may fire dragenter/dragleave multiple times on the same element)
@@ -170,7 +172,7 @@
170172
171173
if (isEvtWithFiles(event)) {
172174
dispatch("dragleave", {
173-
dragEvent: event
175+
dragEvent: event,
174176
});
175177
}
176178
}
@@ -183,55 +185,60 @@
183185
184186
if (isEvtWithFiles(event)) {
185187
dispatch("filedropped", {
186-
event
187-
})
188-
Promise.resolve(getFilesFromEvent(event)).then(files => {
188+
event,
189+
});
190+
Promise.resolve(getFilesFromEvent(event)).then((files) => {
189191
if (isPropagationStopped(event) && !noDragEventsBubbling) {
190192
return;
191193
}
192194
193195
const acceptedFiles = [];
194196
const fileRejections = [];
195197
196-
files.forEach(file => {
198+
files.forEach((file) => {
197199
const [accepted, acceptError] = fileAccepted(file, accept);
198200
const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize);
199201
if (accepted && sizeMatch) {
200202
acceptedFiles.push(file);
201203
} else {
202-
const errors = [acceptError, sizeError].filter(e => e);
204+
const errors = [acceptError, sizeError].filter((e) => e);
203205
fileRejections.push({ file, errors });
204206
}
205207
});
206208
207209
if (!multiple && acceptedFiles.length > 1) {
208210
// Reject everything and empty accepted files
209-
acceptedFiles.forEach(file => {
211+
acceptedFiles.forEach((file) => {
210212
fileRejections.push({ file, errors: [TOO_MANY_FILES_REJECTION] });
211213
});
212214
acceptedFiles.splice(0);
213215
}
214216
217+
// Files dropped keep input in sync
218+
if (event.dataTransfer) {
219+
inputRef.files = event.dataTransfer?.files;
220+
}
221+
215222
state.acceptedFiles = acceptedFiles;
216223
state.fileRejections = fileRejections;
217224
218225
dispatch("drop", {
219226
acceptedFiles,
220227
fileRejections,
221-
event
228+
event,
222229
});
223230
224231
if (fileRejections.length > 0) {
225232
dispatch("droprejected", {
226233
fileRejections,
227-
event
234+
event,
228235
});
229236
}
230237
231238
if (acceptedFiles.length > 0) {
232239
dispatch("dropaccepted", {
233240
acceptedFiles,
234-
event
241+
event,
235242
});
236243
}
237244
});
@@ -305,27 +312,6 @@
305312
}
306313
</script>
307314
308-
<style>
309-
.dropzone {
310-
flex: 1;
311-
display: flex;
312-
flex-direction: column;
313-
align-items: center;
314-
padding: 20px;
315-
border-width: 2px;
316-
border-radius: 2px;
317-
border-color: #eeeeee;
318-
border-style: dashed;
319-
background-color: #fafafa;
320-
color: #bdbdbd;
321-
outline: none;
322-
transition: border 0.24s ease-in-out;
323-
}
324-
.dropzone:focus {
325-
border-color: #2196f3;
326-
}
327-
</style>
328-
329315
<div
330316
bind:this={rootRef}
331317
tabindex="0"
@@ -339,19 +325,45 @@
339325
on:dragenter={composeDragHandler(onDragEnterCb)}
340326
on:dragover={composeDragHandler(onDragOverCb)}
341327
on:dragleave={composeDragHandler(onDragLeaveCb)}
342-
on:drop={composeDragHandler(onDropCb)}>
328+
on:drop={composeDragHandler(onDropCb)}
329+
>
343330
<input
344331
{accept}
345332
{multiple}
333+
{required}
346334
type="file"
347-
name={name}
335+
{name}
348336
autocomplete="off"
349337
tabindex="-1"
338+
on:blur
350339
on:change={onDropCb}
351340
on:click={onInputElementClick}
341+
on:invalid
352342
bind:this={inputRef}
353-
style="display: none;" />
343+
style="display: none;"
344+
/>
354345
<slot>
355346
<p>Drag 'n' drop some files here, or click to select files</p>
356347
</slot>
357348
</div>
349+
350+
<style>
351+
.dropzone {
352+
flex: 1;
353+
display: flex;
354+
flex-direction: column;
355+
align-items: center;
356+
padding: 20px;
357+
border-width: 2px;
358+
border-radius: 2px;
359+
border-color: #eeeeee;
360+
border-style: dashed;
361+
background-color: #fafafa;
362+
color: #bdbdbd;
363+
outline: none;
364+
transition: border 0.24s ease-in-out;
365+
}
366+
.dropzone:focus {
367+
border-color: #2196f3;
368+
}
369+
</style>

0 commit comments

Comments
 (0)