Skip to content

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

Merged
merged 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion generatedTypes/src/components/gridView/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference types="react" />
import React from 'react';
import UIComponent from '../../commons/UIComponent';
import { GridListItemProps } from '../gridListItem';
export interface GridViewProps {
Expand Down Expand Up @@ -36,6 +36,10 @@ export interface GridViewProps {
* Ignored when passing 'maxItemWidth'
*/
keepItemSize?: boolean;
/**
* Pass to render a custom item
*/
renderCustomItem?: (item: GridListItemProps) => React.ReactElement;
}
interface GridViewState {
viewWidth: number;
Expand Down
5 changes: 5 additions & 0 deletions src/components/gridView/gridView.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"name": "keepItemSize",
"type": "boolean",
"description": "whether to keep the items initial size when orientation changes, in which case the apt number of columns will be calculated automatically."
},
{
"name": "renderCustomItem",
"type": "(item: GridListItemProps) => React.ReactElement",
"description": "Pass to render a custom item"
}
]
}
47 changes: 30 additions & 17 deletions src/components/gridView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}/>;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?
Also, I feel like the name (item in the definition above) should be more along "suggested props" if this is how it's called, not sure how to call it though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand the comment.
What you mean by configuration?

What I did is just pass the props I usually pass to GridListItem to the custom render so it can use it.
Technically, we render in private a wrapper of GridListItem (check the private PR)

Copy link
Collaborator

Choose a reason for hiding this comment

The 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() {
Expand Down