Skip to content

Commit f1d9558

Browse files
langehmjannik.langeFabianWilms
authored
💄 354 removed watcher from filedropzone and reformated (#355)
* removed watcher and clearing property, changed function name and defined function as exposed * temporary addition * removed story * removed unused import * formated comments and if statements * renamed function - we are not using lowdash here * formated template * used typescript way of defining emits * oops - missed the type delcaration * added description to exposed function --------- Co-authored-by: langehm <langehm@users.noreply.github.com> Co-authored-by: jannik.lange <jannik.lange@muenchen.de> Co-authored-by: Fabian Wilms <10800158+FabianWilms@users.noreply.github.com>
1 parent e9350c2 commit f1d9558

File tree

2 files changed

+52
-50
lines changed

2 files changed

+52
-50
lines changed

src/components/FileDropzone/MucFileDropzone.stories.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default {
1313
},
1414
};
1515

16+
export const Default = {};
17+
1618
export const Example = {
1719
args: {
1820
buttonText: "Dokument hochladen",
@@ -26,4 +28,3 @@ export const Example = {
2628
"Die Dateien haben zusammen mehr als 10 MB und können nicht angefügt werden.",
2729
},
2830
};
29-
export const Default = {};

src/components/FileDropzone/MucFileDropzone.vue

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
>
1919
{{ buttonText }}
2020
</MucButton>
21-
<span class="drop-zone-additional-information">{{
22-
additionalInformation
23-
}}</span>
21+
<span class="drop-zone-additional-information">
22+
{{ additionalInformation }}
23+
</span>
2424
</div>
2525

2626
<form-error-message v-if="!validFileSizes && maxFileSizeWarning">
@@ -37,7 +37,7 @@
3737
</template>
3838

3939
<script setup lang="ts">
40-
import { onMounted, onUpdated, ref, watch } from "vue";
40+
import { onMounted, onUpdated, ref } from "vue";
4141
4242
import { MucButton } from "../Button";
4343
import FormErrorMessage from "../Form/FormErrorMessage.vue";
@@ -53,7 +53,6 @@ const {
5353
maxFileSizeWarning,
5454
maxTotalFileSize = 0,
5555
maxTotalFileSizeWarning,
56-
showWarning = false,
5756
} = defineProps<{
5857
/**
5958
* Text on the upload button
@@ -91,22 +90,19 @@ const {
9190
* Warning for invalid file size sum
9291
*/
9392
maxTotalFileSizeWarning?: string;
94-
/**
95-
* Clears warnings when changes from 'true' to 'false'.
96-
*/
97-
showWarning?: boolean;
9893
}>();
9994
100-
const emit = defineEmits([
95+
const emit = defineEmits<{
10196
/**
10297
* Dropped files as {@link File[]} array
10398
*/
104-
"files",
99+
files: [files: File[]];
100+
105101
/**
106102
* Event that signals when warnings are displayed.
107103
*/
108-
"warning",
109-
]);
104+
warning: [];
105+
}>();
110106
111107
/** Flag signals if file size is valid */
112108
const validFileSizes = ref(true);
@@ -133,7 +129,7 @@ onMounted(() => {
133129
const target = event.target as HTMLInputElement;
134130
if (target?.files && target.files.length > 0) {
135131
const filesArray = Array.from(target.files);
136-
_emitFiles(filesArray);
132+
emitFiles(filesArray);
137133
}
138134
};
139135
});
@@ -153,9 +149,13 @@ const selectFiles = () => {
153149
fileInput.click();
154150
};
155151
156-
/** Sets flag {@link isDragOver} true. */
152+
/**
153+
* Sets flag {@link isDragOver} true.
154+
*/
157155
const onDragOver = (event: DragEvent) => {
158-
if (disabled) return;
156+
if (disabled) {
157+
return;
158+
}
159159
if (!fileInput?.multiple) {
160160
const dataTransfer: DataTransfer = event.dataTransfer as DataTransfer;
161161
if (dataTransfer?.items?.length > 1) {
@@ -166,7 +166,9 @@ const onDragOver = (event: DragEvent) => {
166166
isDragOver.value = true;
167167
};
168168
169-
/** Sets flag {@link isDragOver} false. */
169+
/**
170+
* Sets flag {@link isDragOver} false.
171+
*/
170172
const onDragLeave = () => {
171173
isDragOver.value = false;
172174
validFilesAmount.value = true;
@@ -177,54 +179,55 @@ const onDragLeave = () => {
177179
* @param {DragEvent} event dropped files
178180
*/
179181
const onDrop = (event: DragEvent) => {
180-
if (disabled) return;
182+
if (disabled) {
183+
return;
184+
}
181185
if (!validFilesAmount.value) {
182186
/*
183187
user drops files with invalid amount
184188
-> warning disappears
185189
*/
186190
validFilesAmount.value = true;
187-
return;
188-
}
189-
isDragOver.value = false;
190-
const dataTransfer: DataTransfer = event.dataTransfer as DataTransfer;
191-
if (dataTransfer?.files?.length > 0) {
192-
const filesArray = Array.from(dataTransfer.files);
193-
_emitFiles(filesArray);
191+
} else {
192+
isDragOver.value = false;
193+
const dataTransfer: DataTransfer = event.dataTransfer as DataTransfer;
194+
if (dataTransfer?.files?.length > 0) {
195+
const filesArray = Array.from(dataTransfer.files);
196+
emitFiles(filesArray);
197+
}
194198
}
195199
};
196200
197201
/**
198202
* Emits the files to upload to the surrounding element.
199203
* @param {File[]} files array of dropped or chosen files to upload
200204
*/
201-
const _emitFiles = (files: File[]) => {
202-
validFileSizes.value = _areFilesValid(files);
203-
validTotalFileSizes.value = _isTotalFilesSumValid(files);
205+
const emitFiles = (files: File[]) => {
206+
validFileSizes.value = areFilesValid(files);
207+
validTotalFileSizes.value = isTotalFilesSumValid(files);
204208
205209
if (!validFileSizes.value || !validTotalFileSizes.value) {
206210
emit("warning");
207-
return;
211+
} else {
212+
emit("files", files);
208213
}
209-
210-
emit("files", files);
211214
};
212215
213216
/**
214217
* Checks if all files are inside the allowed file size range that is given by {@link Props#maxFileSize}.
215218
* @param {File[]} files array files
216219
*/
217-
const _areFilesValid = (files: File[]) => {
218-
if (maxFileSize)
219-
return !files.some((file) => file.size > maxFileSize * 1024 * 1024);
220-
return true;
220+
const areFilesValid = (files: File[]) => {
221+
return maxFileSize
222+
? !files.some((file) => file.size > maxFileSize * 1024 * 1024)
223+
: true;
221224
};
222225
223226
/**
224227
* Checks if the sum of all files is inside the allowed range that is given by {@link Props#maxTotalFileSize}.
225228
* @param {File[]} files array files
226229
*/
227-
const _isTotalFilesSumValid = (files: File[]) => {
230+
const isTotalFilesSumValid = (files: File[]) => {
228231
if (maxTotalFileSize)
229232
return (
230233
files.reduce((acc, file) => acc + (file.size || 0), 0) <=
@@ -233,26 +236,24 @@ const _isTotalFilesSumValid = (files: File[]) => {
233236
return true;
234237
};
235238
236-
/**
237-
* Watches for changes in {@link Props#showWarning} and clears all warnings if it switches to 'false'.
238-
*/
239-
watch(
240-
() => showWarning,
241-
(isShowWarning: boolean) => {
242-
if (!isShowWarning) {
243-
_clearWarnings();
244-
}
245-
}
246-
);
247-
248239
/**
249240
* Clears all warnings.
250241
*/
251-
const _clearWarnings = () => {
242+
const clearWarnings = () => {
252243
validFileSizes.value = true;
253244
validTotalFileSizes.value = true;
254245
validFilesAmount.value = true;
255246
};
247+
248+
/**
249+
* Expose function to be called via ref
250+
*/
251+
defineExpose({
252+
/**
253+
* Exposed function to clear all warnings
254+
*/
255+
clearWarnings,
256+
});
256257
</script>
257258

258259
<style scoped>

0 commit comments

Comments
 (0)