69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
import * as React from "react"
|
||
import { UploadCloudIcon } from "lucide-react"
|
||
|
||
import { cn } from "@/lib/utils"
|
||
|
||
/** Зона загрузки: перетащить файлы или выбрать в диалоге. */
|
||
export function FileDrop({
|
||
onFiles,
|
||
multiple = true,
|
||
accept,
|
||
hint = "PDF, изображения, документы — до 20 МБ",
|
||
className,
|
||
}: {
|
||
onFiles: (files: File[]) => void
|
||
multiple?: boolean
|
||
accept?: string
|
||
hint?: string
|
||
className?: string
|
||
}) {
|
||
const inputRef = React.useRef<HTMLInputElement>(null)
|
||
const [over, setOver] = React.useState(false)
|
||
|
||
return (
|
||
<div
|
||
role="button"
|
||
tabIndex={0}
|
||
onClick={() => inputRef.current?.click()}
|
||
onKeyDown={(e) => (e.key === "Enter" || e.key === " ") && inputRef.current?.click()}
|
||
onDragOver={(e) => {
|
||
e.preventDefault()
|
||
setOver(true)
|
||
}}
|
||
onDragLeave={() => setOver(false)}
|
||
onDrop={(e) => {
|
||
e.preventDefault()
|
||
setOver(false)
|
||
onFiles([...e.dataTransfer.files])
|
||
}}
|
||
className={cn(
|
||
"flex cursor-pointer flex-col items-center gap-1.5 rounded-xl border-2 border-dashed px-6 py-6 text-center transition outline-none focus-visible:ring-2 focus-visible:ring-ring/50",
|
||
over ? "border-primary bg-accent/50" : "border-border hover:border-primary/40 hover:bg-muted/40",
|
||
className,
|
||
)}
|
||
>
|
||
<UploadCloudIcon className={cn("size-6", over ? "text-primary" : "text-muted-foreground")} />
|
||
<div className="text-sm">
|
||
<span className="font-medium text-primary">Выберите файлы</span> или перетащите сюда
|
||
</div>
|
||
<div className="text-xs text-muted-foreground">{hint}</div>
|
||
<input
|
||
ref={inputRef}
|
||
type="file"
|
||
hidden
|
||
multiple={multiple}
|
||
accept={accept}
|
||
onChange={(e) => {
|
||
onFiles([...(e.target.files ?? [])])
|
||
e.target.value = ""
|
||
}}
|
||
/>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** File из браузера → элемент для FileGallery (превью через object URL). */
|
||
export function fileToItem(file: File) {
|
||
return { name: file.name, size: file.size, type: file.type, src: URL.createObjectURL(file) }
|
||
}
|