@codeleap/mobile 3.15.8 → 3.16.0
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.
- package/package.json +1 -1
- package/src/components/Pager/index.tsx +19 -81
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
AnyFunction,
|
|
3
2
|
ComponentVariants,
|
|
4
3
|
onUpdate,
|
|
4
|
+
PropsOf,
|
|
5
5
|
TypeGuards,
|
|
6
6
|
useDefaultComponentStyle,
|
|
7
7
|
useWarning,
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
StyleSheet,
|
|
17
17
|
} from 'react-native'
|
|
18
18
|
import { StylesOf } from '../../types/utility'
|
|
19
|
+
import { ScrollProps } from '../Scroll'
|
|
19
20
|
import { View } from '../View'
|
|
20
21
|
import { PagerPresets, PagerComposition } from './styles'
|
|
21
22
|
export * from './styles'
|
|
@@ -28,9 +29,8 @@ export type PageProps = {
|
|
|
28
29
|
page: number
|
|
29
30
|
index: number
|
|
30
31
|
isPrevious: boolean
|
|
31
|
-
}
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
}
|
|
34
34
|
|
|
35
35
|
export type PagerProps = React.PropsWithChildren<{
|
|
36
36
|
variants?: ComponentVariants<typeof PagerPresets>['variants']
|
|
@@ -43,14 +43,9 @@ export type PagerProps = React.PropsWithChildren<{
|
|
|
43
43
|
renderPageWrapper?: React.FC<PageProps>
|
|
44
44
|
pageWrapperProps?: any
|
|
45
45
|
width?: number
|
|
46
|
-
onScroll
|
|
46
|
+
onScroll: ScrollProps['onScroll']
|
|
47
47
|
/** If TRUE render page, nextPage and prevPage only */
|
|
48
48
|
windowing?: boolean
|
|
49
|
-
scrollRightEnabled?: boolean
|
|
50
|
-
scrollLeftEnabled?: boolean
|
|
51
|
-
scrollDirectionThrottle?: number
|
|
52
|
-
onSwipeLastPage?: (event: ScrollEvent) => void
|
|
53
|
-
waitEventDispatchTimeout?: number
|
|
54
49
|
} & ScrollViewProps>
|
|
55
50
|
|
|
56
51
|
const defaultProps: Partial<PagerProps> = {
|
|
@@ -59,12 +54,8 @@ const defaultProps: Partial<PagerProps> = {
|
|
|
59
54
|
page: 0,
|
|
60
55
|
returnEarly: true,
|
|
61
56
|
windowing: false,
|
|
62
|
-
keyboardShouldPersistTaps: 'handled',
|
|
63
57
|
scrollEnabled: true,
|
|
64
|
-
|
|
65
|
-
scrollLeftEnabled: true,
|
|
66
|
-
scrollDirectionThrottle: 650,
|
|
67
|
-
waitEventDispatchTimeout: 250,
|
|
58
|
+
keyboardShouldPersistTaps: 'handled',
|
|
68
59
|
}
|
|
69
60
|
|
|
70
61
|
export const Pager = (pagerProps: PagerProps) => {
|
|
@@ -81,12 +72,6 @@ export const Pager = (pagerProps: PagerProps) => {
|
|
|
81
72
|
windowing = false,
|
|
82
73
|
setPage,
|
|
83
74
|
scrollEnabled = true,
|
|
84
|
-
scrollLeftEnabled,
|
|
85
|
-
scrollRightEnabled,
|
|
86
|
-
onScroll,
|
|
87
|
-
scrollDirectionThrottle,
|
|
88
|
-
onSwipeLastPage,
|
|
89
|
-
waitEventDispatchTimeout,
|
|
90
75
|
} = {
|
|
91
76
|
...defaultProps,
|
|
92
77
|
...pagerProps,
|
|
@@ -95,10 +80,6 @@ export const Pager = (pagerProps: PagerProps) => {
|
|
|
95
80
|
const childArr = React.Children.toArray(children)
|
|
96
81
|
const scrollRef = useRef<ScrollView>(null)
|
|
97
82
|
const [positionX, setPositionX] = React.useState(0)
|
|
98
|
-
const [scrollPositionX, setScrollPositionX] = React.useState(0)
|
|
99
|
-
const [_scrollEnabled, setScrollEnabled] = React.useState(true)
|
|
100
|
-
|
|
101
|
-
const waitEventDispatch = useRef(false)
|
|
102
83
|
|
|
103
84
|
const variantStyles = useDefaultComponentStyle<'u:Pager', typeof PagerPresets>(
|
|
104
85
|
'u:Pager',
|
|
@@ -116,6 +97,7 @@ export const Pager = (pagerProps: PagerProps) => {
|
|
|
116
97
|
|
|
117
98
|
if (!validWidth) {
|
|
118
99
|
width = windowWidth
|
|
100
|
+
|
|
119
101
|
}
|
|
120
102
|
|
|
121
103
|
useWarning(
|
|
@@ -130,62 +112,18 @@ export const Pager = (pagerProps: PagerProps) => {
|
|
|
130
112
|
|
|
131
113
|
const WrapperComponent = renderPageWrapper || View
|
|
132
114
|
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
waitEventDispatch.current = true
|
|
139
|
-
|
|
140
|
-
const x = event?.nativeEvent.contentOffset.x
|
|
141
|
-
const toPage = Math.floor(((Math.round(x)) / Math.round(width)))
|
|
142
|
-
|
|
143
|
-
const length = childArr.length - 1
|
|
144
|
-
|
|
145
|
-
if (toPage >= length && TypeGuards.isFunction(onSwipeLastPage) && page >= length) {
|
|
146
|
-
onSwipeLastPage?.(event)
|
|
147
|
-
} else if (toPage !== page && toPage <= length) {
|
|
148
|
-
setPage(toPage)
|
|
149
|
-
setPositionX(toPage * width)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
setTimeout(() => {
|
|
153
|
-
waitEventDispatch.current = false
|
|
154
|
-
}, waitEventDispatchTimeout)
|
|
155
|
-
}, [childArr, page, setPage, waitEventDispatch.current])
|
|
156
|
-
|
|
157
|
-
const handleScroll = (event: ScrollEvent) => {
|
|
158
|
-
const scrollX = event?.nativeEvent?.contentOffset?.x
|
|
115
|
+
const handleScrollEnd = useCallback(
|
|
116
|
+
({ nativeEvent }: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
117
|
+
const x = nativeEvent.contentOffset.x
|
|
118
|
+
const toPage = Math.ceil(x / width)
|
|
159
119
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const isRight = scrollX < scrollPositionX
|
|
166
|
-
const isLeft = scrollX > scrollPositionX
|
|
167
|
-
|
|
168
|
-
if (TypeGuards.isFunction(onScroll)) onScroll?.(event, { isLeft, isRight, x: scrollX })
|
|
169
|
-
|
|
170
|
-
if (hasScrollDirectionDisabled) {
|
|
171
|
-
if (isRight && !scrollRightEnabled || isLeft && !scrollLeftEnabled) {
|
|
172
|
-
setScrollEnabled(false)
|
|
173
|
-
|
|
174
|
-
setTimeout(() => {
|
|
175
|
-
scrollRef.current.scrollTo({
|
|
176
|
-
x: positionX,
|
|
177
|
-
animated: true,
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
setTimeout(() => {
|
|
181
|
-
setScrollEnabled(true)
|
|
182
|
-
}, scrollDirectionThrottle)
|
|
183
|
-
})
|
|
120
|
+
if (toPage !== page && toPage <= childArr.length - 1 && !!scrollEnabled) {
|
|
121
|
+
setPage(toPage)
|
|
122
|
+
setPositionX(toPage * width)
|
|
184
123
|
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
124
|
+
},
|
|
125
|
+
[childArr, page, setPage],
|
|
126
|
+
)
|
|
189
127
|
|
|
190
128
|
onUpdate(() => {
|
|
191
129
|
const x = width * page
|
|
@@ -204,12 +142,12 @@ export const Pager = (pagerProps: PagerProps) => {
|
|
|
204
142
|
horizontal
|
|
205
143
|
pagingEnabled
|
|
206
144
|
onMomentumScrollEnd={handleScrollEnd}
|
|
207
|
-
scrollEventThrottle={
|
|
145
|
+
scrollEventThrottle={300}
|
|
208
146
|
showsHorizontalScrollIndicator={false}
|
|
209
147
|
style={[variantStyles.wrapper, style]}
|
|
210
148
|
{...pagerProps}
|
|
211
|
-
|
|
212
|
-
|
|
149
|
+
scrollEnabled={childArr.length > 1 && scrollEnabled}
|
|
150
|
+
|
|
213
151
|
>
|
|
214
152
|
{childArr.map((child: PagerProps['children'][number], index) => {
|
|
215
153
|
|