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

Unsubscribe enter listener when screen is out of focus #27323

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
13 changes: 7 additions & 6 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useCallback, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {useFocusEffect} from '@react-navigation/native';
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import SectionList from '../SectionList';
import Text from '../Text';
import styles from '../../styles/styles';
Expand Down Expand Up @@ -61,6 +61,7 @@ function BaseSelectionList({
const shouldShowTextInput = Boolean(textInputLabel);
const shouldShowSelectAll = Boolean(onSelectAll);
const activeElement = useActiveElement();
const isFocused = useIsFocused();

/**
* Iterates through the sections and items inside each section, and builds 3 arrays along the way:
Expand Down Expand Up @@ -244,15 +245,15 @@ function BaseSelectionList({
const renderItem = ({item, index, section}) => {
const normalizedIndex = index + lodashGet(section, 'indexOffset', 0);
const isDisabled = section.isDisabled;
const isFocused = !isDisabled && focusedIndex === normalizedIndex;
const isItemFocused = !isDisabled && focusedIndex === normalizedIndex;
// We only create tooltips for the first 10 users or so since some reports have hundreds of users, causing performance to degrade.
const showTooltip = normalizedIndex < 10;

if (canSelectMultiple) {
return (
<UserListItem
item={item}
isFocused={isFocused}
isFocused={isItemFocused}
onSelectRow={() => selectRow(item, index)}
onDismissError={onDismissError}
showTooltip={showTooltip}
Expand All @@ -263,7 +264,7 @@ function BaseSelectionList({
return (
<RadioListItem
item={item}
isFocused={isFocused}
isFocused={isItemFocused}
isDisabled={isDisabled}
onSelectRow={() => selectRow(item, index)}
/>
Expand All @@ -289,14 +290,14 @@ function BaseSelectionList({
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, {
captureOnInputs: true,
shouldBubble: () => !flattenedSections.allOptions[focusedIndex],
isActive: !activeElement,
isActive: !activeElement && isFocused,
});

/** Calls confirm action when pressing CTRL (CMD) + Enter */
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.CTRL_ENTER, onConfirm, {
captureOnInputs: true,
shouldBubble: () => !flattenedSections.allOptions[focusedIndex],
isActive: Boolean(onConfirm),
isActive: Boolean(onConfirm) && isFocused,
});

return (
Expand Down
1 change: 1 addition & 0 deletions tests/perf-test/SelectionList.perf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jest.mock('../../src/components/withKeyboardState', () => (Component) => (props)

jest.mock('@react-navigation/native', () => ({
useFocusEffect: () => {},
useIsFocused: () => true,
createNavigationContainerRef: jest.fn(),
}));

Expand Down
Loading