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

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

Open
AleksandrHovhannisyan opened this issue Feb 6, 2024 · 0 comments

Comments

@AleksandrHovhannisyan
Copy link

AleksandrHovhannisyan commented Feb 6, 2024

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:

if (direction === 'column') {
// allow user to calculate columns from containerWidth
if (typeof columns === 'function') {
columns = columns(containerWidth);
}
// set default breakpoints if user doesn't specify columns prop
if (columns === undefined) {
columns = 1;
if (containerWidth >= 500) columns = 2;
if (containerWidth >= 900) columns = 3;
if (containerWidth >= 1500) columns = 4;
}

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:

columns: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant