/
VFileUpload.tsx
243 lines (210 loc) Β· 6.56 KB
/
VFileUpload.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Styles
import './VFileUpload.sass'
// Components
import { VFileUploadItem } from './VFileUploadItem'
import { VBtn } from '@/components/VBtn/VBtn'
import { VDefaultsProvider } from '@/components/VDefaultsProvider/VDefaultsProvider'
import { makeVDividerProps, VDivider } from '@/components/VDivider/VDivider'
import { VIcon } from '@/components/VIcon/VIcon'
import { VOverlay } from '@/components/VOverlay/VOverlay'
import { makeVSheetProps, VSheet } from '@/components/VSheet/VSheet'
// Composables
import { makeDelayProps } from '@/composables/delay'
import { makeDensityProps, useDensity } from '@/composables/density'
import { IconValue } from '@/composables/icons'
import { useLocale } from '@/composables/locale'
import { useProxiedModel } from '@/composables/proxiedModel'
// Utilities
import { computed, onMounted, onUnmounted, ref, shallowRef } from 'vue'
import { genericComponent, humanReadableFileSize, only, propsFactory, useRender, wrapInArray } from '@/util'
// Types
import type { PropType } from 'vue'
export type VFileUploadSlots = {
default: never
icon: never
title: never
divider: never
}
export const makeVFileUploadProps = propsFactory({
browseText: {
type: String,
default: '$vuetify.fileUpload.browse',
},
dividerText: {
type: String,
default: '$vuetify.fileUpload.divider',
},
title: {
type: String,
default: '$vuetify.fileUpload.title',
},
subtitle: String,
icon: {
type: IconValue,
default: '$upload',
},
modelValue: {
type: [Array, Object] as PropType<File[] | File>,
default: null,
validator: (val: any) => {
return wrapInArray(val).every(v => v != null && typeof v === 'object')
},
},
multiple: Boolean,
showSize: Boolean,
...makeDelayProps(),
...makeDensityProps(),
...only(makeVDividerProps({
length: 150,
}), ['length', 'thickness', 'opacity']),
...makeVSheetProps(),
}, 'VFileUpload')
export const VFileUpload = genericComponent<VFileUploadSlots>()({
name: 'VFileUpload',
props: makeVFileUploadProps(),
emits: {
'update:modelValue': (files: File[]) => true,
},
setup (props, { slots }) {
const { t } = useLocale()
const { densityClasses } = useDensity(props)
const model = useProxiedModel(
props,
'modelValue',
props.modelValue,
val => wrapInArray(val),
val => (props.multiple || Array.isArray(props.modelValue)) ? val : val[0],
)
const dragOver = shallowRef(false)
const vSheetRef = ref<InstanceType<typeof VSheet> | null>(null)
const base = computed(() => typeof props.showSize !== 'boolean' ? props.showSize : undefined)
onMounted(() => {
vSheetRef.value?.$el.addEventListener('dragover', onDragOver)
vSheetRef.value?.$el.addEventListener('drop', onDrop)
})
onUnmounted(() => {
vSheetRef.value?.$el.removeEventListener('dragover', onDragOver)
vSheetRef.value?.$el.removeEventListener('drop', onDrop)
})
function onDragOver (e: DragEvent) {
e.preventDefault()
e.stopImmediatePropagation()
dragOver.value = true
}
function onDragLeave (e: DragEvent) {
e.preventDefault()
dragOver.value = false
}
function onDrop (e: DragEvent) {
e.preventDefault()
e.stopImmediatePropagation()
dragOver.value = false
const files = Array.from(e.dataTransfer?.files ?? [])
if (!files.length) return
if (!props.multiple) {
model.value = [files[0]]
return
}
for (const file of files) {
if (!model.value.some(f => f.name === file.name)) {
model.value = [
...model.value,
file,
]
}
}
}
function onClickRemove (index: number) {
model.value = model.value.filter((_, i) => i !== index)
}
useRender(() => {
const hasTitle = !!(slots.title || props.title)
const hasIcon = !!(slots.icon || props.icon)
const cardProps = VSheet.filterProps(props)
const dividerProps = VDivider.filterProps(props)
return (
<>
<VSheet
ref={ vSheetRef }
{ ...cardProps }
class={[
'v-file-upload',
{
'v-file-upload--dragging': dragOver.value,
},
densityClasses.value,
]}
onDragleave={ onDragLeave }
onDragover={ onDragOver }
onDrop={ onDrop }
>
{ hasIcon && (
<div key="icon" class="v-file-upload-icon">
{ !slots.icon ? (
<VIcon
key="icon-icon"
icon={ props.icon }
/>
) : (
<VDefaultsProvider
key="icon-defaults"
defaults={{
VIcon: {
icon: props.icon,
},
}}
>
{ slots.icon() }
</VDefaultsProvider>
)}
</div>
)}
{ hasTitle && (
<div key="title" class="v-file-upload-title">
{ slots.title?.() ?? t(props.title) }
</div>
)}
{ props.density === 'default' && (
<>
<div key="upload-divider" class="v-file-upload-divider">
{ slots.divider?.() ?? (
<VDivider { ...dividerProps }>
{ t(props.dividerText) }
</VDivider>
)}
</div>
<VBtn
text={ t(props.browseText) }
variant="tonal"
size="large"
/>
{ props.subtitle && (
<div class="v-file-upload-subtitle">
{ props.subtitle }
</div>
)}
</>
)}
<VOverlay
model-value={ dragOver.value }
contained
/>
</VSheet>
{ model.value.length > 0 && (
<div class="v-file-upload-items">
{ model.value.map((file, i) => (
<VFileUploadItem
key={ i }
title={ file.name }
subtitle={ props.showSize ? humanReadableFileSize(file.size, base.value) : undefined }
onClick:remove={ () => onClickRemove(i) }
/>
))}
</div>
)}
</>
)
})
},
})
export type VFileUpload = InstanceType<typeof VFileUpload>