1+ /**
2+ * 依赖引入
3+ */
14import React , { Component , PropTypes } from 'react' ;
25import {
36 View ,
47 Text ,
5- StyleSheet ,
6- Dimensions ,
7- PixelRatio ,
8- PanResponder ,
9- TouchableOpacity ,
10- TextInput ,
11- Animated ,
12- Modal
8+ PanResponder
139} from 'react-native' ;
10+ import { rollStyles } from './style' ;
1411
15-
12+ /**
13+ * 组件扩展
14+ */
1615class Pickroll extends Component {
1716
17+ /**
18+ * 类型约束
19+ * @type {{data: PropTypes.array, passData: PropTypes.array, visible: PropTypes.bool, onValueChange: PropTypes.func, selectedValue: PropTypes.array, selectIndex: PropTypes.array} }
20+ */
1821 static propTypes = {
19- inputValue : PropTypes . string || PropTypes . number ,
22+ //所有数据
23+ data : PropTypes . object ,
24+ //单轮的数据
25+ passData : PropTypes . array ,
26+ //modal是否可见
2027 visible : PropTypes . bool ,
21- inputStyle : PropTypes . any ,
28+ //滚轮的值变化后的回调函数
2229 onValueChange : PropTypes . func ,
23- selectedValue : PropTypes . any
30+ //选择的值的位置
31+ selectIndex : PropTypes . number ,
32+ //整个picker的样式
33+ pickerStyle : View . propTypes . style ,
34+ //单轮每个格子的样式
35+ itemAndroidStyle : Text . propTypes . style
2436 } ;
2537
38+ /**
39+ * 构造函数
40+ * @param props {object} 继承的属性
41+ * @param context 上下文
42+ */
2643 constructor ( props , context ) {
2744 super ( props , context ) ;
2845 this . state = this . _stateFromProps ( props ) ;
2946 }
3047
31- componentWillReceiveProps ( newProps ) {
32-
33- }
34-
48+ /**
49+ * 状态初始化
50+ * @param props {object} 继承的属性
51+ * @returns {{selectedIndex: number, items: Array, pickerStyle:View.propTypes.style, itemStyle:View.propTypes.style, onValueChange: func} }
52+ * @private
53+ */
3554 _stateFromProps ( props ) {
3655 let selectedIndex = this . props . selectIndex ;
3756 let items = [ ] ;
@@ -41,69 +60,95 @@ class Pickroll extends Component {
4160 Object . keys ( props . data ) . map ( ( child , index ) => {
4261 child === props . selectedValue && ( selectedIndex = index ) ;
4362 items . push ( { value : child , label : props . data [ child ] . name } ) ;
44- } )
63+ } ) ;
4564 return {
4665 selectedIndex,
4766 items,
4867 pickerStyle,
4968 itemStyle,
50- onValueChange,
69+ onValueChange
5170 } ;
5271 }
5372
73+ /**
74+ * 根据移动的距离重新布局
75+ * @param dy {number} 移动的距离
76+ * @private
77+ */
5478 _move ( dy ) {
5579 let index = this . index ;
5680 this . middleHeight = Math . abs ( - index * 40 + dy ) ;
5781 this . up && this . up . setNativeProps ( {
5882 style : {
59- marginTop : ( 3 - index ) * 30 + dy * .75 ,
60- } ,
83+ marginTop : ( 3 - index ) * 30 + dy * 0 .75
84+ }
6185 } ) ;
6286 this . middle && this . middle . setNativeProps ( {
6387 style : {
64- marginTop : - index * 40 + dy ,
65- } ,
88+ marginTop : - index * 40 + dy
89+ }
6690 } ) ;
6791 this . down && this . down . setNativeProps ( {
6892 style : {
69- marginTop : ( - index - 1 ) * 30 + dy * .75 ,
70- } ,
93+ marginTop : ( - index - 1 ) * 30 + dy * 0 .75
94+ }
7195 } ) ;
7296 }
7397
98+ /**
99+ * 根据移动到的位置计算移动的距离
100+ * @param index {number} 移动到的位置
101+ * @returns {string }
102+ * @private
103+ */
74104 _moveTo ( index ) {
75105 let _index = this . index ;
76106 let diff = _index - index ;
77107 let marginValue ;
78- let that = this ;
79- if ( diff && ! this . isMoving ) {
108+ if ( diff && ! this . isMoving ) {
80109 marginValue = diff * 40 ;
81110 this . _move ( marginValue ) ;
82111 this . index = index ;
83112 this . _onValueChange ( ) ;
84- } else { return " you are moving" }
113+ } else { return ' you are moving' ; }
85114 }
86115
116+ /**
117+ * 处理移动(RN自带的函数)
118+ * @param evt
119+ * @param gestureState {object}
120+ * @returns {string }
121+ * @private
122+ */
87123 _handlePanResponderMove ( evt , gestureState ) {
88124 let dy = gestureState . dy ;
89- if ( this . isMoving ) {
90- return " you are moving" ;
125+ if ( this . isMoving ) {
126+ return ' you are moving' ;
91127 }
92- // turn down
93- if ( dy > 0 ) {
128+
129+ if ( dy > 0 ) {
94130 this . _move ( dy > this . index * 40 ? this . index * 40 : dy ) ;
95- } else {
131+ } else {
96132 this . _move ( dy < ( this . index - this . state . items . length + 1 ) * 40 ? ( this . index - this . state . items . length + 1 ) * 40 : dy ) ;
97133 }
98134 }
99135
136+ /**
137+ * 处理移动停止(RN自带的函数)
138+ * @param evt
139+ * @param gestureState {object}
140+ * @private
141+ */
100142 _handlePanResponderRelease ( evt , gestureState ) {
101143 let middleHeight = this . middleHeight ;
102144 this . index = middleHeight % 40 >= 20 ? Math . ceil ( middleHeight / 40 ) : Math . floor ( middleHeight / 40 ) ;
103145 this . _move ( 0 ) ;
104146 this . _onValueChange ( ) ;
105147 }
106148
149+ /**
150+ * 组件需要渲染前,对手势事件处理进行初始化
151+ */
107152 componentWillMount ( ) {
108153 this . _panResponder = PanResponder . create ( {
109154 onMoveShouldSetPanResponder : ( evt , gestureState ) => true ,
@@ -115,72 +160,86 @@ class Pickroll extends Component {
115160 this . index = this . state . selectedIndex ;
116161 }
117162
118-
163+ /**
164+ * 对单轮的每个格子进行初始化
165+ * @param items {array} 需要渲染的总的数据
166+ * @returns {{upItems: Array, middleItems: Array, downItems: Array} }
167+ * @private
168+ */
119169 _renderItems ( items ) {
120170 let upItems = [ ] , middleItems = [ ] , downItems = [ ] ;
121171 items . forEach ( ( item , index ) => {
122172 upItems [ index ] = < Text
123- key = { 'up' + index }
124- className = { 'up' + index }
125- style = { [ styles . upText , this . state . itemStyle ] }
173+ key = { 'up' + index }
174+ className = { 'up' + index }
175+ style = { [ rollStyles . upText , this . state . itemStyle ] }
126176 onPress = { ( ) => { this . _moveTo ( index ) ; } } >
127177 { item . label }
128178 </ Text > ;
129179
130180 middleItems [ index ] = < Text
131- key = { 'mid' + index }
132- className = { 'mid' + index }
133- style = { [ styles . middleText , this . state . itemStyle ] } > { item . label }
181+ key = { 'mid' + index }
182+ className = { 'mid' + index }
183+ style = { [ rollStyles . middleText , this . state . itemStyle ] } > { item . label }
134184 </ Text > ;
135185 downItems [ index ] = < Text
136- key = { 'down' + index }
137- className = { 'down' + index }
138- style = { [ styles . downText , this . state . itemStyle ] }
186+ key = { 'down' + index }
187+ className = { 'down' + index }
188+ style = { [ rollStyles . downText , this . state . itemStyle ] }
139189 onPress = { ( ) => { this . _moveTo ( index ) ; } } >
140190 { item . label }
141- </ Text >
191+ </ Text > ;
142192 } ) ;
143- return { upItems, middleItems, downItems, } ;
193+ return { upItems, middleItems, downItems } ;
144194 }
145195
196+ /**
197+ * 单轮上的数值变化后
198+ * @returns {object }
199+ * @private
200+ */
146201 _onValueChange ( ) {
147202 var curItem = this . state . items [ this . index ] ;
148203 this . setState ( { selectedIndex :this . index } ) ;
149204 this . state . onValueChange && this . state . onValueChange ( curItem . value , this . index ) ;
150205 }
151206
207+ /**
208+ * 渲染函数
209+ * @returns {XML }
210+ */
152211 render ( ) {
153212 let index = this . state . selectedIndex ;
154213 let length = this . state . items . length ;
155214 let items = this . _renderItems ( this . state . items ) ;
156215
157216 let upViewStyle = {
158217 marginTop : ( 3 - index ) * 30 ,
159- height : length * 30 ,
218+ height : length * 30
160219 } ;
161220 let middleViewStyle = {
162- marginTop : - index * 40 ,
221+ marginTop : - index * 40
163222 } ;
164223 let downViewStyle = {
165224 marginTop : ( - index - 1 ) * 30 ,
166- height : length * 30 ,
225+ height : length * 30
167226 } ;
168227
169228 return (
170229
171- < View style = { [ styles . container , this . state . pickerStyle ] } { ...this . _panResponder . panHandlers } >
172- < View style = { styles . up } >
173- < View style = { [ styles . upView , upViewStyle ] } ref = { ( up ) => { this . up = up } } >
230+ < View style = { [ rollStyles . container , this . state . pickerStyle ] } { ...this . _panResponder . panHandlers } >
231+ < View style = { rollStyles . up } >
232+ < View style = { [ rollStyles . upView , upViewStyle ] } ref = { ( up ) => { this . up = up } } >
174233 { items . upItems }
175234 </ View >
176235 </ View >
177- < View style = { styles . middle } >
178- < View style = { [ styles . middleView , middleViewStyle ] } ref = { ( middle ) => { this . middle = middle } } >
236+ < View style = { rollStyles . middle } >
237+ < View style = { [ rollStyles . middleView , middleViewStyle ] } ref = { ( middle ) => { this . middle = middle } } >
179238 { items . middleItems }
180239 </ View >
181240 </ View >
182- < View style = { styles . down } >
183- < View style = { [ styles . downView , downViewStyle ] } ref = { ( down ) => { this . down = down } } >
241+ < View style = { rollStyles . down } >
242+ < View style = { [ rollStyles . downView , downViewStyle ] } ref = { ( down ) => { this . down = down } } >
184243 { items . downItems }
185244 </ View >
186245 </ View >
@@ -189,79 +248,4 @@ class Pickroll extends Component {
189248 }
190249}
191250
192- let height = Dimensions . get ( 'window' ) . height ;
193- let ratio = PixelRatio . get ( ) ;
194-
195- let styles = StyleSheet . create ( {
196-
197- container : {
198- flex :1 ,
199- justifyContent : 'center' ,
200- alignItems : 'center' ,
201- } ,
202- up : {
203- height : 90 ,
204- overflow : 'hidden' ,
205- alignSelf :'stretch' ,
206- } ,
207- upView : {
208- justifyContent : 'flex-start' ,
209- alignItems : 'center'
210- } ,
211- upText : {
212- paddingTop : 0 ,
213- height : 30 ,
214- fontSize : 20 ,
215- color : '#000' ,
216- opacity : .5 ,
217- paddingBottom : 0 ,
218- marginTop : 0 ,
219- marginBottom : 0
220- } ,
221- middle : {
222- alignSelf :'stretch' ,
223- height : 40 ,
224- overflow : 'hidden' ,
225- borderColor : '#aaa' ,
226- borderTopWidth : 1 / ratio ,
227- borderBottomWidth : 1 / ratio ,
228- } ,
229- middleView : {
230- height : 40 ,
231- justifyContent : 'flex-start' ,
232- alignItems : 'center' ,
233- } ,
234- middleText : {
235- paddingTop : 0 ,
236- height : 40 ,
237- color : '#000' ,
238- fontSize : 28 ,
239- paddingBottom : 0 ,
240- marginTop : 0 ,
241- marginBottom : 0
242- } ,
243- down : {
244- height : 90 ,
245- overflow : 'hidden' ,
246- alignSelf :'stretch' ,
247- } ,
248- downView : {
249- overflow : 'hidden' ,
250- justifyContent : 'flex-start' ,
251- alignItems : 'center'
252- } ,
253- downText : {
254- paddingTop : 0 ,
255- height : 30 ,
256- fontSize : 16 ,
257- color : '#000' ,
258- opacity : .5 ,
259- paddingBottom : 0 ,
260- marginTop : 0 ,
261- marginBottom : 0
262- }
263- } ) ;
264-
265-
266-
267251export default Pickroll ;
0 commit comments