Skip to content

Commit 5e27c9d

Browse files
committed
feat: Add onScroll prop
1 parent 4fdcaa4 commit 5e27c9d

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

src/responsive-grid/index.tsx

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
2121
style = {},
2222
itemContainerStyle = {},
2323
itemUnitHeight,
24+
onScroll: onScrollProp,
2425
onEndReached,
2526
onEndReachedThreshold = 0.5, // default to 50% of the container height
2627
keyExtractor = (_, index) => String(index), // default to item index if no keyExtractor is provided
@@ -83,33 +84,44 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
8384
scrollEventInterval
8485
);
8586

86-
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
87-
const currentScrollY = event.nativeEvent.contentOffset.y;
88-
scrollYPosition.current = currentScrollY;
89-
90-
// Calculate the position to check against the threshold
91-
const contentHeight = gridViewHeight;
92-
const scrollViewHeight = containerSize.height;
93-
const threshold = onEndReachedThreshold * scrollViewHeight;
94-
95-
// Check if we've reached the threshold for calling onEndReached
96-
if (
97-
!onEndReachedCalled.current &&
98-
currentScrollY + scrollViewHeight + threshold >= contentHeight
99-
) {
100-
onEndReachedCalled.current = true; // Marked as called to prevent subsequent calls
101-
onEndReached?.(); // call the onEndReached function if it exists
102-
}
87+
const throttledOnScroll = useThrottle(
88+
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
89+
const currentScrollY = event.nativeEvent.contentOffset.y;
90+
scrollYPosition.current = currentScrollY;
91+
92+
// Calculate the position to check against the threshold
93+
const contentHeight = gridViewHeight;
94+
const scrollViewHeight = containerSize.height;
95+
const threshold = onEndReachedThreshold * scrollViewHeight;
96+
97+
// Check if we've reached the threshold for calling onEndReached
98+
if (
99+
!onEndReachedCalled.current &&
100+
currentScrollY + scrollViewHeight + threshold >= contentHeight
101+
) {
102+
onEndReachedCalled.current = true; // Marked as called to prevent subsequent calls
103+
onEndReached?.(); // call the onEndReached function if it exists
104+
}
105+
106+
// Reset the flag when scrolled away from the bottom
107+
if (currentScrollY + scrollViewHeight + threshold * 2 < contentHeight) {
108+
onEndReachedCalled.current = false;
109+
}
110+
111+
// Update visible items for virtualization
112+
if (virtualization) {
113+
throttledUpdateVisibleItems();
114+
}
115+
},
116+
32
117+
);
103118

104-
// Reset the flag when scrolled away from the bottom
105-
if (currentScrollY + scrollViewHeight + threshold * 2 < contentHeight) {
106-
onEndReachedCalled.current = false;
119+
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
120+
if (onScrollProp) {
121+
onScrollProp(event);
107122
}
108123

109-
// Update visible items for virtualization
110-
if (virtualization) {
111-
throttledUpdateVisibleItems();
112-
}
124+
throttledOnScroll(event);
113125
};
114126

115127
useEffect(() => {
@@ -145,7 +157,6 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
145157
>
146158
<ScrollView
147159
onScroll={onScroll}
148-
scrollEventThrottle={32}
149160
contentContainerStyle={{
150161
height: sumScrollViewHeight || '100%',
151162
width: containerSize.width,

src/responsive-grid/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type React from 'react';
22
import type { ReactNode } from 'react';
3-
import type { StyleProp, ViewStyle } from 'react-native';
3+
import type {
4+
NativeScrollEvent,
5+
NativeSyntheticEvent,
6+
StyleProp,
7+
ViewStyle,
8+
} from 'react-native';
49

510
interface RenderItemProps {
611
item: any;
@@ -49,6 +54,9 @@ export interface ResponsiveGridProps {
4954
/** Defines the base unit height for items within the grid. If not provided, it's calculated based on container width and maxItemsPerColumn. */
5055
itemUnitHeight?: number;
5156

57+
/** Callback function triggered when the scroll view is scrolled. */
58+
onScroll?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
59+
5260
/** Callback function triggered when the scroll reaches near the end of the scrollable grid. */
5361
onEndReached?: () => void;
5462

0 commit comments

Comments
 (0)