GitHub

@@ -1,4 +1,5 @@

11

import { ref, computed, unref, onMounted, watch, reactive } from 'vue'

2+

import type { VisuallyHidden } from 'reka-ui'

23

import { useFileDialog, useDropZone } from '@vueuse/core'

34

import type { MaybeRef } from '@vueuse/core'

45

@@ -45,12 +46,12 @@ export function useFileUpload(options: UseFileUploadOptions) {

4546

dropzone = true,

4647

onUpdate

4748

} = options

48-

const inputRef = ref<HTMLInputElement>()

49+

const inputRef = ref<InstanceType<typeof VisuallyHidden>>()

4950

const dropzoneRef = ref<HTMLDivElement>()

50515152

const dataTypes = computed(() => parseAcceptToDataTypes(unref(accept)))

525353-

const onDrop = (files: FileList | File[] | null) => {

54+

const onDrop = (files: FileList | File[] | null, fromDropZone = false) => {

5455

if (!files || files.length === 0) {

5556

return

5657

}

@@ -60,6 +61,18 @@ export function useFileUpload(options: UseFileUploadOptions) {

6061

if (files.length > 1 && !multiple) {

6162

files = [files[0]!]

6263

}

64+65+

// Sync dropped files to the input element for proper native validation

66+

if (fromDropZone && inputRef.value?.$el) {

67+

try {

68+

const dt = new DataTransfer()

69+

files.forEach(file => dt.items.add(file))

70+

inputRef.value.$el.files = dt.files

71+

} catch (e) {

72+

console.warn('Could not sync files to input element:', e)

73+

}

74+

}

75+6376

onUpdate(files)

6477

}

6578

@@ -75,7 +88,7 @@ export function useFileUpload(options: UseFileUploadOptions) {

75887689

onMounted(() => {

7790

const { isOverDropZone } = dropzone

78-

? useDropZone(dropzoneRef, { dataTypes: dataTypes.value, onDrop })

91+

? useDropZone(dropzoneRef, { dataTypes: dataTypes.value, onDrop: files => onDrop(files, true) })

7992

: { isOverDropZone: ref(false) }

80938194

watch(isOverDropZone, (value) => {

@@ -85,13 +98,13 @@ export function useFileUpload(options: UseFileUploadOptions) {

8598

const { onChange, open } = useFileDialog({

8699

accept: unref(accept),

87100

multiple,

88-

input: unref(inputRef),

101+

input: unref(inputRef)?.$el,

89102

reset

90103

})

9110492105

fileDialog.open = open

9310694-

onChange(fileList => onDrop(fileList))

107+

onChange(fileList => onDrop(fileList, false))

95108

})

9610997110

return {

Read the original on github.com ↗