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 by default the FocusTrap doesn't wrap
the children and therefore there must only be a single child element
under HotKeys. In such a case there is no tabIndex manipulation
unless used explicitly.

In order to wrap the contents one needs to explicitly define the
component property as follows:

  <HotKeys>
     <p>This is NOT going to get wrapped with a div</p>
  </HotKeys>

  <HotKeys component="div">
     <p>This is going to get wrapped around with a div</p>
  </HotKeys>
  • Loading branch information
opichals committed Sep 6, 2016
1 parent 93eb950 commit 43eec23
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Node = React.createClass({
};

return (
<HotKeys handlers={handlers}>
<HotKeys component="div" handlers={handlers}>
Node contents
</HotKeys>
);
Expand Down
8 changes: 5 additions & 3 deletions examples/master/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {HotKeys, HotKeyMapMixin} from 'react-hotkeys';
import React from 'react';
import ReactDOM from 'react-dom';
import rand from 'lodash/number/random';
import rand from 'lodash/random';

const DEFAULT_NODE_SIZE = 100;
const SIZE_INCREMENT = 5;
Expand Down 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 @@ -110,7 +112,7 @@ const Node = React.createClass({
// - by default we would set it to -1 so it can only be directly clicked (& tapped?)
// or focused programattically
return (
<HotKeys tabIndex="0" handlers={handlers} className="node" style={style}>
<HotKeys tabIndex="0" component="div" handlers={handlers} className="node" style={style}>
Node
</HotKeys>
);
Expand Down
42 changes: 36 additions & 6 deletions lib/FocusTrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';

const DOCUMENT_FRAGMENT = 'documentFragment';

const FocusTrap = React.createClass({

propTypes: {
Expand All @@ -11,22 +13,50 @@ const FocusTrap = React.createClass({

getDefaultProps() {
return {
component: 'div'
component: DOCUMENT_FRAGMENT
};
},

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

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

render() {
const {
component: Component,
children,
...props
} = this.props;

return (
<Component tabIndex="-1" {...props}>
{children}
</Component>
);
if (Component !== DOCUMENT_FRAGMENT) {
return (
<Component tabIndex="-1" {...props}>
{children}
</Component>
);
}

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 43eec23

Please sign in to comment.