Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix selection list focus #34196

Merged
merged 21 commits into from
Feb 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d733aa2
fix selection list focus
abzokhattab Jan 9, 2024
2a50e43
Using memo on the sorted sections
abzokhattab Jan 12, 2024
3920a4f
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 12, 2024
0e2f487
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 14, 2024
10f06b7
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 17, 2024
f702796
Convert changes to typescript
abzokhattab Jan 17, 2024
d53c12f
Minor
abzokhattab Jan 17, 2024
8ab6576
migrating to ts
abzokhattab Jan 17, 2024
216793f
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 20, 2024
54cdcc3
Merge branch 'main' into fix-base-selection-list-focus
abzokhattab Jan 25, 2024
6d9a69f
fix arrow selection
abzokhattab Jan 26, 2024
a123043
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 26, 2024
17c2f7e
Update src/components/SelectionList/BaseSelectionList.tsx
abzokhattab Jan 27, 2024
a015eb5
comments changes
abzokhattab Jan 27, 2024
b8f58a2
minor
abzokhattab Jan 27, 2024
b43f5c4
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Feb 5, 2024
322d554
Removing the sortedsections logic
abzokhattab Feb 5, 2024
ec03b9a
Update src/components/SelectionList/BaseSelectionList.tsx
abzokhattab Feb 6, 2024
86d242d
Adjusting useEffect dependencies
abzokhattab Feb 6, 2024
3dab35f
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Feb 12, 2024
f4393b1
minor edit
abzokhattab Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import withKeyboardState, {keyboardStatePropTypes} from '@components/withKeyboar
import useActiveElement from '@hooks/useActiveElement';
import useKeyboardShortcut from '@hooks/useKeyboardShortcut';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -362,18 +363,37 @@ function BaseSelectionList({
}, [shouldShowTextInput]),
);

const prevTextInputValue = usePrevious(textInputValue);
useEffect(() => {
// do not change focus on the first render, as it should focus on the selected item
if (isInitialSectionListRender) {
if (isInitialSectionListRender || prevTextInputValue === textInputValue) {
return;
}

// set the focus on the first item when the sections list is changed
if (sections.length > 0) {
updateAndScrollToFocusedIndex(0);
let newSelectedIndex;

if (textInputValue === '') {
// if the textInputValue is empty then focus is removed
newSelectedIndex = -1;
} else {
// if multiple selection then focus on the first non-selected item
// else focus on the first item
newSelectedIndex = canSelectMultiple ? flattenedSections.selectedOptions.length + flattenedSections.disabledOptionsIndexes.length : 0;
}

updateAndScrollToFocusedIndex(newSelectedIndex);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sections]);
}, [
canSelectMultiple,
flattenedSections.disabledOptionsIndexes.length,
flattenedSections.selectedOptions.length,
isInitialSectionListRender,
prevTextInputValue,
sections,
textInputValue,
updateAndScrollToFocusedIndex,
]);

/** Selects row when pressing Enter */
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, {
Expand All @@ -390,6 +410,39 @@ function BaseSelectionList({
isActive: !disableKeyboardShortcuts && Boolean(onConfirm) && isFocused,
});

function sortSectionItems(sectionsList) {
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
// If multiple selection is not allowed, return the original list
if (!canSelectMultiple) {
return sectionsList;
}

return _.map(sectionsList, (section) => {
// Classify each item in the section
const disabledItems = [];
const selectedItems = [];
const unselectedItems = [];

section.data.forEach((item) => {
if (item.isDisabled) {
disabledItems.push(item);
} else if (item.isSelected) {
selectedItems.push(item);
} else {
unselectedItems.push(item);
}
});

// Combine items in the order: disabled, selected, unselected
const sortedData = [...disabledItems, ...selectedItems, ...unselectedItems];

// Return the section with updated data
return {
...section,
data: sortedData,
};
});
}

return (
<ArrowKeyFocusManager
disabledIndexes={flattenedSections.disabledOptionsIndexes}
Expand Down Expand Up @@ -460,7 +513,7 @@ function BaseSelectionList({
)}
<SectionList
ref={listRef}
sections={sections}
sections={sortSectionItems(sections)}
stickySectionHeadersEnabled={false}
renderSectionHeader={renderSectionHeader}
renderItem={renderItem}
Expand Down
Loading