-
Notifications
You must be signed in to change notification settings - Fork 734
Support custom item render in GridView #1775
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,10 @@ export interface GridViewProps { | |
* Ignored when passing 'maxItemWidth' | ||
*/ | ||
keepItemSize?: boolean; | ||
/** | ||
* Pass to render a custom item | ||
*/ | ||
renderCustomItem?: (item: GridListItemProps) => React.ReactElement; | ||
} | ||
|
||
interface GridViewState { | ||
|
@@ -177,30 +181,39 @@ class GridView extends UIComponent<GridViewProps, GridViewState> { | |
|
||
renderItem = (item: GridListItemProps, index: number) => { | ||
const {itemSize} = this.state; | ||
const {items, itemSpacing} = this.props; | ||
|
||
const {items, itemSpacing, renderCustomItem} = this.props; | ||
const {numColumns = DEFAULT_NUM_COLUMNS} = this.state; | ||
|
||
const itemsCount = _.size(items); | ||
const rowCount = Math.ceil(itemsCount / numColumns); | ||
const isLastItemInRow = (index + 1) % numColumns === 0; | ||
const isLastRow = index + 1 > (rowCount - 1) * numColumns; | ||
const isLastItem = index === itemsCount - 1; | ||
const size = | ||
typeof item.itemSize === 'object' ? {width: itemSize, height: item.itemSize?.height || itemSize} : itemSize; | ||
return ( | ||
<GridListItem | ||
key={index} | ||
{...item} | ||
itemSize={size} | ||
containerStyle={[ | ||
!isLastItemInRow && {marginRight: itemSpacing}, | ||
!isLastRow && {marginBottom: itemSpacing}, | ||
item.containerStyle | ||
]} | ||
> | ||
{isLastItem && this.renderLastItemOverlay()} | ||
</GridListItem> | ||
); | ||
typeof item.itemSize === 'object' | ||
? { | ||
width: itemSize, | ||
height: item.itemSize?.height || itemSize | ||
} | ||
: itemSize; | ||
|
||
const itemProps = { | ||
key: index, | ||
...item, | ||
itemSize: size, | ||
containerStyle: [ | ||
!isLastItemInRow && { | ||
marginRight: itemSpacing | ||
}, | ||
!isLastRow && { | ||
marginBottom: itemSpacing | ||
}, | ||
item.containerStyle | ||
], | ||
children: isLastItem ? this.renderLastItemOverlay() : undefined | ||
}; | ||
|
||
return renderCustomItem ? renderCustomItem(itemProps) : <GridListItem {...itemProps}/>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't usually send (as far as I remember) our configuration to the custom render function, is this a new approach or a special case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I understand the comment. What I did is just pass the props I usually pass to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I totally missed the email from private (probably archived it, thinking it's the same one). |
||
}; | ||
|
||
render() { | ||
|
Uh oh!
There was an error while loading. Please reload this page.