Skip to content

Commit

Permalink
优化picker_group里面props改变或者初始化时获取translate和slected,更改select为selectedIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ruoru committed Mar 22, 2017
1 parent 01bc1c5 commit b58a43d
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions src/components/picker/picker_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PickerGroup extends Component {
touchId: undefined,
translate: 0,
totalHeight: 0,
selected: 0,
selectedIndex: 0,
animating: this.props.animation
};

Expand All @@ -54,51 +54,45 @@ class PickerGroup extends Component {
this.adjustPosition(nextProps);
}

adjustPosition(props){
const { items, itemHeight, indicatorTop, defaultIndex } = props;
adjustPosition(props) {
const { items, defaultIndex, itemHeight, indicatorTop, indicatorHeight } = props;
const totalHeight = items.length * itemHeight;
let translate = totalHeight <= indicatorTop ? indicatorTop : 0;
let { translate } = this.state;

if (defaultIndex > -1) {
if (translate === 0){
let upperCount = Math.floor(indicatorTop / itemHeight);
if ( defaultIndex > upperCount ){
//over
let overCount = defaultIndex - upperCount;
translate -= overCount * itemHeight;
} else if ( defaultIndex === upperCount){
translate = 0;
} else {
//less
translate += ( Math.abs(upperCount - defaultIndex) * itemHeight);
}
//if(props.groupIndex == 2) console.log(defaultIndex,translate, upperCount)
} else {
//total item less than indicator height
translate -= itemHeight * defaultIndex;
}
translate = indicatorTop - itemHeight * defaultIndex;
}

this.setState({
selected: defaultIndex,
ogTranslate: translate,
totalHeight,
translate,
}, () => defaultIndex > -1 ? this.updateSelected(false) : this.updateSelected() );
this.setState({
selectedIndex : defaultIndex,
ogTranslate : translate,
totalHeight,
translate,
}, () => {if (defaultIndex <= -1) this.updateSelected()});
}

updateSelected(propagate = true){
const { items, itemHeight, indicatorTop, indicatorHeight, onChange, groupIndex } = this.props;
let selected = 0;
items.forEach( (item, i) => {
//console.log(i, this.state.translate, (this.state.translate + (itemHeight * i)), indicatorTop, this.state.translate + (itemHeight * i) + itemHeight , indicatorTop + indicatorHeight)
if ( !item.disabled && (this.state.translate + (itemHeight * i)) >= indicatorTop &&
( this.state.translate + (itemHeight * i) + itemHeight ) <= indicatorTop + indicatorHeight ){
selected = i;
adjustSelectedIndex() {
const { items, itemHeight, indicatorTop, indicatorHeight } = this.props;
const { translate } = this.state;
let selectedIndex = 0;

for (let i = 0; i < items.length; i++) {
if (!items[i].disabled && (itemHeight * i + translate) >= indicatorTop
&& ((i + 1) * itemHeight + translate) <= indicatorTop + indicatorHeight){
selectedIndex = i;
break;
}
});
}

return selectedIndex;
}

if (onChange && propagate) onChange(items[selected], selected, groupIndex);
updateSelected() {
const { items, onChange, groupIndex } = this.props
const selectedIndex = this.adjustSelectedIndex()
if (onChange) {
onChange(items[selectedIndex], selectedIndex, groupIndex);
}
}

handleTouchStart(e){
Expand Down

0 comments on commit b58a43d

Please sign in to comment.