@finema/core 1.4.217 → 1.4.218

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 (67) 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/Empty.vue +18 -18
  15. package/dist/runtime/components/FlexDeck/Base.vue +159 -159
  16. package/dist/runtime/components/FlexDeck/index.vue +68 -68
  17. package/dist/runtime/components/Form/FieldWrapper.vue +23 -23
  18. package/dist/runtime/components/Form/Fields.vue +230 -230
  19. package/dist/runtime/components/Form/InputCheckbox/index.vue +28 -28
  20. package/dist/runtime/components/Form/InputDateTime/index.vue +68 -68
  21. package/dist/runtime/components/Form/InputDateTimeRange/index.vue +91 -91
  22. package/dist/runtime/components/Form/InputNumber/index.vue +27 -27
  23. package/dist/runtime/components/Form/InputRadio/index.vue +27 -27
  24. package/dist/runtime/components/Form/InputSelect/index.vue +55 -55
  25. package/dist/runtime/components/Form/InputSelectMultiple/index.vue +54 -54
  26. package/dist/runtime/components/Form/InputStatic/index.vue +16 -16
  27. package/dist/runtime/components/Form/InputTags/index.vue +141 -141
  28. package/dist/runtime/components/Form/InputText/index.vue +68 -68
  29. package/dist/runtime/components/Form/InputTextarea/index.vue +25 -25
  30. package/dist/runtime/components/Form/InputToggle/index.vue +27 -27
  31. package/dist/runtime/components/Form/InputUploadDropzone/index.vue +206 -206
  32. package/dist/runtime/components/Form/InputUploadDropzoneAuto/index.vue +362 -350
  33. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/ItemUpload.vue +241 -241
  34. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/ItemView.vue +110 -102
  35. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/index.vue +171 -171
  36. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/ItemUpload.vue +161 -161
  37. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/ItemView.vue +64 -64
  38. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/index.vue +178 -178
  39. package/dist/runtime/components/Form/InputUploadFileClassic/index.vue +95 -95
  40. package/dist/runtime/components/Form/InputUploadFileClassicAuto/index.vue +151 -151
  41. package/dist/runtime/components/Form/InputUploadImageAuto/index.vue +219 -219
  42. package/dist/runtime/components/Form/InputWYSIWYG/UploadImageForm.vue +55 -55
  43. package/dist/runtime/components/Form/InputWYSIWYG/index.vue +228 -228
  44. package/dist/runtime/components/Form/index.vue +6 -6
  45. package/dist/runtime/components/Icon.vue +23 -23
  46. package/dist/runtime/components/Image.vue +36 -36
  47. package/dist/runtime/components/Loader.vue +27 -27
  48. package/dist/runtime/components/Log.vue +22 -22
  49. package/dist/runtime/components/LogItem.vue +41 -41
  50. package/dist/runtime/components/Modal/index.vue +146 -146
  51. package/dist/runtime/components/OTPInput.vue +127 -127
  52. package/dist/runtime/components/QRCode.vue +22 -22
  53. package/dist/runtime/components/SimplePagination.vue +96 -96
  54. package/dist/runtime/components/Slideover/index.vue +110 -110
  55. package/dist/runtime/components/Table/Base.vue +161 -161
  56. package/dist/runtime/components/Table/ColumnDate.vue +16 -16
  57. package/dist/runtime/components/Table/ColumnDateTime.vue +18 -18
  58. package/dist/runtime/components/Table/ColumnImage.vue +15 -15
  59. package/dist/runtime/components/Table/ColumnNumber.vue +14 -14
  60. package/dist/runtime/components/Table/ColumnText.vue +29 -29
  61. package/dist/runtime/components/Table/Simple.vue +70 -70
  62. package/dist/runtime/components/Table/index.vue +65 -65
  63. package/dist/runtime/components/Tabs/index.vue +64 -64
  64. package/dist/runtime/components/TeleportSafe.vue +40 -40
  65. package/dist/runtime/helpers/componentHelper.d.ts +1 -0
  66. package/dist/runtime/helpers/componentHelper.mjs +5 -0
  67. package/package.json +100 -100
@@ -1,350 +1,362 @@
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) || isVideoFromPath(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
123
+ v-if="isImageFromPath(value.path)"
124
+ :src="value.url"
125
+ alt="img-preview"
126
+ class="mx-auto max-w-full rounded-lg"
127
+ />
128
+ <video
129
+ v-else-if="isVideoFromPath(value.path)"
130
+ :src="value.url"
131
+ controls
132
+ class="mx-auto max-w-full"
133
+ />
134
+ </Modal>
135
+ </div>
136
+ </div>
137
+ </div>
138
+
139
+ <!-- Failed State -->
140
+ <div v-if="selectedFile && upload.status.value.isError" :class="[ui.onFailed.wrapper]">
141
+ <div :class="[ui.onFailed.failedImgWrapper]">
142
+ <Icon
143
+ :name="
144
+ isImage(selectedFile)
145
+ ? ui.onFailed.failedImgIcon || ui.default.placeholderImgIcon
146
+ : ui.onFailed.failedFileIcon || ui.default.filePreviewIcon
147
+ "
148
+ :class="[ui.onFailed.failedIconClass]"
149
+ />
150
+ </div>
151
+ <div :class="[ui.onFailed.textWrapper]">
152
+ <div class="truncate">
153
+ <h1 class="truncate font-bold">{{ selectedFile?.name }}</h1>
154
+ <p class="text-danger truncate font-light">
155
+ {{ uploadFailedLabel }}
156
+ </p>
157
+ <Button
158
+ variant="ghost"
159
+ :label="retryLabel"
160
+ :leading-icon="ui.action.retryIcon"
161
+ :class="[ui.action.retryBtnClass]"
162
+ size="sm"
163
+ @click="handleRetryUpload"
164
+ />
165
+ </div>
166
+ <div :class="[ui.action.wrapper]">
167
+ <Icon
168
+ title="ลบไฟล์"
169
+ :name="ui.action.deleteIcon"
170
+ :class="[ui.action.deleteIconClass]"
171
+ @click="handleDeleteFile"
172
+ />
173
+ </div>
174
+ </div>
175
+ </div>
176
+ </div>
177
+ </div>
178
+ </FieldWrapper>
179
+ </template>
180
+
181
+ <script lang="ts" setup>
182
+ import { useDropZone } from '@vueuse/core'
183
+ import { type IUploadDropzoneAutoProps } from './types'
184
+ import {
185
+ checkMaxSize,
186
+ checkFileType,
187
+ isImage,
188
+ downloadFileFromURL,
189
+ useFileAllocate,
190
+ useFileProgress,
191
+ isImageFromPath,
192
+ useFileSize,
193
+ isPDFFromPath,
194
+ isVideoFromPath,
195
+ } from '#core/helpers/componentHelper'
196
+ import FieldWrapper from '#core/components/Form/FieldWrapper.vue'
197
+ import { useFieldHOC } from '#core/composables/useForm'
198
+ import {
199
+ type IUploadRequest,
200
+ ref,
201
+ computed,
202
+ toRef,
203
+ useUI,
204
+ useUiConfig,
205
+ useUploadLoader,
206
+ useWatchTrue,
207
+ StringHelper,
208
+ _get,
209
+ } from '#imports'
210
+ import { uploadFileDropzone } from '#core/ui.config'
211
+ import i18next from 'i18next'
212
+ import type { IFileValue } from '#core/components/Form/types'
213
+
214
+ const config = useUiConfig<typeof uploadFileDropzone>(uploadFileDropzone, 'uploadFileDropzone')
215
+
216
+ const props = withDefaults(defineProps<IUploadDropzoneAutoProps>(), {
217
+ bodyKey: 'file',
218
+ responseURL: 'url',
219
+ responsePath: 'path',
220
+ selectFileLabel: 'คลิกเพื่อเลือกไฟล์',
221
+ selectFileSubLabel: 'หรือ ลากและวางที่นี่',
222
+ uploadingLabel: 'กำลังอัพโหลด...',
223
+ uploadFailedLabel: 'อัพโหลดล้มเหลว, กรุณาลองอีกครั้ง',
224
+ })
225
+
226
+ const emits = defineEmits(['change', 'success', 'delete'])
227
+
228
+ const { wrapperProps, handleChange: onChange, setErrors, value } = useFieldHOC<IFileValue>(props)
229
+
230
+ const request: IUploadRequest = {
231
+ pathURL: props.uploadPathURL,
232
+ requestOptions: props.requestOptions,
233
+ }
234
+
235
+ const upload = useUploadLoader(request)
236
+
237
+ const { ui } = useUI('uploadFileDropzone', toRef(props, 'ui'), config)
238
+
239
+ const fileInputRef = ref<HTMLInputElement>()
240
+ const dropzoneRef = ref<HTMLDivElement>()
241
+ const selectedFile = ref<File | undefined>()
242
+ const isPreviewOpen = ref<boolean>(false)
243
+
244
+ const { onUploadProgress, onDownloadProgress, percent } = useFileProgress()
245
+ const fileAllocate = useFileAllocate(selectedFile, props)
246
+ const fileAllocateFromPath = computed(() => useFileSize(value.value?.size))
247
+
248
+ const onDrop = (files: File[] | null) => {
249
+ if (props.isDisabled || files?.length === 0 || !files) return
250
+
251
+ const file = files[0]
252
+ const result = handleCheckFileCondition(file)
253
+
254
+ if (result && file) {
255
+ selectedFile.value = file
256
+ const formData = new FormData()
257
+
258
+ emits('change', file)
259
+
260
+ formData.append(props.bodyKey, file)
261
+ upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
262
+ }
263
+ }
264
+
265
+ const { isOverDropZone } = useDropZone(dropzoneRef as unknown as HTMLElement, {
266
+ onDrop,
267
+ })
268
+
269
+ const handleChange = (e: Event) => {
270
+ if (props.isDisabled || props.isReadonly) return
271
+
272
+ const file = (e.target as HTMLInputElement).files?.[0]
273
+ const result = handleCheckFileCondition(file)
274
+
275
+ if (result && file) {
276
+ selectedFile.value = file
277
+ const formData = new FormData()
278
+
279
+ emits('change', file)
280
+
281
+ formData.append(props.bodyKey, file)
282
+ upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
283
+ }
284
+ }
285
+
286
+ const handleOpenFile = () => {
287
+ fileInputRef.value?.click()
288
+ }
289
+
290
+ const handleDeleteFile = () => {
291
+ fileInputRef.value?.value && (fileInputRef.value.value = '')
292
+ selectedFile.value = undefined
293
+ onChange(undefined)
294
+ emits('delete')
295
+ }
296
+
297
+ const handleCheckFileCondition = (file: File | undefined): boolean => {
298
+ if (!file) return false
299
+
300
+ const fileType = checkFileType(file, fileAllocate.acceptFile.value ?? '')
301
+
302
+ if (!fileType) {
303
+ setErrors(i18next.t('custom:invalid_file_type'))
304
+
305
+ return false
306
+ }
307
+
308
+ const maxSize = checkMaxSize(file, fileAllocate.acceptFileSizeKb.value)
309
+
310
+ if (!maxSize) {
311
+ if (fileAllocate.isAcceptFileUseMb.value) {
312
+ setErrors(
313
+ i18next.t('custom:invalid_file_size_mb', { size: fileAllocate.acceptFileSizeMb.value })
314
+ )
315
+ } else {
316
+ setErrors(
317
+ i18next.t('custom:invalid_file_size_kb', { size: fileAllocate.acceptFileSizeKb.value })
318
+ )
319
+ }
320
+
321
+ return false
322
+ }
323
+
324
+ setErrors('')
325
+
326
+ return true
327
+ }
328
+
329
+ const handleDownloadFile = () => {
330
+ downloadFileFromURL(value.value?.url, value.value?.name)
331
+ }
332
+
333
+ const handleRetryUpload = () => {
334
+ if (selectedFile.value) {
335
+ const formData = new FormData()
336
+
337
+ formData.append(props.bodyKey, selectedFile.value)
338
+ upload.run(formData, { data: { onUploadProgress, onDownloadProgress } })
339
+ }
340
+ }
341
+
342
+ useWatchTrue(
343
+ () => upload.status.value.isSuccess,
344
+ () => {
345
+ value.value = {
346
+ url: _get(upload.data.value, props.responseURL),
347
+ path: _get(upload.data.value, props.responsePath),
348
+ name: upload.data.value.file_name,
349
+ size: upload.data.value.size,
350
+ }
351
+
352
+ emits('success', value.value)
353
+ }
354
+ )
355
+
356
+ useWatchTrue(
357
+ () => upload.status.value.isError,
358
+ () => {
359
+ setErrors(StringHelper.getError(upload.status.value.errorData))
360
+ }
361
+ )
362
+ </script>