mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 10:02:27 -04:00
41 lines
1015 B
TypeScript
41 lines
1015 B
TypeScript
import {useRef} from 'react'
|
|
|
|
/** button that opens file upload window */
|
|
export function FileUploadButton(props: {
|
|
onFiles: (files: File[]) => void
|
|
className?: string
|
|
children?: React.ReactNode
|
|
disabled?: boolean
|
|
}) {
|
|
const {onFiles, className, children, disabled} = props
|
|
const ref = useRef<HTMLInputElement>(null)
|
|
return (
|
|
<>
|
|
<button
|
|
type={'button'}
|
|
className={className}
|
|
disabled={disabled}
|
|
onClick={() => ref.current?.click()}
|
|
>
|
|
{children}
|
|
</button>
|
|
<input
|
|
ref={ref}
|
|
type="file"
|
|
accept=".gif,.jpg,.jpeg,.png,.webp, image/*"
|
|
multiple
|
|
className="hidden"
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const files = e.target.files
|
|
if (files) {
|
|
onFiles(Array.from(files))
|
|
if (ref.current) {
|
|
ref.current.value = '' // clear file input in case user reuploads
|
|
}
|
|
}
|
|
}}
|
|
/>
|
|
</>
|
|
)
|
|
}
|