@onekeyfe/react-native-pager-view 3.0.117 → 3.0.118

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -112,6 +112,7 @@ export class CollapsiblePagerView extends React.PureComponent {
112
112
  onMountedPagesChanged: _onMountedPagesChanged,
113
113
  onPageSelected: _onPageSelected,
114
114
  layoutDirection,
115
+ initialPage: _initialPage,
115
116
  offscreenPageLimit,
116
117
  ...nativeProps
117
118
  } = this.props;
@@ -125,6 +126,7 @@ export class CollapsiblePagerView extends React.PureComponent {
125
126
  this.nativeRef = ref;
126
127
  },
127
128
  layoutDirection: deducedLayoutDirection,
129
+ initialPage: clampedPageIndex(this.props.initialPage ?? 0, pages.length),
128
130
  offscreenPageLimit: Math.max(1, offscreenPageLimit ?? 1),
129
131
  headerHeight: Math.max(0, Math.round(headerHeight)),
130
132
  stickyHeaderHeight: Math.max(0, Math.round(stickyHeaderHeight)),
@@ -3,13 +3,19 @@
3
3
  import React from "react";
4
4
  import { I18nManager, Keyboard, StyleSheet, View } from "react-native";
5
5
  import { jsx as _jsx } from "react/jsx-runtime";
6
+ const pageIndex = value => {
7
+ const index = Math.trunc(value);
8
+ return Number.isFinite(index) ? index : null;
9
+ };
10
+ const clampedPageIndex = (value, pageCount) => Math.min(Math.max(0, pageIndex(value) ?? 0), Math.max(0, pageCount - 1));
11
+
6
12
  /**
7
13
  * Web fallback for the ordinary PagerView API. All pages stay mounted, matching
8
14
  * the package's pre-existing PagerView lifetime contract.
9
15
  */
10
16
  export class PagerView extends React.Component {
11
17
  state = {
12
- selectedPage: Math.max(0, Math.trunc(this.props.initialPage ?? 0)),
18
+ selectedPage: clampedPageIndex(this.props.initialPage ?? 0, React.Children.toArray(this.props.children).length),
13
19
  animateTransition: true
14
20
  };
15
21
  pointerStart = null;
@@ -18,7 +24,7 @@ export class PagerView extends React.Component {
18
24
  if (previousProps.scrollEnabled !== this.props.scrollEnabled) {
19
25
  this.scrollEnabled = this.props.scrollEnabled ?? true;
20
26
  }
21
- const pageCount = React.Children.count(this.props.children);
27
+ const pageCount = this.pages().length;
22
28
  if (pageCount > 0 && this.state.selectedPage >= pageCount) {
23
29
  this.setState({
24
30
  selectedPage: pageCount - 1,
@@ -40,10 +46,13 @@ export class PagerView extends React.Component {
40
46
  nativeEvent
41
47
  };
42
48
  }
49
+ pages() {
50
+ return React.Children.toArray(this.props.children);
51
+ }
43
52
  selectPage(selectedPage, animated) {
44
- const pageCount = React.Children.count(this.props.children);
45
- const position = Math.trunc(selectedPage);
46
- if (position < 0 || position >= pageCount || position === this.state.selectedPage) {
53
+ const pageCount = this.pages().length;
54
+ const position = pageIndex(selectedPage);
55
+ if (position === null || position < 0 || position >= pageCount || position === this.state.selectedPage) {
47
56
  return;
48
57
  }
49
58
  this.props.onPageScrollStateChanged?.(this.nativeEvent({
@@ -87,7 +96,7 @@ export class PagerView extends React.Component {
87
96
  const isRTL = !vertical && (this.props.layoutDirection === "rtl" || (!this.props.layoutDirection || this.props.layoutDirection === "locale") && I18nManager.isRTL);
88
97
  const direction = Math.abs(delta) >= 40 ? delta < 0 ? 1 : -1 : 0;
89
98
  const next = this.state.selectedPage + (isRTL ? -direction : direction);
90
- if (direction === 0 || next < 0 || next >= React.Children.count(this.props.children)) {
99
+ if (direction === 0 || next < 0 || next >= this.pages().length) {
91
100
  this.props.onPageScrollStateChanged?.(this.nativeEvent({
92
101
  pageScrollState: "idle"
93
102
  }));
@@ -97,7 +106,7 @@ export class PagerView extends React.Component {
97
106
  };
98
107
  render() {
99
108
  const {
100
- children,
109
+ children: _children,
101
110
  style,
102
111
  orientation = "horizontal",
103
112
  pageMargin = 0,
@@ -115,7 +124,7 @@ export class PagerView extends React.Component {
115
124
  onPageScrollStateChanged: _onPageScrollStateChanged,
116
125
  ...viewProps
117
126
  } = this.props;
118
- const pages = React.Children.toArray(children);
127
+ const pages = this.pages();
119
128
  const vertical = orientation === "vertical";
120
129
  const pageExtent = pages.length > 0 ? `${100 / pages.length}%` : "100%";
121
130
  const transform = vertical ? [{
@@ -43,6 +43,7 @@ export declare class PagerView extends React.Component<PagerViewProps, State> {
43
43
  setPageWithoutAnimation: (selectedPage: number) => void;
44
44
  setScrollEnabled: (scrollEnabled: boolean) => void;
45
45
  private nativeEvent;
46
+ private pages;
46
47
  private selectPage;
47
48
  private onTouchStart;
48
49
  private onTouchEnd;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-pager-view",
3
- "version": "3.0.117",
3
+ "version": "3.0.118",
4
4
  "description": "React Native wrapper for Android and iOS ViewPager",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/module/index.js",
@@ -210,6 +210,7 @@ export class CollapsiblePagerView extends React.PureComponent<
210
210
  onMountedPagesChanged: _onMountedPagesChanged,
211
211
  onPageSelected: _onPageSelected,
212
212
  layoutDirection,
213
+ initialPage: _initialPage,
213
214
  offscreenPageLimit,
214
215
  ...nativeProps
215
216
  } = this.props;
@@ -230,6 +231,10 @@ export class CollapsiblePagerView extends React.PureComponent<
230
231
  this.nativeRef = ref;
231
232
  }}
232
233
  layoutDirection={deducedLayoutDirection}
234
+ initialPage={clampedPageIndex(
235
+ this.props.initialPage ?? 0,
236
+ pages.length
237
+ )}
233
238
  offscreenPageLimit={Math.max(1, offscreenPageLimit ?? 1)}
234
239
  headerHeight={Math.max(0, Math.round(headerHeight))}
235
240
  stickyHeaderHeight={Math.max(0, Math.round(stickyHeaderHeight))}
@@ -43,13 +43,27 @@ type State = Readonly<{
43
43
  animateTransition: boolean;
44
44
  }>;
45
45
 
46
+ const pageIndex = (value: number) => {
47
+ const index = Math.trunc(value);
48
+ return Number.isFinite(index) ? index : null;
49
+ };
50
+
51
+ const clampedPageIndex = (value: number, pageCount: number) =>
52
+ Math.min(
53
+ Math.max(0, pageIndex(value) ?? 0),
54
+ Math.max(0, pageCount - 1)
55
+ );
56
+
46
57
  /**
47
58
  * Web fallback for the ordinary PagerView API. All pages stay mounted, matching
48
59
  * the package's pre-existing PagerView lifetime contract.
49
60
  */
50
61
  export class PagerView extends React.Component<PagerViewProps, State> {
51
62
  state: State = {
52
- selectedPage: Math.max(0, Math.trunc(this.props.initialPage ?? 0)),
63
+ selectedPage: clampedPageIndex(
64
+ this.props.initialPage ?? 0,
65
+ React.Children.toArray(this.props.children).length
66
+ ),
53
67
  animateTransition: true,
54
68
  };
55
69
 
@@ -61,7 +75,7 @@ export class PagerView extends React.Component<PagerViewProps, State> {
61
75
  this.scrollEnabled = this.props.scrollEnabled ?? true;
62
76
  }
63
77
 
64
- const pageCount = React.Children.count(this.props.children);
78
+ const pageCount = this.pages().length;
65
79
  if (pageCount > 0 && this.state.selectedPage >= pageCount) {
66
80
  this.setState({
67
81
  selectedPage: pageCount - 1,
@@ -86,10 +100,15 @@ export class PagerView extends React.Component<PagerViewProps, State> {
86
100
  return { nativeEvent } as ReactNative.NativeSyntheticEvent<T>;
87
101
  }
88
102
 
103
+ private pages() {
104
+ return React.Children.toArray(this.props.children);
105
+ }
106
+
89
107
  private selectPage(selectedPage: number, animated: boolean) {
90
- const pageCount = React.Children.count(this.props.children);
91
- const position = Math.trunc(selectedPage);
108
+ const pageCount = this.pages().length;
109
+ const position = pageIndex(selectedPage);
92
110
  if (
111
+ position === null ||
93
112
  position < 0 ||
94
113
  position >= pageCount ||
95
114
  position === this.state.selectedPage
@@ -159,7 +178,7 @@ export class PagerView extends React.Component<PagerViewProps, State> {
159
178
  if (
160
179
  direction === 0 ||
161
180
  next < 0 ||
162
- next >= React.Children.count(this.props.children)
181
+ next >= this.pages().length
163
182
  ) {
164
183
  this.props.onPageScrollStateChanged?.(
165
184
  this.nativeEvent<PageScrollStateChangedNativeEventData>({
@@ -173,7 +192,7 @@ export class PagerView extends React.Component<PagerViewProps, State> {
173
192
 
174
193
  render() {
175
194
  const {
176
- children,
195
+ children: _children,
177
196
  style,
178
197
  orientation = "horizontal",
179
198
  pageMargin = 0,
@@ -191,7 +210,7 @@ export class PagerView extends React.Component<PagerViewProps, State> {
191
210
  onPageScrollStateChanged: _onPageScrollStateChanged,
192
211
  ...viewProps
193
212
  } = this.props;
194
- const pages = React.Children.toArray(children);
213
+ const pages = this.pages();
195
214
  const vertical = orientation === "vertical";
196
215
  const pageExtent = pages.length > 0 ? `${100 / pages.length}%` : "100%";
197
216
  const transform = vertical