From a4786d6259434d2785b031833b165883cd1ff406 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 13 Sep 2023 17:51:11 +0800 Subject: [PATCH 1/2] unsubscribe enter listener when out of focus --- src/components/SelectionList/BaseSelectionList.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/components/SelectionList/BaseSelectionList.js b/src/components/SelectionList/BaseSelectionList.js index b76ded8a542f..67733c121ff0 100644 --- a/src/components/SelectionList/BaseSelectionList.js +++ b/src/components/SelectionList/BaseSelectionList.js @@ -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'; @@ -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: @@ -243,13 +244,13 @@ function BaseSelectionList({ const renderItem = ({item, index, section}) => { const isDisabled = section.isDisabled; - const isFocused = !isDisabled && focusedIndex === index + lodashGet(section, 'indexOffset', 0); + const isItemFocused = !isDisabled && focusedIndex === index + lodashGet(section, 'indexOffset', 0); if (canSelectMultiple) { return ( selectRow(item, index)} onDismissError={onDismissError} /> @@ -259,7 +260,7 @@ function BaseSelectionList({ return ( selectRow(item, index)} /> @@ -285,14 +286,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 ( From 1419120bb384e60262b86f59460d938e8aec42ce Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 13 Sep 2023 18:33:31 +0800 Subject: [PATCH 2/2] mock useIsFocused --- tests/perf-test/SelectionList.perf-test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/perf-test/SelectionList.perf-test.js b/tests/perf-test/SelectionList.perf-test.js index d16875e31357..2d9cd4a87829 100644 --- a/tests/perf-test/SelectionList.perf-test.js +++ b/tests/perf-test/SelectionList.perf-test.js @@ -31,6 +31,7 @@ jest.mock('../../src/components/withKeyboardState', () => (Component) => (props) jest.mock('@react-navigation/native', () => ({ useFocusEffect: () => {}, + useIsFocused: () => true, createNavigationContainerRef: jest.fn(), }));