@@ -129,7 +129,7 @@ export const GridCell = observer(({ view, selected, row, fields, onClick, column
129
129
export const GridView = observer ( ( { data, view, loadMore, fields, onChange, hiddenFields } ) => {
130
130
const columnCount = view . gridWidth ?? 4 ;
131
131
132
- const getCellIndex = ( row , column ) => columnCount * row + column ;
132
+ const getCellIndex = useCallback ( ( row , column ) => columnCount * row + column , [ columnCount ] ) ;
133
133
134
134
const fieldsData = useMemo ( ( ) => {
135
135
return prepareColumns ( fields , hiddenFields ) ;
@@ -148,11 +148,15 @@ export const GridView = observer(({ data, view, loadMore, fields, onChange, hidd
148
148
const finalRowHeight =
149
149
CELL_HEADER_HEIGHT + rowHeight * ( hasImage ? Math . max ( 1 , ( IMAGE_SIZE_COEFFICIENT - columnCount ) * 0.5 ) : 1 ) ;
150
150
151
+ // Calculate the total number of rows needed to display all items
152
+ const itemCount = view . dataStore . total || data . length ;
153
+ const totalRows = Math . ceil ( itemCount / columnCount ) ;
154
+
151
155
const renderItem = useCallback (
152
156
( { style, rowIndex, columnIndex } ) => {
153
157
const index = getCellIndex ( rowIndex , columnIndex ) ;
154
- if ( ! data || ! ( index in data ) ) return null ;
155
158
const row = data [ index ] ;
159
+ if ( ! row ) return null ;
156
160
157
161
const props = {
158
162
style : {
@@ -173,30 +177,42 @@ export const GridView = observer(({ data, view, loadMore, fields, onChange, hidd
173
177
/>
174
178
) ;
175
179
} ,
176
- [ data , columnCount , fieldsData , view . selected , view , view . selected . list , view . selected . all , getCellIndex ] ,
180
+ [ data , columnCount , fieldsData , view , onChange , getCellIndex ] ,
177
181
) ;
178
182
179
- const onItemsRenderedWrap =
183
+ const onItemsRenderedWrap = useCallback (
180
184
( cb ) =>
181
- ( { visibleRowStartIndex, visibleRowStopIndex, overscanRowStopIndex, overscanRowStartIndex } ) => {
182
- cb ( {
183
- overscanStartIndex : overscanRowStartIndex ,
184
- overscanStopIndex : overscanRowStopIndex ,
185
- visibleStartIndex : visibleRowStartIndex ,
186
- visibleStopIndex : visibleRowStopIndex ,
187
- } ) ;
188
- } ;
189
-
190
- const itemCount = Math . ceil ( data . length / columnCount ) ;
185
+ ( { visibleRowStartIndex, visibleRowStopIndex, overscanRowStopIndex, overscanRowStartIndex } ) => {
186
+ // Check if we're near the end and need to load more
187
+ const visibleItemStopIndex = getCellIndex ( visibleRowStopIndex , columnCount - 1 ) ;
188
+
189
+ // If we're showing items near the end of our loaded data, trigger loading
190
+ if (
191
+ visibleItemStopIndex >= data . length - columnCount * 2 &&
192
+ view . dataStore . hasNextPage &&
193
+ ! view . dataStore . loading
194
+ ) {
195
+ loadMore ?. ( ) ;
196
+ }
197
+
198
+ cb ( {
199
+ overscanStartIndex : overscanRowStartIndex ,
200
+ overscanStopIndex : overscanRowStopIndex ,
201
+ visibleStartIndex : visibleRowStartIndex ,
202
+ visibleStopIndex : visibleRowStopIndex ,
203
+ } ) ;
204
+ } ,
205
+ [ data . length , columnCount , view . dataStore . hasNextPage , view . dataStore . loading , loadMore , getCellIndex ] ,
206
+ ) ;
191
207
208
+ // Check if a specific item index is loaded
192
209
const isItemLoaded = useCallback (
193
210
( index ) => {
194
- const rowIndex = index * columnCount ;
195
- const rowFullfilled = data . slice ( rowIndex , columnCount ) . length === columnCount ;
196
-
197
- return ! view . dataStore . hasNextPage || rowFullfilled ;
211
+ const rowExists = index < data . length && ! ! data [ index ] ;
212
+ const hasNextPage = view . dataStore . hasNextPage ;
213
+ return ! hasNextPage || rowExists ;
198
214
} ,
199
- [ columnCount , data , view . dataStore . hasNextPage ] ,
215
+ [ data . length , view . dataStore . hasNextPage ] ,
200
216
) ;
201
217
202
218
return (
@@ -208,8 +224,8 @@ export const GridView = observer(({ data, view, loadMore, fields, onChange, hidd
208
224
itemCount = { itemCount }
209
225
isItemLoaded = { isItemLoaded }
210
226
loadMoreItems = { loadMore }
211
- threshold = { Math . floor ( view . dataStore . pageSize / 2 ) }
212
- minimumBatchSize = { view . dataStore . pageSize }
227
+ threshold = { Math . max ( 1 , Math . floor ( view . dataStore . pageSize / 4 ) ) }
228
+ minimumBatchSize = { Math . max ( 1 , Math . floor ( view . dataStore . pageSize / 2 ) ) }
213
229
>
214
230
{ ( { onItemsRendered, ref } ) => (
215
231
< Elem
@@ -219,10 +235,10 @@ export const GridView = observer(({ data, view, loadMore, fields, onChange, hidd
219
235
height = { height }
220
236
name = "list"
221
237
rowHeight = { finalRowHeight }
222
- overscanRowCount = { view . dataStore . pageSize }
238
+ overscanRowCount = { Math . max ( 2 , Math . floor ( view . dataStore . pageSize / 2 ) ) }
223
239
columnCount = { columnCount }
240
+ rowCount = { totalRows }
224
241
columnWidth = { width / columnCount - 9.5 }
225
- rowCount = { itemCount }
226
242
onItemsRendered = { onItemsRenderedWrap ( onItemsRendered ) }
227
243
style = { { overflowX : "hidden" } }
228
244
>
0 commit comments