@finema/core 1.4.34 → 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.
Files changed (60) hide show
  1. package/README.md +63 -63
  2. package/dist/module.d.mts +4 -4
  3. package/dist/module.d.ts +4 -4
  4. package/dist/module.json +1 -1
  5. package/dist/module.mjs +7 -4
  6. package/dist/runtime/components/Alert.vue +49 -49
  7. package/dist/runtime/components/Avatar.vue +27 -27
  8. package/dist/runtime/components/Badge.vue +54 -54
  9. package/dist/runtime/components/Breadcrumb.vue +45 -45
  10. package/dist/runtime/components/Button/Group.vue +37 -37
  11. package/dist/runtime/components/Button/index.vue +77 -77
  12. package/dist/runtime/components/Card.vue +38 -38
  13. package/dist/runtime/components/Core.vue +13 -13
  14. package/dist/runtime/components/Dialog/index.vue +108 -108
  15. package/dist/runtime/components/Dropdown/index.vue +71 -71
  16. package/dist/runtime/components/Form/FieldWrapper.vue +23 -23
  17. package/dist/runtime/components/Form/Fields.vue +147 -136
  18. package/dist/runtime/components/Form/InputCheckbox/index.vue +21 -21
  19. package/dist/runtime/components/Form/InputDateTime/index.vue +51 -51
  20. package/dist/runtime/components/Form/InputRadio/index.vue +27 -27
  21. package/dist/runtime/components/Form/InputSelect/index.vue +37 -37
  22. package/dist/runtime/components/Form/InputStatic/index.vue +16 -16
  23. package/dist/runtime/components/Form/InputText/index.vue +27 -27
  24. package/dist/runtime/components/Form/InputTextarea/index.vue +25 -25
  25. package/dist/runtime/components/Form/InputToggle/index.vue +14 -14
  26. package/dist/runtime/components/Form/InputUploadDropzone/index.vue +170 -0
  27. package/dist/runtime/components/Form/InputUploadDropzone/types.d.ts +11 -0
  28. package/dist/runtime/components/Form/InputUploadDropzone/types.mjs +0 -0
  29. package/dist/runtime/components/Form/InputUploadFileClassic/index.vue +36 -36
  30. package/dist/runtime/components/Form/InputUploadFileClassicAuto/index.vue +160 -152
  31. package/dist/runtime/components/Form/index.vue +6 -6
  32. package/dist/runtime/components/Form/types.d.ts +4 -2
  33. package/dist/runtime/components/Form/types.mjs +1 -0
  34. package/dist/runtime/components/Icon.vue +23 -23
  35. package/dist/runtime/components/Image.vue +36 -36
  36. package/dist/runtime/components/Loader.vue +14 -14
  37. package/dist/runtime/components/Modal/index.vue +146 -146
  38. package/dist/runtime/components/SimplePagination.vue +96 -96
  39. package/dist/runtime/components/Slideover/index.vue +110 -110
  40. package/dist/runtime/components/Table/Base.vue +132 -132
  41. package/dist/runtime/components/Table/ColumnDate.vue +16 -16
  42. package/dist/runtime/components/Table/ColumnDateTime.vue +30 -30
  43. package/dist/runtime/components/Table/ColumnImage.vue +13 -13
  44. package/dist/runtime/components/Table/ColumnNumber.vue +14 -14
  45. package/dist/runtime/components/Table/Simple.vue +57 -57
  46. package/dist/runtime/components/Table/index.vue +52 -52
  47. package/dist/runtime/components/Tabs/index.vue +65 -65
  48. package/dist/runtime/lib/Requester.d.ts +2 -0
  49. package/dist/runtime/lib/Requester.mjs +3 -0
  50. package/dist/runtime/plugin.mjs +4 -2
  51. package/dist/runtime/types/config.d.ts +1 -1
  52. package/dist/runtime/types/utils.d.ts +29 -29
  53. package/dist/runtime/ui.config/breadcrumb.mjs +1 -1
  54. package/dist/runtime/ui.config/button.mjs +1 -1
  55. package/dist/runtime/ui.config/index.d.ts +1 -0
  56. package/dist/runtime/ui.config/index.mjs +1 -0
  57. package/dist/runtime/ui.config/uploadFileDropzone.d.ts +31 -0
  58. package/dist/runtime/ui.config/uploadFileDropzone.mjs +31 -0
  59. package/dist/runtime/ui.css +32 -32
  60. 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
+ }>;
@@ -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"> {{ selectedFileSize }} MB </Badge>
20
- </div>
21
- <div v-if="selectedFile">
22
- <Icon
23
- v-if="upload.status.value.isSuccess"
24
- name="heroicons:check-circle-20-solid"
25
- class="text-success"
26
- />
27
- <Icon
28
- v-if="upload.status.value.isError"
29
- name="heroicons:x-circle-20-solid"
30
- class="text-danger"
31
- />
32
- <Icon
33
- v-if="upload.status.value.isLoading"
34
- name="i-svg-spinners:180-ring-with-bg"
35
- class="text-primary"
36
- />
37
- </div>
38
- </div>
39
- </div>
40
- <img v-if="imagePreviewURL && value" :src="imagePreviewURL" alt="" :class="ui.previewURL" />
41
- </FieldWrapper>
42
- </template>
43
-
44
- <script lang="tsx" setup>
45
- import { computed, ref, StringHelper, toRef, useUI, useUiConfig, useWatchTrue } from '#imports'
46
- import { type IUploadFileProps } from './types'
47
- import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
48
- import { useFieldHOC } from '#core/composables/useForm'
49
- import { type IUploadRequest, useUploadLoader } from '#core/composables/useUpload'
50
- import { uploadFileInputClassicAuto } from '#core/ui.config'
51
- import i18next from 'i18next'
52
-
53
- const config = useUiConfig<typeof uploadFileInputClassicAuto>(
54
- uploadFileInputClassicAuto,
55
- 'uploadFileInputClassicAuto'
56
- )
57
-
58
- const emits = defineEmits(['success'])
59
- const props = withDefaults(defineProps<IUploadFileProps>(), {})
60
-
61
- const { wrapperProps, setErrors, value } = useFieldHOC<string>(props)
62
-
63
- const request: IUploadRequest = {
64
- pathURL: props.uploadPathURL,
65
- requestOptions: props.requestOptions,
66
- }
67
-
68
- const upload = useUploadLoader(request)
69
-
70
- const fileInput = ref<HTMLInputElement>()
71
- const selectedFile = ref<File | undefined>()
72
- const percent = ref<number>(0)
73
-
74
- const selectedFileSize = computed(() => ((selectedFile.value?.size || 0) / 1000 / 1000).toFixed(2))
75
- const acceptFileSize = computed(() => props.maxSize)
76
- const acceptFileSizeMb = computed(() => ((acceptFileSize.value || 0) / 1024).toFixed(2))
77
- const acceptFile = computed(() =>
78
- typeof props.accept === 'string' ? props.accept : props.accept?.join(',')
79
- )
80
-
81
- const { ui } = useUI('uploadFileInputClassicAuto', toRef(props, 'ui'), config)
82
-
83
- const handleOpenFile = () => {
84
- fileInput.value?.click()
85
- }
86
-
87
- const handleChange = (e: Event) => {
88
- const file = (e.target as HTMLInputElement).files?.[0]
89
- const result = handleCheckFileCondition(file)
90
-
91
- if (result && file) {
92
- selectedFile.value = file
93
- const formData = new FormData()
94
-
95
- formData.append(props.bodyKey || 'file', file)
96
- upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
97
- }
98
- }
99
-
100
- const handleCheckFileCondition = (file: File | undefined): boolean => {
101
- if (!file) return false
102
-
103
- const maxSize = checkMaxSize(file)
104
-
105
- if (!maxSize) {
106
- setErrors(i18next.t('custom:invalid_file_size', { size: acceptFileSizeMb.value }))
107
-
108
- return false
109
- }
110
-
111
- setErrors('')
112
-
113
- return true
114
- }
115
-
116
- const checkMaxSize = (file: File): boolean => {
117
- if (acceptFileSize.value) {
118
- return file.size / 1000 <= acceptFileSize.value
119
- }
120
-
121
- return true
122
- }
123
-
124
- const onUploadProgress = (progressEvent: ProgressEvent) => {
125
- percent.value = (Math.floor((progressEvent.loaded * 100) / progressEvent.total) || 0) * 0.8
126
- }
127
-
128
- const onDownloadProgress = (progressEvent: ProgressEvent) => {
129
- if (progressEvent.total === 0) {
130
- percent.value = 100
131
-
132
- return
133
- }
134
-
135
- percent.value = (Math.floor((progressEvent.loaded * 100) / progressEvent.total) || 0) * 0.2 + 80
136
- }
137
-
138
- useWatchTrue(
139
- () => upload.status.value.isSuccess,
140
- () => {
141
- value.value = upload.data.value[props.responseKey || 'url']
142
- emits('success', upload.data.value)
143
- }
144
- )
145
-
146
- useWatchTrue(
147
- () => upload.status.value.isError,
148
- () => {
149
- setErrors(StringHelper.getError(upload.status.value.errorData))
150
- }
151
- )
152
- </script>
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>