Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When form validation trigger on the add or update product, the cropped image disappear. #53

Open
Duckinm opened this issue Jul 20, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@Duckinm
Copy link
Contributor

Duckinm commented Jul 20, 2023

croppedImage doesn't generate path and preview so I do rewrite some code with better type guard using satisfies

const onCrop = React.useCallback(() => {
    if (!files || !cropperRef.current) return
    setCropData(cropperRef.current?.cropper.getCroppedCanvas().toDataURL())

    cropperRef.current?.cropper.getCroppedCanvas().toBlob((blob) => {
      if (!blob) return
      const croppedImage = new File([blob], file.name, {
        type: file.type,
        lastModified: Date.now(),
      })
      files.splice(i, 1, croppedImage as FileWithPreview)
      setValue(name, files as PathValue<TFieldValues, Path<TFieldValues>>)
    })
  }, [file.name, file.type, files, i, name, setValue])
const onCrop = React.useCallback(() => {
    if (!files || !cropperRef.current) return

    const croppedCanvas = cropperRef.current?.cropper.getCroppedCanvas()
    setCropData(croppedCanvas.toDataURL())

    croppedCanvas.toBlob((blob) => {
      if (!blob) {
        console.error("Blob creation failed")
        return
      }
      const croppedImage = new File([blob], file.name, {
        type: file.type,
        lastModified: Date.now(),
      })

      const croppedFile = Object.assign(croppedImage, {
        preview: URL.createObjectURL(croppedImage),
        path: file.name,
      }) satisfies FileWithPreview

      const newFiles = [...files]
      newFiles.splice(i, 1, croppedFile)
      setValue(name, newFiles as PathValue<TFieldValues, Path<TFieldValues>>)
    })
  }, [file.name, file.type, files, i, name, setValue])
@sadmann7 sadmann7 added good first issue Good for newcomers enhancement New feature or request and removed good first issue Good for newcomers labels Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants