From eb08db3d4a2687461a6204b42e654c9bf491acad Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sun, 11 Feb 2024 14:16:11 -0800 Subject: [PATCH] Fix dropdown open index --- src-web/components/core/Dropdown.tsx | 42 +++++++++++++--------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src-web/components/core/Dropdown.tsx b/src-web/components/core/Dropdown.tsx index 4a802793..3cc60b3d 100644 --- a/src-web/components/core/Dropdown.tsx +++ b/src-web/components/core/Dropdown.tsx @@ -20,7 +20,7 @@ import React, { useRef, useState, } from 'react'; -import { useKey, useKeyPressEvent, useWindowSize } from 'react-use'; +import { useKey, useWindowSize } from 'react-use'; import type { HotkeyAction } from '../../hooks/useHotKey'; import { useHotKey } from '../../hooks/useHotKey'; import { Overlay } from '../Overlay'; @@ -60,8 +60,8 @@ export interface DropdownProps { export interface DropdownRef { isOpen: boolean; - open: (activeIndex?: number) => void; - toggle: (activeIndex?: number) => void; + open: () => void; + toggle: () => void; close?: () => void; next?: () => void; prev?: () => void; @@ -88,21 +88,17 @@ export const Dropdown = forwardRef(function Dropdown useHotKey(openOnHotKeyAction ?? null, () => { setIsOpen(true); + menuRef.current?.next?.(); }); useImperativeHandle(ref, () => ({ ...menuRef.current, isOpen: isOpen, - toggle(activeIndex?: number) { - if (!isOpen) this.open(activeIndex); + toggle() { + if (!isOpen) this.open(); else setIsOpen(false); }, - open(activeIndex?: number) { - if (activeIndex === undefined) { - setDefaultSelectedIndex(undefined); - } else { - setDefaultSelectedIndex(activeIndex >= 0 ? activeIndex : items.length + activeIndex); - } + open() { setIsOpen(true); }, })); @@ -225,21 +221,23 @@ const Menu = forwardRef, MenuPro setMenuStyles({ maxHeight: windowBox.height - menuBox.top - 5 }); }, []); + const handleClose = useCallback(() => { + onClose(); + setSelectedIndex(null); + }, [onClose]); + // Close menu on space bar const handleMenuKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === ' ') { e.preventDefault(); - onClose(); + handleClose(); } }, - [onClose], + [handleClose], ); - useKeyPressEvent('Escape', (e) => { - e.preventDefault(); - onClose(); - }); + useHotKey('dropdown.close', handleClose); const handlePrev = useCallback(() => { setSelectedIndex((currIndex) => { @@ -287,19 +285,19 @@ const Menu = forwardRef, MenuPro const handleSelect = useCallback( (i: DropdownItem) => { - onClose(); + handleClose(); setSelectedIndex(null); if (i.type !== 'separator') { i.onSelect?.(); } }, - [onClose], + [handleClose], ); useImperativeHandle( ref, () => ({ - close: onClose, + close: handleClose, prev: handlePrev, next: handleNext, select: () => { @@ -308,7 +306,7 @@ const Menu = forwardRef, MenuPro handleSelect(item); }, }), - [handleNext, handlePrev, handleSelect, items, onClose, selectedIndex], + [handleClose, handleNext, handlePrev, handleSelect, items, selectedIndex], ); const { containerStyles, triangleStyles } = useMemo<{ @@ -365,7 +363,7 @@ const Menu = forwardRef, MenuPro {isOpen && (
-
+