Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/youtube-react-tv/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ReactTVApp extends React.Component {

render() {
return (
<Navigation>
<Navigation supportedKeys={[{code:32, stringValue:"space"}, {code:27, stringValue:"esc"}]}>
<div id="container">
<HorizontalList>
<Sidebar/>
Expand Down
14 changes: 13 additions & 1 deletion examples/youtube-react-tv/src/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@ export default class Search extends React.Component {
navigation.forceFocus('sidebar');
}

onSupportedKeyDown(event, navigation) {
console.log('onSupportedKeyDown', event, navigation);

if(event === "space"){
this.onEnterDown(event, navigation);
}else if(event === "esc"){
console.log("esc was pressed");
navigation.focusDefault();
}
}


render() {
return (
<Focusable onFocus={() => this.onFocus()} onBlur={() => this.onBlur()} onEnterDown={(e, n) => this.onEnterDown(e, n)} navDefault>
<Focusable onFocus={() => this.onFocus()} onBlur={() => this.onBlur()} onEnterDown={(e, n) => this.onEnterDown(e, n)} onSupportedKeyDown={(e, n) => this.onSupportedKeyDown(e, n)} navDefault>
<div class={this.state.active ? 'search-box-placeholder-focus' : ''} id="search-box-placeholder"><i class="fa fa-search"></i></div>
</Focusable>
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-key-navigation",
"version": "0.0.13",
"version": "0.0.14",
"description": "Use the key to navigate around components",
"main": "build/index.js",
"bugs": {
Expand Down
3 changes: 2 additions & 1 deletion src/Focusable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ Focusable.defaultProps = {
retainLastFocus: false,
onFocus: PropTypes.function,
onBlur: PropTypes.function,
onEnterDown: PropTypes.function
onEnterDown: PropTypes.function,
onSupportedKeyDown: PropTypes.function
};

export default Focusable;
27 changes: 27 additions & 0 deletions src/Navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class Navigation extends Component {
root = null;
focusableComponents = {};
focusableIds = 0;
supportedKeys = {};

constructor(props){
super(props);
this.supportedKeys = props.supportedKeys;
}

onKeyDown = (evt) => {
if (this._pause || evt.altKey || evt.ctrlKey || evt.metaKey || evt.shiftKey) {
Expand All @@ -44,6 +50,22 @@ class Navigation extends Component {
return preventDefault();
}
}
}else{

if(this.supportedKeys){
let keyFound = this.supportedKeys.find(function(item) {
return item.code === evt.keyCode;
});

if(keyFound){
// console.log("keyFound", keyFound)
if (this.currentFocusedPath) {
if (!this.fireEvent(this.getLastFromPath(this.currentFocusedPath), 'supportedKey-down', keyFound.stringValue)) {
return preventDefault();
}
}
}
}
}
return;
}
Expand Down Expand Up @@ -80,6 +102,11 @@ class Navigation extends Component {
if (element.props.onEnterDown)
element.props.onEnterDown(evtProps, this);
break;
case 'supportedKey-down':
if (element.props.onSupportedKeyDown)
element.props.onSupportedKeyDown(evtProps, this);
break;

default:
return false;
}
Expand Down