neptunian / react-photo-gallery

React Photo Gallery
http://neptunian.github.io/react-photo-gallery/
Other
1.96k stars 309 forks source link

Update return type for `columns` function prop to `number | undefined` #228

Open AleksandrHovhannisyan opened 7 months ago

AleksandrHovhannisyan commented 7 months ago

The way the code is written here (two if statements) suggests that the columns function prop can technically return undefined, allowing a user to fall back to the default internal calculations in the if statement on L82:

https://github.com/neptunian/react-photo-gallery/blob/0bb8e4c4a027c021f8a5a06de71e89026596fd95/src/Gallery.js#L76-L87

I was able to verify this locally. However, the return type for this function is strictly number, so I get type errors if I try to return undefined from the function:

https://github.com/neptunian/react-photo-gallery/blob/0bb8e4c4a027c021f8a5a06de71e89026596fd95/src/Gallery.js#L120

I think it would be nice to update the prop types to allow returning undefined since it seems to work as expected: I can either specify custom logic or return undefined to signal that I want to fall back to the default calculations.


Alternatively, the function could accept a second argument—the default number of columns for containerWidth—so you could do something like this:

columns={(containerWidth, defaultNumColumns) => containerWidth <= 320 ? 2 : defaultNumColumns}

Happy to put in a PR for this if you approve.