-
Notifications
You must be signed in to change notification settings - Fork 73
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
map virtual object to props #41
map virtual object to props #41
Conversation
I definitely like this idea, although I'm going to need to think it through a bit more. Maybe it could be an optional second parameter to |
Yea, maybe we leave the virtual prop but document the new way? A kind of deprecation path similar to what other libs are doing would allow people to move to it over time. Let me know what you decide, I'll be available to make the necessary changes. |
Yeah, I think we can keep things backwards-compatible by providing a default function returning the same I'm not sure if it makes more sense to have const MyVirtualList = VirtualList(options, mapVirtualToProps)(MyList); I could go either way, although Redux implements it (as well as Either way, I think it makes sense to have additional tests to cover when |
I like the idea of using the second argument like they did. I set the default |
I've been thinking about this... and what if we give const defaultMapVirtualToProps = ({
firstItemIndex,
lastItemIndex,
items,
itemHeight,
}) => {
const visibleItems = lastItemIndex > -1 ? items.slice(firstItemIndex, lastItemIndex + 1) : [];
const height = items.length * itemHeight;
const paddingTop = firstItemIndex * itemHeight;
return {
virtual: {
items: visibleItems,
style: {
height,
paddingTop,
},
},
};
}; Or, alternatively, maybe we just make the incoming arguments a little more agnostic instead of assuming which style rules they'll be used for (I could see people wanting to use absolute positioning or maybe const defaultMapVirtualToProps = ({
visibleItems,
listHeight, // total height of the list
listTop, // distance between the top of the list and the first visible item
}) => ({
virtual: {
items: visibleItems,
style: {
height: listHeight,
paddingTop: listTop,
},
},
}); Maybe we include the other props here as well, like I think it's important to get this right the first time, because if we change it later on, it will definitely be a breaking change. |
I was wondering about this actually, have you implemented this with |
@developerdizzle what if we take your calculations for The style calculations are simplistic enough that they shouldn't really need any kind of helper necessarily. If you felt strongly about those we could export an additional helper for those attributes as well. At the end of the day, Below is an example of what I mean. const defaultMapVirtualToProps = ({
items,
itemHeight,
}, {
firstItemIndex,
lastItemIndex,
}) => {
const visibleItems = getVisibleItems(firstItemIndex, lastItemIndex);
const height = items.length * itemHeight;
const paddingTop = firstItemIndex * itemHeight;
return {
virtual: {
items: visibleItems,
style: {
height,
paddingTop,
},
},
};
};
// Further down in the code
render() {
const props = {
...this.props,
...mapVirtualToProps(this.props, this.state)
}
return (<InnerComponent {...props} />);
}; This would allow a developer to not only use whatever helpers we deign important enough, but also allow them access to all the information we keep track of in the virtual list. Although moving towards this starts to make the name |
@jordansexton I did a long time ago, but I can't recall why I didn't end up using it. @vinnymac I think I like this, although maybe we don't need |
Thanks for making those updates @vinnymac - I'm going to take a look at the tests this weekend and possibly push some changes. |
@vinnymac Just pushed up some changes - let me know what you think! |
ac8b0ee
to
2c4f21b
Compare
Moving the changes for defaultMapToVirtualProps to a separate file with its own tests makes sense. The code looks a lot cleaner this way. Also nice job on the documentation 👍 Hopefully this API is flexible enough for users, I can't think of anything else they might need from it right now. |
README.md
Outdated
@@ -93,6 +93,17 @@ Name | Type | Default | Description | |||
`itemHeight` | Number | - | Height in pixels of a single item. **You must have a CSS rule that sets the height of each list item to this value.** | |||
`itemBuffer` | Number | 0 | Number of items that should be rendered before and after the visible viewport. Try using this if you have a complex list that suffers from a bit of lag when scrolling. | |||
|
|||
#### Mapping | |||
|
|||
`VirtualList` allows a second, optional, parameter, named `mapVirtualToProps`, which functions similarly to [Redux's `mapStateToProps`](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options). This function can be provided to change the properties passed to `MyList`. It's arguments are: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's arguments are
should be Its arguments are
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch @eryon ! Grammar fail on my part..
2c4f21b
to
30593cf
Compare
I was thinking about the API for this lib and I thought that the virtual prop seemed a bit odd to me. Wouldn't it make more sense if it worked like
redux
does withconnect
?So in redux we might use
mapStateToProps
like in the example below.We can simplify things by removing the virtual prop and adding a new option called
mapVirtualToProps
this would be a breaking change, but now users have total control over what props the HOC adds.I would love to hear feedback on this idea.