Skip to content

Commit 250d029

Browse files
committed
Seperate and improve custom events
2 parents 853e371 + 55edb66 commit 250d029

File tree

6 files changed

+139
-131
lines changed

6 files changed

+139
-131
lines changed

Makefile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,23 @@ genStand:
2626
@$(NODE_BIN)/browserify -t babelify -t browserify-shim $(SRC)/index.js --standalone ReactTooltip | $(NODE_BIN)/uglifyjs > $(STANDALONE)/react-tooltip.min.js
2727
@cp $(DIST)/style.js $(STANDALONE)/style.js
2828

29+
devJS:
30+
@$(NODE_BIN)/watchify -t babelify $(EXAMPLE_SRC)/index.js -o $(EXAMPLE_DIST)/index.js -dv
31+
32+
devCSS:
33+
@$(NODE_BIN)/node-sass $(EXAMPLE_SRC)/index.scss $(EXAMPLE_DIST)/index.css
34+
@$(NODE_BIN)/node-sass -w $(EXAMPLE_SRC)/index.scss $(EXAMPLE_DIST)/index.css
35+
36+
devServer:
37+
@echo Listening 8888...
38+
@$(NODE_BIN)/http-server example -p 8888 -s
39+
2940
dev:
3041
@echo starting dev server...
3142
@rm -rf $(EXAMPLE_DIST)
3243
@mkdir -p $(EXAMPLE_DIST)
3344
@make convertCSS
34-
@$(NODE_BIN)/watchify -t babelify $(EXAMPLE_SRC)/index.js -o $(EXAMPLE_DIST)/index.js -dv
35-
@$(NODE_BIN)/node-sass $(EXAMPLE_SRC)/index.scss $(EXAMPLE_DIST)/index.css
36-
@$(NODE_BIN)/node-sass -w $(EXAMPLE_SRC)/index.scss $(EXAMPLE_DIST)/index.css
37-
@$(NODE_BIN)/http-server example -p 8888 -s -o
45+
@$(NODE_BIN)/concurrently --kill-others "make devJS" "make devCSS" "make devServer"
3846

3947
deployJS:
4048
@echo Generating deploy JS files...
@@ -56,4 +64,4 @@ deploy: lint
5664
@make genStand
5765
@echo success!
5866

59-
.PHONY: lint convertCSS genStand dev deployJS deployCSS deploy
67+
.PHONY: lint convertCSS genStand devJS devCSS devServer dev deployJS deployCSS deploy

example/src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React from 'react'
44
import {render} from 'react-dom'
5-
import ReactTooltip from '../../src/index'
5+
import ReactTooltip from '../../src'
66

77
const Test = React.createClass({
88

@@ -152,14 +152,23 @@ const Test = React.createClass({
152152

153153
<div className="example-jsx">
154154
<div className="side">
155-
<a data-for='customer-event' data-tip='customer event' data-event='click' data-type='info'>( •̀д•́)</a>
155+
<a data-for='customer-event' data-tip='customer show' data-event='click'>( •̀д•́)</a>
156156
<ReactTooltip id='customer-event' />
157157
</div>
158+
159+
<div className="side">
160+
<a data-for='customer-off-event' data-tip='custom show and hide' data-event='click' data-event-off='dblclick'>( •̀д•́)</a>
161+
<ReactTooltip id='customer-off-event'/>
162+
</div>
158163
</div>
159164
<br />
160165
<pre className='example-pre'>
161166
<div>
162-
<p>{"<a data-tip='customer event' data-event='click' data-type='info'>( •̀д•́)</a>\n" +
167+
<p>{"<a data-tip='customer show' data-event='click'>( •̀д•́)</a>\n" +
168+
"<ReactTooltip />"}</p>
169+
</div>
170+
<div>
171+
<p>{"<a data-tip='custom show and hide' data-event='click' data-event-off='dblclick'>( •̀д•́)</a>\n" +
163172
"<ReactTooltip/>"}</p>
164173
</div>
165174
</pre>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"chai": "^3.5.0",
6060
"chai-enzyme": "^0.5.0",
6161
"cheerio": "^0.20.0",
62+
"concurrently": "^2.1.0",
6263
"enzyme": "^2.3.0",
6364
"http-server": "^0.8.0",
6465
"jsdom": "^9.2.1",

src/decorators/customEvent.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Custom events to control showing and hiding of tooltip
3+
*
4+
* @attributes
5+
* - `event` {String}
6+
* - `eventOff` {String}
7+
*/
8+
9+
const checkStatus = function (dataEventOff, e) {
10+
const {show} = this.state
11+
const dataIsCapture = e.currentTarget.getAttribute('data-iscapture')
12+
const isCapture = dataIsCapture && dataIsCapture === 'true' || this.state.isCapture
13+
const currentItem = e.currentTarget.getAttribute('currentItem')
14+
15+
if (!isCapture) e.stopPropagation()
16+
if (show && currentItem === 'true') {
17+
if (!dataEventOff) this.hideTooltip(e)
18+
} else {
19+
e.currentTarget.setAttribute('currentItem', 'true')
20+
setUntargetItems(e.currentTarget, this.getTargetArray())
21+
this.showTooltip(e)
22+
}
23+
}
24+
25+
const setUntargetItems = function (currentTarget, targetArray) {
26+
for (let i = 0; i < targetArray.length; i++) {
27+
if (currentTarget !== targetArray[i]) {
28+
targetArray[i].setAttribute('currentItem', 'false')
29+
} else {
30+
targetArray[i].setAttribute('currentItem', 'true')
31+
}
32+
}
33+
}
34+
35+
export default function (target) {
36+
target.prototype.isCustomEvent = function (ele) {
37+
const {event} = this.state
38+
return event || ele.getAttribute('data-event')
39+
}
40+
41+
/* Bind listener for custom event */
42+
target.prototype.customBindListener = function (ele) {
43+
const {event, eventOff} = this.state
44+
const dataEvent = event || ele.getAttribute('data-event')
45+
const dataEventOff = eventOff || ele.getAttribute('data-event-off')
46+
47+
ele.removeEventListener(dataEvent, checkStatus)
48+
ele.addEventListener(dataEvent, checkStatus.bind(this, dataEventOff), false)
49+
if (dataEventOff) {
50+
ele.removeEventListener(dataEventOff, this.hideTooltip)
51+
ele.addEventListener(dataEventOff, ::this.hideTooltip, false)
52+
}
53+
}
54+
55+
/* Unbind listener for custom event */
56+
target.prototype.customUnbindListener = function (ele) {
57+
const {event, eventOff} = this.state
58+
const dataEvent = event || ele.getAttribute('data-event')
59+
const dataEventOff = eventOff || ele.getAttribute('data-event-off')
60+
61+
ele.removeEventListener(dataEvent, checkStatus)
62+
if (dataEventOff) ele.removeEventListener(dataEventOff, this.hideTooltip)
63+
}
64+
}

0 commit comments

Comments
 (0)