og-image/src/ui/FileForm.vue

55 lines
1.4 KiB
Vue
Raw Normal View History

2023-02-24 21:40:50 +01:00
<template>
2023-02-24 21:46:46 +01:00
<label
v-if="label"
class="block text-sm font-medium text-gray-700"
:for="id"
v-text="label"
/>
2023-05-09 20:53:34 +02:00
<button
class="px-3 py-1 mr-2 text-sm font-medium text-gray-900 bg-white border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-2 focus:ring-blue-700 focus:text-blue-700 dark:bg-gray-700 dark:border-gray-600 dark:text-white dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-blue-500 dark:focus:text-white rounded-md"
v-on:click="reset"
>X</button>
2023-02-24 21:40:50 +01:00
<input
2023-02-24 21:46:46 +01:00
:id="id"
2023-05-09 20:53:34 +02:00
class="mt-2 w-50 rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
2023-02-24 21:40:50 +01:00
type="file"
2023-02-26 17:44:43 +01:00
:class="$attrs.class"
2023-02-24 21:40:50 +01:00
@change="convertFileToBase64($event.target.files)"
2023-02-24 21:46:46 +01:00
>
2023-02-24 21:40:50 +01:00
</template>
<script>
import { toBase64 } from '../util/file'
export default {
props: {
modelValue: {
2023-02-26 17:44:43 +01:00
type: String,
default: null,
2023-02-24 21:40:50 +01:00
},
label: {
type: String,
2023-02-26 17:44:43 +01:00
default: null,
2023-02-24 21:40:50 +01:00
required: false,
},
id: {
type: String,
required: true,
},
},
emits: ['update:modelValue'],
methods: {
2023-05-09 20:53:34 +02:00
reset() {
this.$emit('update:modelValue', '')
},
2023-02-24 21:40:50 +01:00
async convertFileToBase64(files) {
if (files.length) {
const value = await toBase64(files[0])
this.$emit('update:modelValue', value)
}
},
}
}
</script>