Skip to content

Commit 8adb152

Browse files
authored
Merge pull request #172 from wwayne/aria
Add support for aria- and role props #159
2 parents 076461f + d4d9fb4 commit 8adb152

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

example/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const Test = React.createClass({
117117
<div className="example-jsx">
118118
<div className="side"><a data-tip data-for='global'> σ`∀´)σ </a></div>
119119
<div className="side"><a data-tip data-for='global'> (〃∀〃) </a></div>
120-
<ReactTooltip id='global'>
120+
<ReactTooltip id='global' aria-haspopup="true" role="example">
121121
<p>This is a global react component tooltip</p>
122122
<p>You can put every thing here</p>
123123
<ul>
@@ -131,7 +131,7 @@ const Test = React.createClass({
131131
<div>
132132
<p>{"<a data-tip data-for='global'> σ`∀´)σ </a>\n" +
133133
"<a data-tip data-for='global'> (〃∀〃) </a>\n" +
134-
"<ReactTooltip id='global'>\n" +
134+
"<ReactTooltip id='global' aria-haspopup='true' role='example'>\n" +
135135
" <p>This is a global react component tooltip</p>\n" +
136136
" <p>You can put every thing here</p>\n" +
137137
" <ul>\n" +

src/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import isCapture from './decorators/isCapture'
1313
/* Utils */
1414
import getPosition from './utils/getPosition'
1515
import getTipContent from './utils/getTipContent'
16+
import { parseAria } from './utils/aria'
1617

1718
/* CSS */
1819
import cssStyle from './style'
@@ -59,7 +60,8 @@ class ReactTooltip extends Component {
5960
event: props.event || null,
6061
eventOff: props.eventOff || null,
6162
currentEvent: null, // Current mouse event
62-
currentTarget: null // Current target of mouse event
63+
currentTarget: null, // Current target of mouse event
64+
ariaProps: parseAria(props) // aria- and role attributes
6365
}
6466

6567
this.bind([
@@ -91,6 +93,18 @@ class ReactTooltip extends Component {
9193
this.bindWindowEvents() // Bind global event for static method
9294
}
9395

96+
componentWillReceiveProps (props) {
97+
const { ariaProps } = this.state
98+
const newAriaProps = parseAria(props)
99+
100+
const isChanged = Object.keys(newAriaProps).some(props => {
101+
return newAriaProps[props] !== ariaProps[props]
102+
})
103+
if (isChanged) {
104+
this.setState({ ariaProps: newAriaProps })
105+
}
106+
}
107+
94108
componentWillUnmount () {
95109
this.mount = false
96110

@@ -343,7 +357,7 @@ class ReactTooltip extends Component {
343357
}
344358

345359
render () {
346-
const {placeholder, extraClass, html} = this.state
360+
const {placeholder, extraClass, html, ariaProps} = this.state
347361
let tooltipClass = classname(
348362
'__react_component_tooltip',
349363
{'show': this.state.show},
@@ -363,12 +377,14 @@ class ReactTooltip extends Component {
363377
if (html) {
364378
return (
365379
<div className={`${tooltipClass} ${extraClass}`}
380+
{...ariaProps}
366381
data-id='tooltip'
367382
dangerouslySetInnerHTML={{__html: placeholder}}></div>
368383
)
369384
} else {
370385
return (
371386
<div className={`${tooltipClass} ${extraClass}`}
387+
{...ariaProps}
372388
data-id='tooltip'>{placeholder}</div>
373389
)
374390
}

src/utils/aria.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Support aria- and role in ReactTooltip
3+
*
4+
* @params props {Object}
5+
* @return {Object}
6+
*/
7+
export function parseAria (props) {
8+
let ariaObj = {}
9+
Object.keys(props).filter(prop => {
10+
// aria-xxx and role is acceptable
11+
return /(^aria-\w+$|^role$)/.test(prop)
12+
}).forEach(prop => {
13+
ariaObj[prop] = props[prop]
14+
})
15+
16+
return ariaObj
17+
}

0 commit comments

Comments
 (0)