Skip to content

Commit d09e9ae

Browse files
committed
fix Switch thumb transition bug
1 parent 4c7a473 commit d09e9ae

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

lib/mdl/Switch.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,19 @@ class Switch extends Component {
205205
}
206206

207207
componentWillMount() {
208-
this.setState({ checked: this.props.checked });
209-
this._layoutThumb(this.props);
208+
const nextState = this._layoutThumb(this.props.checked,
209+
this.props.thumbRadius,
210+
this.props.trackLength,
211+
this.props.trackSize);
212+
this.setState(Object.assign(nextState, { checked: this.props.checked }));
210213
}
211214

212215
componentWillReceiveProps(nextProps) {
213-
if (nextProps.checked !== this.props.checked) {
214-
this.setState({ checked: nextProps.checked });
215-
}
216-
this._layoutThumb(nextProps);
216+
const nextState = this._layoutThumb(nextProps.checked,
217+
nextProps.thumbRadius,
218+
nextProps.trackLength,
219+
nextProps.trackSize);
220+
this.setState(Object.assign(nextState, { checked: nextProps.checked }));
217221
}
218222

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

251255
// Layout the thumb according to the size of the track
252-
_layoutThumb({ trackLength, trackSize }) {
256+
_layoutThumb(checked, thumbRadius, trackLength, trackSize) {
253257
const trackRadii = trackSize / 2;
254-
const thumbRadii = this.props.thumbRadius;
258+
const thumbRadii = thumbRadius;
255259
const rippleRadii = trackLength - trackSize;
256260
const trackMargin = rippleRadii - trackRadii; // make room for ripple
257-
const thumbLeft = this.state.checked ? trackMargin + trackRadii : 0;
261+
const thumbLeft = checked ? trackMargin + trackRadii : 0;
258262
this._animatedThumbLeft.setValue(thumbLeft);
259-
this.setState({
263+
264+
return {
260265
trackSize,
261266
trackLength,
262267
trackRadii,
@@ -267,7 +272,7 @@ class Switch extends Component {
267272
r: thumbRadii,
268273
padding: rippleRadii - thumbRadii,
269274
},
270-
});
275+
};
271276
}
272277

273278
// Move the thumb left or right according to the current state

0 commit comments

Comments
 (0)