Skip to content

Commit 1479445

Browse files
celiolatorracaraphamorim
authored andcommitted
Refact withFocusable to be a HOC and turn focusPath mandatory
1 parent 129a704 commit 1479445

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tl;dr: [Based on Netflix TV Navigation System](https://medium.com/netflix-techbl
1010

1111
[See code from this example](https://github.com/react-tv/react-tv/blob/master/examples/navigation/src/App.js)
1212

13-
React-TV-Navigation is a separated package from renderer to manage focusable components.
13+
React-TV-Navigation is a separated package from React-TV renderer to manage focusable components.
1414

1515
## Installing
1616

@@ -24,7 +24,7 @@ React and [React-TV](http://github.com/react-tv/react-tv) are peer-dependencies.
2424

2525
React-TV Navigation exports two functions: `withFocusable` and `withNavigation`.
2626

27-
Navigation based on HOC and HOF beeing declarative.
27+
A declarative navigation system based on HOC's for focus and navigation control.
2828

2929
```jsx
3030
import React from 'react'
@@ -40,28 +40,31 @@ const Item = ({focused, setFocus, focusPath}) => {
4040
)
4141
}
4242
43-
const Button = ({focused, setFocus, focusPath}) => {
43+
const Button = ({setFocus}) => {
4444
return (
45-
<div onClick={() => { setFocus('focusPath-1') }}>
45+
<div onClick={() => { setFocus('item-1') }}>
4646
Back To First Item!
4747
</div>
4848
)
4949
}
5050
51-
const FocusableItem = withFocusable({focusPath: 'item-1'})(Item)
52-
const FocusableOtherItem = withFocusable({focusPath: 'item-2'})(Item)
53-
const FocusableButton = withFocusable({focusPath: 'button'})(Button)
51+
const FocusableItem = withFocusable(Item)
52+
const FocusableButton = withFocusable(Button)
5453
5554
function App({currentFocusPath}) {
5655
return (
5756
<div>
5857
<h1>Current FocusPath: '{currentFocusPath}'</h1>,
59-
<FocusableItem/>
60-
<FocusableOtherItem/>
61-
<FocusableButton/>
58+
<FocusableItem focusPath='item-1'/>
59+
<FocusableItem focusPath='item-2'/>
60+
<FocusableButton focusPath: 'button'/>
6261
</div>
6362
)
6463
}
64+
65+
const NavigableApp = withNavigation(App)
66+
67+
ReactTV.render(<NavigableApp/>, document.querySelector('#app'))
6568
```
6669
6770
Soon we'll write a decent README.md :)

__tests__/with-focusable-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('withFocusable', () => {
1414
currentFocusPath,
1515
setFocus = jest.fn(),
1616
}) => {
17-
const EnhancedComponent = withFocusable({ focusPath })(Component);
17+
const EnhancedComponent = withFocusable(Component);
1818
return mount(
19-
<EnhancedComponent />,
19+
<EnhancedComponent focusPath={focusPath} />,
2020
{ context: { currentFocusPath, setFocus } }
2121
);
2222
};

src/with-focusable.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
import PropTypes from 'prop-types';
21
import ReactTV from 'react-tv';
2+
import PropTypes from 'prop-types';
33

44
import compose from 'recompose/compose';
55
import mapProps from 'recompose/mapProps';
66
import lifecycle from 'recompose/lifecycle';
77
import getContext from 'recompose/getContext';
8+
import setPropTypes from 'recompose/setPropTypes';
89

910
import SpatialNavigation from './spatial-navigation';
1011

11-
const withFocusable = ({
12-
focusPath = '',
13-
} = {}) => compose(
12+
const withFocusable = compose(
13+
setPropTypes({
14+
focusPath: PropTypes.string.isRequired,
15+
}),
1416
getContext({
1517
setFocus: PropTypes.func,
1618
currentFocusPath: PropTypes.string,
1719
}),
18-
mapProps(({ currentFocusPath, setFocus, ...props }) => ({
20+
mapProps(({ currentFocusPath, setFocus, focusPath, ...props }) => ({
1921
focused: currentFocusPath === focusPath,
2022
setFocus: setFocus.bind(null, focusPath),
2123
focusPath,
2224
...props,
2325
})),
2426
lifecycle({
2527
componentDidMount() {
26-
SpatialNavigation.addFocusable(ReactTV.findDOMNode(this), focusPath);
28+
SpatialNavigation.addFocusable(
29+
ReactTV.findDOMNode(this),
30+
this.props.focusPath
31+
);
2732
},
2833
componentWillUnmount() {
2934
SpatialNavigation.removeFocusable(ReactTV.findDOMNode(this));

0 commit comments

Comments
 (0)