Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue May 9, 2017 · 15 comments
Closed

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

ghost opened this issue May 9, 2017 · 15 comments
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@ghost
Copy link

ghost commented May 9, 2017

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

@dobrienlzd
Copy link

dobrienlzd commented May 9, 2017

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

@ghost
Copy link
Author

ghost commented May 9, 2017

Yes I have tried:
https://facebook.github.io/react-native/docs/touchablehighlight.html

But nothing to set the focus on an element for Apple Tv

I use this in my app

@dobrienlzd
Copy link

dobrienlzd commented May 9, 2017

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?

@ghost
Copy link
Author

ghost commented May 9, 2017

Thanks for you answer 👍

I'm on tvOS,
I use Touchablehighlight and I know when an item is focus because OnShow is called,
But I have no idea to force a Touchablehighlight to be focused

@dobrienlzd
Copy link

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

@ghost
Copy link
Author

ghost commented May 9, 2017

Yes ! Not really a user touch

A user selection, when the user on tvOS move to up it select a cell, and I want to select it programatically

@ghost
Copy link
Author

ghost commented May 9, 2017

Do you have an idea to do that ?

@dobrienlzd
Copy link

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)

@ghost
Copy link
Author

ghost commented May 9, 2017

Thanks, I will try with that and update you as soon as possible

@dobrienlzd
Copy link

dobrienlzd commented May 9, 2017

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.

@JulienKode
Copy link
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

@douglowder
Copy link
Contributor

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

@douglowder
Copy link
Contributor

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

@JulienKode
Copy link
Contributor

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

@stale
Copy link

stale bot commented Oct 19, 2017

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.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 19, 2017
@stale stale bot closed this as completed Oct 26, 2017
@facebook facebook locked and limited conversation to collaborators May 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

3 participants