Skip to content

Commit

Permalink
Avoid wrapping children
Browse files Browse the repository at this point in the history
This changes the API so that there must only be a single child
element under HotKeys.

No tabIndex manipulation unless used explicitly.

Deprecate the onFocus/onBlur event handlers in favor of using those
set in the child element:

  i.e. would change this:

    <HotKeys onBlur={hotKeyOnBlur}>
       <div onBlur={divOnBlur} />
    </HotKeys>

  essentially into:

    <HotKeys>
       <div onBlur={() => { divOnBlur(); hotKeyOnBlur() }}/>
    </HotKeys>
  • Loading branch information
opichals committed Aug 4, 2016
1 parent 93eb950 commit 7c476da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 6 additions & 2 deletions examples/master/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const App = React.createClass({
</ul>
</div>
<HotKeys handlers={handlers} className={'viewport ' + (this.state && this.state.konamiTime ? 'konamiTime' : '')}>
{Array.apply(null, new Array(10)).map((e, i) => <Node key={i} />)}
<div>
{Array.apply(null, new Array(10)).map((e, i) => <Node key={i} />)}
</div>
</HotKeys>
</div>
);
Expand Down Expand Up @@ -111,7 +113,9 @@ const Node = React.createClass({
// or focused programattically
return (
<HotKeys tabIndex="0" handlers={handlers} className="node" style={style}>
Node
<div>
Node
</div>
</HotKeys>
);
}
Expand Down
26 changes: 17 additions & 9 deletions lib/HotKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const HotKeys = React.createClass({

propTypes: {
children: React.PropTypes.node,
onFocus: React.PropTypes.func,
onBlur: React.PropTypes.func,
onFocus: React.PropTypes.func, //.isDeprecated('in favor of children[0].props.onFocus')
onBlur: React.PropTypes.func, //.isDeprecated('in favor of children[0].props.onBlur')
keyMap: React.PropTypes.object,
handlers: React.PropTypes.object,
focused: React.PropTypes.bool, // externally controlled focus
Expand Down Expand Up @@ -144,15 +144,23 @@ const HotKeys = React.createClass({
onFocus() {
this.__isFocused__ = true;

if (this.props.onFocus) {
const child = React.Children.only(this.props.children);
if (child.onFocus) {
child.onFocus(...arguments);
}
if (this.props.onFocus) { // isDeprecated
this.props.onFocus(...arguments);
}
},

onBlur() {
this.__isFocused__ = false;

if (this.props.onBlur) {
const child = React.Children.only(this.props.children);
if (child.onBlur) {
child.onBlur(...arguments);
}
if (this.props.onBlur) { // isDeprecated
this.props.onBlur(...arguments);
}
if (this.context.hotKeyParent) {
Expand All @@ -163,11 +171,11 @@ const HotKeys = React.createClass({
render() {
const {children, keyMap, handlers, focused, attach, ...props} = this.props;

return (
<FocusTrap {...props} onFocus={this.onFocus} onBlur={this.onBlur}>
{children}
</FocusTrap>
);
const child = React.Children.only(children);
const onFocus = this.onFocus;
const onBlur = this.onBlur;

return React.cloneElement(child, { ...props, onFocus, onBlur });
}

});
Expand Down

0 comments on commit 7c476da

Please sign in to comment.