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

💄 354 removed watcher from filedropzone and reformated #355

Merged
merged 19 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1d01cfa
removed watcher and clearing property, changed function name and defi…
langehm Dec 8, 2024
e89fe17
temporary addition
langehm Dec 8, 2024
36ac461
removed story
Dec 9, 2024
526121d
removed unused import
langehm Dec 10, 2024
fa9794f
formated comments and if statements
langehm Dec 10, 2024
b8c614c
renamed function - we are not using lowdash here
langehm Dec 10, 2024
a98c1ec
formated template
langehm Dec 10, 2024
5143570
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
langehm Dec 10, 2024
499d559
used typescript way of defining emits
langehm Dec 10, 2024
66a0c08
Merge remote-tracking branch 'origin/354-add-example-to-delete-warnin…
langehm Dec 10, 2024
218c53b
oops - missed the type delcaration
langehm Dec 10, 2024
55f7520
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
langehm Dec 10, 2024
67027cf
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
langehm Dec 11, 2024
8fb4d5e
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
langehm Dec 12, 2024
bcb2c3c
added description to exposed function
langehm Dec 20, 2024
c386624
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
langehm Dec 22, 2024
d32aae2
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
langehm Dec 29, 2024
da2b8d0
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
FabianWilms Jan 10, 2025
0afbe35
Merge branch 'beta' into 354-add-example-to-delete-warnings-without-w…
FabianWilms Jan 14, 2025
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
3 changes: 2 additions & 1 deletion src/components/FileDropzone/MucFileDropzone.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default {
},
};

export const Default = {};

FabianWilms marked this conversation as resolved.
Show resolved Hide resolved
export const Example = {
args: {
buttonText: "Dokument hochladen",
Expand All @@ -26,4 +28,3 @@ export const Example = {
"Die Dateien haben zusammen mehr als 10 MB und können nicht angefügt werden.",
},
};
export const Default = {};
99 changes: 50 additions & 49 deletions src/components/FileDropzone/MucFileDropzone.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
>
{{ buttonText }}
</MucButton>
<span class="drop-zone-additional-information">{{
additionalInformation
}}</span>
<span class="drop-zone-additional-information">
{{ additionalInformation }}
</span>
</div>

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

<script setup lang="ts">
import { onMounted, onUpdated, ref, watch } from "vue";
import { onMounted, onUpdated, ref } from "vue";

import { MucButton } from "../Button";
import FormErrorMessage from "../Form/FormErrorMessage.vue";
Expand All @@ -53,7 +53,6 @@ const {
maxFileSizeWarning,
maxTotalFileSize = 0,
maxTotalFileSizeWarning,
showWarning = false,
} = defineProps<{
/**
* Text on the upload button
Expand Down Expand Up @@ -91,22 +90,19 @@ const {
* Warning for invalid file size sum
*/
maxTotalFileSizeWarning?: string;
/**
* Clears warnings when changes from 'true' to 'false'.
*/
showWarning?: boolean;
}>();

const emit = defineEmits([
const emit = defineEmits<{
/**
* Dropped files as {@link File[]} array
*/
"files",
files: [files: File[]];

/**
* Event that signals when warnings are displayed.
*/
"warning",
]);
warning: [];
}>();

/** Flag signals if file size is valid */
const validFileSizes = ref(true);
Expand All @@ -133,7 +129,7 @@ onMounted(() => {
const target = event.target as HTMLInputElement;
if (target?.files && target.files.length > 0) {
const filesArray = Array.from(target.files);
_emitFiles(filesArray);
emitFiles(filesArray);
}
};
});
Expand All @@ -153,9 +149,13 @@ const selectFiles = () => {
fileInput.click();
};

/** Sets flag {@link isDragOver} true. */
/**
* Sets flag {@link isDragOver} true.
*/
const onDragOver = (event: DragEvent) => {
if (disabled) return;
if (disabled) {
return;
}
if (!fileInput?.multiple) {
const dataTransfer: DataTransfer = event.dataTransfer as DataTransfer;
if (dataTransfer?.items?.length > 1) {
Expand All @@ -166,7 +166,9 @@ const onDragOver = (event: DragEvent) => {
isDragOver.value = true;
};

/** Sets flag {@link isDragOver} false. */
/**
* Sets flag {@link isDragOver} false.
*/
const onDragLeave = () => {
isDragOver.value = false;
validFilesAmount.value = true;
Expand All @@ -177,54 +179,55 @@ const onDragLeave = () => {
* @param {DragEvent} event dropped files
*/
const onDrop = (event: DragEvent) => {
if (disabled) return;
if (disabled) {
return;
}
if (!validFilesAmount.value) {
/*
user drops files with invalid amount
-> warning disappears
*/
validFilesAmount.value = true;
return;
}
isDragOver.value = false;
const dataTransfer: DataTransfer = event.dataTransfer as DataTransfer;
if (dataTransfer?.files?.length > 0) {
const filesArray = Array.from(dataTransfer.files);
_emitFiles(filesArray);
} else {
isDragOver.value = false;
const dataTransfer: DataTransfer = event.dataTransfer as DataTransfer;
if (dataTransfer?.files?.length > 0) {
const filesArray = Array.from(dataTransfer.files);
emitFiles(filesArray);
}
}
};

/**
* Emits the files to upload to the surrounding element.
* @param {File[]} files array of dropped or chosen files to upload
*/
const _emitFiles = (files: File[]) => {
validFileSizes.value = _areFilesValid(files);
validTotalFileSizes.value = _isTotalFilesSumValid(files);
const emitFiles = (files: File[]) => {
validFileSizes.value = areFilesValid(files);
validTotalFileSizes.value = isTotalFilesSumValid(files);

if (!validFileSizes.value || !validTotalFileSizes.value) {
emit("warning");
return;
} else {
emit("files", files);
}

emit("files", files);
};

/**
* Checks if all files are inside the allowed file size range that is given by {@link Props#maxFileSize}.
* @param {File[]} files array files
*/
const _areFilesValid = (files: File[]) => {
if (maxFileSize)
return !files.some((file) => file.size > maxFileSize * 1024 * 1024);
return true;
const areFilesValid = (files: File[]) => {
return maxFileSize
? !files.some((file) => file.size > maxFileSize * 1024 * 1024)
: true;
};

/**
* Checks if the sum of all files is inside the allowed range that is given by {@link Props#maxTotalFileSize}.
* @param {File[]} files array files
*/
const _isTotalFilesSumValid = (files: File[]) => {
const isTotalFilesSumValid = (files: File[]) => {
if (maxTotalFileSize)
return (
files.reduce((acc, file) => acc + (file.size || 0), 0) <=
Expand All @@ -233,26 +236,24 @@ const _isTotalFilesSumValid = (files: File[]) => {
return true;
};

/**
* Watches for changes in {@link Props#showWarning} and clears all warnings if it switches to 'false'.
*/
watch(
() => showWarning,
(isShowWarning: boolean) => {
if (!isShowWarning) {
_clearWarnings();
}
}
);

/**
* Clears all warnings.
*/
const _clearWarnings = () => {
const clearWarnings = () => {
validFileSizes.value = true;
validTotalFileSizes.value = true;
validFilesAmount.value = true;
};

/**
* Expose function to be called via ref
*/
defineExpose({
/**
* Exposed function to clear all warnings
*/
clearWarnings,
});
</script>

<style scoped>
Expand Down
Loading