-
Notifications
You must be signed in to change notification settings - Fork 4
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
Moving between items #48
Comments
Hello, it's possible, but it could be really challenging. To implement this, many steps need to be taken, and there are also a lot of nuances.
Challenges:
I could give it a try later, but I can't say when. |
Thanks for quick reply! |
Hello @oleksii-ilchenko, This means that only two things are needed to do for implemnting the logic you've described above:
Global key handlingFirst, I recommend trying these libraries: I thought about writing my own library, but there’s likely nothing I can make really different... Perhaps adding support for the New Architecture would be beneficial, but it might be better to contribute directly to an existing library. ScrollingWhether you need to control scrolling depends on the content of your ScrollView and whether everything fits within the visible frame. In some cases, scrolling may not be necessary, but if I'm not mistaken, React Native already provides built-in scrolling functionality. For example: scrollViewRef.current.scrollTo({ y: y, animated: true }); Or you could try https://github.com/slorber/react-native-scroll-into-view FocusThe final step is focusing on the component. import GlobalKeyEvent from 'react-native-global-keyevent';
import {
Pressable,
KeyboardFocus,
} from 'react-native-external-keyboard';
export const Example = () => {
const firstRef = React.useRef<KeyboardFocus>(null);
const secondRef = React.useRef<KeyboardFocus>(null);
const scrollViewRef = React.useRef<ScrollView>(null);
const refMap = {
'1': firstRef,
'2': secondRef,
};
const onFocusByPress = (key: string) => {
const targetRef = refMap?.[key];
if (!targetRef?.current || !scrollViewRef.current) return;
targetRef.current.measure((_x: number, y: number) => {
scrollViewRef.current?.scrollTo({ y: y, animated: true });
//maybe dellay to be sure that the view on the view point
targetRef.current.focus();
});
};
React.useEffect(() => {
GlobalKeyEvent.addKeyUpListener((evt) => onFocusByPress(evt.pressedKey));
// Probably something to remove listener
return () => {}
}, []);
return (
<ScrollView ref={scrollViewRef}>
<Pressable ref={firstRef} />
<Pressable ref={secondRef} />
</ScrollView>
);
}; I think this would be the best and only correct way to do it. |
Hello!
Is it possible to move between items in list with the help of 0-9 num buttons? For example pressed button 0 moves focus to first item in list, 1 - to second and so on
Thanks a lot for answer!
The text was updated successfully, but these errors were encountered: