Skip to content

Commit 9e75e04

Browse files
committed
Origin component now accepts 'delay' prop
1 parent 0f1a8ed commit 9e75e04

File tree

7 files changed

+71
-22
lines changed

7 files changed

+71
-22
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,16 @@ npm run test:feature
150150

151151
### TODO
152152

153-
+ Options for 'delay' feature
154153
+ Options to change offsets
155154
+ Supports auto placement
156155
+ Supports custom themes
157156
+ Introduce ESLint
158157
+ API documentation using ESDoc
159158

159+
### Memo
160+
161+
+ When using delay feature, `onHover` and `onLeave` handlers should be called on triggered.
162+
160163
## Changelog
161164

162165
See the [Releases](https://github.com/kuy/redux-tooltip/releases) page on GitHub.

examples/delay/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class App extends Component {
2323
<h2>Advanced usage</h2>
2424
<p>
2525
You can specify a duration to delay hiding of a tooltip.<br />
26-
For example: <Origin delay={500} className="target">0.5 second</Origin>, <Origin delay="1000" className="target">1 second</Origin>, <Origin delay={5000.0} className="target">5 seconds</Origin>
26+
For example: <Origin delay={500} className="target">0.5 second</Origin>, <Origin delay="1000" className="target">1 second</Origin>, <Origin delay={2000.0} className="target">2 seconds</Origin>, <Origin delay="3000.0" className="target">3 seconds</Origin>
2727
</p>
2828

2929
<Tooltip>

lib/actions.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ var keep = exports.keep = (0, _reduxActions.createAction)(KEEP);
2626
var content = exports.content = (0, _reduxActions.createAction)(CONTENT);
2727
var place = exports.place = (0, _reduxActions.createAction)(PLACE);
2828

29+
var DEFAULT_DURATION = 1500;
2930
function delay(action) {
30-
var duration = arguments.length <= 1 || arguments[1] === undefined ? 1500 : arguments[1];
31+
var duration = arguments.length <= 1 || arguments[1] === undefined ? DEFAULT_DURATION : arguments[1];
3132

3233
if (!action.meta) {
3334
action.meta = {};
3435
}
35-
action.meta.delay = duration;
36+
if (duration === true) {
37+
duration = DEFAULT_DURATION;
38+
}
39+
action.meta.delay = parseInt(duration);
3640
return action;
3741
}
3842

lib/origin.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ var Origin = function (_Component) {
3434
}
3535

3636
_createClass(Origin, [{
37+
key: 'createWithDelay',
38+
value: function createWithDelay(creator) {
39+
var extras = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
40+
var delayVal = this.props.delay;
41+
42+
var action = creator(_extends({}, this.props, extras));
43+
if (delayVal) {
44+
action = (0, _actions.delay)(action, delayVal || undefined);
45+
}
46+
return action;
47+
}
48+
}, {
3749
key: 'render',
3850
value: function render() {
3951
var _this2 = this;
@@ -52,7 +64,7 @@ var Origin = function (_Component) {
5264
if (!props.onMouseLeave) {
5365
// Set default leave handler
5466
props.onMouseLeave = function (e) {
55-
_this2.props.dispatch((0, _actions.hide)(_extends({}, _this2.props)));
67+
_this2.props.dispatch(_this2.createWithDelay(_actions.hide));
5668
_this2.props.onLeave && _this2.props.onLeave(e);
5769
};
5870
}

src/actions.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ export const keep = createAction(KEEP);
1616
export const content = createAction(CONTENT);
1717
export const place = createAction(PLACE);
1818

19-
export function delay(action, duration=1500) {
19+
const DEFAULT_DURATION = 1500;
20+
export function delay(action, duration = DEFAULT_DURATION) {
2021
if (!action.meta) {
2122
action.meta = {};
2223
}
23-
action.meta.delay = duration;
24+
if (duration === true) {
25+
duration = DEFAULT_DURATION;
26+
}
27+
action.meta.delay = parseInt(duration);
2428
return action;
2529
}
2630

src/origin.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import React, { Component, PropTypes } from 'react';
22
import { connect } from 'react-redux';
3-
import { show, hide } from './actions';
3+
import { show, hide, delay } from './actions';
44

55
class Origin extends Component {
66
static get displayName() {
77
return 'Origin';
88
}
99

10+
createWithDelay(creator, extras = {}) {
11+
const { delay: delayVal } = this.props;
12+
let action = creator({ ...this.props, ...extras });
13+
if (delayVal) {
14+
action = delay(action, delayVal || undefined);
15+
}
16+
return action;
17+
}
18+
1019
render () {
1120
const props = { ...this.props };
1221
delete props['dispatch'];
@@ -22,7 +31,7 @@ class Origin extends Component {
2231
if (!props.onMouseLeave) {
2332
// Set default leave handler
2433
props.onMouseLeave = e => {
25-
this.props.dispatch(hide({ ...this.props }));
34+
this.props.dispatch(this.createWithDelay(hide));
2635
this.props.onLeave && this.props.onLeave(e);
2736
};
2837
}

tests/feature/delay.js

+30-13
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ describe('Delay Example', () => {
5151
assert(getStyleValue(tooltip, 'visibility') === 'visible');
5252

5353
// Wait more
54-
clock.tick(1200);
54+
clock.tick(1000);
5555
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden');
5656
});
5757
});
5858

5959
describe('advanced usage', () => {
60-
it('should be worked for 0.5 second', () => {
60+
it('should be worked for 0.5 second as an integer', () => {
6161
// Mouseover
62-
const origin = firstComponent(tree, Origin.WrappedComponent, { delay: true }).refs.wrapper;
62+
const origin = firstComponent(tree, Origin.WrappedComponent, { delay: 500 }).refs.wrapper;
6363
TestUtils.Simulate.mouseEnter(origin);
6464

65-
const tooltip = firstComponent(tree, Tooltip.WrappedComponent, { delay: 500 }).refs.tooltip;
65+
const tooltip = firstComponent(tree, Tooltip.WrappedComponent).refs.tooltip;
6666
assert(getStyleValue(tooltip, 'visibility') === 'visible');
6767

6868
// Mouseout
@@ -74,12 +74,12 @@ describe('Delay Example', () => {
7474
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden after 0.5 second');
7575
});
7676

77-
it('should be worked for 1 second', () => {
77+
it('should be worked for 1 second as a string', () => {
7878
// Mouseover
79-
const origin = firstComponent(tree, Origin.WrappedComponent, { delay: true }).refs.wrapper;
79+
const origin = firstComponent(tree, Origin.WrappedComponent, { delay: '1000' }).refs.wrapper;
8080
TestUtils.Simulate.mouseEnter(origin);
8181

82-
const tooltip = firstComponent(tree, Tooltip.WrappedComponent, { delay: '1000' }).refs.tooltip;
82+
const tooltip = firstComponent(tree, Tooltip.WrappedComponent).refs.tooltip;
8383
assert(getStyleValue(tooltip, 'visibility') === 'visible');
8484

8585
// Mouseout
@@ -91,21 +91,38 @@ describe('Delay Example', () => {
9191
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden after 1 second');
9292
});
9393

94-
it('should be worked for 5 seconds', () => {
94+
it('should be worked for 2 seconds as a float', () => {
9595
// Mouseover
96-
const origin = firstComponent(tree, Origin.WrappedComponent, { delay: true }).refs.wrapper;
96+
const origin = firstComponent(tree, Origin.WrappedComponent, { delay: 2000.0 }).refs.wrapper;
9797
TestUtils.Simulate.mouseEnter(origin);
9898

99-
const tooltip = firstComponent(tree, Tooltip.WrappedComponent, { delay: 5000.0 }).refs.tooltip;
99+
const tooltip = firstComponent(tree, Tooltip.WrappedComponent).refs.tooltip;
100+
assert(getStyleValue(tooltip, 'visibility') === 'visible');
101+
102+
// Mouseout
103+
TestUtils.Simulate.mouseLeave(origin);
104+
assert(getStyleValue(tooltip, 'visibility') === 'visible', 'tooltip should be kept');
105+
106+
// 2 seconds later
107+
clock.tick(2000);
108+
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden after 2 seconds');
109+
});
110+
111+
it('should be worked for 3 seconds as a string of float', () => {
112+
// Mouseover
113+
const origin = firstComponent(tree, Origin.WrappedComponent, { delay: '3000.0' }).refs.wrapper;
114+
TestUtils.Simulate.mouseEnter(origin);
115+
116+
const tooltip = firstComponent(tree, Tooltip.WrappedComponent).refs.tooltip;
100117
assert(getStyleValue(tooltip, 'visibility') === 'visible');
101118

102119
// Mouseout
103120
TestUtils.Simulate.mouseLeave(origin);
104121
assert(getStyleValue(tooltip, 'visibility') === 'visible', 'tooltip should be kept');
105122

106-
// 5 seconds later
107-
clock.tick(5000);
108-
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden after 5 seconds');
123+
// 3 seconds later
124+
clock.tick(3000);
125+
assert(getStyleValue(tooltip, 'visibility') === 'hidden', 'tooltip should be hidden after 3 seconds');
109126
});
110127
});
111128
});

0 commit comments

Comments
 (0)