This repository was archived by the owner on Dec 5, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathSelectImageField.vue
More file actions
73 lines (66 loc) · 1.73 KB
/
SelectImageField.vue
File metadata and controls
73 lines (66 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<template>
<NeoUpload
v-model="vSelectedFile"
accept="image/*"
drag-drop
expanded
native
@update:model-value="fileSelected"
>
<div class="flex items-center p-2">
<div
v-if="!vSelectedFile && !preview"
class="flex items-center justify-center bg-neutral-3 dark:bg-neutral-11 w-16 h-16"
>
<KIcon
name="i-mdi:upload"
/>
</div>
<NuxtImg
v-else
:src="selectedFilePreview || preview"
alt="Selected file"
class="w-16 h-16 object-cover"
/>
<span class="text-center truncate ml-5">{{
vSelectedFile?.name ?? 'Click To Select A File'
}}</span>
<NeoButton
v-if="vSelectedFile || preview"
class="absolute right-3 top-3"
variant="icon"
no-shadow
icon="xmark"
@click="clear"
/>
</div>
</NeoUpload>
</template>
<script setup lang="ts">
import { NeoButton, NeoUpload } from '@kodadot1/brick'
const NuxtImg = resolveComponent('NuxtImg')
const ONE_MB = 1024 * 1024
const emit = defineEmits(['clear'])
const props = defineProps<{
modelValue: File | null
preview?: string
maxSizeInMb?: number
}>()
const vSelectedFile = useVModel(props, 'modelValue')
const { $i18n } = useNuxtApp()
const selectedFilePreview = computed(() =>
vSelectedFile.value ? URL.createObjectURL(vSelectedFile.value as File) : '',
)
const clear = () => {
vSelectedFile.value = null
emit('clear')
}
const fileSelected = (file: File | null) => {
if (file && props.maxSizeInMb && file.size > props.maxSizeInMb * ONE_MB) {
vSelectedFile.value = null
dangerMessage($i18n.t('toast.uploadFileSizeLimit', [props.maxSizeInMb]))
return
}
vSelectedFile.value = file
}
</script>