ergonomie upload

This commit is contained in:
Theophyl Hoguet committed 2023-03-14 21:44:23 +01:00
1 parent 68c72d619b
commit f896de9041
2 files changed
+83 -27

No files matched your search

+73 -23
View File
@@ -16,8 +16,9 @@ export default defineComponent({
emits: ['updateImageList', 'delete'], emits: ['updateImageList', 'delete'],
data() { data() {
return { return {
sent: false, file: null as FileList | null,
file: null as FileList | null titleSendButton: "Envoyer",
classSendButton: ""
} }
}, },
props: { props: {
@@ -28,32 +29,48 @@ export default defineComponent({
}, },
methods: { methods: {
async submitImage() { async submitImage() {
if (this.sent === true || this.file === null) if (this.classSendButton === "sent") {
this.titleSendButton = "Images déjà envoyées"
this.classSendButton = "error"
return; return;
}
if (this.file === null || this.file.length === 0) {
this.titleSendButton = "Aucune image sélectionnée"
this.classSendButton = "error"
return;
}
Array.from(this.file).forEach((file, id) => { Array.from(this.file).forEach((file, id) => {
let formData = new FormData(); let formData = new FormData();
formData.append('file', file); formData.append('file', file);
api.createImage(formData); api.createImage(formData);
}); });
this.sent = true;
this.$emit('updateImageList'); this.$emit('updateImageList');
this.resetFile(); this.resetFile();
this.titleSendButton = "Envoyé"
this.classSendButton = "sent"
}, },
handleFilesUpload(event: InputFileEvent) { handleFilesUpload(event: InputFileEvent) {
this.file = event.target.files; this.file = event.target.files;
this.sent = false; this.resetSendButton(true)
}, },
resetFile() { resetFile() {
this.file = null; this.file = null;
(document.getElementById('fileUpload') as HTMLInputElement).value = ""; (document.getElementById('fileUpload') as HTMLInputElement).value = "";
}, },
titleSendButton() { nameLabelInput() {
if (this.sent === true) if (this.file === null || this.file.length === 0)
return "Envoyé";
if (this.file === null)
return "Aucune image sélectionnée"; return "Aucune image sélectionnée";
return "Envoyer (" + this.file.length + " image(s) sélectionnée(s))"; if (this.file.length === 1)
return this.file[0].name;
return this.file.length + " images sélectionnées";
},
resetSendButton(force: boolean = false) {
if (force || this.classSendButton === "error") {
this.titleSendButton = "Envoyer"
this.classSendButton = ""
}
} }
}, },
watch: { watch: {
@@ -78,15 +95,18 @@ export default defineComponent({
</script> </script>
<template> <template>
<div> <div class="form">
<label>Ajouter une ou plusieurs image(s): <div class="inputform" for="fileUpload">
<input id="fileUpload" type="file" @change="handleFilesUpload($event as InputFileEvent)" accept="image/jpeg" <input style="position: absolute; left: -9999px; opacity: 0;" class="fileUpload" id="fileUpload" type="file"
multiple /> @change="handleFilesUpload($event as InputFileEvent)" accept="image/png, image/jpeg" multiple />
</label> <label class="button" for="fileUpload" id="labelInput">Choisir une ou plusieurs image(s)...</label>
<button :class="sent ? 'green' : 'normal'" @click="submitImage()">{{ titleSendButton() }}</button> <label class="filename button" for="fileUpload">{{ nameLabelInput() }}</label>
<button v-if="file !== null" @click="resetFile()">Reinitialiser</button> </div>
<button class="button" :class="classSendButton" @click="submitImage()" @mouseleave="resetSendButton()">{{
titleSendButton }}</button>
<button class="button" v-if="file !== null && file.length !== 0" @click="resetFile()">Reinitialiser</button>
</div> </div>
<h2 v-if="file !== null">Preview :</h2> <h2 v-if="file !== null && file.length !== 0">Preview :</h2>
<div class="previewBox"> <div class="previewBox">
<label v-for="(f, id) in (file as FileList)" :key="id" class="imageLabel">{{ (f as File).name }}: <label v-for="(f, id) in (file as FileList)" :key="id" class="imageLabel">{{ (f as File).name }}:
<img :id="'preview-' + id" /> <img :id="'preview-' + id" />
@@ -97,21 +117,46 @@ export default defineComponent({
img { img {
max-width: 100%; max-width: 100%;
max-height: 90%; max-height: 90%;
border: 1px solid black; }
.form {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-evenly;
gap: 20px;
} }
label { label {
color: white; color: white;
} }
input {
color: white;
}
h2 { h2 {
color: white; color: white;
} }
.fileUpload:focus~label,
.fileUpload:focus-visible~label {
outline: 4px auto -webkit-focus-ring-color;
}
.filename {
background-color: white;
color: black;
border: 1px solid rgb(255, 0, 98);
border-radius: 0 8px 8px 0;
}
#labelInput {
border-radius: 8px 0 0 8px;
background-color: rgb(255, 0, 98);
}
.inputform:hover label {
border-color: #646cff;
}
.imageLabel { .imageLabel {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -130,7 +175,12 @@ h2 {
flex-wrap: wrap; flex-wrap: wrap;
} }
.green { .error {
color: red;
font-weight: bold;
}
.sent {
color: green; color: green;
font-weight: bold; font-weight: bold;
} }
+10 -4
View File
@@ -29,7 +29,7 @@ h1 {
line-height: 1.1; line-height: 1.1;
} }
button { .button {
border-radius: 8px; border-radius: 8px;
border: 1px solid transparent; border: 1px solid transparent;
padding: 0.6em 1.2em; padding: 0.6em 1.2em;
@@ -39,14 +39,20 @@ button {
background-color: #1a1a1a; background-color: #1a1a1a;
cursor: pointer; cursor: pointer;
transition: border-color 0.25s; transition: border-color 0.25s;
color: white;
flex-shrink: 0;
} }
button:hover { .button:hover {
border-color: #646cff; border-color: #646cff;
} }
button:focus, .button:focus,
button:focus-visible { .button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
input:focus+label {
outline: 4px auto -webkit-focus-ring-color; outline: 4px auto -webkit-focus-ring-color;
} }