Skip to content

Commit

Permalink
merge selectFocusedOption and selectRow function
Browse files Browse the repository at this point in the history
  • Loading branch information
akamefi202 committed Sep 18, 2023
1 parent 7b01c37 commit 0ead7d2
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down

0 comments on commit 0ead7d2

Please sign in to comment.