Skip to content

Commit

Permalink
fix: simplify renderDefaultPhoto callback params
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed Mar 8, 2023
1 parent 198a557 commit cc56f5a
Show file tree
Hide file tree
Showing 4 changed files with 459 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/components/renderers/PhotoRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export type PhotoRendererProps<T extends Photo = Photo> = Omit<
"imageProps" | "renderDefaultPhoto" | "wrapperStyle"
> & {
imageProps?: ImageElementAttributes;
} & { renderPhoto?: RenderPhoto<T> };
renderPhoto?: RenderPhoto<T>;
};

export default function PhotoRenderer<T extends Photo = Photo>(props: PhotoRendererProps<T>) {
const { photo, layout, layoutOptions, imageProps: { style, ...restImageProps } = {}, renderPhoto } = props;
Expand Down Expand Up @@ -99,19 +100,19 @@ export default function PhotoRenderer<T extends Photo = Photo>(props: PhotoRende
...restImageProps,
};

function renderDefaultPhoto({ wrapped }: { wrapped?: boolean } = {}) {
const renderDefaultPhoto: RenderPhotoProps<T>["renderDefaultPhoto"] = (options) => {
const { src, alt, srcSet, sizes, style: unwrappedStyle, ...rest } = imageProps;

return (
<img
alt={alt}
{...(srcSet ? { srcSet, sizes } : null)}
src={src}
style={wrapped ? { display: "block", width: "100%", height: "100%" } : unwrappedStyle}
style={options?.wrapped ? { display: "block", width: "100%", height: "100%" } : unwrappedStyle}
{...rest}
/>
);
}
};

const wrapperStyle = (({ display, boxSizing, width, aspectRatio, padding, marginBottom, cursor }) => ({
display,
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type RenderPhotoProps<T extends Photo = Photo> = {
imageProps: NonOptional<ImageElementAttributes, "src" | "alt" | "style">;
/** A callback to render the default photo implementation. If `wrapped` is `true`, the image is styled with `width`
* and `height` set to 100%. Use this option when rendering image wrapper styled with wrapperStyle. */
renderDefaultPhoto: RenderFunction<{ wrapped?: boolean }>;
renderDefaultPhoto: RenderFunction<{ wrapped?: boolean } | void>;
/** CSS styles to properly size image wrapper (i.e. <div> wrapper) */
wrapperStyle: React.CSSProperties;
};
Expand Down Expand Up @@ -203,7 +203,7 @@ export type RenderColumnContainerProps<T extends Photo = Photo> = React.PropsWit

export type RenderColumnContainer<T extends Photo = Photo> = RenderFunction<RenderColumnContainerProps<T>>;

export type RenderFunction<T> = (props: T) => React.ReactNode;
export type RenderFunction<T = void> = (props: T) => React.ReactNode;

export type DivElementAttributes = React.HTMLAttributes<HTMLDivElement>;

Expand Down
4 changes: 4 additions & 0 deletions test/PhotoAlbum.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ describe("PhotoAlbum", () => {
)}
/>
);

whenAskedToRender(
<PhotoAlbum layout="rows" photos={photos} renderPhoto={({ renderDefaultPhoto }) => renderDefaultPhoto()} />
);
});

it("de-duplicates srcSet images", () => {
Expand Down
Loading

0 comments on commit cc56f5a

Please sign in to comment.