@finema/core 1.4.35 → 1.4.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -63
- package/dist/module.d.mts +4 -4
- package/dist/module.d.ts +4 -4
- package/dist/module.json +1 -1
- package/dist/module.mjs +2 -2
- package/dist/runtime/components/Alert.vue +49 -49
- package/dist/runtime/components/Avatar.vue +27 -27
- package/dist/runtime/components/Badge.vue +54 -54
- package/dist/runtime/components/Breadcrumb.vue +45 -45
- package/dist/runtime/components/Button/Group.vue +37 -37
- package/dist/runtime/components/Button/index.vue +77 -77
- package/dist/runtime/components/Card.vue +38 -38
- package/dist/runtime/components/Core.vue +13 -13
- package/dist/runtime/components/Dialog/index.vue +108 -108
- package/dist/runtime/components/Dropdown/index.vue +71 -71
- package/dist/runtime/components/Form/FieldWrapper.vue +23 -23
- package/dist/runtime/components/Form/Fields.vue +147 -136
- package/dist/runtime/components/Form/InputCheckbox/index.vue +21 -21
- package/dist/runtime/components/Form/InputDateTime/index.vue +51 -51
- package/dist/runtime/components/Form/InputRadio/index.vue +27 -27
- package/dist/runtime/components/Form/InputSelect/index.vue +37 -37
- package/dist/runtime/components/Form/InputStatic/index.vue +16 -16
- package/dist/runtime/components/Form/InputText/index.vue +27 -27
- package/dist/runtime/components/Form/InputTextarea/index.vue +25 -25
- package/dist/runtime/components/Form/InputToggle/index.vue +14 -14
- package/dist/runtime/components/Form/InputUploadDropzone/index.vue +170 -0
- package/dist/runtime/components/Form/InputUploadDropzone/types.d.ts +11 -0
- package/dist/runtime/components/Form/InputUploadDropzone/types.mjs +0 -0
- package/dist/runtime/components/Form/InputUploadFileClassic/index.vue +36 -36
- package/dist/runtime/components/Form/InputUploadFileClassicAuto/index.vue +160 -152
- package/dist/runtime/components/Form/index.vue +6 -6
- package/dist/runtime/components/Form/types.d.ts +4 -2
- package/dist/runtime/components/Form/types.mjs +1 -0
- package/dist/runtime/components/Icon.vue +23 -23
- package/dist/runtime/components/Image.vue +36 -36
- package/dist/runtime/components/Loader.vue +14 -14
- package/dist/runtime/components/Modal/index.vue +146 -146
- package/dist/runtime/components/SimplePagination.vue +96 -96
- package/dist/runtime/components/Slideover/index.vue +110 -110
- package/dist/runtime/components/Table/Base.vue +132 -132
- package/dist/runtime/components/Table/ColumnDate.vue +16 -16
- package/dist/runtime/components/Table/ColumnDateTime.vue +30 -30
- package/dist/runtime/components/Table/ColumnImage.vue +13 -13
- package/dist/runtime/components/Table/ColumnNumber.vue +14 -14
- package/dist/runtime/components/Table/Simple.vue +57 -57
- package/dist/runtime/components/Table/index.vue +52 -52
- package/dist/runtime/components/Tabs/index.vue +65 -65
- package/dist/runtime/plugin.mjs +4 -2
- package/dist/runtime/types/config.d.ts +1 -1
- package/dist/runtime/types/utils.d.ts +29 -29
- package/dist/runtime/ui.config/index.d.ts +1 -0
- package/dist/runtime/ui.config/index.mjs +1 -0
- package/dist/runtime/ui.config/uploadFileDropzone.d.ts +31 -0
- package/dist/runtime/ui.config/uploadFileDropzone.mjs +31 -0
- package/dist/runtime/ui.css +32 -32
- package/package.json +86 -86
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
-
<UTextarea
|
|
4
|
-
v-model="value"
|
|
5
|
-
:disabled="wrapperProps.isDisabled"
|
|
6
|
-
:name="name"
|
|
7
|
-
:resize="resize"
|
|
8
|
-
:placeholder="wrapperProps.placeholder"
|
|
9
|
-
:autofocus="!!autoFocus"
|
|
10
|
-
:autoresize="autoresize"
|
|
11
|
-
:rows="rows"
|
|
12
|
-
:readonly="isReadonly"
|
|
13
|
-
:ui="ui"
|
|
14
|
-
/>
|
|
15
|
-
</FieldWrapper>
|
|
16
|
-
</template>
|
|
17
|
-
<script lang="ts" setup>
|
|
18
|
-
import { useFieldHOC } from '#core/composables/useForm'
|
|
19
|
-
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
20
|
-
import { type ITextareaFieldProps } from '#core/components/Form/InputTextarea/types'
|
|
21
|
-
|
|
22
|
-
const props = withDefaults(defineProps<ITextareaFieldProps>(), {})
|
|
23
|
-
|
|
24
|
-
const { value, wrapperProps } = useFieldHOC<string>(props)
|
|
25
|
-
</script>
|
|
1
|
+
<template>
|
|
2
|
+
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
+
<UTextarea
|
|
4
|
+
v-model="value"
|
|
5
|
+
:disabled="wrapperProps.isDisabled"
|
|
6
|
+
:name="name"
|
|
7
|
+
:resize="resize"
|
|
8
|
+
:placeholder="wrapperProps.placeholder"
|
|
9
|
+
:autofocus="!!autoFocus"
|
|
10
|
+
:autoresize="autoresize"
|
|
11
|
+
:rows="rows"
|
|
12
|
+
:readonly="isReadonly"
|
|
13
|
+
:ui="ui"
|
|
14
|
+
/>
|
|
15
|
+
</FieldWrapper>
|
|
16
|
+
</template>
|
|
17
|
+
<script lang="ts" setup>
|
|
18
|
+
import { useFieldHOC } from '#core/composables/useForm'
|
|
19
|
+
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
20
|
+
import { type ITextareaFieldProps } from '#core/components/Form/InputTextarea/types'
|
|
21
|
+
|
|
22
|
+
const props = withDefaults(defineProps<ITextareaFieldProps>(), {})
|
|
23
|
+
|
|
24
|
+
const { value, wrapperProps } = useFieldHOC<string>(props)
|
|
25
|
+
</script>
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
-
<UToggle v-model="value" :disabled="wrapperProps.isDisabled" :name="name" :ui="ui" />
|
|
4
|
-
</FieldWrapper>
|
|
5
|
-
</template>
|
|
6
|
-
|
|
7
|
-
<script lang="ts" setup>
|
|
8
|
-
import { useFieldHOC } from '#core/composables/useForm'
|
|
9
|
-
import { type IToggleFieldProps } from '#core/components/Form/InputToggle/types'
|
|
10
|
-
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
11
|
-
|
|
12
|
-
const props = withDefaults(defineProps<IToggleFieldProps>(), {})
|
|
13
|
-
const { value, wrapperProps } = useFieldHOC<boolean>(props)
|
|
14
|
-
</script>
|
|
1
|
+
<template>
|
|
2
|
+
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
+
<UToggle v-model="value" :disabled="wrapperProps.isDisabled" :name="name" :ui="ui" />
|
|
4
|
+
</FieldWrapper>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script lang="ts" setup>
|
|
8
|
+
import { useFieldHOC } from '#core/composables/useForm'
|
|
9
|
+
import { type IToggleFieldProps } from '#core/components/Form/InputToggle/types'
|
|
10
|
+
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
11
|
+
|
|
12
|
+
const props = withDefaults(defineProps<IToggleFieldProps>(), {})
|
|
13
|
+
const { value, wrapperProps } = useFieldHOC<boolean>(props)
|
|
14
|
+
</script>
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
+
<div
|
|
4
|
+
ref="dropzoneRef"
|
|
5
|
+
:class="[
|
|
6
|
+
ui.base,
|
|
7
|
+
{
|
|
8
|
+
[ui.disabled]: isDisabled,
|
|
9
|
+
[ui.background.default]: !isOverDropZone && !isDisabled,
|
|
10
|
+
[ui.background.dragover]: isOverDropZone && !isDisabled,
|
|
11
|
+
},
|
|
12
|
+
]"
|
|
13
|
+
>
|
|
14
|
+
<Icon
|
|
15
|
+
v-if="selectedFile"
|
|
16
|
+
:name="ui.default.closeIcon"
|
|
17
|
+
:class="[ui.button.delete]"
|
|
18
|
+
@click="handleDeleteFile"
|
|
19
|
+
/>
|
|
20
|
+
<input
|
|
21
|
+
ref="fileInputRef"
|
|
22
|
+
type="file"
|
|
23
|
+
class="hidden"
|
|
24
|
+
:accept="acceptFile"
|
|
25
|
+
:disabled="isDisabled"
|
|
26
|
+
@change="handleChange"
|
|
27
|
+
/>
|
|
28
|
+
<div :class="[ui.wrapper]">
|
|
29
|
+
<div v-if="selectedFile" :class="[ui.preview.wrapper]">
|
|
30
|
+
<div v-if="isImage(selectedFile)" :class="[ui.preview.image.wrapper]">
|
|
31
|
+
<img :src="generateURL(selectedFile)" :class="[ui.preview.image.img]" alt="file" />
|
|
32
|
+
</div>
|
|
33
|
+
<Icon v-else :name="ui.default.filePreviewIcon" :class="[ui.preview.icon]" />
|
|
34
|
+
<div :class="[ui.preview.filename]">
|
|
35
|
+
<p>{{ selectedFile.name }}</p>
|
|
36
|
+
</div>
|
|
37
|
+
<div :class="[ui.preview.fileInfo]">
|
|
38
|
+
{{ isSelectedFileUseMb ? `${selectedFileSizeMb} MB` : `${selectedFileSizeKb} KB` }}
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
<div v-if="!selectedFile" :class="[ui.placeholderWrapper]">
|
|
42
|
+
<Icon :name="ui.default.uploadIcon" :class="[ui.labelIcon]" />
|
|
43
|
+
<div :class="[ui.labelWrapper]">
|
|
44
|
+
<p class="text-primary cursor-pointer" @click="handleOpenFile">
|
|
45
|
+
{{ selectFileLabel ?? 'คลิกเพื่อเลือกไฟล์' }}
|
|
46
|
+
</p>
|
|
47
|
+
<p>{{ selectFileSubLabel ?? 'หรือ ลากและวางที่นี่' }}</p>
|
|
48
|
+
</div>
|
|
49
|
+
<p v-if="placeholder" :class="[ui.placeholder]">{{ placeholder }}</p>
|
|
50
|
+
</div>
|
|
51
|
+
</div>
|
|
52
|
+
</div>
|
|
53
|
+
</FieldWrapper>
|
|
54
|
+
</template>
|
|
55
|
+
|
|
56
|
+
<script lang="ts" setup>
|
|
57
|
+
import { useDropZone } from '@vueuse/core'
|
|
58
|
+
import { type IUploadDropzoneProps } from './types'
|
|
59
|
+
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
60
|
+
import { useFieldHOC } from '#core/composables/useForm'
|
|
61
|
+
import { computed, ref, toRef, useUI, useUiConfig } from '#imports'
|
|
62
|
+
import { uploadFileDropzone } from '#core/ui.config'
|
|
63
|
+
import i18next from 'i18next'
|
|
64
|
+
|
|
65
|
+
const config = useUiConfig<typeof uploadFileDropzone>(uploadFileDropzone, 'uploadFileDropzone')
|
|
66
|
+
|
|
67
|
+
const props = withDefaults(defineProps<IUploadDropzoneProps>(), {})
|
|
68
|
+
const emit = defineEmits(['change', 'delete'])
|
|
69
|
+
|
|
70
|
+
const { wrapperProps, handleChange: onChange, setErrors, value } = useFieldHOC<File>(props)
|
|
71
|
+
|
|
72
|
+
const { ui } = useUI('uploadFileDropzone', toRef(props, 'ui'), config)
|
|
73
|
+
|
|
74
|
+
const fileInputRef = ref<HTMLInputElement>()
|
|
75
|
+
const dropzoneRef = ref<HTMLDivElement>()
|
|
76
|
+
|
|
77
|
+
const acceptFile = computed(() =>
|
|
78
|
+
typeof props.accept === 'string' ? props.accept : props.accept?.join(',')
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
const acceptFileSizeKb = computed(() => props.maxSize)
|
|
82
|
+
const acceptFileSizeMb = computed(() => ((acceptFileSizeKb.value ?? 0) / 1024).toFixed(2))
|
|
83
|
+
const isAcceptFileUseMb = computed(() => acceptFileSizeKb.value && acceptFileSizeKb.value > 1024)
|
|
84
|
+
|
|
85
|
+
const selectedFileSizeKb = computed(() => ((value?.value.size || 0) / 1000).toFixed(2))
|
|
86
|
+
const selectedFileSizeMb = computed(() => ((value?.value.size || 0) / 1000 / 1000).toFixed(2))
|
|
87
|
+
const isSelectedFileUseMb = computed(() => (value?.value.size || 0) / 1000 > 1024)
|
|
88
|
+
const selectedFile = computed(() => value?.value)
|
|
89
|
+
|
|
90
|
+
const onDrop = (files: File[] | null) => {
|
|
91
|
+
if (props.isDisabled || files?.length === 0 || !files) return
|
|
92
|
+
|
|
93
|
+
const file = files[0]
|
|
94
|
+
const result = handleCheckFileCondition(file)
|
|
95
|
+
|
|
96
|
+
if (result) {
|
|
97
|
+
onChange(file)
|
|
98
|
+
emit('change', file)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const { isOverDropZone } = useDropZone(dropzoneRef, {
|
|
103
|
+
onDrop,
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const handleChange = (e: Event) => {
|
|
107
|
+
if (props.isDisabled) return
|
|
108
|
+
|
|
109
|
+
const file = (e.target as HTMLInputElement).files?.[0]
|
|
110
|
+
const result = handleCheckFileCondition(file)
|
|
111
|
+
|
|
112
|
+
if (result) {
|
|
113
|
+
onChange(file)
|
|
114
|
+
value.value = file
|
|
115
|
+
emit('change', file)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const handleOpenFile = () => {
|
|
120
|
+
fileInputRef.value?.click()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const handleDeleteFile = () => {
|
|
124
|
+
fileInputRef.value?.value && (fileInputRef.value.value = '')
|
|
125
|
+
onChange(undefined)
|
|
126
|
+
emit('delete')
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const handleCheckFileCondition = (file: File | undefined): boolean => {
|
|
130
|
+
if (!file) return false
|
|
131
|
+
|
|
132
|
+
const maxSize = checkMaxSize(file)
|
|
133
|
+
|
|
134
|
+
if (!maxSize) {
|
|
135
|
+
if (isAcceptFileUseMb.value) {
|
|
136
|
+
setErrors(i18next.t('custom:invalid_file_size_mb', { size: acceptFileSizeMb.value }))
|
|
137
|
+
} else {
|
|
138
|
+
setErrors(i18next.t('custom:invalid_file_size_kb', { size: acceptFileSizeKb.value }))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
setErrors('')
|
|
145
|
+
|
|
146
|
+
return true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const isImage = (file: File) => {
|
|
150
|
+
return file.type.startsWith('image/')
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const checkMaxSize = (file: File): boolean => {
|
|
154
|
+
if (acceptFileSizeKb.value) {
|
|
155
|
+
return file.size / 1000 <= acceptFileSizeKb.value
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return true
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const generateURL = (file: File) => {
|
|
162
|
+
const fileSrc = URL.createObjectURL(file)
|
|
163
|
+
|
|
164
|
+
setTimeout(() => {
|
|
165
|
+
URL.revokeObjectURL(fileSrc)
|
|
166
|
+
}, 1000)
|
|
167
|
+
|
|
168
|
+
return fileSrc
|
|
169
|
+
}
|
|
170
|
+
</script>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type IFieldProps, type IFormFieldBase, type INPUT_TYPES } from '../types';
|
|
2
|
+
export interface IUploadDropzoneProps extends IFieldProps {
|
|
3
|
+
selectFileLabel?: string;
|
|
4
|
+
selectFileSubLabel?: string;
|
|
5
|
+
accept?: string[] | string;
|
|
6
|
+
maxSize?: number;
|
|
7
|
+
}
|
|
8
|
+
export type IUploadDropzoneField = IFormFieldBase<INPUT_TYPES.UPLOAD_DROPZONE, IUploadDropzoneProps, {
|
|
9
|
+
change: (value: File | undefined) => void;
|
|
10
|
+
delete: () => void;
|
|
11
|
+
}>;
|
|
File without changes
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
-
<UInput
|
|
4
|
-
type="file"
|
|
5
|
-
trailing
|
|
6
|
-
:loading-icon="loadingIcon"
|
|
7
|
-
:icon="icon"
|
|
8
|
-
:leading-icon="leadingIcon"
|
|
9
|
-
:trailing-icon="trailingIcon"
|
|
10
|
-
:disabled="wrapperProps.isDisabled"
|
|
11
|
-
:name="name"
|
|
12
|
-
:placeholder="wrapperProps.placeholder"
|
|
13
|
-
:autofocus="!!autoFocus"
|
|
14
|
-
:readonly="isReadonly"
|
|
15
|
-
:accept="accept"
|
|
16
|
-
:ui="ui"
|
|
17
|
-
@change="handleChange"
|
|
18
|
-
/>
|
|
19
|
-
</FieldWrapper>
|
|
20
|
-
</template>
|
|
21
|
-
<script lang="ts" setup>
|
|
22
|
-
import { useFieldHOC } from '#core/composables/useForm'
|
|
23
|
-
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
24
|
-
import { type IUploadFileClassicFieldProps } from '#core/components/Form/InputUploadFileClassic/types'
|
|
25
|
-
|
|
26
|
-
const props = withDefaults(defineProps<IUploadFileClassicFieldProps>(), {})
|
|
27
|
-
|
|
28
|
-
const { value, wrapperProps } = useFieldHOC<File | undefined>(props)
|
|
29
|
-
|
|
30
|
-
const emits = defineEmits(['change'])
|
|
31
|
-
|
|
32
|
-
const handleChange = (e: Event) => {
|
|
33
|
-
value.value = (e.target as HTMLInputElement).files?.[0]
|
|
34
|
-
emits('change', e)
|
|
35
|
-
}
|
|
36
|
-
</script>
|
|
1
|
+
<template>
|
|
2
|
+
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
+
<UInput
|
|
4
|
+
type="file"
|
|
5
|
+
trailing
|
|
6
|
+
:loading-icon="loadingIcon"
|
|
7
|
+
:icon="icon"
|
|
8
|
+
:leading-icon="leadingIcon"
|
|
9
|
+
:trailing-icon="trailingIcon"
|
|
10
|
+
:disabled="wrapperProps.isDisabled"
|
|
11
|
+
:name="name"
|
|
12
|
+
:placeholder="wrapperProps.placeholder"
|
|
13
|
+
:autofocus="!!autoFocus"
|
|
14
|
+
:readonly="isReadonly"
|
|
15
|
+
:accept="accept"
|
|
16
|
+
:ui="ui"
|
|
17
|
+
@change="handleChange"
|
|
18
|
+
/>
|
|
19
|
+
</FieldWrapper>
|
|
20
|
+
</template>
|
|
21
|
+
<script lang="ts" setup>
|
|
22
|
+
import { useFieldHOC } from '#core/composables/useForm'
|
|
23
|
+
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
24
|
+
import { type IUploadFileClassicFieldProps } from '#core/components/Form/InputUploadFileClassic/types'
|
|
25
|
+
|
|
26
|
+
const props = withDefaults(defineProps<IUploadFileClassicFieldProps>(), {})
|
|
27
|
+
|
|
28
|
+
const { value, wrapperProps } = useFieldHOC<File | undefined>(props)
|
|
29
|
+
|
|
30
|
+
const emits = defineEmits(['change'])
|
|
31
|
+
|
|
32
|
+
const handleChange = (e: Event) => {
|
|
33
|
+
value.value = (e.target as HTMLInputElement).files?.[0]
|
|
34
|
+
emits('change', e)
|
|
35
|
+
}
|
|
36
|
+
</script>
|
|
@@ -1,152 +1,160 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
-
<div :class="[ui.base]">
|
|
4
|
-
<input
|
|
5
|
-
ref="fileInput"
|
|
6
|
-
type="file"
|
|
7
|
-
class="hidden"
|
|
8
|
-
:accept="acceptFile"
|
|
9
|
-
:required="isRequired"
|
|
10
|
-
:disabled="isDisabled"
|
|
11
|
-
@change="handleChange"
|
|
12
|
-
/>
|
|
13
|
-
<div :class="[ui.wrapper]">
|
|
14
|
-
<div :class="[ui.selectFileBox]">
|
|
15
|
-
<Button size="2xs" @click="handleOpenFile">{{ selectFileLabel || 'Choose File' }}</Button>
|
|
16
|
-
<p :class="ui.placeholder">
|
|
17
|
-
{{ selectedFile?.name ?? placeholder ?? 'No file chosen' }}
|
|
18
|
-
</p>
|
|
19
|
-
<Badge v-if="selectedFile" size="xs" variant="outline">
|
|
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
|
-
import
|
|
48
|
-
import {
|
|
49
|
-
import
|
|
50
|
-
import {
|
|
51
|
-
import
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return true
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
useWatchTrue(
|
|
147
|
-
() => upload.status.value.
|
|
148
|
-
() => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
1
|
+
<template>
|
|
2
|
+
<FieldWrapper v-bind="wrapperProps">
|
|
3
|
+
<div :class="[ui.base]">
|
|
4
|
+
<input
|
|
5
|
+
ref="fileInput"
|
|
6
|
+
type="file"
|
|
7
|
+
class="hidden"
|
|
8
|
+
:accept="acceptFile"
|
|
9
|
+
:required="isRequired"
|
|
10
|
+
:disabled="isDisabled"
|
|
11
|
+
@change="handleChange"
|
|
12
|
+
/>
|
|
13
|
+
<div :class="[ui.wrapper]">
|
|
14
|
+
<div :class="[ui.selectFileBox]">
|
|
15
|
+
<Button size="2xs" @click="handleOpenFile">{{ selectFileLabel || 'Choose File' }}</Button>
|
|
16
|
+
<p :class="ui.placeholder">
|
|
17
|
+
{{ selectedFile?.name ?? placeholder ?? 'No file chosen' }}
|
|
18
|
+
</p>
|
|
19
|
+
<Badge v-if="selectedFile" size="xs" variant="outline">
|
|
20
|
+
{{ isSelectedFileUseMb ? `${selectedFileSizeMb} MB` : `${selectedFileSizeKb} KB` }}
|
|
21
|
+
</Badge>
|
|
22
|
+
</div>
|
|
23
|
+
<div v-if="selectedFile">
|
|
24
|
+
<Icon
|
|
25
|
+
v-if="upload.status.value.isSuccess"
|
|
26
|
+
name="heroicons:check-circle-20-solid"
|
|
27
|
+
class="text-success"
|
|
28
|
+
/>
|
|
29
|
+
<Icon
|
|
30
|
+
v-if="upload.status.value.isError"
|
|
31
|
+
name="heroicons:x-circle-20-solid"
|
|
32
|
+
class="text-danger"
|
|
33
|
+
/>
|
|
34
|
+
<Icon
|
|
35
|
+
v-if="upload.status.value.isLoading"
|
|
36
|
+
name="i-svg-spinners:180-ring-with-bg"
|
|
37
|
+
class="text-primary"
|
|
38
|
+
/>
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
</div>
|
|
42
|
+
<img v-if="imagePreviewURL && value" :src="imagePreviewURL" alt="" :class="ui.previewURL" />
|
|
43
|
+
</FieldWrapper>
|
|
44
|
+
</template>
|
|
45
|
+
|
|
46
|
+
<script lang="tsx" setup>
|
|
47
|
+
import { computed, ref, StringHelper, toRef, useUI, useUiConfig, useWatchTrue } from '#imports'
|
|
48
|
+
import { type IUploadFileProps } from './types'
|
|
49
|
+
import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
|
|
50
|
+
import { useFieldHOC } from '#core/composables/useForm'
|
|
51
|
+
import { type IUploadRequest, useUploadLoader } from '#core/composables/useUpload'
|
|
52
|
+
import { uploadFileInputClassicAuto } from '#core/ui.config'
|
|
53
|
+
import i18next from 'i18next'
|
|
54
|
+
|
|
55
|
+
const config = useUiConfig<typeof uploadFileInputClassicAuto>(
|
|
56
|
+
uploadFileInputClassicAuto,
|
|
57
|
+
'uploadFileInputClassicAuto'
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const emits = defineEmits(['success'])
|
|
61
|
+
const props = withDefaults(defineProps<IUploadFileProps>(), {})
|
|
62
|
+
|
|
63
|
+
const { wrapperProps, setErrors, value } = useFieldHOC<string>(props)
|
|
64
|
+
|
|
65
|
+
const request: IUploadRequest = {
|
|
66
|
+
pathURL: props.uploadPathURL,
|
|
67
|
+
requestOptions: props.requestOptions,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const upload = useUploadLoader(request)
|
|
71
|
+
|
|
72
|
+
const fileInput = ref<HTMLInputElement>()
|
|
73
|
+
const selectedFile = ref<File | undefined>()
|
|
74
|
+
const percent = ref<number>(0)
|
|
75
|
+
|
|
76
|
+
const selectedFileSizeKb = computed(() => ((selectedFile.value?.size || 0) / 1000).toFixed(2))
|
|
77
|
+
const selectedFileSizeMb = computed(() =>
|
|
78
|
+
((selectedFile.value?.size || 0) / 1000 / 1000).toFixed(2)
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
const isSelectedFileUseMb = computed(() => (selectedFile.value?.size || 0) / 1000 > 1024)
|
|
82
|
+
const acceptFileSize = computed(() => props.maxSize)
|
|
83
|
+
const acceptFileSizeMb = computed(() => ((acceptFileSize.value || 0) / 1024).toFixed(2))
|
|
84
|
+
|
|
85
|
+
const acceptFile = computed(() =>
|
|
86
|
+
typeof props.accept === 'string' ? props.accept : props.accept?.join(',')
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
const { ui } = useUI('uploadFileInputClassicAuto', toRef(props, 'ui'), config)
|
|
90
|
+
|
|
91
|
+
const handleOpenFile = () => {
|
|
92
|
+
fileInput.value?.click()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const handleChange = (e: Event) => {
|
|
96
|
+
const file = (e.target as HTMLInputElement).files?.[0]
|
|
97
|
+
const result = handleCheckFileCondition(file)
|
|
98
|
+
|
|
99
|
+
if (result && file) {
|
|
100
|
+
selectedFile.value = file
|
|
101
|
+
const formData = new FormData()
|
|
102
|
+
|
|
103
|
+
formData.append(props.bodyKey || 'file', file)
|
|
104
|
+
upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const handleCheckFileCondition = (file: File | undefined): boolean => {
|
|
109
|
+
if (!file) return false
|
|
110
|
+
|
|
111
|
+
const maxSize = checkMaxSize(file)
|
|
112
|
+
|
|
113
|
+
if (!maxSize) {
|
|
114
|
+
setErrors(i18next.t('custom:invalid_file_size', { size: acceptFileSizeMb.value }))
|
|
115
|
+
|
|
116
|
+
return false
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
setErrors('')
|
|
120
|
+
|
|
121
|
+
return true
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const checkMaxSize = (file: File): boolean => {
|
|
125
|
+
if (acceptFileSize.value) {
|
|
126
|
+
return file.size / 1000 <= acceptFileSize.value
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return true
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const onUploadProgress = (progressEvent: ProgressEvent) => {
|
|
133
|
+
percent.value = (Math.floor((progressEvent.loaded * 100) / progressEvent.total) || 0) * 0.8
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const onDownloadProgress = (progressEvent: ProgressEvent) => {
|
|
137
|
+
if (progressEvent.total === 0) {
|
|
138
|
+
percent.value = 100
|
|
139
|
+
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
percent.value = (Math.floor((progressEvent.loaded * 100) / progressEvent.total) || 0) * 0.2 + 80
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
useWatchTrue(
|
|
147
|
+
() => upload.status.value.isSuccess,
|
|
148
|
+
() => {
|
|
149
|
+
value.value = upload.data.value[props.responseKey || 'url']
|
|
150
|
+
emits('success', upload.data.value)
|
|
151
|
+
}
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
useWatchTrue(
|
|
155
|
+
() => upload.status.value.isError,
|
|
156
|
+
() => {
|
|
157
|
+
setErrors(StringHelper.getError(upload.status.value.errorData))
|
|
158
|
+
}
|
|
159
|
+
)
|
|
160
|
+
</script>
|