1- import { createElement , ReactElement , ReactNode } from "react" ;
1+ import { createElement , ReactElement , ReactNode , useCallback } from "react" ;
22import { Text , FlatList , Pressable , View , ViewProps , Platform , TouchableOpacity } from "react-native" ;
33import { ObjectItem , DynamicValue } from "mendix" ;
44import 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 } : { } ) }
@@ -127,10 +141,11 @@ export const Gallery = <T extends ObjectItem>(props: GalleryProps<T>): ReactElem
127141 keyExtractor = { item => item . id }
128142 ListEmptyComponent = { renderEmptyPlaceholder }
129143 onEndReached = { onEndReached }
144+ onEndReachedThreshold = { 0.6 }
130145 scrollEventThrottle = { 50 }
131146 renderItem = { renderItem }
132147 style = { props . style . list }
133- testID = { `${ props . name } -list` }
148+ testID = { `${ name } -list` }
134149 />
135150 </ View >
136151 ) ;
0 commit comments