Mention Tooltip: Fix resetting active item (#1509)

This commit is contained in:
Alexander Zinchuk 2021-10-22 02:24:48 +03:00
parent b798d92f07
commit 05a5ce0e17
2 changed files with 8 additions and 2 deletions

View File

@ -52,6 +52,7 @@ const MentionTooltip: FC<OwnProps> = ({
items: filteredUsers,
onSelect: handleSelectMention,
shouldSelectOnTab: true,
shouldSaveSelectionOnUpdateItems: true,
onClose,
});

View File

@ -5,6 +5,7 @@ import cycleRestrict from '../../../../util/cycleRestrict';
export function useKeyboardNavigation({
isActive,
isHorizontal,
shouldSaveSelectionOnUpdateItems,
shouldRemoveSelectionOnReset,
noArrowNavigation,
items,
@ -14,6 +15,7 @@ export function useKeyboardNavigation({
}: {
isActive: boolean;
isHorizontal?: boolean;
shouldSaveSelectionOnUpdateItems?: boolean;
shouldRemoveSelectionOnReset?: boolean;
noArrowNavigation?: boolean;
items?: any[];
@ -46,9 +48,12 @@ export function useKeyboardNavigation({
}
}, [items, onSelect, selectedItemIndex]);
const isSelectionOutOfRange = !items || selectedItemIndex > items.length - 1;
useEffect(() => {
setSelectedItemIndex(shouldRemoveSelectionOnReset ? -1 : 0);
}, [items, shouldRemoveSelectionOnReset]);
if (!shouldSaveSelectionOnUpdateItems || isSelectionOutOfRange) {
setSelectedItemIndex(shouldRemoveSelectionOnReset ? -1 : 0);
}
}, [isSelectionOutOfRange, shouldRemoveSelectionOnReset, shouldSaveSelectionOnUpdateItems]);
useEffect(() => (isActive ? captureKeyboardListeners({
onEsc: onClose,