105 lines
2.9 KiB
Vue
Executable File
105 lines
2.9 KiB
Vue
Executable File
<template>
|
|
<div>
|
|
<label v-if="label" class="cursor-pointer text-gray-light text-lg mb-2"
|
|
@click="browse"
|
|
>{{ label }}:</label>
|
|
<div :class="{ error: error }">
|
|
<input
|
|
ref="file"
|
|
type="file"
|
|
:accept="accept"
|
|
multiple
|
|
class="hidden"
|
|
@change="change"
|
|
/>
|
|
<div v-if="!modelValue" class="py-2">
|
|
<button
|
|
type="button"
|
|
class="px-6 py-2 bg-indigo-300 focus:ring-4 focus:ring-offset-1 focus:ring-orange focus:ring-opacity-20 focus:ring-offset-orange focus:outline-none focus:border-transparent rounded-sm text-sm text-white"
|
|
@click="browse"
|
|
>
|
|
Выбрать файлы
|
|
</button>
|
|
</div>
|
|
<div v-else class="flex flex-col justify-center max-w-2xl mt-3 p-2 border rounded-md">
|
|
<div
|
|
v-for="(file, index) in modelValue"
|
|
:key="index"
|
|
class="flex flex-col md:flex-row md:items-start md:justify-between p-1"
|
|
>
|
|
<div class="md:w-5/6 text-sm md:text-base flex-1 pr-2 text-gray flex flex-col">
|
|
<span class="truncate">{{ file.name }}</span>
|
|
<span class="text-xs text-gray-light">({{ filesize(file.size) }})</span>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
class="md:w-1/6 px-1 py-1 bg-indigo-300 hover:bg-indigo-100 rounded-sm text-xs font-medium text-white"
|
|
@click="remove(index)"
|
|
>
|
|
Удалить
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div v-if="error" class="text-red text-sm">
|
|
{{ error }}
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
|
|
export default {
|
|
props: {
|
|
modelValue: FileList,
|
|
label: String,
|
|
accept: String,
|
|
error: String,
|
|
},
|
|
emits:['update:modelValue'],
|
|
|
|
watch: {
|
|
modelValue(value) {
|
|
if (!value) {
|
|
this.$refs.file.value = ''
|
|
}
|
|
},
|
|
},
|
|
|
|
methods: {
|
|
filesize(size) {
|
|
var i = Math.floor(Math.log(size) / Math.log(1024))
|
|
return (
|
|
(size / Math.pow(1024, i)).toFixed(2) * 1 +
|
|
' ' +
|
|
['B', 'kB', 'MB', 'GB', 'TB'][i]
|
|
)
|
|
},
|
|
browse() {
|
|
this.$refs.file.click()
|
|
},
|
|
change(e) {
|
|
this.$emit('update:modelValue', e.target.files)
|
|
},
|
|
|
|
remove(file_index) {
|
|
const dt = new DataTransfer()
|
|
let files = Array.from(this.$refs.file.files)
|
|
|
|
files.map(function (file, index) {
|
|
if (index !== file_index) {
|
|
dt.items.add(file)
|
|
}
|
|
})
|
|
if (dt.files.length) {
|
|
this.$refs.file.files = dt.files
|
|
this.$emit('update:modelValue', dt.files)
|
|
} else {
|
|
this.$refs.file.files = null
|
|
this.$emit('update:modelValue', null)
|
|
}
|
|
},
|
|
},
|
|
}
|
|
</script>
|