From 0ead7d25b2a4757a7d8d9b48dd84d0d21fadd2f6 Mon Sep 17 00:00:00 2001 From: akamefi202 Date: Mon, 18 Sep 2023 09:00:17 -0400 Subject: [PATCH] merge selectFocusedOption and selectRow function --- .../SelectionList/BaseSelectionList.js | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/components/SelectionList/BaseSelectionList.js b/src/components/SelectionList/BaseSelectionList.js index 35222ffad12c..fc83a5d82161 100644 --- a/src/components/SelectionList/BaseSelectionList.js +++ b/src/components/SelectionList/BaseSelectionList.js @@ -168,36 +168,32 @@ function BaseSelectionList({ listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight}); }; - const selectRow = (item, index) => { + const selectFocusedOption = () => { + const focusedOption = flattenedSections.allOptions[focusedIndex]; + + if (!focusedOption || focusedOption.isDisabled) { + return; + } + // In single-selection lists we don't care about updating the focused index, because the list is closed after selecting an item if (canSelectMultiple) { if (sections.length === 1) { // If the list has only 1 section (e.g. Workspace Members list), we always focus the next available item - const nextAvailableIndex = _.findIndex(flattenedSections.allOptions, (option, i) => i > index && !option.isDisabled); + const nextAvailableIndex = _.findIndex(flattenedSections.allOptions, (option, i) => i > focusedIndex && !option.isDisabled); setFocusedIndex(nextAvailableIndex); } else { // 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; + const selectedOptionsCount = focusedOption.isSelected ? flattenedSections.selectedOptions.length - 1 : flattenedSections.selectedOptions.length + 1; setFocusedIndex(selectedOptionsCount); - if (!item.isSelected) { + if (!focusedOption.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); } } } - onSelectRow(item); - }; - - const selectFocusedOption = () => { - const focusedOption = flattenedSections.allOptions[focusedIndex]; - - if (!focusedOption || focusedOption.isDisabled) { - return; - } - - selectRow(focusedOption, focusedIndex); + onSelectRow(focusedOption); }; /**