diff --git a/src/image/Image.tsx b/src/image/Image.tsx index 55cd98d2f..8da54e984 100644 --- a/src/image/Image.tsx +++ b/src/image/Image.tsx @@ -13,22 +13,22 @@ import { StyledProps } from '../common'; export type ImageProps = TdImageProps & StyledProps; -const Image = (props: ImageProps) => { +const InternalImage: React.ForwardRefRenderFunction = (props, ref) => { const { className, src, style, alt, - fit, - position, - shape, + fit = imageDefaultProps.fit, + position = imageDefaultProps.position, + shape = imageDefaultProps.shape, placeholder, loading, error, - overlayTrigger, + overlayTrigger = imageDefaultProps.overlayTrigger, + lazy = imageDefaultProps.lazy, + gallery = imageDefaultProps.gallery, overlayContent, - lazy, - gallery, srcset, onLoad, onError, @@ -43,6 +43,8 @@ const Image = (props: ImageProps) => { ImageIcon: TdImageIcon, }); + React.useImperativeHandle(ref, () => imageRef.current); + // replace image url const imageSrc = useMemo( () => (isFunction(local.replaceImageSrc) ? local.replaceImageSrc(props) : src), @@ -61,7 +63,9 @@ const Image = (props: ImageProps) => { }; useEffect(() => { - if (!lazy || !imageRef?.current) return; + if (!lazy || !imageRef?.current) { + return; + } // https://stackoverflow.com/questions/67069827/cleanup-ref-issues-in-react let observerRefValue = null; @@ -86,7 +90,9 @@ const Image = (props: ImageProps) => { setShouldShowOverlay(overlay); }; const renderOverlay = () => { - if (!overlayContent) return null; + if (!overlayContent) { + return null; + } return (
{ }; const renderGalleryShadow = () => { - if (!gallery) return null; + if (!gallery) { + return null; + } return
; }; const renderImage = (url: string) => ( - {alt} - ); + {alt} + ); const renderImageSrcset = () => ( - - {Object.entries(props.srcset).map(([type, url]) => ( - - ))} - {props.src && renderImage(props.src)} - - ); + + {Object.entries(props.srcset).map(([type, url]) => ( + + ))} + {props.src && renderImage(props.src)} + + ); return (
{ )}
)} - {renderOverlay()}
); }; +const Image = React.forwardRef(InternalImage); + Image.displayName = 'Image'; -Image.defaultProps = imageDefaultProps; export default Image;