Dropzone: is it supposed to be used within <from> only or direct usage is also supported? #2759
-
|
Basically I need to open image cropper on file selection, and no need to send file via form. I mean, I see there way to clear context (acceptedFiles) after file was accepted? Should I clear it at all? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Yes, File Upload works standalone (no form required). Consider using When files are selected,
Here's what that looks like: function ImageUploadWithCropper() {
const [openCropper, setOpenCropper] = useState(false)
const [currentFile, setCurrentFile] = useState<File | null>(null)
const [state, send] = useMachine(
fileUpload.machine({
id: useId(),
transformFiles: async (files) => {
const croppedFiles = await Promise.all(
files.map(async (file) => {
const blob = await new Promise<Blob>((resolve) => {
setCurrentFile(file)
setOpenCropper(true)
document.addEventListener("crop:complete", (e) => resolve(e.detail), { once: true })
})
return new File([blob], file.name, { type: blob.type })
}),
)
return croppedFiles
},
}),
)
const api = fileUpload.connect(state, send, normalizeProps)
return (
<div>
{/* Your file upload UI */}
<div {...api.getDropzoneProps()}>...</div>
{/* Your cropper modal */}
{openCropper && (
<CropperModal
file={currentFile}
onComplete={(blob) => {
document.dispatchEvent(new CustomEvent("crop:complete", { detail: blob }))
setOpenCropper(false)
}}
/>
)}
</div>
)
}Bonus: Show Loading StateWhile the user is cropping, the component exposes <div {...api.getDropzoneProps()}>{api.transforming ? "Processing your images..." : "Drop images here"}</div>Clearing FilesAfter you've done whatever you need with the cropped files, clear them with: api.clearFiles()No Form RequiredTo directly answer your question: No, File Upload doesn't need to be inside a Hope this helps! Let me know if you need any clarification. |
Beta Was this translation helpful? Give feedback.
Yes, File Upload works standalone (no form required). Consider using
transformFilesin your code for opening an imagecropper before accepting files.
When files are selected,
transformFilesruns before they're added toacceptedFiles. This means you can:Here's what that looks like: