From acfc614ecc8caedf3c46b83120b51c5fa50b9232 Mon Sep 17 00:00:00 2001 From: Thiago Brezinski Date: Fri, 25 Aug 2023 17:10:34 +0100 Subject: [PATCH 1/2] fix(selection-list): scroll to top, cmd enter, tab key --- .../SelectionList/BaseSelectionList.js | 27 ++++++++++++++--- .../SelectionList/CheckboxListItem.js | 30 ++++++++++++++----- .../SelectionList/selectionListPropTypes.js | 3 ++ src/hooks/useActiveElement/index.js | 25 ++++++++++++++++ src/hooks/useActiveElement/index.native.js | 3 ++ src/pages/workspace/WorkspaceInvitePage.js | 1 + 6 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 src/hooks/useActiveElement/index.js create mode 100644 src/hooks/useActiveElement/index.native.js diff --git a/src/components/SelectionList/BaseSelectionList.js b/src/components/SelectionList/BaseSelectionList.js index 60214d60715b..d13a9cced57d 100644 --- a/src/components/SelectionList/BaseSelectionList.js +++ b/src/components/SelectionList/BaseSelectionList.js @@ -22,6 +22,7 @@ import Button from '../Button'; import useLocalize from '../../hooks/useLocalize'; import Log from '../../libs/Log'; import OptionsListSkeletonView from '../OptionsListSkeletonView'; +import useActiveElement from '../../hooks/useActiveElement'; const propTypes = { ...keyboardStatePropTypes, @@ -49,6 +50,7 @@ function BaseSelectionList({ onConfirm, showScrollIndicator = false, showLoadingPlaceholder = false, + showConfirmButton = false, isKeyboardShown = false, }) { const {translate} = useLocalize(); @@ -58,7 +60,9 @@ function BaseSelectionList({ const focusTimeoutRef = useRef(null); const shouldShowTextInput = Boolean(textInputLabel); const shouldShowSelectAll = Boolean(onSelectAll); - const shouldShowConfirmButton = Boolean(onConfirm); + const activeElement = useActiveElement(); + + console.log('activeElement', activeElement); /** * Iterates through the sections and items inside each section, and builds 3 arrays along the way: @@ -162,7 +166,7 @@ function BaseSelectionList({ } } - listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated}); + listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight}); }; const selectRow = (item, index) => { @@ -176,6 +180,11 @@ function BaseSelectionList({ // If the list has multiple sections (e.g. Workspace Invite list), we focus the first one after all the selected (selected items are always at the top) const selectedOptionsCount = item.isSelected ? flattenedSections.selectedOptions.length - 1 : flattenedSections.selectedOptions.length + 1; setFocusedIndex(selectedOptionsCount); + + if (!item.isSelected) { + // If we're selecting an item, scroll to it's position at the top, so we can see it + scrollToIndex(Math.max(selectedOptionsCount - 1, 0), true); + } } } @@ -183,6 +192,8 @@ function BaseSelectionList({ }; const selectFocusedOption = () => { + console.log('ENTER'); + const focusedOption = flattenedSections.allOptions[focusedIndex]; if (!focusedOption || focusedOption.isDisabled) { @@ -277,10 +288,18 @@ function BaseSelectionList({ }; }, [shouldDelayFocus, shouldShowTextInput]); - /** Selects row when pressing enter */ + /** Selects row when pressing Enter */ useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, { captureOnInputs: true, shouldBubble: () => !flattenedSections.allOptions[focusedIndex], + isActive: !activeElement, + }); + + /** Calls confirm action when pressing CTRL (CMD) + Enter */ + useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.CTRL_ENTER, onConfirm, { + captureOnInputs: true, + shouldBubble: () => !flattenedSections.allOptions[focusedIndex], + isActive: Boolean(onConfirm), }); return ( @@ -371,7 +390,7 @@ function BaseSelectionList({ /> )} - {shouldShowConfirmButton && ( + {showConfirmButton && (