Skip to content

React-native: tvOS How to force a Button to be focused? #13855

Description

How to force a Button to be focused?
I have been trying to do this since several weeks

For example if I have a ListView and if I want to have a specific cell focused, how I can do that ?

There are posts on Stackoverflow but nobody knows the answer

Activity

  1. dobrienlzd commented on May 9, 2017

    @dobrienlzd

    Have you tried using touchable highlight? see https://facebook.github.io/react-native/docs/touchablehighlight.html

  2. dobrienlzd commented on May 9, 2017

    @dobrienlzd

    I would forget about Apple TV for a moment and just see if it is possible outside Apple TV. Perhaps you should also consider using newer list like FlatList for example as well. Put function on Highlight methods like onTouch etc and see if they fire for items in a list. If they do than this can be shown. I did notice that one method for touchable focus highlight seems to be iOS only. Are you on Android?

  3. dobrienlzd commented on May 9, 2017

    @dobrienlzd

    I think I understand it better now. Ok, you want to almost simulate a user touch of the component. Something like that ...

  4. dobrienlzd commented on May 9, 2017

    @dobrienlzd

    https://facebook.github.io/react-native/docs/flatlist.html
    scrollToIndex(pos) even though it might be on same page it might give it the highlight.
    scrollToItem(params: object)

  5. dobrienlzd commented on May 9, 2017

    @dobrienlzd

    Also take a look at the example above in https://facebook.github.io/react-native/docs/flatlist.html in the section on pure components. class MyList extends React.PureComponent {
    state = {selected: (new Map(): Map<string, boolean>)};

    Interesting are:

    _onPressItem = (id: string) => {
    // updater functions are preferred for transactional updates
    this.setState((state) => {
    // copy the map rather than modifying state.
    const selected = new Map(state.selected);
    selected.set(id, !state.get(id)); // toggle
    return {selected};
    });
    };

    _renderItem = ({item}) => (

    // On Press Item will toggle the selected state.
    // This deals with some of the issues surrounding FlatList. I would stick with newer list components like FlatList. ListView may be deprecated and won't get improvements.
    I think you will be able to create a solution out of custom PureComponent and FlatList.
    Might have to do both a scrollTo(pos) and also have method on the Component to set the selected status.

  6. JulienKode commented on Jun 22, 2017

    @JulienKode
    Contributor

    Yes it is possible, with tvOS and react native if you want to force a focusable component to be focus you just need to set hasTVPreferredFocus to true

    For example:

        <TouchableHighlight
            hasTVPreferredFocus={true}
        	onPress={() => {}}
        >
        	<Text>Hello</Text>
        </TouchableHighlight>
    

    You can see this post on Stackoverflow:
    https://stackoverflow.com/questions/43862802/react-native-tvos-how-to-force-a-button-to-be-focused/44700590#44700590

  7. douglowder commented on Aug 18, 2017

    @douglowder
    Contributor

    RN Apple TV author here.... @JulienKode is correct, hasTVPreferredFocus is the right way to do this.

  8. douglowder commented on Aug 20, 2017

    @douglowder
    Contributor

    I've submitted a PR to add hasTVPreferredFocus to TouchableOpacity (see #15561).

  9. JulienKode commented on Aug 20, 2017

    @JulienKode
    Contributor

    @dlowder-salesforce That's a nice a idea 👍

  10. stale commented on Oct 19, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

  11. added
    StaleThere has been a lack of activity on this issue and it may be closed soon.
    on Oct 19, 2017
  12. locked and limited conversation to collaborators on May 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions