Skip to content

Commit 7d5eff8

Browse files
committed
add renderPage prop
1 parent 91a2245 commit 7d5eff8

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,55 @@ Fully native interactions powered by [Reanimated 2](https://github.com/kmagiera/
1414
### Props
1515

1616
```typescript
17-
type PageComponentType = (props: {
17+
type PageProps = {
1818
index: number;
1919
focusAnim: Animated.DerivedValue<number>;
2020
isActive: boolean;
21-
}) => JSX.Element | null;
21+
pageWidthAnim: Animated.SharedValue<number>;
22+
pageAnim: Animated.SharedValue<number>;
23+
}
24+
25+
type PageComponentType = (props: PageProps) => JSX.Element | null;
2226

2327
type AnyStyle = StyleProp<ViewStyle> | ReturnType<typeof useAnimatedStyle>;
28+
29+
type Props = {
30+
PageComponent?:
31+
| PageComponentType
32+
| React.MemoExoticComponent<PageComponentType>;
33+
renderPage?: PageComponentType
34+
pageCallbackNode?: Animated.SharedValue<number>;
35+
onPageChange?: (page: number) => void;
36+
pageBuffer?: number;
37+
style?: AnyStyle;
38+
pageWrapperStyle?: AnyStyle;
39+
pageInterpolator?: typeof defaultPageInterpolator;
40+
minIndex?: number;
41+
maxIndex?: number;
42+
simultaneousGestures?: (ComposedGesture | GestureType)[];
43+
gesturesDisabled?: boolean;
44+
animationConfig?: Partial<WithSpringConfig>;
45+
};
2446
```
2547

2648
| Name | Type | Description |
2749
| :----------------- | :----------------------- | :---------------------------------------------- |
28-
| `PageComponent` | `PageComponentType` | Component to be rendered as each page. |
50+
| `PageComponent` | `PageComponentType` | Component to be rendered as each page (either PageComponent OR renderPage must be defined, but not both — choose the version that suits your use case). |
51+
| `renderPage` | `PageComponentType` | Function to be called to render each page. |
2952
| `onPageChange` | `(page: number) => void` | Callback invoked when the current page changes. |
3053
| `style` | `AnyStyle` | Style of the pager container. |
3154
| `pageWrapperStyle` | `AnyStyle` | Style of the container that wraps each page. |
55+
| `pageCallbackNode` | `Animated.SharedValue<number>` | SharedValue that represents the index of the current page. |
56+
| `pageBuffer` | `number` | Number of pages to render on either side of the active page. |
57+
| `pageInterpolator` | `(params: PageInterpolatorParams) => ReturnType<typeof useAnimatedStyle>` | Interpolator for custom page animations. |
58+
| `minIndex` | `number` | Minimum page index for non-infinite behavior (optional). |
59+
| `maxIndex` | `number` | Maximum page index for non-infinite behavior (optional). |
60+
| `simultaneousGestures` | `(ComposedGesture | GestureType)[]` | Simultaneous RNGH gestures. |
61+
| `gesturesDisabled` | `boolean` | Disables pan gestures. |
62+
| `animationConfig` | `Partial<WithSpringConfig>` | Customizes paging animations. |
63+
64+
65+
3266

3367
### Imperative Api
3468

src/index.tsx

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ export const DEFAULT_ANIMATION_CONFIG: WithSpringConfig = {
3131
restDisplacementThreshold: 0.2,
3232
};
3333

34-
type PageComponentType = (props: {
34+
type PageProps = {
3535
index: number;
3636
focusAnim: Animated.DerivedValue<number>;
3737
isActive: boolean;
3838
pageWidthAnim: Animated.SharedValue<number>;
3939
pageAnim: Animated.SharedValue<number>;
40-
}) => JSX.Element | null;
40+
};
41+
type PageComponentType = (props: PageProps) => JSX.Element | null;
4142

4243
type AnyStyle = StyleProp<ViewStyle> | ReturnType<typeof useAnimatedStyle>;
4344

4445
type Props = {
45-
PageComponent:
46+
PageComponent?:
4647
| PageComponentType
4748
| React.MemoExoticComponent<PageComponentType>;
49+
renderPage?: PageComponentType;
4850
pageCallbackNode?: Animated.SharedValue<number>;
4951
onPageChange?: (page: number) => void;
5052
pageBuffer?: number; // number of pages to render on either side of active page
@@ -82,6 +84,7 @@ function InfinitePager(
8284
simultaneousGestures = [],
8385
gesturesDisabled,
8486
animationConfig = {},
87+
renderPage,
8588
}: Props,
8689
ref: React.ForwardedRef<InfinitePagerImperativeApi>
8790
) {
@@ -211,6 +214,7 @@ function InfinitePager(
211214
pageWidth={pageWidth}
212215
isActive={pageIndex === curIndex}
213216
PageComponent={PageComponent}
217+
renderPage={renderPage}
214218
style={pageWrapperStyle}
215219
pageInterpolatorRef={pageInterpolatorRef}
216220
/>
@@ -225,7 +229,8 @@ type PageWrapperProps = {
225229
pageAnim: Animated.SharedValue<number>;
226230
index: number;
227231
pageWidth: Animated.SharedValue<number>;
228-
PageComponent: PageComponentType;
232+
PageComponent?: PageComponentType;
233+
renderPage?: PageComponentType;
229234
isActive: boolean;
230235
style?: AnyStyle;
231236
pageInterpolatorRef: React.MutableRefObject<typeof defaultPageInterpolator>;
@@ -262,6 +267,7 @@ const PageWrapper = React.memo(
262267
pageAnim,
263268
pageWidth,
264269
PageComponent,
270+
renderPage,
265271
isActive,
266272
style,
267273
pageInterpolatorRef,
@@ -289,6 +295,16 @@ const PageWrapper = React.memo(
289295
});
290296
}, [pageWidth, pageAnim, index, translation]);
291297

298+
if (PageComponent && renderPage) {
299+
console.warn(
300+
"PageComponent and renderPage both defined, defaulting to PageComponent"
301+
);
302+
}
303+
304+
if (!PageComponent && !renderPage) {
305+
throw new Error("Either PageComponent or renderPage must be defined.");
306+
}
307+
292308
return (
293309
<Animated.View
294310
style={[
@@ -298,13 +314,23 @@ const PageWrapper = React.memo(
298314
isActive && styles.activePage,
299315
]}
300316
>
301-
<PageComponent
302-
index={index}
303-
isActive={isActive}
304-
focusAnim={focusAnim}
305-
pageWidthAnim={pageWidth}
306-
pageAnim={pageAnim}
307-
/>
317+
{PageComponent ? (
318+
<PageComponent
319+
index={index}
320+
isActive={isActive}
321+
focusAnim={focusAnim}
322+
pageWidthAnim={pageWidth}
323+
pageAnim={pageAnim}
324+
/>
325+
) : (
326+
renderPage?.({
327+
index,
328+
isActive,
329+
focusAnim,
330+
pageWidthAnim: pageWidth,
331+
pageAnim,
332+
})
333+
)}
308334
</Animated.View>
309335
);
310336
}

0 commit comments

Comments
 (0)