|
| 1 | +import * as React from 'react'; |
| 2 | +import reactMixin from 'react-mixin'; |
| 3 | +import ReactTimerMixin from 'react-timer-mixin'; |
| 4 | +import Pagination from '../pagination'; |
| 5 | +import { |
| 6 | + View, |
| 7 | + ScrollView, |
| 8 | + ViewPagerAndroid, |
| 9 | + Platform, |
| 10 | + Dimensions, |
| 11 | + ViewStyle, |
| 12 | + ActivityIndicator, |
| 13 | +} from 'react-native'; |
| 14 | +import styles from './style'; |
| 15 | + |
| 16 | +let { width, height } = Dimensions.get('window'); |
| 17 | + |
| 18 | +export interface ViewPagerProps { |
| 19 | + selectedIndex: number; |
| 20 | + bounces?: boolean; |
| 21 | + children?: any; |
| 22 | + style?: any; |
| 23 | + dots?: boolean; |
| 24 | + autoplay?: boolean; |
| 25 | + autoplayTimeout?: number; |
| 26 | + infinite?: boolean; |
| 27 | + onScrollBeginDrag: Function; |
| 28 | + onMomentumScrollEnd: Function; |
| 29 | + loadMinimal?: boolean; |
| 30 | + loadMinimalSize?: number; |
| 31 | +} |
| 32 | + |
| 33 | +export default class ViewPager extends React.Component<ViewPagerProps, any> { |
| 34 | + static defaultProps = { |
| 35 | + bounces: true, |
| 36 | + infinite: true, |
| 37 | + dots: true, |
| 38 | + loadMinimal: false, |
| 39 | + loadMinimalSize: 1, |
| 40 | + autoplay: false, |
| 41 | + autoplayTimeout: 2.5, |
| 42 | + selectedIndex: 0, |
| 43 | + }; |
| 44 | + autoplayTimer: any; |
| 45 | + setTimeout: any; |
| 46 | + |
| 47 | + constructor(props) { |
| 48 | + super(props); |
| 49 | + this.state = this.initState(this.props); |
| 50 | + |
| 51 | + this.onScrollEnd = this.onScrollEnd.bind(this); |
| 52 | + this.onScrollBegin = this.onScrollBegin.bind(this); |
| 53 | + this.onScrollEndDrag = this.onScrollEndDrag.bind(this); |
| 54 | + } |
| 55 | + |
| 56 | + componentDidMount() { |
| 57 | + this.autoplay(); |
| 58 | + } |
| 59 | + |
| 60 | + initState(props) { |
| 61 | + // set the current state |
| 62 | + // const state = this.state || {}; |
| 63 | + const count = props.children ? props.children.length || 1 : 0; |
| 64 | + width = props.width || width; |
| 65 | + height = props.height || height; |
| 66 | + const selectedIndex = count > 1 ? Math.min(props.selectedIndex, count - 1) : 0; |
| 67 | + |
| 68 | + let initState = { |
| 69 | + width, |
| 70 | + height, |
| 71 | + isScrolling: false, |
| 72 | + autoplayEnd: false, |
| 73 | + loopJump: false, |
| 74 | + count, |
| 75 | + selectedIndex, |
| 76 | + offset: { |
| 77 | + x: width * (selectedIndex + (props.infinite ? 1 : 0)), |
| 78 | + y: 0, |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + return initState; |
| 83 | + } |
| 84 | + |
| 85 | + loopJump() { |
| 86 | + if (this.state.loopJump) { |
| 87 | + const index = this.state.selectedIndex + (this.props.infinite ? 1 : 0); |
| 88 | + setTimeout(() => (this.refs as any).scrollview.setPageWithoutAnimation |
| 89 | + && (this.refs as any).scrollview.setPageWithoutAnimation(index), 50); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + autoplay() { |
| 94 | + if ( |
| 95 | + !Array.isArray(this.props.children) |
| 96 | + || !this.props.autoplay |
| 97 | + || this.state.isScrolling |
| 98 | + || this.state.autoplayEnd |
| 99 | + ) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + clearTimeout(this.autoplayTimer); |
| 104 | + |
| 105 | + this.autoplayTimer = this.setTimeout(() => { |
| 106 | + if (!this.props.infinite && ( this.state.selectedIndex === this.state.count - 1)) { |
| 107 | + return this.setState({ autoplayEnd: true }); |
| 108 | + } |
| 109 | + this.scrollNextPage(); |
| 110 | + }, this.props.autoplayTimeout * 1000); |
| 111 | + } |
| 112 | + |
| 113 | + onScrollBegin(e) { |
| 114 | + this.setState({ isScrolling: true }); |
| 115 | + |
| 116 | + this.setTimeout(() => { |
| 117 | + if (this.props.onScrollBeginDrag) { |
| 118 | + this.props.onScrollBeginDrag(e, this.state, this); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + onScrollEnd(e) { |
| 124 | + this.setState({isScrolling: false}); |
| 125 | + |
| 126 | + // android incompatible |
| 127 | + if (!e.nativeEvent.contentOffset) { |
| 128 | + e.nativeEvent.contentOffset = {x: e.nativeEvent.position * this.state.width}; |
| 129 | + } |
| 130 | + |
| 131 | + this.updateIndex(e.nativeEvent.contentOffset); |
| 132 | + |
| 133 | + this.setTimeout(() => { |
| 134 | + this.autoplay(); |
| 135 | + this.loopJump(); |
| 136 | + if (this.props.onMomentumScrollEnd) { |
| 137 | + this.props.onMomentumScrollEnd(e, this.state, this); |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + onScrollEndDrag(e) { |
| 143 | + const { offset, selectedIndex, count } = this.state; |
| 144 | + const previousOffset = offset.x; |
| 145 | + const newOffset = e.nativeEvent.x; |
| 146 | + |
| 147 | + if (previousOffset === newOffset && (selectedIndex === 0 || selectedIndex === count - 1)) { |
| 148 | + this.setState({ |
| 149 | + isScrolling: false, |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + updateIndex(offset) { |
| 155 | + let state = this.state; |
| 156 | + let selectedIndex = state.selectedIndex; |
| 157 | + let diff = offset.x - state.offset.x; |
| 158 | + let step = state.width; |
| 159 | + let loopJump = false; |
| 160 | + |
| 161 | + // Do nothing if offset no change. |
| 162 | + if (!diff) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + selectedIndex = parseInt(selectedIndex + Math.round(diff / step), 10); |
| 167 | + |
| 168 | + if (this.props.infinite) { |
| 169 | + if (selectedIndex <= -1) { |
| 170 | + selectedIndex = state.count - 1; |
| 171 | + offset.x = step * state.count; |
| 172 | + loopJump = true; |
| 173 | + } else if (selectedIndex >= state.count) { |
| 174 | + selectedIndex = 0; |
| 175 | + offset.x = step; |
| 176 | + loopJump = true; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + this.setState({ |
| 181 | + selectedIndex, |
| 182 | + offset, |
| 183 | + loopJump: loopJump, |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + scrollNextPage() { |
| 188 | + if (this.state.isScrolling || this.state.count < 2) { |
| 189 | + return; |
| 190 | + } |
| 191 | + let state = this.state; |
| 192 | + let diff = (this.props.infinite ? 1 : 0) + this.state.selectedIndex + 1; |
| 193 | + let x = 0; |
| 194 | + let y = 0; |
| 195 | + x = diff * state.width; |
| 196 | + |
| 197 | + if (Platform.OS === 'android') { |
| 198 | + (this.refs as any).scrollview.setPage(diff); |
| 199 | + } else { |
| 200 | + (this.refs as any).scrollview.scrollTo({ x, y }); |
| 201 | + } |
| 202 | + |
| 203 | + this.setState({ |
| 204 | + isScrolling: true, |
| 205 | + autoplayEnd: false, |
| 206 | + }); |
| 207 | + |
| 208 | + // trigger onScrollEnd manually in android |
| 209 | + if (Platform.OS === 'android') { |
| 210 | + this.setTimeout(() => { |
| 211 | + this.onScrollEnd({ |
| 212 | + nativeEvent: { |
| 213 | + position: diff, |
| 214 | + }, |
| 215 | + }); |
| 216 | + }, 0); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + renderContent(pages) { |
| 221 | + if (Platform.OS === 'ios') { |
| 222 | + return ( |
| 223 | + <ScrollView ref="scrollview" |
| 224 | + {...this.props} |
| 225 | + horizontal={true} |
| 226 | + pagingEnabled={true} |
| 227 | + bounces={!!this.props.bounces} |
| 228 | + scrollEventThrottle={100} |
| 229 | + removeClippedSubviews={true} |
| 230 | + automaticallyAdjustContentInsets={false} |
| 231 | + directionalLockEnabled={true} |
| 232 | + showsHorizontalScrollIndicator={false} |
| 233 | + showsVerticalScrollIndicator={false} |
| 234 | + contentContainerStyle={[styles.wrapper, this.props.style]} |
| 235 | + contentOffset={this.state.offset} |
| 236 | + onScrollBeginDrag={this.onScrollBegin} |
| 237 | + onMomentumScrollEnd={this.onScrollEnd} |
| 238 | + onScrollEndDrag={this.onScrollEndDrag}> |
| 239 | + {pages} |
| 240 | + </ScrollView> |
| 241 | + ); |
| 242 | + } else { |
| 243 | + return ( |
| 244 | + <ViewPagerAndroid |
| 245 | + {...this.props} |
| 246 | + ref="scrollview" |
| 247 | + initialPage={this.props.infinite ? this.state.selectedIndex + 1 : this.state.selectedIndex} |
| 248 | + onPageSelected={this.onScrollEnd} |
| 249 | + style={{flex: 1}} |
| 250 | + > |
| 251 | + {pages} |
| 252 | + </ViewPagerAndroid> |
| 253 | + ); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + renderDots(index) { |
| 258 | + return ( |
| 259 | + <Pagination |
| 260 | + style={styles.pagination} |
| 261 | + current={index} |
| 262 | + mode="pointer" |
| 263 | + total={5} |
| 264 | + size="small" |
| 265 | + /> |
| 266 | + ); |
| 267 | + } |
| 268 | + |
| 269 | + render() { |
| 270 | + let state = this.state; |
| 271 | + let props = this.props; |
| 272 | + let children = props.children; |
| 273 | + let selectedIndex = state.selectedIndex; |
| 274 | + let count = state.count; |
| 275 | + let infinite = props.infinite; |
| 276 | + // let key = 0; |
| 277 | + let loopVal = infinite ? 1 : 0; |
| 278 | + |
| 279 | + let pages: any = []; |
| 280 | + |
| 281 | + let pageStyle = [{width: state.width, height: state.height}, styles.slide]; |
| 282 | + let pageStyleLoading = { |
| 283 | + width: this.state.width, |
| 284 | + height: this.state.height, |
| 285 | + justifyContent: 'center', |
| 286 | + alignItems: 'center', |
| 287 | + } as ViewStyle; |
| 288 | + |
| 289 | + // For make infinite at least count > 1 |
| 290 | + if (count > 1) { |
| 291 | + pages = Object.keys(children); |
| 292 | + if (infinite) { |
| 293 | + pages.unshift(count - 1 + ''); |
| 294 | + pages.push('0'); |
| 295 | + } |
| 296 | + |
| 297 | + pages = pages.map((page, i) => { |
| 298 | + if (props.loadMinimal) { |
| 299 | + if (i >= (selectedIndex + loopVal - props.loadMinimalSize) |
| 300 | + && i <= (selectedIndex + loopVal + props.loadMinimalSize) |
| 301 | + ) { |
| 302 | + return (<View style={pageStyle} key={i}>{children[page]}</View>); |
| 303 | + } else { |
| 304 | + return (<View style={pageStyleLoading} key={`loading-${i}`}><ActivityIndicator /></View>); |
| 305 | + } |
| 306 | + } else { |
| 307 | + return (<View style={pageStyle} key={i}>{children[page]}</View>); |
| 308 | + } |
| 309 | + }); |
| 310 | + } else { |
| 311 | + pages = (<View style={pageStyle}>{children}</View>); |
| 312 | + } |
| 313 | + |
| 314 | + return ( |
| 315 | + <View style={[styles.container, { |
| 316 | + width: state.width, |
| 317 | + height: state.height, |
| 318 | + }]}> |
| 319 | + {this.renderContent(pages)} |
| 320 | + {props.dots && this.renderDots(this.state.selectedIndex)} |
| 321 | + </View> |
| 322 | + ); |
| 323 | + } |
| 324 | +} |
| 325 | + |
| 326 | +reactMixin(ViewPager.prototype, ReactTimerMixin); |
0 commit comments