-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: migrate class components to functions+hooks (#3701)
Co-authored-by: Cody Olsen <81981+stipsan@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
157 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 19 additions & 41 deletions
60
packages/sanity/src/form/inputs/files/ImageToolInput/imagetool/ImageLoader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,42 @@ | ||
import React from 'react' | ||
/* eslint-disable @typescript-eslint/no-shadow */ | ||
import {useEffect, useState, type ReactElement} from 'react' | ||
|
||
interface ImageLoaderProps { | ||
src: string | ||
children: (props: { | ||
isLoading: boolean | ||
image: HTMLImageElement | null | ||
error: Error | null | ||
}) => React.ReactNode | ||
} | ||
interface ImageLoaderState { | ||
isLoading: boolean | ||
image: HTMLImageElement | null | ||
error: Error | null | ||
}) => ReactElement | null | ||
} | ||
|
||
export class ImageLoader extends React.Component<ImageLoaderProps, ImageLoaderState> { | ||
state: ImageLoaderState = { | ||
isLoading: true, | ||
image: null, | ||
error: null, | ||
} | ||
export function ImageLoader(props: ImageLoaderProps) { | ||
const {src, children} = props | ||
const [isLoading, setIsLoading] = useState(true) | ||
const [image, setImage] = useState<HTMLImageElement | null>(null) | ||
const [error, setError] = useState<Error | null>(null) | ||
|
||
UNSAFE_componentWillMount() { | ||
this.loadImage(this.props.src) | ||
} | ||
useEffect(() => { | ||
setImage(null) | ||
setError(null) | ||
setIsLoading(true) | ||
|
||
loadImage(src: string) { | ||
const image = new Image() | ||
this.setState({ | ||
image: null, | ||
error: null, | ||
}) | ||
|
||
image.onload = () => { | ||
this.setState({ | ||
image: image, | ||
error: null, | ||
isLoading: false, | ||
}) | ||
setImage(image) | ||
setError(null) | ||
setIsLoading(false) | ||
} | ||
|
||
image.onerror = () => { | ||
this.setState({ | ||
error: new Error(`Could not load image from ${JSON.stringify(this.props.src)}`), | ||
isLoading: false, | ||
}) | ||
setError(new Error(`Could not load image from ${JSON.stringify(src)}`)) | ||
setIsLoading(false) | ||
} | ||
|
||
image.referrerPolicy = 'strict-origin-when-cross-origin' | ||
image.src = src | ||
} | ||
|
||
UNSAFE_componentWillReceiveProps(nextProps: ImageLoaderProps) { | ||
if (nextProps.src !== this.props.src) { | ||
this.loadImage(nextProps.src) | ||
} | ||
} | ||
}, [src]) | ||
|
||
render() { | ||
const {error, image, isLoading} = this.state | ||
return this.props.children({image, error, isLoading}) | ||
} | ||
return children({image, error, isLoading}) | ||
} |
Oops, something went wrong.