|
| 1 | +<template> |
| 2 | + <ValidationProvider ref="fileObserver" :tag="tag" :rules="rules" :name="name" :vid="vid" v-slot="{ errors }"> |
| 3 | + <slot></slot> |
| 4 | + <input :id="name" name="archive" ref="inputFile" @change="selectedFile" class="input--secondary-color input__file" |
| 5 | + type="file"> |
| 6 | + <label :for="name"> |
| 7 | + <Icon type="archive" theme="theme--white"/> |
| 8 | + {{labelFileText}}</label> |
| 9 | + <span class="error-message">{{errors[0]}}</span> |
| 10 | + <slot name="footer"></slot> |
| 11 | + <span class="message message--red" v-if="filename" |
| 12 | + style="text-decoration: underline; cursor: pointer;" |
| 13 | + @click="deleteFile">Supprimer le fichier</span> |
| 14 | + </ValidationProvider> |
| 15 | +</template> |
| 16 | + |
| 17 | +<script lang="ts"> |
| 18 | + import {ValidationProvider} from 'vee-validate'; |
| 19 | + import {Component, Emit, Prop, Ref, Vue} from "vue-property-decorator"; |
| 20 | + import Icon from "~/components/Symbols/Icon.vue"; |
| 21 | +
|
| 22 | + @Component({ |
| 23 | + components: { |
| 24 | + Icon, |
| 25 | + ValidationProvider |
| 26 | + } |
| 27 | + }) |
| 28 | + export default class TextInput extends Vue { |
| 29 | +
|
| 30 | + @Prop({type: [String, Object], default: ''}) rules!: string | object; |
| 31 | + @Prop({type: String, default: ''}) name!: string; |
| 32 | + @Prop({type: String, default: undefined}) vid!: string | undefined; |
| 33 | + @Prop({type: String, default: 'div'}) tag!: string; |
| 34 | + @Prop({type:String, default: ''}) defaultFilename!:string; |
| 35 | +
|
| 36 | + /** |
| 37 | + * Observer for the input file element |
| 38 | + */ |
| 39 | + @Ref() inputFile!: HTMLInputElement; |
| 40 | + /** |
| 41 | + * Validation Observer for the zip archive and the url |
| 42 | + */ |
| 43 | + @Ref() fileObserver!: InstanceType<typeof ValidationProvider>; |
| 44 | +
|
| 45 | + /** |
| 46 | + * The name of the uploaded file |
| 47 | + * Default is null |
| 48 | + */ |
| 49 | + filename: string | null = null; |
| 50 | +
|
| 51 | + /** |
| 52 | + * Returns the name of the uploaded file or a default message instead |
| 53 | + */ |
| 54 | + protected get labelFileText() { |
| 55 | + if (this.filename !== null) { |
| 56 | + if (this.filename.length > 18) { |
| 57 | + return this.filename.slice(0, 18) + '...' |
| 58 | + } |
| 59 | +
|
| 60 | + return this.filename |
| 61 | + } |
| 62 | +
|
| 63 | + return 'Choisir un fichier...' |
| 64 | + } |
| 65 | +
|
| 66 | +
|
| 67 | + @Emit() |
| 68 | + input(file: File | null) { |
| 69 | + return file; |
| 70 | + } |
| 71 | +
|
| 72 | + /** |
| 73 | + * Get the file from the input file element |
| 74 | + */ |
| 75 | + file(): File | null { |
| 76 | + const inputFile: any = this.fileObserver.value; |
| 77 | + if (!inputFile) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + return inputFile |
| 81 | + } |
| 82 | +
|
| 83 | + /** |
| 84 | + * Event for the changed state of the input file |
| 85 | + */ |
| 86 | + async selectedFile() { |
| 87 | + const inputElement: HTMLInputElement = this.inputFile; |
| 88 | + const files = inputElement.files; |
| 89 | + if (files !== null) { |
| 90 | + const file: File | null = files.item(0); |
| 91 | + this.filename = file !== null ? file.name : null; |
| 92 | + await this.fileObserver.validate(file); |
| 93 | + this.input(file); |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + /** |
| 98 | + * Delete file from input and reset the filename |
| 99 | + */ |
| 100 | + deleteFile() { |
| 101 | + this.$nextTick(async () => { |
| 102 | + this.filename = null; |
| 103 | + this.inputFile.files = null; |
| 104 | + this.inputFile.value = ''; |
| 105 | + this.fileObserver.value = undefined; |
| 106 | + this.fileObserver.reset(); |
| 107 | + this.input(null); |
| 108 | + }); |
| 109 | + } |
| 110 | +
|
| 111 | + mounted() { |
| 112 | + this.filename = this.defaultFilename === '' ? null : this.defaultFilename; |
| 113 | + } |
| 114 | +
|
| 115 | + } |
| 116 | +</script> |
| 117 | + |
| 118 | + |
| 119 | +<style lang="scss" scoped> |
| 120 | + label { |
| 121 | + align-self: flex-start; |
| 122 | + } |
| 123 | +</style> |
0 commit comments