@finema/core 1.4.100 → 1.4.102

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 +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 +192 -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/InputDateTimeRange/date_range_time_field.types.d.ts +17 -0
  21. package/dist/runtime/components/Form/InputDateTimeRange/date_range_time_field.types.mjs +0 -0
  22. package/dist/runtime/components/Form/InputDateTimeRange/index.vue +83 -0
  23. package/dist/runtime/components/Form/InputNumber/index.vue +27 -27
  24. package/dist/runtime/components/Form/InputRadio/index.vue +27 -27
  25. package/dist/runtime/components/Form/InputSelect/index.vue +36 -36
  26. package/dist/runtime/components/Form/InputStatic/index.vue +16 -16
  27. package/dist/runtime/components/Form/InputText/index.vue +67 -67
  28. package/dist/runtime/components/Form/InputTextarea/index.vue +25 -25
  29. package/dist/runtime/components/Form/InputToggle/index.vue +14 -14
  30. package/dist/runtime/components/Form/InputUploadDropzone/index.vue +206 -206
  31. package/dist/runtime/components/Form/InputUploadDropzoneAuto/index.vue +333 -333
  32. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/Item.vue +260 -260
  33. package/dist/runtime/components/Form/InputUploadDropzoneAutoMultiple/index.vue +140 -140
  34. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/index.vue +148 -148
  35. package/dist/runtime/components/Form/InputUploadDropzoneImageAutoMultiple/item.vue +167 -167
  36. package/dist/runtime/components/Form/InputUploadFileClassic/index.vue +95 -95
  37. package/dist/runtime/components/Form/InputUploadFileClassicAuto/index.vue +143 -143
  38. package/dist/runtime/components/Form/index.vue +6 -6
  39. package/dist/runtime/components/Form/types.d.ts +4 -1
  40. package/dist/runtime/components/Form/types.mjs +2 -0
  41. package/dist/runtime/components/Icon.vue +23 -23
  42. package/dist/runtime/components/Image.vue +36 -36
  43. package/dist/runtime/components/Loader.vue +27 -27
  44. package/dist/runtime/components/Modal/index.vue +146 -146
  45. package/dist/runtime/components/SimplePagination.vue +96 -96
  46. package/dist/runtime/components/Slideover/index.vue +110 -110
  47. package/dist/runtime/components/Table/Base.vue +139 -139
  48. package/dist/runtime/components/Table/ColumnDate.vue +16 -16
  49. package/dist/runtime/components/Table/ColumnDateTime.vue +18 -18
  50. package/dist/runtime/components/Table/ColumnImage.vue +15 -15
  51. package/dist/runtime/components/Table/ColumnNumber.vue +14 -14
  52. package/dist/runtime/components/Table/ColumnText.vue +25 -25
  53. package/dist/runtime/components/Table/Simple.vue +69 -69
  54. package/dist/runtime/components/Table/index.vue +65 -65
  55. package/dist/runtime/components/Tabs/index.vue +64 -64
  56. package/dist/runtime/composables/useForm.d.ts +1 -1
  57. package/dist/runtime/helpers/apiPageHelper.d.ts +1 -1
  58. package/dist/runtime/utils/TimeHelper.d.ts +4 -0
  59. package/dist/runtime/utils/TimeHelper.mjs +16 -0
  60. package/package.json +88 -89
@@ -1,333 +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="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>
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>