Skip to content

Commit 3a4cf9c

Browse files
dimensisilvenon
authored andcommitted
Make exit timeout optional (#464)
* fix bug from issue #460 * Update src/Transition.js Co-Authored-By: dimensi <eddimensi@gmail.com> * fix grammar and remove required from exit, enter props * rewrite test * little update
1 parent 7e6785b commit 3a4cf9c

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/Transition.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,18 @@ class Transition extends React.Component {
325325
onTransitionEnd(node, timeout, handler) {
326326
this.setNextCallback(handler)
327327

328-
if (node) {
329-
if (this.props.addEndListener) {
330-
this.props.addEndListener(node, this.nextCallback)
331-
}
332-
if (timeout != null) {
333-
setTimeout(this.nextCallback, timeout)
334-
}
335-
} else {
328+
const doesNotHaveTimeoutOrListener = timeout == null && !this.props.addEndListener
329+
if (!node || doesNotHaveTimeoutOrListener) {
336330
setTimeout(this.nextCallback, 0)
331+
return
332+
}
333+
334+
if (this.props.addEndListener) {
335+
this.props.addEndListener(node, this.nextCallback)
336+
}
337+
338+
if (timeout != null) {
339+
setTimeout(this.nextCallback, timeout)
337340
}
338341
}
339342

@@ -442,9 +445,11 @@ Transition.propTypes = {
442445
* }}
443446
* ```
444447
*
445-
* If the value of appear is not set, then the value from enter is taken.
448+
* If the value of `appear` is not set, then the value from enter is taken.
449+
*
450+
* If the `enter` or `exit` value is `null` or `undefined`, then the timer is set to `0`
446451
*
447-
* @type {number | { enter?: number, exit?: number }}
452+
* @type {number | { enter?: number, exit?: number, appear?: number }}
448453
*/
449454
timeout: (props, ...args) => {
450455
let pt = timeoutsShape

test/Transition-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ describe('Transition', () => {
131131
inst.setProps({ in: true })
132132
})
133133

134+
it('should mount/unmount immediately if not have enter/exit timeout', (done) => {
135+
const wrapper = mount(
136+
<Transition in={true} timeout={{}}>
137+
<div />
138+
</Transition>
139+
)
140+
141+
expect(wrapper.state('status')).toEqual(ENTERED)
142+
let calledAfterTimeout = false
143+
setTimeout(() => {
144+
calledAfterTimeout = true
145+
}, 10)
146+
wrapper.setProps({
147+
in: false,
148+
onExited() {
149+
expect(wrapper.state('status')).toEqual(EXITED)
150+
if (!calledAfterTimeout) {
151+
return done()
152+
}
153+
throw new Error('wrong timeout')
154+
}
155+
})
156+
})
157+
134158
describe('appearing timeout', () => {
135159
it('should use enter timeout if appear not set', done => {
136160
let calledBeforeEntered = false

0 commit comments

Comments
 (0)