Skip to content

Commit e13cb9e

Browse files
committed
Now available 'both' for 'delayOn' props, closes #9
1 parent 60d9c42 commit e13cb9e

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

examples/delay/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class App extends Component {
2525
<h2>Delay on show</h2>
2626
<p>
2727
Hovering on an <Origin delay delayOn="show" className="target">origin</Origin>, but it is'n shown immediately.<br />
28-
You need to stay a while on it.
28+
You need to stay a while on it. Delay on <Origin delay delayOn="both" className="target">both</Origin>.
2929
</p>
3030

3131
<Tooltip>

src/middleware.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { startTimeout, endTimeout, DELAY } from './actions';
1+
import { SHOW, HIDE, DELAY, startTimeout, endTimeout } from './actions';
22
import { resolve } from './utils';
33

44
function getToken(state, name) {
@@ -9,14 +9,18 @@ function getToken(state, name) {
99
}
1010
}
1111

12+
const CANCEL_TYPES = [SHOW, HIDE];
13+
1214
export default function middleware(store) {
1315
return next => action => {
14-
// Clear timeout
1516
const names = resolve(action);
16-
names.forEach(name => {
17-
const token = getToken(store.getState(), name);
18-
token && clearTimeout(token);
19-
});
17+
if (CANCEL_TYPES.indexOf(action.type) !== -1) {
18+
// Clear timeout
19+
names.forEach(name => {
20+
const token = getToken(store.getState(), name);
21+
token && clearTimeout(token);
22+
});
23+
}
2024

2125
if (!action.meta || !action.meta[DELAY]) {
2226
return next(action);

src/origin.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Origin extends Component {
2323
PropTypes.number,
2424
PropTypes.string
2525
]),
26-
delayOn: PropTypes.oneOf(['show', 'hide']),
26+
delayOn: PropTypes.oneOf(['show', 'hide', 'both']),
2727
onMouseEnter: PropTypes.func,
2828
onMouseLeave: PropTypes.func,
2929
};
@@ -51,7 +51,7 @@ class Origin extends Component {
5151
if (!props.onMouseEnter) {
5252
// Set default hover handler
5353
props.onMouseEnter = e => {
54-
const action = this.props.delayOn === 'show'
54+
const action = ['show', 'both'].indexOf(this.props.delayOn) !== -1
5555
? this.createWithDelay(show, { el: e.target })
5656
: show({ ...this.props, el: e.target });
5757
this.props.dispatch(action);
@@ -62,7 +62,7 @@ class Origin extends Component {
6262
if (!props.onMouseLeave) {
6363
// Set default leave handler
6464
props.onMouseLeave = e => {
65-
const action = this.props.delayOn === 'hide'
65+
const action = ['hide', 'both'].indexOf(this.props.delayOn) !== -1
6666
? this.createWithDelay(hide)
6767
: hide({ ...this.props });
6868
this.props.dispatch(action);

tests/feature/delay.js

+49
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,54 @@ describe('Delay Example', () => {
158158
TestUtils.Simulate.mouseLeave(origin);
159159
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden immediately');
160160
});
161+
162+
it('should be worked on both', () => {
163+
// Mouseover
164+
const origin = firstComponent(tree, Origin.WrappedComponent, { delayOn: 'both' }).refs.wrapper;
165+
TestUtils.Simulate.mouseEnter(origin);
166+
167+
const tooltip = firstComponent(tree, Tooltip.WrappedComponent).refs.tooltip;
168+
assert(getStyleValue(tooltip, 'visibility') === 'hidden');
169+
170+
// 0.5 second later
171+
clock.tick(500);
172+
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden');
173+
174+
// Mouseout before showing
175+
TestUtils.Simulate.mouseLeave(origin);
176+
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden');
177+
178+
// Wait 2 seconds
179+
clock.tick(2000);
180+
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip is still hidden');
181+
182+
// Mouseover again
183+
TestUtils.Simulate.mouseEnter(origin);
184+
185+
// 1.5 seconds later
186+
clock.tick(1500);
187+
assert(getStyleValue(tooltip, 'visibility') === 'visible', 'tooltip should be shown');
188+
189+
// Mouseout
190+
TestUtils.Simulate.mouseLeave(origin);
191+
192+
// 0.5 second later
193+
clock.tick(500);
194+
assert(getStyleValue(tooltip, 'visibility') === 'visible', 'tooltip is still shown');
195+
196+
// Mouseover again
197+
TestUtils.Simulate.mouseEnter(origin);
198+
199+
// Wait 2 seconds
200+
clock.tick(2000);
201+
assert(getStyleValue(tooltip, 'visibility') === 'visible', 'tooltip is still shown');
202+
203+
// Mouseout
204+
TestUtils.Simulate.mouseLeave(origin);
205+
206+
// 1.5 seconds later
207+
clock.tick(1500);
208+
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden');
209+
});
161210
});
162211
});

0 commit comments

Comments
 (0)