Skip to content

Commit

Permalink
fix: keyboard shortcuts in selection lists
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispader committed May 22, 2024
1 parent b0b7859 commit e7e73c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Log from '@libs/Log';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import arraysEqual from '@src/utils/arraysEqual';
import type {BaseSelectionListProps, ButtonOrCheckBoxRoles, FlattenedSectionsReturn, ListItem, SectionListDataType, SectionWithIndexOffset, SelectionListHandle} from './types';

function BaseSelectionList<TItem extends ListItem>(
Expand Down Expand Up @@ -228,11 +229,21 @@ function BaseSelectionList<TItem extends ListItem>(
[flattenedSections.allOptions],
);

const [disabledIndexes, setDisabledIndexes] = useState(flattenedSections.disabledOptionsIndexes);
useEffect(() => {
if (arraysEqual(disabledIndexes, flattenedSections.disabledOptionsIndexes)) {
return;
}

setDisabledIndexes(flattenedSections.disabledOptionsIndexes);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [flattenedSections.disabledOptionsIndexes]);

// If `initiallyFocusedOptionKey` is not passed, we fall back to `-1`, to avoid showing the highlight on the first member
const [focusedIndex, setFocusedIndex] = useArrowKeyFocusManager({
initialFocusedIndex: flattenedSections.allOptions.findIndex((option) => option.keyForList === initiallyFocusedOptionKey),
maxIndex: Math.min(flattenedSections.allOptions.length - 1, CONST.MAX_SELECTION_LIST_PAGE_LENGTH * currentPage - 1),
disabledIndexes: flattenedSections.disabledOptionsIndexes,
disabledIndexes,
isActive: true,
onFocusedIndexChange: (index: number) => {
scrollToIndex(index, true);
Expand Down
25 changes: 25 additions & 0 deletions src/utils/arraysEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function arraysEqual<T>(a: T[], b: T[]): boolean {
if (a === b) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length !== b.length) {
return false;
}

// If you don't care about the order of the elements inside
// the array, you should sort both arrays here.
// Please note that calling sort on an array will modify that array.
// you might want to clone your array first.

for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}

export default arraysEqual;

0 comments on commit e7e73c8

Please sign in to comment.