@@ -2,7 +2,7 @@ import React, { Component } from 'react';
22import PropTypes from 'prop-types' ;
33import classNames from '../../utils/classnames' ;
44
5- class PickerGroup extends Component {
5+ class PickerColumn extends Component {
66 static propTypes = {
77 height : PropTypes . number ,
88 itemHeight : PropTypes . number ,
@@ -31,14 +31,22 @@ class PickerGroup extends Component {
3131 super ( props ) ;
3232
3333 this . state = {
34- touching : false ,
35- ogY : 0 ,
36- ogTranslate : 0 ,
37- touchId : undefined ,
38- translate : 0 ,
39- totalHeight : 0 ,
40- selected : 0 ,
41- animating : this . props . animation
34+ translate : 0 ,
35+ selectedIndex : 0 ,
36+ animating : props . animation ,
37+ touching : false ,
38+ ogY : 0 ,
39+ ogTranslate : 0 ,
40+ touchId : undefined ,
41+ totalHeight : 0 ,
42+ } ;
43+
44+ this . cache = {
45+ touching : false ,
46+ ogY : 0 ,
47+ ogTranslate : 0 ,
48+ touchId : undefined ,
49+ totalHeight : 0 ,
4250 } ;
4351
4452 this . handleTouchStart = this . handleTouchStart . bind ( this ) ;
@@ -55,58 +63,55 @@ class PickerGroup extends Component {
5563 this . adjustPosition ( nextProps ) ;
5664 }
5765
58- adjustPosition ( props ) {
59- const { items, itemHeight, indicatorTop, defaultIndex } = props ;
66+ adjustPosition ( props ) {
67+ const { items, defaultIndex , itemHeight, indicatorTop, indicatorHeight } = props ;
6068 const totalHeight = items . length * itemHeight ;
61- let translate = totalHeight <= indicatorTop ? indicatorTop : 0 ;
69+ let { translate } = this . state ;
6270
6371 if ( defaultIndex > - 1 ) {
64- if ( translate === 0 ) {
65- let upperCount = Math . floor ( indicatorTop / itemHeight ) ;
66- if ( defaultIndex > upperCount ) {
67- //over
68- let overCount = defaultIndex - upperCount ;
69- translate -= overCount * itemHeight ;
70- } else if ( defaultIndex === upperCount ) {
71- translate = 0 ;
72- } else {
73- //less
74- translate += ( Math . abs ( upperCount - defaultIndex ) * itemHeight ) ;
75- }
76- //if(props.groupIndex == 2) console.log(defaultIndex,translate, upperCount)
77- } else {
78- //total item less than indicator height
79- translate -= itemHeight * defaultIndex ;
80- }
72+ translate = indicatorTop - itemHeight * defaultIndex ;
8173 }
8274
83- this . setState ( { selected : defaultIndex , ogTranslate : translate , totalHeight, translate,
84- } , ( ) => defaultIndex > - 1 ? this . updateSelected ( false ) : this . updateSelected ( ) ) ;
75+ this . setState ( {
76+ selectedIndex : defaultIndex ,
77+ ogTranslate : translate ,
78+ totalHeight,
79+ translate,
80+ } , ( ) => { if ( defaultIndex <= - 1 ) this . updateSelected ( ) } ) ;
8581 }
8682
87- updateSelected ( propagate = true ) {
88- const { items, itemHeight, indicatorTop, indicatorHeight, onChange, groupIndex } = this . props ;
89- let selected = 0 ;
90- items . forEach ( ( item , i ) => {
91- //console.log(i, this.state.translate, (this.state.translate + (itemHeight * i)), indicatorTop, this.state.translate + (itemHeight * i) + itemHeight , indicatorTop + indicatorHeight)
92- if ( ! item . disabled && ( this . state . translate + ( itemHeight * i ) ) >= indicatorTop &&
93- ( this . state . translate + ( itemHeight * i ) + itemHeight ) <= indicatorTop + indicatorHeight ) {
94- selected = i ;
83+ adjustSelectedIndex ( ) {
84+ const { items, itemHeight, indicatorTop, indicatorHeight } = this . props ;
85+ const { translate } = this . state ;
86+ let selectedIndex = 0 ;
87+
88+ for ( let i = 0 ; i < items . length ; i ++ ) {
89+ if ( ! items [ i ] . disabled && ( itemHeight * i + translate ) >= indicatorTop
90+ && ( ( i + 1 ) * itemHeight + translate ) <= indicatorTop + indicatorHeight ) {
91+ selectedIndex = i ;
92+ break ;
9593 }
96- } ) ;
94+ }
9795
98- if ( onChange && propagate ) onChange ( items [ selected ] , selected , groupIndex ) ;
96+ return selectedIndex ;
97+ }
98+
99+ updateSelected ( ) {
100+ const { items, onChange, groupIndex } = this . props
101+ const selectedIndex = this . adjustSelectedIndex ( )
102+ if ( onChange ) {
103+ onChange ( items [ selectedIndex ] , selectedIndex , groupIndex ) ;
104+ }
99105 }
100106
101107 handleTouchStart ( e ) {
102108 if ( this . state . touching || this . props . items . length <= 1 ) return ;
103-
104109 this . setState ( {
105- touching : true ,
106- ogTranslate : this . state . translate ,
107- touchId : e . targetTouches [ 0 ] . identifier ,
108- ogY : this . state . translate === 0 ? e . targetTouches [ 0 ] . pageY : e . targetTouches [ 0 ] . pageY - this . state . translate ,
109- animating : false
110+ touching : true ,
111+ ogTranslate : this . state . translate ,
112+ touchId : e . targetTouches [ 0 ] . identifier ,
113+ ogY : e . targetTouches [ 0 ] . pageY - this . state . translate ,
114+ animating : false
110115 } ) ;
111116 }
112117
@@ -197,4 +202,4 @@ class PickerGroup extends Component {
197202 }
198203}
199204
200- export default PickerGroup ;
205+ export default PickerColumn ;
0 commit comments