|
| 1 | +// Wheel.js |
| 2 | +//问题2:不支持受控,对于月份-日期不合法时有问题,如3月31日换到2月 |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import React, {Component} from "react"; |
| 7 | +import PropTypes from 'prop-types'; |
| 8 | +import {StyleSheet, View, Text, Animated, PanResponder} from 'react-native'; |
| 9 | + |
| 10 | +import Theme from 'teaset/themes/Theme'; |
| 11 | +import WheelItem from './WheelItem'; |
| 12 | + |
| 13 | +export default class Wheel extends Component { |
| 14 | + |
| 15 | + static propTypes = { |
| 16 | + ...View.propTypes, |
| 17 | + items: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.element, PropTypes.string, PropTypes.number])).isRequired, |
| 18 | + itemStyle: Text.propTypes.style, |
| 19 | + holeStyle: View.propTypes.style, //height is required |
| 20 | + maskStyle: View.propTypes.style, |
| 21 | + index: PropTypes.number, |
| 22 | + defaultIndex: PropTypes.number, |
| 23 | + onChange: PropTypes.func, //(index) |
| 24 | + }; |
| 25 | + |
| 26 | + static defaultProps = { |
| 27 | + ...View.defaultProps, |
| 28 | + pointerEvents: 'box-only', |
| 29 | + defaultIndex: 0, |
| 30 | + }; |
| 31 | + |
| 32 | + static Item = WheelItem; |
| 33 | + static preRenderCount = 10; |
| 34 | + |
| 35 | + constructor(props) { |
| 36 | + super(props); |
| 37 | + this.createPanResponder(); |
| 38 | + this.prevTouches = []; |
| 39 | + this.index = props.index || props.index === 0 ? props.index : props.defaultIndex; |
| 40 | + this.lastRenderIndex = this.index; |
| 41 | + this.height = 0; |
| 42 | + this.holeHeight = 0; |
| 43 | + this.hiddenOffset = 0; |
| 44 | + this.currentPosition = new Animated.Value(0); |
| 45 | + this.targetPositionValue = null; |
| 46 | + } |
| 47 | + |
| 48 | + componentWillMount() { |
| 49 | + if (!this.positionListenerId) { |
| 50 | + this.positionListenerId = this.currentPosition.addListener(e => this.handlePositionChange(e.value)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + componentWillUnmount() { |
| 55 | + if (this.positionListenerId) { |
| 56 | + this.currentPosition.removeListener(this.positionListenerId); |
| 57 | + this.positionListenerId = null; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + componentWillReceiveProps(nextProps) { |
| 62 | + if (nextProps.index || nextProps.index === 0) { |
| 63 | + this.index = nextProps.index; |
| 64 | + this.currentPosition.setValue(nextProps.index * this.holeHeight); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + createPanResponder() { |
| 69 | + this.panResponder = PanResponder.create({ |
| 70 | + onStartShouldSetPanResponder: (e, gestureState) => true, |
| 71 | + onStartShouldSetPanResponderCapture: (e, gestureState) => false, |
| 72 | + onMoveShouldSetPanResponder: (e, gestureState) => true, |
| 73 | + onMoveShouldSetPanResponderCapture: (e, gestureState) => false, |
| 74 | + onPanResponderGrant: (e, gestureState) => this.onPanResponderGrant(e, gestureState), |
| 75 | + onPanResponderMove: (e, gestureState) => this.onPanResponderMove(e, gestureState), |
| 76 | + onPanResponderTerminationRequest: (e, gestureState) => true, |
| 77 | + onPanResponderRelease: (e, gestureState) => this.onPanResponderRelease(e, gestureState), |
| 78 | + onPanResponderTerminate: (e, gestureState) => null, |
| 79 | + onShouldBlockNativeResponder: (e, gestureState) => true, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + onPanResponderGrant(e, gestureState) { |
| 84 | + this.currentPosition.stopAnimation(); |
| 85 | + this.prevTouches = e.nativeEvent.touches; |
| 86 | + this.speed = 0; |
| 87 | + } |
| 88 | + |
| 89 | + onPanResponderMove(e, gestureState) { |
| 90 | + let {touches} = e.nativeEvent; |
| 91 | + let prevTouches = this.prevTouches; |
| 92 | + this.prevTouches = touches; |
| 93 | + |
| 94 | + if (touches.length != 1 || touches[0].identifier != prevTouches[0].identifier) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + let dy = touches[0].pageY - prevTouches[0].pageY; |
| 99 | + let pos = this.currentPosition._value - dy; |
| 100 | + this.currentPosition.setValue(pos); |
| 101 | + |
| 102 | + let t = touches[0].timestamp - prevTouches[0].timestamp; |
| 103 | + if (t) this.speed = dy / t; |
| 104 | + } |
| 105 | + |
| 106 | + onPanResponderRelease(e, gestureState) { |
| 107 | + this.prevTouches = []; |
| 108 | + if (Math.abs(this.speed) > 0.1) this.handleSwipeScroll(); |
| 109 | + else this.handleStopScroll(); |
| 110 | + } |
| 111 | + |
| 112 | + handlePositionChange(value) { |
| 113 | + let newIndex = Math.round(value / this.holeHeight); |
| 114 | + if (newIndex != this.index && newIndex >= 0 && newIndex < this.props.items.length) { |
| 115 | + let moveCount = Math.abs(newIndex - this.lastRenderIndex); |
| 116 | + this.index = newIndex; |
| 117 | + if (moveCount > this.constructor.preRenderCount) { |
| 118 | + this.forceUpdate(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // let the animation stop faster |
| 123 | + if (this.targetPositionValue != null && Math.abs(this.targetPositionValue - value) <= 2) { |
| 124 | + this.targetPositionValue = null; |
| 125 | + this.currentPosition.stopAnimation(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + handleSwipeScroll() { |
| 130 | + let {items} = this.props; |
| 131 | + |
| 132 | + let inertiaPos = this.currentPosition._value - this.speed * 300; |
| 133 | + let newIndex = Math.round(inertiaPos / this.holeHeight); |
| 134 | + if (newIndex < 0) newIndex = 0; |
| 135 | + else if (newIndex > items.length - 1) newIndex = items.length - 1; |
| 136 | + |
| 137 | + let toValue = newIndex * this.holeHeight; |
| 138 | + this.targetPositionValue = toValue; |
| 139 | + Animated.spring(this.currentPosition, { |
| 140 | + toValue: toValue, |
| 141 | + friction: 9, |
| 142 | + }).start(() => { |
| 143 | + this.currentPosition.setValue(toValue); |
| 144 | + this.props.onChange && this.props.onChange(newIndex); |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + handleStopScroll() { |
| 149 | + let toValue = this.index * this.holeHeight; |
| 150 | + this.targetPositionValue = toValue; |
| 151 | + Animated.spring(this.currentPosition, { |
| 152 | + toValue: toValue, |
| 153 | + friction: 9, |
| 154 | + }).start(() => { |
| 155 | + this.currentPosition.setValue(toValue); |
| 156 | + this.props.onChange && this.props.onChange(this.index); |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + handleLayout(height, holeHeight) { |
| 161 | + this.height = height; |
| 162 | + this.holeHeight = holeHeight; |
| 163 | + if (holeHeight) { |
| 164 | + let maskHeight = (height - holeHeight) / 2; |
| 165 | + this.hiddenOffset = Math.ceil(maskHeight / holeHeight) + this.constructor.preRenderCount; |
| 166 | + } |
| 167 | + this.forceUpdate(() => this.currentPosition.setValue(this.index * holeHeight)); |
| 168 | + } |
| 169 | + |
| 170 | + onLayout(e) { |
| 171 | + this.handleLayout(e.nativeEvent.layout.height, this.holeHeight); |
| 172 | + this.props.onLayout && this.props.onLayout(e); |
| 173 | + } |
| 174 | + |
| 175 | + onHoleLayout(e) { |
| 176 | + this.handleLayout(this.height, e.nativeEvent.layout.height); |
| 177 | + } |
| 178 | + |
| 179 | + buildProps() { |
| 180 | + let {style, items, itemStyle, holeStyle, maskStyle, ...others} = this.props; |
| 181 | + |
| 182 | + style = [{ |
| 183 | + backgroundColor: Theme.wheelColor, |
| 184 | + overflow: 'hidden', |
| 185 | + }].concat(style); |
| 186 | + itemStyle = [{ |
| 187 | + backgroundColor: 'rgba(0, 0, 0, 0)', |
| 188 | + fontSize: Theme.wheelFontSize, |
| 189 | + color: Theme.wheelTextColor, |
| 190 | + }].concat(itemStyle); |
| 191 | + holeStyle = [{ |
| 192 | + backgroundColor: 'rgba(0, 0, 0, 0)', |
| 193 | + height: Theme.wheelHoleHeight, |
| 194 | + borderColor: Theme.wheelHoleLineColor, |
| 195 | + borderTopWidth: Theme.wheelHoleLineWidth, |
| 196 | + borderBottomWidth: Theme.wheelHoleLineWidth, |
| 197 | + zIndex: 1, |
| 198 | + }].concat(holeStyle); |
| 199 | + maskStyle = [{ |
| 200 | + backgroundColor: Theme.wheelMaskColor, |
| 201 | + opacity: Theme.wheelMaskOpacity, |
| 202 | + flex: 1, |
| 203 | + zIndex: 100, |
| 204 | + }].concat(maskStyle); |
| 205 | + |
| 206 | + this.props = {style, items, itemStyle, holeStyle, maskStyle, ...others}; |
| 207 | + } |
| 208 | + |
| 209 | + renderItem(item, itemIndex) { |
| 210 | + let {itemStyle} = this.props; |
| 211 | + |
| 212 | + if (Math.abs(this.index - itemIndex) > this.hiddenOffset) return null; |
| 213 | + |
| 214 | + if (typeof item === 'string' || typeof item === 'number') { |
| 215 | + item = <Text style={itemStyle}>{item}</Text>; |
| 216 | + } |
| 217 | + |
| 218 | + return ( |
| 219 | + <this.constructor.Item |
| 220 | + itemHeight={this.holeHeight} |
| 221 | + wheelHeight={this.height} |
| 222 | + index={itemIndex} |
| 223 | + currentPosition={this.currentPosition} |
| 224 | + key={itemIndex} |
| 225 | + > |
| 226 | + {item} |
| 227 | + </this.constructor.Item> |
| 228 | + ); |
| 229 | + } |
| 230 | + |
| 231 | + render() { |
| 232 | + this.buildProps(); |
| 233 | + this.lastRenderIndex = this.index; |
| 234 | + |
| 235 | + let {items, itemStyle, holeStyle, maskStyle, defaultIndex, onChange, onLayout, ...others} = this.props; |
| 236 | + |
| 237 | + return ( |
| 238 | + <View |
| 239 | + {...others} |
| 240 | + onLayout={e => this.onLayout(e)} |
| 241 | + {...this.panResponder.panHandlers} |
| 242 | + > |
| 243 | + {items.map((item, index) => this.renderItem(item, index))} |
| 244 | + <View style={maskStyle} /> |
| 245 | + <View style={holeStyle} onLayout={e => this.onHoleLayout(e)} /> |
| 246 | + <View style={maskStyle} /> |
| 247 | + </View> |
| 248 | + ) |
| 249 | + } |
| 250 | + |
| 251 | +} |
| 252 | + |
0 commit comments