Skip to content

Commit

Permalink
fix Switch thumb transition bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xinthink committed Oct 17, 2016
1 parent 4c7a473 commit d09e9ae
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/mdl/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,19 @@ class Switch extends Component {
}

componentWillMount() {
this.setState({ checked: this.props.checked });
this._layoutThumb(this.props);
const nextState = this._layoutThumb(this.props.checked,
this.props.thumbRadius,
this.props.trackLength,
this.props.trackSize);
this.setState(Object.assign(nextState, { checked: this.props.checked }));
}

componentWillReceiveProps(nextProps) {
if (nextProps.checked !== this.props.checked) {
this.setState({ checked: nextProps.checked });
}
this._layoutThumb(nextProps);
const nextState = this._layoutThumb(nextProps.checked,
nextProps.thumbRadius,
nextProps.trackLength,
nextProps.trackSize);
this.setState(Object.assign(nextState, { checked: nextProps.checked }));
}

// Un-boxing the `Thumb` node from `AnimatedComponent`,
Expand Down Expand Up @@ -249,14 +253,15 @@ class Switch extends Component {
// property initializers end

// Layout the thumb according to the size of the track
_layoutThumb({ trackLength, trackSize }) {
_layoutThumb(checked, thumbRadius, trackLength, trackSize) {
const trackRadii = trackSize / 2;
const thumbRadii = this.props.thumbRadius;
const thumbRadii = thumbRadius;
const rippleRadii = trackLength - trackSize;
const trackMargin = rippleRadii - trackRadii; // make room for ripple
const thumbLeft = this.state.checked ? trackMargin + trackRadii : 0;
const thumbLeft = checked ? trackMargin + trackRadii : 0;
this._animatedThumbLeft.setValue(thumbLeft);
this.setState({

return {
trackSize,
trackLength,
trackRadii,
Expand All @@ -267,7 +272,7 @@ class Switch extends Component {
r: thumbRadii,
padding: rippleRadii - thumbRadii,
},
});
};
}

// Move the thumb left or right according to the current state
Expand Down

0 comments on commit d09e9ae

Please sign in to comment.