Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6445,6 +6445,7 @@ const CONST = {

MIGRATED_USER_WELCOME_MODAL: 'migratedUserWelcomeModal',

BASE_LIST_ITEM_TEST_ID: 'base-list-item-',
PRODUCT_TRAINING_TOOLTIP_NAMES: {
CONCEIRGE_LHN_GBR: 'conciergeLHNGBR',
RENAME_SAVED_SEARCH: 'renameSavedSearch',
Expand Down
2 changes: 2 additions & 0 deletions src/components/SelectionList/BaseListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ function BaseListItem<TItem extends ListItem>({
wrapperStyle={pressableWrapperStyle}
>
<View
testID={`${CONST.BASE_LIST_ITEM_TEST_ID}${item.keyForList}`}
accessibilityState={{selected: !!isFocused}}
style={[
wrapperStyle,
isFocused && StyleUtils.getItemBackgroundColorStyle(!!item.isSelected, !!isFocused, !!item.isDisabled, theme.activeComponentBG, theme.hoverComponentBG),
Expand Down
9 changes: 9 additions & 0 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ function BaseSelectionList<TItem extends ListItem>(
isFocused,
});

useEffect(() => {
const selectedItemIndex = flattenedSections.allOptions.findIndex((option) => option.isSelected);
if (selectedItemIndex === -1 || selectedItemIndex === focusedIndex) {
return;
}
setFocusedIndex(selectedItemIndex);
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [flattenedSections]);
Comment on lines +336 to +343

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Coming from here #58017, when the search text contains letters from the currently selected item, and the selected item is initiallyFocusedOptionKey, the setFocusedIndex will call two times to focus index 1 then focus initiallyFocusedOptionKey index. We fixed it by add new condition to the early return logic to return early when the user is searching and keep calling setFocusedIndex only for background updates


const clearInputAfterSelect = useCallback(() => {
onChangeText?.('');
}, [onChangeText]);
Expand Down
67 changes: 67 additions & 0 deletions tests/unit/BaseSelectionListTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {fireEvent, render, screen} from '@testing-library/react-native';
import BaseSelectionList from '@components/SelectionList/BaseSelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import type {ListItem} from '@components/SelectionList/types';
import type Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';

type BaseSelectionListSections<TItem extends ListItem> = {
sections: TItem[];
};

const mockSections = Array.from({length: 10}, (_, index) => ({
text: `Item ${index}`,
keyForList: `${index}`,
isSelected: index === 1,
}));

jest.mock('@react-navigation/native', () => {
const actualNav = jest.requireActual<typeof Navigation>('@react-navigation/native');
return {
...actualNav,
useIsFocused: jest.fn(),
useFocusEffect: jest.fn(),
};
});

describe('BaseSelectionList', () => {
const onSelectRowMock = jest.fn();

function BaseListItemRenderer<TItem extends ListItem>(props: BaseSelectionListSections<TItem>) {
const {sections} = props;
const focusedKey = sections.find((item) => item.isSelected)?.keyForList;
return (
<BaseSelectionList
sections={[{data: sections}]}
ListItem={RadioListItem}
onSelectRow={onSelectRowMock}
shouldSingleExecuteRowSelect
initiallyFocusedOptionKey={focusedKey}
/>
);
}

it('should handle item press correctly', () => {
render(<BaseListItemRenderer sections={mockSections} />);
fireEvent.press(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}1`));
expect(onSelectRowMock).toHaveBeenCalledWith({
...mockSections.at(1),
shouldAnimateInHighlight: false,
});
});

it('should update focused item when sections are updated from BE', () => {
const updatedMockSections = mockSections.map((section) => ({
...section,
isSelected: section.keyForList === '2',
}));
const {rerender} = render(<BaseListItemRenderer sections={mockSections} />);
expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}1`)).toHaveAccessibilityState({
selected: true,
});
rerender(<BaseListItemRenderer sections={updatedMockSections} />);
expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}2`)).toHaveAccessibilityState({
selected: true,
});
});
});