Skip to content

Commit

Permalink
Avoid contents wrapping
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.
  • Loading branch information
opichals committed Aug 4, 2016
1 parent 93eb950 commit c977900
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 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
22 changes: 17 additions & 5 deletions lib/HotKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,23 @@ 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 childProps = child.props;

props.onFocus = (...args) => {
if (childProps.onFocus) {
childProps.onFocus(...args);
}
this.onFocus(...args);
};
props.onBlur = (...args) => {
if (childProps.onBlur) {
childProps.onBlur(...args);
}
this.onBlur(...args);
};

return React.cloneElement(child, props);
}

});
Expand Down

0 comments on commit c977900

Please sign in to comment.