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, items: filteredUsers,
onSelect: handleSelectMention, onSelect: handleSelectMention,
shouldSelectOnTab: true, shouldSelectOnTab: true,
shouldSaveSelectionOnUpdateItems: true,
onClose, onClose,
}); });

View File

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