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

fix: 🐛 Image support ref props #2198

Merged
merged 1 commit into from
May 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions src/image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import { StyledProps } from '../common';

export type ImageProps = TdImageProps & StyledProps;

const Image = (props: ImageProps) => {
const InternalImage: React.ForwardRefRenderFunction<HTMLDivElement, ImageProps> = (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,
honkinglin marked this conversation as resolved.
Show resolved Hide resolved
overlayContent,
lazy,
gallery,
srcset,
onLoad,
onError,
Expand All @@ -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),
Expand All @@ -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;
Expand All @@ -86,7 +90,9 @@ const Image = (props: ImageProps) => {
setShouldShowOverlay(overlay);
};
const renderOverlay = () => {
if (!overlayContent) return null;
if (!overlayContent) {
return null;
}
return (
<div
className={classNames(
Expand All @@ -107,32 +113,34 @@ const Image = (props: ImageProps) => {
};

const renderGalleryShadow = () => {
if (!gallery) return null;
if (!gallery) {
return null;
}
return <div className={`${classPrefix}-image__gallery-shadow`} />;
};

const renderImage = (url: string) => (
<img
src={url}
onError={handleError}
onLoad={handleLoad}
className={classNames(
`${classPrefix}-image`,
`${classPrefix}-image--fit-${fit}`,
`${classPrefix}-image--position-${position}`,
)}
alt={alt}
/>
);
<img
src={url}
onError={handleError}
onLoad={handleLoad}
className={classNames(
`${classPrefix}-image`,
`${classPrefix}-image--fit-${fit}`,
`${classPrefix}-image--position-${position}`,
)}
alt={alt}
/>
);

const renderImageSrcset = () => (
<picture>
{Object.entries(props.srcset).map(([type, url]) => (
<source key={url} type={type} srcSet={url} />
))}
{props.src && renderImage(props.src)}
</picture>
);
<picture>
{Object.entries(props.srcset).map(([type, url]) => (
<source key={url} type={type} srcSet={url} />
))}
{props.src && renderImage(props.src)}
</picture>
);

return (
<div
Expand Down Expand Up @@ -183,13 +191,13 @@ const Image = (props: ImageProps) => {
)}
</div>
)}

{renderOverlay()}
</div>
);
};

const Image = React.forwardRef<HTMLDivElement, ImageProps>(InternalImage);

Image.displayName = 'Image';
Image.defaultProps = imageDefaultProps;

export default Image;