pingcrm/resources/js/Shared/FileInput.vue

56 lines
1.6 KiB
Vue
Raw Normal View History

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">
2021-12-08 14:52:56 -03:00
<button type="button" class="px-4 py-1 text-white text-xs font-medium bg-gray-500 hover:bg-gray-700 rounded-sm" @click="browse">Browse</button>
2019-08-09 12:33:47 -03:00
</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>
2021-12-08 14:52:56 -03:00
<button type="button" class="px-4 py-1 text-white text-xs font-medium bg-gray-500 hover:bg-gray-700 rounded-sm" @click="remove">Remove</button>
2019-08-09 12:33:47 -03:00
</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: () => [],
},
},
2021-12-08 14:52:56 -03:00
emits: ['update:modelValue'],
2019-08-09 12:33:47 -03:00
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>