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

Sync keyboard navigation in SelectionList and PopoverMenu #39201

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ type MenuItemBaseProps = {

/** Adds padding to the left of the text when there is no icon. */
shouldPutLeftPaddingWhenNoIcon?: boolean;

/** Handles what to do when the item is focused */
onFocus?: () => void;
};

type MenuItemProps = (IconProps | AvatarProps | NoIcon) & MenuItemBaseProps;
Expand Down Expand Up @@ -317,6 +320,7 @@ function MenuItem(
contentFit = 'cover',
isPaneMenu = false,
shouldPutLeftPaddingWhenNoIcon = false,
onFocus,
}: MenuItemProps,
ref: ForwardedRef<View>,
) {
Expand Down Expand Up @@ -447,6 +451,7 @@ function MenuItem(
role={CONST.ROLE.MENUITEM}
accessibilityLabel={title ? title.toString() : ''}
accessible
onFocus={onFocus}
>
{({pressed}) => (
<>
Expand Down
4 changes: 3 additions & 1 deletion src/components/PopoverMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type AnchorAlignment from '@src/types/utils/AnchorAlignment';
import * as Expensicons from './Icon/Expensicons';
import type {MenuItemProps} from './MenuItem';
import MenuItem from './MenuItem';
import PopoverMenuListItem from './PopoverMenuItem';
import PopoverWithMeasuredContent from './PopoverWithMeasuredContent';
import Text from './Text';

Expand Down Expand Up @@ -193,7 +194,7 @@ function PopoverMenu({
{!!headerText && <Text style={[styles.createMenuHeaderText, styles.ml3]}>{headerText}</Text>}
{enteredSubMenuIndexes.length > 0 && renderBackButtonItem()}
{currentMenuItems.map((item, menuIndex) => (
<MenuItem
<PopoverMenuListItem
WojtekBoman marked this conversation as resolved.
Show resolved Hide resolved
key={item.text}
icon={item.icon}
iconWidth={item.iconWidth}
Expand All @@ -215,6 +216,7 @@ function PopoverMenu({
floatRightAvatarSize={item.floatRightAvatarSize}
shouldShowSubscriptRightAvatar={item.shouldShowSubscriptRightAvatar}
disabled={item.disabled}
onFocus={() => setFocusedIndex(menuIndex)}
/>
))}
</View>
Expand Down
29 changes: 29 additions & 0 deletions src/components/PopoverMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, {useLayoutEffect, useRef} from 'react';
import type {View} from 'react-native';
import MenuItem from './MenuItem';
import type {MenuItemProps} from './MenuItem';

function PopoverMenuItem(props: MenuItemProps) {
const ref = useRef<View>(null);

// Sync focus on an item
useLayoutEffect(() => {
if (!props.focused) {
return;
}

ref?.current?.focus();
}, [props.focused]);

return (
<MenuItem
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
/>
);
}

PopoverMenuItem.displayName = 'PopoverMenuItem';

export default PopoverMenuItem;
16 changes: 15 additions & 1 deletion src/components/SelectionList/BaseListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useLayoutEffect, useRef} from 'react';
import {View} from 'react-native';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
Expand Down Expand Up @@ -31,12 +31,16 @@ function BaseListItem<TItem extends ListItem>({
pendingAction,
FooterComponent,
children,
isFocused,
onFocus = () => {},
}: BaseListItemProps<TItem>) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {hovered, bind} = useHover();

const pressableRef = useRef<View | HTMLDivElement>(null);

const rightHandSideComponentRender = () => {
if (canSelectMultiple || !rightHandSideComponent) {
return null;
Expand All @@ -57,6 +61,14 @@ function BaseListItem<TItem extends ListItem>({
}
};

// Sync focus on an item
useLayoutEffect(() => {
if (!isFocused) {
return;
}
pressableRef?.current?.focus();
}, [isFocused]);

return (
<OfflineWithFeedback
onClose={() => onDismissError(item)}
Expand All @@ -68,6 +80,7 @@ function BaseListItem<TItem extends ListItem>({
<PressableWithFeedback
// eslint-disable-next-line react/jsx-props-no-spreading
{...bind}
ref={pressableRef}
onPress={() => onSelectRow(item)}
disabled={isDisabled}
accessibilityLabel={item.text ?? ''}
Expand All @@ -78,6 +91,7 @@ function BaseListItem<TItem extends ListItem>({
onMouseDown={shouldPreventDefaultFocusOnSelectRow ? (e) => e.preventDefault() : undefined}
nativeID={keyForList ?? ''}
style={pressableStyle}
onFocus={onFocus}
>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from this issue #41922, we have added the onFocus to BaseListItem in this PR. We already had the onMouseDown conditional prop passed, so the bug might have been there with the conditional onMouseDown prop but became visible after the onFocus change. When we long press the currency in CurrencySelection, it triggers the onFocus function, and we see a scroll to the long-pressed index. We have fixed this by removing the conditional onMouseDown in BaseListItem.

<View style={wrapperStyle}>
{canSelectMultiple && checkmarkPosition === CONST.DIRECTION.LEFT && (
Expand Down
Loading
Loading