Skip to content

Commit

Permalink
Merge pull request #27323 from bernhardoj/fix/26667-enter-listener-in…
Browse files Browse the repository at this point in the history
…-workspace-invite

Unsubscribe enter listener when screen is out of focus
  • Loading branch information
mountiny authored Sep 15, 2023
2 parents 2099aa6 + ff81f95 commit 1ec7186
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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

0 comments on commit 1ec7186

Please sign in to comment.