Skip to content

Commit a60dad6

Browse files
authored
Support custom item render in GridView (#1775)
* Suport custom item render in GridView * Add missing api
1 parent 5b8d20b commit a60dad6

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

generatedTypes/src/components/gridView/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference types="react" />
1+
import React from 'react';
22
import UIComponent from '../../commons/UIComponent';
33
import { GridListItemProps } from '../gridListItem';
44
export interface GridViewProps {
@@ -36,6 +36,10 @@ export interface GridViewProps {
3636
* Ignored when passing 'maxItemWidth'
3737
*/
3838
keepItemSize?: boolean;
39+
/**
40+
* Pass to render a custom item
41+
*/
42+
renderCustomItem?: (item: GridListItemProps) => React.ReactElement;
3943
}
4044
interface GridViewState {
4145
viewWidth: number;

src/components/gridView/gridView.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
"name": "keepItemSize",
1919
"type": "boolean",
2020
"description": "whether to keep the items initial size when orientation changes, in which case the apt number of columns will be calculated automatically."
21+
},
22+
{
23+
"name": "renderCustomItem",
24+
"type": "(item: GridListItemProps) => React.ReactElement",
25+
"description": "Pass to render a custom item"
2126
}
2227
]
2328
}

src/components/gridView/index.tsx

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export interface GridViewProps {
4747
* Ignored when passing 'maxItemWidth'
4848
*/
4949
keepItemSize?: boolean;
50+
/**
51+
* Pass to render a custom item
52+
*/
53+
renderCustomItem?: (item: GridListItemProps) => React.ReactElement;
5054
}
5155

5256
interface GridViewState {
@@ -177,30 +181,39 @@ class GridView extends UIComponent<GridViewProps, GridViewState> {
177181

178182
renderItem = (item: GridListItemProps, index: number) => {
179183
const {itemSize} = this.state;
180-
const {items, itemSpacing} = this.props;
181-
184+
const {items, itemSpacing, renderCustomItem} = this.props;
182185
const {numColumns = DEFAULT_NUM_COLUMNS} = this.state;
186+
183187
const itemsCount = _.size(items);
184188
const rowCount = Math.ceil(itemsCount / numColumns);
185189
const isLastItemInRow = (index + 1) % numColumns === 0;
186190
const isLastRow = index + 1 > (rowCount - 1) * numColumns;
187191
const isLastItem = index === itemsCount - 1;
188192
const size =
189-
typeof item.itemSize === 'object' ? {width: itemSize, height: item.itemSize?.height || itemSize} : itemSize;
190-
return (
191-
<GridListItem
192-
key={index}
193-
{...item}
194-
itemSize={size}
195-
containerStyle={[
196-
!isLastItemInRow && {marginRight: itemSpacing},
197-
!isLastRow && {marginBottom: itemSpacing},
198-
item.containerStyle
199-
]}
200-
>
201-
{isLastItem && this.renderLastItemOverlay()}
202-
</GridListItem>
203-
);
193+
typeof item.itemSize === 'object'
194+
? {
195+
width: itemSize,
196+
height: item.itemSize?.height || itemSize
197+
}
198+
: itemSize;
199+
200+
const itemProps = {
201+
key: index,
202+
...item,
203+
itemSize: size,
204+
containerStyle: [
205+
!isLastItemInRow && {
206+
marginRight: itemSpacing
207+
},
208+
!isLastRow && {
209+
marginBottom: itemSpacing
210+
},
211+
item.containerStyle
212+
],
213+
children: isLastItem ? this.renderLastItemOverlay() : undefined
214+
};
215+
216+
return renderCustomItem ? renderCustomItem(itemProps) : <GridListItem {...itemProps}/>;
204217
};
205218

206219
render() {

0 commit comments

Comments
 (0)