raphamorim / react-tv

[ Unmaintained due to raphamorim/react-ape ] React Renderer for low memory applications
https://www.npmjs.com/package/react-tv
MIT License
2.01k stars 148 forks source link

Default focused element #129

Closed davidgarsan closed 6 years ago

davidgarsan commented 6 years ago

Is there a way to set a default focused element as original Spatial Navigation does with config sections?

raphamorim commented 6 years ago

doesn't exist a directly API for it yet. but you can do something like:

const Item = ({focused, setFocus, focusPath}) => {
  focused = (focused) ? 'focused' : 'unfocused'
  setFocus() // set inital setFocus
  return <div onClick={() => { setFocus() }} >It's {focused} Item</div>
}
davidgarsan commented 6 years ago

Thank you! But maybe that can be lead to some warnings for updating state in render time (collateral effects). Anyway, finally I came to this in the Focusable HOC:

componentDidMount() {
    this.addFocusable(findFocusable(this));
    if(this.props.focusPath === this.props.defaultFocus) {
        this.props.setFocus();
    }
}

This way I can keep a focusable default element even in complex layouts, at page component level, accesible via react-router too.