Skip to content

Commit

Permalink
feat: improve onClick callback DX
Browse files Browse the repository at this point in the history
BREAKING CHANGE: change onClick callback method signature
  • Loading branch information
igordanchenko committed Oct 27, 2022
1 parent cfa8ce5 commit aedfaf9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/renderers/PhotoRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const PhotoRenderer = <T extends Photo = Photo>(props: PhotoRendererProps<T>) =>

const handleClick = onClick
? (event: React.MouseEvent) => {
onClick(event, photo, layout.index);
onClick({ event, photo, index: layout.index });
}
: undefined;

Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import * as React from "react";

export type LayoutType = "columns" | "rows" | "masonry";

export type ClickHandler<T extends Photo = Photo> = (event: React.MouseEvent, photo: T, index: number) => void;
export type ClickHandler<T extends Photo = Photo> = ({
event,
photo,
index,
}: {
event: React.MouseEvent;
photo: T;
index: number;
}) => void;

export type ResponsiveParameterProvider<T = number> = (containerWidth: number) => T;

Expand Down
6 changes: 3 additions & 3 deletions test/PhotoAlbum.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe("PhotoAlbum", () => {
imageProps: { src, alt, ...restImageProps },
}) => <img src={src} alt={alt} data-custom-attribute={photo.customAttribute} {...restImageProps} />;

const clickHandler: ClickHandler<CustomPhoto> = (event, photo, index) => {
const clickHandler: ClickHandler<CustomPhoto> = ({ photo, index }) => {
// this check doesn't actually get called, but it's here to type check the click handler
expect(photo.customAttribute).toBe(index);
};
Expand All @@ -273,7 +273,7 @@ describe("PhotoAlbum", () => {
renderPhoto={({ photo, imageProps: { src, alt, ...restImageProps } }) => (
<img src={src} alt={alt} data-custom-attribute={photo.customAttribute} {...restImageProps} />
)}
onClick={(event, photo, index) => {
onClick={({ photo, index }) => {
// this check doesn't actually get called, but it's here to type check the click handler
expect(photo.customAttribute).toBe(index);
}}
Expand Down Expand Up @@ -344,7 +344,7 @@ describe("PhotoAlbum", () => {
screen.getByAltText("photo-0").click();

expect(onClick.mock.calls.length).toBe(1);
expect(onClick.mock.calls[0][1]).toBe(testPhotos[0]);
expect(onClick.mock.calls[0][0].photo).toBe(testPhotos[0]);
});

it("supports sizes attribute", () => {
Expand Down

0 comments on commit aedfaf9

Please sign in to comment.