@finema/core 1.4.216 → 1.4.217

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 (63) hide show
  1. package/README.md +60 -60
  2. package/dist/module.json +1 -1
  3. package/dist/module.mjs +1 -1
  4. package/dist/runtime/components/Alert.vue +48 -48
  5. package/dist/runtime/components/Avatar.vue +27 -27
  6. package/dist/runtime/components/Badge.vue +11 -11
  7. package/dist/runtime/components/Breadcrumb.vue +44 -44
  8. package/dist/runtime/components/Button/Group.vue +37 -37
  9. package/dist/runtime/components/Button/index.vue +75 -75
  10. package/dist/runtime/components/Card.vue +38 -38
  11. package/dist/runtime/components/Core.vue +45 -45
  12. package/dist/runtime/components/Dialog/index.vue +108 -108
  13. package/dist/runtime/components/Dropdown/index.vue +70 -70
  14. package/dist/runtime/components/FlexDeck/Base.vue +159 -159
  15. package/dist/runtime/components/FlexDeck/index.vue +68 -68
  16. package/dist/runtime/components/Form/FieldWrapper.vue +23 -23
  17. package/dist/runtime/components/Form/Fields.vue +230 -230
  18. package/dist/runtime/components/Form/InputCheckbox/index.vue +28 -28
  19. package/dist/runtime/components/Form/InputDateTime/index.vue +68 -61
  20. package/dist/runtime/components/Form/InputDateTimeRange/index.vue +91 -84
  21. package/dist/runtime/components/Form/InputNumber/index.vue +27 -27
  22. package/dist/runtime/components/Form/InputRadio/index.vue +27 -27
  23. package/dist/runtime/components/Form/InputSelect/index.vue +55 -55
  24. package/dist/runtime/components/Form/InputSelectMultiple/index.vue +54 -54
  25. package/dist/runtime/components/Form/InputStatic/index.vue +16 -16
  26. package/dist/runtime/components/Form/InputTags/index.vue +141 -141
  27. package/dist/runtime/components/Form/InputText/index.vue +68 -68
  28. package/dist/runtime/components/Form/InputTextarea/index.vue +25 -25
  29. package/dist/runtime/components/Form/InputToggle/index.vue +27 -27
  30. package/dist/runtime/components/Form/InputUploadDropzone/index.vue +206 -206
  31. package/dist/runtime/components/Form/InputUploadDropzoneAuto/index.vue +350 -350
  32. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/ItemUpload.vue +241 -241
  33. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/ItemView.vue +102 -102
  34. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/index.vue +171 -170
  35. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/ItemUpload.vue +161 -161
  36. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/ItemView.vue +64 -64
  37. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/index.vue +178 -178
  38. package/dist/runtime/components/Form/InputUploadFileClassic/index.vue +95 -95
  39. package/dist/runtime/components/Form/InputUploadFileClassicAuto/index.vue +151 -151
  40. package/dist/runtime/components/Form/InputUploadImageAuto/index.vue +219 -219
  41. package/dist/runtime/components/Form/InputWYSIWYG/UploadImageForm.vue +55 -55
  42. package/dist/runtime/components/Form/InputWYSIWYG/index.vue +228 -228
  43. package/dist/runtime/components/Form/index.vue +6 -6
  44. package/dist/runtime/components/Icon.vue +23 -23
  45. package/dist/runtime/components/Image.vue +36 -36
  46. package/dist/runtime/components/Loader.vue +27 -27
  47. package/dist/runtime/components/Modal/index.vue +146 -146
  48. package/dist/runtime/components/QRCode.vue +22 -22
  49. package/dist/runtime/components/SimplePagination.vue +96 -96
  50. package/dist/runtime/components/Slideover/index.vue +110 -110
  51. package/dist/runtime/components/Table/Base.vue +161 -161
  52. package/dist/runtime/components/Table/ColumnDate.vue +16 -16
  53. package/dist/runtime/components/Table/ColumnDateTime.vue +18 -18
  54. package/dist/runtime/components/Table/ColumnImage.vue +15 -15
  55. package/dist/runtime/components/Table/ColumnNumber.vue +14 -14
  56. package/dist/runtime/components/Table/ColumnText.vue +29 -29
  57. package/dist/runtime/components/Table/Simple.vue +70 -70
  58. package/dist/runtime/components/Table/index.vue +65 -65
  59. package/dist/runtime/components/Tabs/index.vue +64 -64
  60. package/dist/runtime/components/TeleportSafe.vue +40 -40
  61. package/package.json +100 -100
  62. package/dist/runtime/components/Form/InputDateTime/index.vue~ +0 -61
  63. package/dist/runtime/ui.config/table.ts~ +0 -48
@@ -1,350 +1,350 @@
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 || !isReadonly),
11
- [ui.failed]: upload.status.value.isError,
12
- },
13
- ]"
14
- >
15
- <input
16
- ref="fileInputRef"
17
- type="file"
18
- class="hidden"
19
- :name="name"
20
- :accept="fileAllocate.acceptFile.value"
21
- :disabled="isDisabled || isReadonly"
22
- @change="handleChange"
23
- />
24
- <div :class="[ui.wrapper]">
25
- <div v-if="!selectedFile && !value" :class="[ui.placeholderWrapper]">
26
- <Icon :name="ui.default.uploadIcon" :class="[ui.labelIcon]" />
27
- <div :class="[ui.labelWrapper]">
28
- <p class="text-primary cursor-pointer" @click="handleOpenFile">
29
- {{ selectFileLabel }}
30
- </p>
31
- <p>{{ selectFileSubLabel }}</p>
32
- </div>
33
- <p v-if="placeholder" :class="[ui.placeholder]">{{ placeholder }}</p>
34
- </div>
35
-
36
- <!-- Loading State -->
37
- <div v-if="selectedFile && upload.status.value.isLoading" :class="[ui.onLoading.wrapper]">
38
- <div :class="[ui.onLoading.placeholderWrapper]">
39
- <Icon
40
- :name="
41
- isImage(selectedFile)
42
- ? ui.onLoading.placeholderImgIcon || ui.default.placeholderImgIcon
43
- : ui.onLoading.placeholderFileIcon || ui.default.filePreviewIcon
44
- "
45
- :class="[ui.onLoading.placeholderIconClass]"
46
- />
47
- </div>
48
- <div :class="[ui.onLoading.textWrapper]">
49
- <div class="truncate">
50
- <h1 class="truncate font-bold">{{ selectedFile?.name }}</h1>
51
- <p class="truncate font-light text-gray-400">
52
- {{
53
- fileAllocate.isSelectedFileUseMb.value
54
- ? `${fileAllocate.selectedFileSizeMb.value} MB`
55
- : `${fileAllocate.selectedFileSizeKb.value} KB`
56
- }}
57
- - {{ percent }}% {{ uploadingLabel }}
58
- </p>
59
- </div>
60
- <div>
61
- <Icon
62
- :name="ui.onLoading.loadingIcon || ui.default.loadingIcon"
63
- :class="[ui.onLoading.loadingIconClass]"
64
- />
65
- </div>
66
- </div>
67
- </div>
68
-
69
- <!-- Success State -->
70
- <div v-if="value" :class="[ui.onPreview.wrapper]">
71
- <div :class="[ui.onPreview.previewImgWrapper]">
72
- <div v-if="isImageFromPath(value.path)" class="size-full overflow-hidden">
73
- <img :src="value.url" :class="[ui.onPreview.previewImgClass]" alt="img-preview" />
74
- </div>
75
- <div v-else>
76
- <Icon
77
- :name="ui.onPreview.previewFileIcon || ui.default.filePreviewIcon"
78
- :class="[ui.onPreview.previewFileClass]"
79
- />
80
- </div>
81
- </div>
82
- <div :class="[ui.onPreview.textWrapper]">
83
- <div class="truncate">
84
- <h1 class="truncate font-bold">{{ value.name }}</h1>
85
- <p class="truncate text-sm font-light text-gray-400">
86
- {{
87
- fileAllocateFromPath.isSelectedFileUseMb.value
88
- ? `${fileAllocate.selectedFileSizeMb.value} MB`
89
- : `${fileAllocateFromPath.selectedFileSizeKb.value} KB`
90
- }}
91
- </p>
92
- </div>
93
- <div :class="[ui.action.wrapper]">
94
- <a v-if="isPDFFromPath(value.path)" :href="value.url" target="_blank" class="flex">
95
- <Icon
96
- :name="ui.action.previewIcon"
97
- :class="[ui.action.iconClass]"
98
- title="ดูตัวอย่าง"
99
- />
100
- </a>
101
- <Icon
102
- v-if="isImageFromPath(value.path)"
103
- :name="ui.action.previewIcon"
104
- :class="[ui.action.iconClass]"
105
- title="ดูตัวอย่าง"
106
- @click="() => (isPreviewOpen = true)"
107
- />
108
- <Icon
109
- :name="ui.action.downloadIcon"
110
- :class="[ui.action.iconClass]"
111
- title="ดาวน์โหลดไฟล์"
112
- @click="handleDownloadFile"
113
- />
114
- <Icon
115
- v-if="!(isDisabled || isReadonly)"
116
- :name="ui.action.deleteIcon"
117
- :class="[ui.action.iconClass]"
118
- title="ลบไฟล์"
119
- @click="handleDeleteFile"
120
- />
121
- <Modal v-model="isPreviewOpen" :title="value.name">
122
- <img :src="value.url" alt="img-preview" />
123
- </Modal>
124
- </div>
125
- </div>
126
- </div>
127
-
128
- <!-- Failed State -->
129
- <div v-if="selectedFile && upload.status.value.isError" :class="[ui.onFailed.wrapper]">
130
- <div :class="[ui.onFailed.failedImgWrapper]">
131
- <Icon
132
- :name="
133
- isImage(selectedFile)
134
- ? ui.onFailed.failedImgIcon || ui.default.placeholderImgIcon
135
- : ui.onFailed.failedFileIcon || ui.default.filePreviewIcon
136
- "
137
- :class="[ui.onFailed.failedIconClass]"
138
- />
139
- </div>
140
- <div :class="[ui.onFailed.textWrapper]">
141
- <div class="truncate">
142
- <h1 class="truncate font-bold">{{ selectedFile?.name }}</h1>
143
- <p class="text-danger truncate font-light">
144
- {{ uploadFailedLabel }}
145
- </p>
146
- <Button
147
- variant="ghost"
148
- :label="retryLabel"
149
- :leading-icon="ui.action.retryIcon"
150
- :class="[ui.action.retryBtnClass]"
151
- size="sm"
152
- @click="handleRetryUpload"
153
- />
154
- </div>
155
- <div :class="[ui.action.wrapper]">
156
- <Icon
157
- title="ลบไฟล์"
158
- :name="ui.action.deleteIcon"
159
- :class="[ui.action.deleteIconClass]"
160
- @click="handleDeleteFile"
161
- />
162
- </div>
163
- </div>
164
- </div>
165
- </div>
166
- </div>
167
- </FieldWrapper>
168
- </template>
169
-
170
- <script lang="ts" setup>
171
- import { useDropZone } from '@vueuse/core'
172
- import { type IUploadDropzoneAutoProps } from './types'
173
- import {
174
- checkMaxSize,
175
- checkFileType,
176
- isImage,
177
- downloadFileFromURL,
178
- useFileAllocate,
179
- useFileProgress,
180
- isImageFromPath,
181
- useFileSize,
182
- isPDFFromPath,
183
- } from '#core/helpers/componentHelper'
184
- import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
185
- import { useFieldHOC } from '#core/composables/useForm'
186
- import {
187
- type IUploadRequest,
188
- ref,
189
- computed,
190
- toRef,
191
- useUI,
192
- useUiConfig,
193
- useUploadLoader,
194
- useWatchTrue,
195
- StringHelper,
196
- _get,
197
- } from '#imports'
198
- import { uploadFileDropzone } from '#core/ui.config'
199
- import i18next from 'i18next'
200
- import type { IFileValue } from '#core/components/Form/types'
201
-
202
- const config = useUiConfig<typeof uploadFileDropzone>(uploadFileDropzone, 'uploadFileDropzone')
203
-
204
- const props = withDefaults(defineProps<IUploadDropzoneAutoProps>(), {
205
- bodyKey: 'file',
206
- responseURL: 'url',
207
- responsePath: 'path',
208
- selectFileLabel: 'คลิกเพื่อเลือกไฟล์',
209
- selectFileSubLabel: 'หรือ ลากและวางที่นี่',
210
- uploadingLabel: 'กำลังอัพโหลด...',
211
- uploadFailedLabel: 'อัพโหลดล้มเหลว, กรุณาลองอีกครั้ง',
212
- })
213
-
214
- const emits = defineEmits(['change', 'success', 'delete'])
215
-
216
- const { wrapperProps, handleChange: onChange, setErrors, value } = useFieldHOC<IFileValue>(props)
217
-
218
- const request: IUploadRequest = {
219
- pathURL: props.uploadPathURL,
220
- requestOptions: props.requestOptions,
221
- }
222
-
223
- const upload = useUploadLoader(request)
224
-
225
- const { ui } = useUI('uploadFileDropzone', toRef(props, 'ui'), config)
226
-
227
- const fileInputRef = ref<HTMLInputElement>()
228
- const dropzoneRef = ref<HTMLDivElement>()
229
- const selectedFile = ref<File | undefined>()
230
- const isPreviewOpen = ref<boolean>(false)
231
-
232
- const { onUploadProgress, onDownloadProgress, percent } = useFileProgress()
233
- const fileAllocate = useFileAllocate(selectedFile, props)
234
- const fileAllocateFromPath = computed(() => useFileSize(value.value?.size))
235
-
236
- const onDrop = (files: File[] | null) => {
237
- if (props.isDisabled || files?.length === 0 || !files) return
238
-
239
- const file = files[0]
240
- const result = handleCheckFileCondition(file)
241
-
242
- if (result && file) {
243
- selectedFile.value = file
244
- const formData = new FormData()
245
-
246
- emits('change', file)
247
-
248
- formData.append(props.bodyKey, file)
249
- upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
250
- }
251
- }
252
-
253
- const { isOverDropZone } = useDropZone(dropzoneRef as unknown as HTMLElement, {
254
- onDrop,
255
- })
256
-
257
- const handleChange = (e: Event) => {
258
- if (props.isDisabled || props.isReadonly) return
259
-
260
- const file = (e.target as HTMLInputElement).files?.[0]
261
- const result = handleCheckFileCondition(file)
262
-
263
- if (result && file) {
264
- selectedFile.value = file
265
- const formData = new FormData()
266
-
267
- emits('change', file)
268
-
269
- formData.append(props.bodyKey, file)
270
- upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
271
- }
272
- }
273
-
274
- const handleOpenFile = () => {
275
- fileInputRef.value?.click()
276
- }
277
-
278
- const handleDeleteFile = () => {
279
- fileInputRef.value?.value && (fileInputRef.value.value = '')
280
- selectedFile.value = undefined
281
- onChange(undefined)
282
- emits('delete')
283
- }
284
-
285
- const handleCheckFileCondition = (file: File | undefined): boolean => {
286
- if (!file) return false
287
-
288
- const fileType = checkFileType(file, fileAllocate.acceptFile.value ?? '')
289
-
290
- if (!fileType) {
291
- setErrors(i18next.t('custom:invalid_file_type'))
292
-
293
- return false
294
- }
295
-
296
- const maxSize = checkMaxSize(file, fileAllocate.acceptFileSizeKb.value)
297
-
298
- if (!maxSize) {
299
- if (fileAllocate.isAcceptFileUseMb.value) {
300
- setErrors(
301
- i18next.t('custom:invalid_file_size_mb', { size: fileAllocate.acceptFileSizeMb.value })
302
- )
303
- } else {
304
- setErrors(
305
- i18next.t('custom:invalid_file_size_kb', { size: fileAllocate.acceptFileSizeKb.value })
306
- )
307
- }
308
-
309
- return false
310
- }
311
-
312
- setErrors('')
313
-
314
- return true
315
- }
316
-
317
- const handleDownloadFile = () => {
318
- downloadFileFromURL(value.value?.url, value.value?.name)
319
- }
320
-
321
- const handleRetryUpload = () => {
322
- if (selectedFile.value) {
323
- const formData = new FormData()
324
-
325
- formData.append(props.bodyKey, selectedFile.value)
326
- upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
327
- }
328
- }
329
-
330
- useWatchTrue(
331
- () => upload.status.value.isSuccess,
332
- () => {
333
- value.value = {
334
- url: _get(upload.data.value, props.responseURL),
335
- path: _get(upload.data.value, props.responsePath),
336
- name: upload.data.value.file_name,
337
- size: upload.data.value.size,
338
- }
339
-
340
- emits('success', value.value)
341
- }
342
- )
343
-
344
- useWatchTrue(
345
- () => upload.status.value.isError,
346
- () => {
347
- setErrors(StringHelper.getError(upload.status.value.errorData))
348
- }
349
- )
350
- </script>
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 || !isReadonly),
11
+ [ui.failed]: upload.status.value.isError,
12
+ },
13
+ ]"
14
+ >
15
+ <input
16
+ ref="fileInputRef"
17
+ type="file"
18
+ class="hidden"
19
+ :name="name"
20
+ :accept="fileAllocate.acceptFile.value"
21
+ :disabled="isDisabled || isReadonly"
22
+ @change="handleChange"
23
+ />
24
+ <div :class="[ui.wrapper]">
25
+ <div v-if="!selectedFile && !value" :class="[ui.placeholderWrapper]">
26
+ <Icon :name="ui.default.uploadIcon" :class="[ui.labelIcon]" />
27
+ <div :class="[ui.labelWrapper]">
28
+ <p class="text-primary cursor-pointer" @click="handleOpenFile">
29
+ {{ selectFileLabel }}
30
+ </p>
31
+ <p>{{ selectFileSubLabel }}</p>
32
+ </div>
33
+ <p v-if="placeholder" :class="[ui.placeholder]">{{ placeholder }}</p>
34
+ </div>
35
+
36
+ <!-- Loading State -->
37
+ <div v-if="selectedFile && upload.status.value.isLoading" :class="[ui.onLoading.wrapper]">
38
+ <div :class="[ui.onLoading.placeholderWrapper]">
39
+ <Icon
40
+ :name="
41
+ isImage(selectedFile)
42
+ ? ui.onLoading.placeholderImgIcon || ui.default.placeholderImgIcon
43
+ : ui.onLoading.placeholderFileIcon || ui.default.filePreviewIcon
44
+ "
45
+ :class="[ui.onLoading.placeholderIconClass]"
46
+ />
47
+ </div>
48
+ <div :class="[ui.onLoading.textWrapper]">
49
+ <div class="truncate">
50
+ <h1 class="truncate font-bold">{{ selectedFile?.name }}</h1>
51
+ <p class="truncate font-light text-gray-400">
52
+ {{
53
+ fileAllocate.isSelectedFileUseMb.value
54
+ ? `${fileAllocate.selectedFileSizeMb.value} MB`
55
+ : `${fileAllocate.selectedFileSizeKb.value} KB`
56
+ }}
57
+ - {{ percent }}% {{ uploadingLabel }}
58
+ </p>
59
+ </div>
60
+ <div>
61
+ <Icon
62
+ :name="ui.onLoading.loadingIcon || ui.default.loadingIcon"
63
+ :class="[ui.onLoading.loadingIconClass]"
64
+ />
65
+ </div>
66
+ </div>
67
+ </div>
68
+
69
+ <!-- Success State -->
70
+ <div v-if="value" :class="[ui.onPreview.wrapper]">
71
+ <div :class="[ui.onPreview.previewImgWrapper]">
72
+ <div v-if="isImageFromPath(value.path)" class="size-full overflow-hidden">
73
+ <img :src="value.url" :class="[ui.onPreview.previewImgClass]" alt="img-preview" />
74
+ </div>
75
+ <div v-else>
76
+ <Icon
77
+ :name="ui.onPreview.previewFileIcon || ui.default.filePreviewIcon"
78
+ :class="[ui.onPreview.previewFileClass]"
79
+ />
80
+ </div>
81
+ </div>
82
+ <div :class="[ui.onPreview.textWrapper]">
83
+ <div class="truncate">
84
+ <h1 class="truncate font-bold">{{ value.name }}</h1>
85
+ <p class="truncate text-sm font-light text-gray-400">
86
+ {{
87
+ fileAllocateFromPath.isSelectedFileUseMb.value
88
+ ? `${fileAllocate.selectedFileSizeMb.value} MB`
89
+ : `${fileAllocateFromPath.selectedFileSizeKb.value} KB`
90
+ }}
91
+ </p>
92
+ </div>
93
+ <div :class="[ui.action.wrapper]">
94
+ <a v-if="isPDFFromPath(value.path)" :href="value.url" target="_blank" class="flex">
95
+ <Icon
96
+ :name="ui.action.previewIcon"
97
+ :class="[ui.action.iconClass]"
98
+ title="ดูตัวอย่าง"
99
+ />
100
+ </a>
101
+ <Icon
102
+ v-if="isImageFromPath(value.path)"
103
+ :name="ui.action.previewIcon"
104
+ :class="[ui.action.iconClass]"
105
+ title="ดูตัวอย่าง"
106
+ @click="() => (isPreviewOpen = true)"
107
+ />
108
+ <Icon
109
+ :name="ui.action.downloadIcon"
110
+ :class="[ui.action.iconClass]"
111
+ title="ดาวน์โหลดไฟล์"
112
+ @click="handleDownloadFile"
113
+ />
114
+ <Icon
115
+ v-if="!(isDisabled || isReadonly)"
116
+ :name="ui.action.deleteIcon"
117
+ :class="[ui.action.iconClass]"
118
+ title="ลบไฟล์"
119
+ @click="handleDeleteFile"
120
+ />
121
+ <Modal v-model="isPreviewOpen" :title="value.name">
122
+ <img :src="value.url" alt="img-preview" />
123
+ </Modal>
124
+ </div>
125
+ </div>
126
+ </div>
127
+
128
+ <!-- Failed State -->
129
+ <div v-if="selectedFile && upload.status.value.isError" :class="[ui.onFailed.wrapper]">
130
+ <div :class="[ui.onFailed.failedImgWrapper]">
131
+ <Icon
132
+ :name="
133
+ isImage(selectedFile)
134
+ ? ui.onFailed.failedImgIcon || ui.default.placeholderImgIcon
135
+ : ui.onFailed.failedFileIcon || ui.default.filePreviewIcon
136
+ "
137
+ :class="[ui.onFailed.failedIconClass]"
138
+ />
139
+ </div>
140
+ <div :class="[ui.onFailed.textWrapper]">
141
+ <div class="truncate">
142
+ <h1 class="truncate font-bold">{{ selectedFile?.name }}</h1>
143
+ <p class="text-danger truncate font-light">
144
+ {{ uploadFailedLabel }}
145
+ </p>
146
+ <Button
147
+ variant="ghost"
148
+ :label="retryLabel"
149
+ :leading-icon="ui.action.retryIcon"
150
+ :class="[ui.action.retryBtnClass]"
151
+ size="sm"
152
+ @click="handleRetryUpload"
153
+ />
154
+ </div>
155
+ <div :class="[ui.action.wrapper]">
156
+ <Icon
157
+ title="ลบไฟล์"
158
+ :name="ui.action.deleteIcon"
159
+ :class="[ui.action.deleteIconClass]"
160
+ @click="handleDeleteFile"
161
+ />
162
+ </div>
163
+ </div>
164
+ </div>
165
+ </div>
166
+ </div>
167
+ </FieldWrapper>
168
+ </template>
169
+
170
+ <script lang="ts" setup>
171
+ import { useDropZone } from '@vueuse/core'
172
+ import { type IUploadDropzoneAutoProps } from './types'
173
+ import {
174
+ checkMaxSize,
175
+ checkFileType,
176
+ isImage,
177
+ downloadFileFromURL,
178
+ useFileAllocate,
179
+ useFileProgress,
180
+ isImageFromPath,
181
+ useFileSize,
182
+ isPDFFromPath,
183
+ } from '#core/helpers/componentHelper'
184
+ import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
185
+ import { useFieldHOC } from '#core/composables/useForm'
186
+ import {
187
+ type IUploadRequest,
188
+ ref,
189
+ computed,
190
+ toRef,
191
+ useUI,
192
+ useUiConfig,
193
+ useUploadLoader,
194
+ useWatchTrue,
195
+ StringHelper,
196
+ _get,
197
+ } from '#imports'
198
+ import { uploadFileDropzone } from '#core/ui.config'
199
+ import i18next from 'i18next'
200
+ import type { IFileValue } from '#core/components/Form/types'
201
+
202
+ const config = useUiConfig<typeof uploadFileDropzone>(uploadFileDropzone, 'uploadFileDropzone')
203
+
204
+ const props = withDefaults(defineProps<IUploadDropzoneAutoProps>(), {
205
+ bodyKey: 'file',
206
+ responseURL: 'url',
207
+ responsePath: 'path',
208
+ selectFileLabel: 'คลิกเพื่อเลือกไฟล์',
209
+ selectFileSubLabel: 'หรือ ลากและวางที่นี่',
210
+ uploadingLabel: 'กำลังอัพโหลด...',
211
+ uploadFailedLabel: 'อัพโหลดล้มเหลว, กรุณาลองอีกครั้ง',
212
+ })
213
+
214
+ const emits = defineEmits(['change', 'success', 'delete'])
215
+
216
+ const { wrapperProps, handleChange: onChange, setErrors, value } = useFieldHOC<IFileValue>(props)
217
+
218
+ const request: IUploadRequest = {
219
+ pathURL: props.uploadPathURL,
220
+ requestOptions: props.requestOptions,
221
+ }
222
+
223
+ const upload = useUploadLoader(request)
224
+
225
+ const { ui } = useUI('uploadFileDropzone', toRef(props, 'ui'), config)
226
+
227
+ const fileInputRef = ref<HTMLInputElement>()
228
+ const dropzoneRef = ref<HTMLDivElement>()
229
+ const selectedFile = ref<File | undefined>()
230
+ const isPreviewOpen = ref<boolean>(false)
231
+
232
+ const { onUploadProgress, onDownloadProgress, percent } = useFileProgress()
233
+ const fileAllocate = useFileAllocate(selectedFile, props)
234
+ const fileAllocateFromPath = computed(() => useFileSize(value.value?.size))
235
+
236
+ const onDrop = (files: File[] | null) => {
237
+ if (props.isDisabled || files?.length === 0 || !files) return
238
+
239
+ const file = files[0]
240
+ const result = handleCheckFileCondition(file)
241
+
242
+ if (result && file) {
243
+ selectedFile.value = file
244
+ const formData = new FormData()
245
+
246
+ emits('change', file)
247
+
248
+ formData.append(props.bodyKey, file)
249
+ upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
250
+ }
251
+ }
252
+
253
+ const { isOverDropZone } = useDropZone(dropzoneRef as unknown as HTMLElement, {
254
+ onDrop,
255
+ })
256
+
257
+ const handleChange = (e: Event) => {
258
+ if (props.isDisabled || props.isReadonly) return
259
+
260
+ const file = (e.target as HTMLInputElement).files?.[0]
261
+ const result = handleCheckFileCondition(file)
262
+
263
+ if (result && file) {
264
+ selectedFile.value = file
265
+ const formData = new FormData()
266
+
267
+ emits('change', file)
268
+
269
+ formData.append(props.bodyKey, file)
270
+ upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
271
+ }
272
+ }
273
+
274
+ const handleOpenFile = () => {
275
+ fileInputRef.value?.click()
276
+ }
277
+
278
+ const handleDeleteFile = () => {
279
+ fileInputRef.value?.value && (fileInputRef.value.value = '')
280
+ selectedFile.value = undefined
281
+ onChange(undefined)
282
+ emits('delete')
283
+ }
284
+
285
+ const handleCheckFileCondition = (file: File | undefined): boolean => {
286
+ if (!file) return false
287
+
288
+ const fileType = checkFileType(file, fileAllocate.acceptFile.value ?? '')
289
+
290
+ if (!fileType) {
291
+ setErrors(i18next.t('custom:invalid_file_type'))
292
+
293
+ return false
294
+ }
295
+
296
+ const maxSize = checkMaxSize(file, fileAllocate.acceptFileSizeKb.value)
297
+
298
+ if (!maxSize) {
299
+ if (fileAllocate.isAcceptFileUseMb.value) {
300
+ setErrors(
301
+ i18next.t('custom:invalid_file_size_mb', { size: fileAllocate.acceptFileSizeMb.value })
302
+ )
303
+ } else {
304
+ setErrors(
305
+ i18next.t('custom:invalid_file_size_kb', { size: fileAllocate.acceptFileSizeKb.value })
306
+ )
307
+ }
308
+
309
+ return false
310
+ }
311
+
312
+ setErrors('')
313
+
314
+ return true
315
+ }
316
+
317
+ const handleDownloadFile = () => {
318
+ downloadFileFromURL(value.value?.url, value.value?.name)
319
+ }
320
+
321
+ const handleRetryUpload = () => {
322
+ if (selectedFile.value) {
323
+ const formData = new FormData()
324
+
325
+ formData.append(props.bodyKey, selectedFile.value)
326
+ upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
327
+ }
328
+ }
329
+
330
+ useWatchTrue(
331
+ () => upload.status.value.isSuccess,
332
+ () => {
333
+ value.value = {
334
+ url: _get(upload.data.value, props.responseURL),
335
+ path: _get(upload.data.value, props.responsePath),
336
+ name: upload.data.value.file_name,
337
+ size: upload.data.value.size,
338
+ }
339
+
340
+ emits('success', value.value)
341
+ }
342
+ )
343
+
344
+ useWatchTrue(
345
+ () => upload.status.value.isError,
346
+ () => {
347
+ setErrors(StringHelper.getError(upload.status.value.errorData))
348
+ }
349
+ )
350
+ </script>