Skip to content

Commit

Permalink
fix: add safeguards against invalid responsive parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed May 3, 2022
1 parent c81483e commit b7eab8c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@testing-library/react": "^13.1.1",
"@types/jest": "^27.4.1",
"@types/react": "^18.0.8",
"@types/react-dom": "^18.0.0",
"@types/react-dom": "^18.0.3",
"@types/react-test-renderer": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
Expand All @@ -67,7 +67,7 @@
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-typescript2": "^0.31.2",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3"
"typescript": "^4.6.4"
},
"keywords": [
"react photo album",
Expand Down
7 changes: 3 additions & 4 deletions src/PhotoAlbum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const resolveLayoutOptions = <T extends Photo>({
onClick,
viewportWidth,
containerWidth,
columns: resolveResponsiveParameter(columns, containerWidth, [5, 4, 3, 2]),
columns: resolveResponsiveParameter(columns, containerWidth, [5, 4, 3, 2], 1),
spacing: resolveResponsiveParameter(spacing, containerWidth, [20, 15, 10, 5]),
padding: resolveResponsiveParameter(padding, containerWidth, [0, 0, 0, 0, 0]),
targetRowHeight: resolveResponsiveParameter(targetRowHeight, containerWidth, [
Expand All @@ -48,9 +48,8 @@ const resolveLayoutOptions = <T extends Photo>({
rowConstraints,
});

const resolveComponentsProps = (componentsProps: ComponentsPropsParameter | undefined, containerWidth: number) => {
return typeof componentsProps === "function" ? componentsProps(containerWidth) : componentsProps;
};
const resolveComponentsProps = (componentsProps: ComponentsPropsParameter | undefined, containerWidth: number) =>
typeof componentsProps === "function" ? componentsProps(containerWidth) : componentsProps;

const PhotoAlbum = <T extends Photo>(props: PhotoAlbumProps<T>): JSX.Element => {
const {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/responsive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ const selectResponsiveValue = (values: ResponsiveParameter[], containerWidth: nu
const resolveResponsiveParameter = (
parameter: ResponsiveParameter | undefined,
containerWidth: number,
values: ResponsiveParameter[]
values: ResponsiveParameter[],
minValue = 0
): number => {
const value = unwrapParameter(parameter, containerWidth);
return value === undefined ? selectResponsiveValue(values, containerWidth) : value;
return Math.round(Math.max(value === undefined ? selectResponsiveValue(values, containerWidth) : value, minValue));
};

export default resolveResponsiveParameter;

0 comments on commit b7eab8c

Please sign in to comment.