2019-08-09 12:33:47 -03:00
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<label v-if="label" class="form-label">{{ label }}:</label>
|
|
|
|
<div class="form-input p-0" :class="{ error: errors.length }">
|
2021-02-27 11:00:09 -03:00
|
|
|
<input ref="file" type="file" :accept="accept" class="hidden" @change="change" />
|
2021-12-08 12:15:39 -03:00
|
|
|
<div v-if="!modelValue" class="p-2">
|
2020-01-09 00:16:10 -03:00
|
|
|
<button type="button" class="px-4 py-1 bg-gray-500 hover:bg-gray-700 rounded-sm text-xs font-medium text-white" @click="browse">
|
2019-08-09 12:33:47 -03:00
|
|
|
Browse
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div v-else class="flex items-center justify-between p-2">
|
2021-02-27 11:00:09 -03:00
|
|
|
<div class="flex-1 pr-1">
|
2021-12-08 12:15:39 -03:00
|
|
|
{{ modelValue.name }} <span class="text-gray-500 text-xs">({{ filesize(modelValue.size) }})</span>
|
2021-02-27 11:00:09 -03:00
|
|
|
</div>
|
2020-01-09 00:16:10 -03:00
|
|
|
<button type="button" class="px-4 py-1 bg-gray-500 hover:bg-gray-700 rounded-sm text-xs font-medium text-white" @click="remove">
|
2019-08-09 12:33:47 -03:00
|
|
|
Remove
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div v-if="errors.length" class="form-error">{{ errors[0] }}</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
2021-12-08 12:15:39 -03:00
|
|
|
modelValue: File,
|
2019-08-09 12:33:47 -03:00
|
|
|
label: String,
|
|
|
|
accept: String,
|
|
|
|
errors: {
|
|
|
|
type: Array,
|
|
|
|
default: () => [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
watch: {
|
2021-12-08 12:15:39 -03:00
|
|
|
modelValue(value) {
|
2019-08-09 12:33:47 -03:00
|
|
|
if (!value) {
|
|
|
|
this.$refs.file.value = ''
|
|
|
|
}
|
2019-08-09 13:50:45 -03:00
|
|
|
},
|
2019-08-09 12:33:47 -03:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
filesize(size) {
|
|
|
|
var i = Math.floor(Math.log(size) / Math.log(1024))
|
2021-02-27 11:00:09 -03:00
|
|
|
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]
|
2019-08-09 12:33:47 -03:00
|
|
|
},
|
|
|
|
browse() {
|
|
|
|
this.$refs.file.click()
|
|
|
|
},
|
|
|
|
change(e) {
|
2021-12-08 12:15:39 -03:00
|
|
|
this.$emit('update:modelValue', e.target.files[0])
|
2019-08-09 12:33:47 -03:00
|
|
|
},
|
|
|
|
remove() {
|
2021-12-08 12:15:39 -03:00
|
|
|
this.$emit('update:modelValue', null)
|
2019-08-09 12:33:47 -03:00
|
|
|
},
|
2019-08-09 13:50:45 -03:00
|
|
|
},
|
2019-08-09 12:33:47 -03:00
|
|
|
}
|
2019-08-09 13:50:45 -03:00
|
|
|
</script>
|