Skip to content

Commit 2d368b9

Browse files
authored
fix(Transition): schedule changes only on status change (#4060)
1 parent 859146f commit 2d368b9

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/modules/Transition/Transition.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ export default class Transition extends Component {
8787
const durationType = TRANSITION_CALLBACK_TYPE[nextStatus]
8888
const durationValue = normalizeTransitionDuration(duration, durationType)
8989

90-
clearTimeout(this.timeoutId)
91-
this.timeoutId = setTimeout(
92-
() => this.setState((state) => ({ status: state.nextStatus })),
93-
durationValue,
94-
)
90+
this.timeoutId = setTimeout(() => this.setState({ status: nextStatus }), durationValue)
9591
}
9692

9793
updateStatus = (prevState) => {
98-
if (this.state.status !== this.state.nextStatus && this.state.nextStatus) {
99-
this.handleStart(this.state.nextStatus)
94+
if (prevState.status !== this.state.status) {
95+
// Timeout should be cleared in any case as previous can lead set to unexpected `nextStatus`
96+
clearTimeout(this.timeoutId)
97+
98+
if (this.state.nextStatus) {
99+
this.handleStart(this.state.nextStatus)
100+
}
100101
}
101102

102103
if (!prevState.animating && this.state.animating) {

test/specs/modules/Transition/Transition-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,27 @@ describe('Transition', () => {
419419
</Transition>,
420420
)
421421
})
422+
423+
it('is called after a render with visibility changes', (done) => {
424+
// This test ensures that a setTimeout will not be cleared on a simple rerender
425+
// https://github.com/Semantic-Org/Semantic-UI-React/issues/4059
426+
427+
const onComplete = sandbox.spy()
428+
429+
wrapperMount(
430+
<Transition duration={200} onComplete={onComplete} transitionOnMount>
431+
<p />
432+
</Transition>,
433+
)
434+
435+
setTimeout(() => {
436+
wrapper.setProps({})
437+
}, 100)
438+
setTimeout(() => {
439+
onComplete.should.have.been.calledOnce()
440+
done()
441+
}, 250)
442+
})
422443
})
423444

424445
describe('onHide', () => {

0 commit comments

Comments
 (0)