Skip to content

Commit 6a33304

Browse files
committed
fix(gallery-native): removed padding and memoized item renderer function
1 parent 357890d commit 6a33304

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

packages/pluggableWidgets/gallery-native/src/components/Gallery.tsx

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createElement, ReactElement, ReactNode } from "react";
1+
import { createElement, ReactElement, ReactNode, useCallback } from "react";
22
import { Text, FlatList, Pressable, View, ViewProps, Platform, TouchableOpacity } from "react-native";
33
import { ObjectItem, DynamicValue } from "mendix";
44
import DeviceInfo from "react-native-device-info";
@@ -32,38 +32,52 @@ export const Gallery = <T extends ObjectItem>(props: GalleryProps<T>): ReactElem
3232
const numColumns = DeviceInfo.isTablet() ? props.tabletColumns : props.phoneColumns;
3333
const firstItemId = props.items?.[0]?.id;
3434
const lastItemId = props.items?.[props.items.length - 1]?.id;
35+
const { name, style, itemRenderer } = props;
3536

3637
const onEndReached = (): void => {
3738
if (props.pagination === "virtualScrolling" && props.hasMoreItems) {
3839
props.loadMoreItems();
3940
}
4041
};
4142

42-
const renderItem = (item: { item: T }): ReactElement =>
43-
props.itemRenderer((children, onPress) => {
44-
const listItemWrapperProps: ViewProps = {
45-
style: isScrollDirectionVertical && { width: `${100 / numColumns}%` },
46-
testID: `${props.name}-list-item-${item.item.id}`
47-
};
48-
const renderListItemContent = (
49-
<View
50-
style={[
51-
props.style.listItem,
52-
firstItemId === item.item.id && props.style.firstItem,
53-
lastItemId === item.item.id && props.style.lastItem
54-
]}
55-
>
56-
{children}
57-
</View>
58-
);
59-
return onPress ? (
60-
<Pressable {...listItemWrapperProps} onPress={onPress}>
61-
{renderListItemContent}
62-
</Pressable>
63-
) : (
64-
<View {...listItemWrapperProps}>{renderListItemContent}</View>
65-
);
66-
}, item.item);
43+
const renderItem = useCallback(
44+
(item: { item: T }): ReactElement =>
45+
itemRenderer((children, onPress) => {
46+
const listItemWrapperProps: ViewProps = {
47+
style: isScrollDirectionVertical && { width: `${100 / numColumns}%` },
48+
testID: `${name}-list-item-${item.item.id}`
49+
};
50+
const renderListItemContent = (
51+
<View
52+
style={[
53+
style.listItem,
54+
firstItemId === item.item.id && style.firstItem,
55+
lastItemId === item.item.id && style.lastItem
56+
]}
57+
>
58+
{children}
59+
</View>
60+
);
61+
return onPress ? (
62+
<Pressable {...listItemWrapperProps} onPress={onPress}>
63+
{renderListItemContent}
64+
</Pressable>
65+
) : (
66+
<View {...listItemWrapperProps}>{renderListItemContent}</View>
67+
);
68+
}, item.item),
69+
[
70+
isScrollDirectionVertical,
71+
numColumns,
72+
itemRenderer,
73+
name,
74+
style.listItem,
75+
style.firstItem,
76+
style.lastItem,
77+
firstItemId,
78+
lastItemId
79+
]
80+
);
6781

6882
const loadMoreButton = (): ReactElement | null => {
6983
const renderButton = (
@@ -80,7 +94,7 @@ export const Gallery = <T extends ObjectItem>(props: GalleryProps<T>): ReactElem
8094
);
8195

8296
const buttonProps = {
83-
testID: `${props.name}-pagination-button`,
97+
testID: `${name}-pagination-button`,
8498
onPress: () => props.hasMoreItems && props.loadMoreItems && props.loadMoreItems(),
8599
style: loadMoreButtonContainerStyle
86100
};
@@ -111,7 +125,7 @@ export const Gallery = <T extends ObjectItem>(props: GalleryProps<T>): ReactElem
111125
);
112126

113127
return (
114-
<View testID={`${props.name}`} style={props.style.container}>
128+
<View testID={`${name}`} style={props.style.container}>
115129
{props.filters ? <View>{props.filters}</View> : null}
116130
<FlatList
117131
{...(isScrollDirectionVertical && props.pullDown ? { onRefresh: props.pullDown } : {})}
@@ -121,7 +135,6 @@ export const Gallery = <T extends ObjectItem>(props: GalleryProps<T>): ReactElem
121135
...props.style.loadMoreButtonContainer,
122136
...(isScrollDirectionVertical ? { marginTop: 8 } : { marginStart: 8 })
123137
}}
124-
contentContainerStyle={props.pagination === "virtualScrolling" && props.style.listContainer}
125138
refreshing={props.pullDownIsExecuting}
126139
data={props.items}
127140
horizontal={!isScrollDirectionVertical}
@@ -132,7 +145,7 @@ export const Gallery = <T extends ObjectItem>(props: GalleryProps<T>): ReactElem
132145
scrollEventThrottle={50}
133146
renderItem={renderItem}
134147
style={props.style.list}
135-
testID={`${props.name}-list`}
148+
testID={`${name}-list`}
136149
/>
137150
</View>
138151
);

packages/pluggableWidgets/gallery-native/src/components/__tests__/__snapshots__/Gallery.spec.tsx.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ exports[`Gallery rendering rendering with load more button it shouldn't render t
1313
"marginTop": 8,
1414
}
1515
}
16-
contentContainerStyle={false}
1716
data={
1817
[
1918
{
@@ -342,7 +341,6 @@ exports[`Gallery rendering rendering with load more button renders correctly 1`]
342341
"marginTop": 8,
343342
}
344343
}
345-
contentContainerStyle={false}
346344
data={
347345
[
348346
{
@@ -717,7 +715,6 @@ exports[`Gallery rendering rendering with load more button renders correctly wit
717715
"marginTop": 8,
718716
}
719717
}
720-
contentContainerStyle={false}
721718
data={
722719
[
723720
{

packages/pluggableWidgets/gallery-native/src/ui/Styles.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export interface GalleryStyle {
1414
emptyPlaceholder?: ViewStyle;
1515
firstItem?: ViewStyle;
1616
lastItem?: ViewStyle;
17-
listContainer?: ViewStyle;
1817
list?: ViewStyle;
1918
listItem?: ViewStyle;
2019
loadMoreButtonContainer?: ViewStyle;
@@ -47,8 +46,5 @@ export const defaultGalleryStyle: GalleryStyle = {
4746
fontSize: 12,
4847
fontWeight: "bold",
4948
lineHeight: 14
50-
},
51-
listContainer: {
52-
paddingBottom: 250
5349
}
5450
};

0 commit comments

Comments
 (0)