Skip to content

Commit

Permalink
Add support for aria- and role props #159
Browse files Browse the repository at this point in the history
  • Loading branch information
wwayne committed Aug 3, 2016
1 parent 076461f commit d4d9fb4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const Test = React.createClass({
<div className="example-jsx">
<div className="side"><a data-tip data-for='global'> σ`∀´)σ </a></div>
<div className="side"><a data-tip data-for='global'> (〃∀〃) </a></div>
<ReactTooltip id='global'>
<ReactTooltip id='global' aria-haspopup="true" role="example">
<p>This is a global react component tooltip</p>
<p>You can put every thing here</p>
<ul>
Expand All @@ -131,7 +131,7 @@ const Test = React.createClass({
<div>
<p>{"<a data-tip data-for='global'> σ`∀´)σ </a>\n" +
"<a data-tip data-for='global'> (〃∀〃) </a>\n" +
"<ReactTooltip id='global'>\n" +
"<ReactTooltip id='global' aria-haspopup='true' role='example'>\n" +
" <p>This is a global react component tooltip</p>\n" +
" <p>You can put every thing here</p>\n" +
" <ul>\n" +
Expand Down
20 changes: 18 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import isCapture from './decorators/isCapture'
/* Utils */
import getPosition from './utils/getPosition'
import getTipContent from './utils/getTipContent'
import { parseAria } from './utils/aria'

/* CSS */
import cssStyle from './style'
Expand Down Expand Up @@ -59,7 +60,8 @@ class ReactTooltip extends Component {
event: props.event || null,
eventOff: props.eventOff || null,
currentEvent: null, // Current mouse event
currentTarget: null // Current target of mouse event
currentTarget: null, // Current target of mouse event
ariaProps: parseAria(props) // aria- and role attributes
}

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

componentWillReceiveProps (props) {
const { ariaProps } = this.state
const newAriaProps = parseAria(props)

const isChanged = Object.keys(newAriaProps).some(props => {
return newAriaProps[props] !== ariaProps[props]
})
if (isChanged) {
this.setState({ ariaProps: newAriaProps })
}
}

componentWillUnmount () {
this.mount = false

Expand Down Expand Up @@ -343,7 +357,7 @@ class ReactTooltip extends Component {
}

render () {
const {placeholder, extraClass, html} = this.state
const {placeholder, extraClass, html, ariaProps} = this.state
let tooltipClass = classname(
'__react_component_tooltip',
{'show': this.state.show},
Expand All @@ -363,12 +377,14 @@ class ReactTooltip extends Component {
if (html) {
return (
<div className={`${tooltipClass} ${extraClass}`}
{...ariaProps}
data-id='tooltip'
dangerouslySetInnerHTML={{__html: placeholder}}></div>
)
} else {
return (
<div className={`${tooltipClass} ${extraClass}`}
{...ariaProps}
data-id='tooltip'>{placeholder}</div>
)
}
Expand Down
17 changes: 17 additions & 0 deletions src/utils/aria.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Support aria- and role in ReactTooltip
*
* @params props {Object}
* @return {Object}
*/
export function parseAria (props) {
let ariaObj = {}
Object.keys(props).filter(prop => {
// aria-xxx and role is acceptable
return /(^aria-\w+$|^role$)/.test(prop)
}).forEach(prop => {
ariaObj[prop] = props[prop]
})

return ariaObj
}

0 comments on commit d4d9fb4

Please sign in to comment.