@finema/core 1.4.98 → 1.4.100

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