@codeleap/web 3.12.18 → 3.12.20
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.
- package/package.json +1 -1
- package/src/components/FileInput.tsx +20 -8
package/package.json
CHANGED
|
@@ -4,23 +4,30 @@ import React, {
|
|
|
4
4
|
useImperativeHandle,
|
|
5
5
|
useRef,
|
|
6
6
|
} from 'react'
|
|
7
|
-
import { WebInputFile } from '@codeleap/common'
|
|
7
|
+
import { WebInputFile, useCallback } from '@codeleap/common'
|
|
8
8
|
import { HTMLProps } from '../types'
|
|
9
9
|
|
|
10
10
|
export type FileInputRef = {
|
|
11
11
|
openFilePicker: () => Promise<WebInputFile[]>
|
|
12
|
+
clear: () => void
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
export type FileInputProps = Omit<HTMLProps<'input'>, 'type'|'ref'> & {
|
|
15
|
+
export type FileInputProps = Omit<HTMLProps<'input'>, 'type' | 'ref'> & {
|
|
15
16
|
onFileSelect?: (files: WebInputFile[]) => void
|
|
17
|
+
autoClear?: boolean
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
export const _FileInput = (inputProps: FileInputProps, ref:React.Ref<FileInputRef>) => {
|
|
20
|
+
export const _FileInput = (inputProps: FileInputProps, ref: React.Ref<FileInputRef>) => {
|
|
19
21
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
20
22
|
|
|
21
|
-
const { onFileSelect, ...props } = inputProps
|
|
23
|
+
const { onFileSelect, autoClear = true, ...props } = inputProps
|
|
22
24
|
|
|
23
|
-
const resolveWithFile = useRef<(file:WebInputFile[]) => any>()
|
|
25
|
+
const resolveWithFile = useRef<(file: WebInputFile[]) => any>()
|
|
26
|
+
|
|
27
|
+
const clearInput = useCallback(() => {
|
|
28
|
+
if (!inputRef.current) return
|
|
29
|
+
inputRef.current.value = null
|
|
30
|
+
}, [])
|
|
24
31
|
|
|
25
32
|
useImperativeHandle(ref, () => ({
|
|
26
33
|
openFilePicker: () => {
|
|
@@ -30,9 +37,10 @@ export const _FileInput = (inputProps: FileInputProps, ref:React.Ref<FileInputRe
|
|
|
30
37
|
resolveWithFile.current = resolve
|
|
31
38
|
})
|
|
32
39
|
},
|
|
40
|
+
clear: clearInput,
|
|
33
41
|
}))
|
|
34
42
|
|
|
35
|
-
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
|
|
43
|
+
async function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
|
|
36
44
|
if (!e.target.files.length) return
|
|
37
45
|
inputProps.onChange && inputProps.onChange(e)
|
|
38
46
|
const fileArray = Array.from(e.target?.files || []) as File[]
|
|
@@ -45,9 +53,13 @@ export const _FileInput = (inputProps: FileInputProps, ref:React.Ref<FileInputRe
|
|
|
45
53
|
onFileSelect && onFileSelect(files)
|
|
46
54
|
|
|
47
55
|
if (resolveWithFile.current) {
|
|
48
|
-
resolveWithFile.current(files)
|
|
56
|
+
await resolveWithFile.current(files)
|
|
49
57
|
resolveWithFile.current = undefined
|
|
50
58
|
}
|
|
59
|
+
|
|
60
|
+
if (autoClear) {
|
|
61
|
+
clearInput()
|
|
62
|
+
}
|
|
51
63
|
}
|
|
52
64
|
|
|
53
65
|
return (
|
|
@@ -66,7 +78,7 @@ export const FileInput = React.forwardRef<FileInputRef, FileInputProps>(_FileInp
|
|
|
66
78
|
)
|
|
67
79
|
|
|
68
80
|
export const useFileInput = () => {
|
|
69
|
-
const inputRef = useRef<FileInputRef|null>(null)
|
|
81
|
+
const inputRef = useRef<FileInputRef | null>(null)
|
|
70
82
|
|
|
71
83
|
const openFilePicker = () => {
|
|
72
84
|
return inputRef.current?.openFilePicker()
|