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

fix: focused background is not updated #54457

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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]);

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,
});
});
});
Loading