diff --git a/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx b/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx index 15dd3bf89..42a91b8cf 100644 --- a/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx +++ b/src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx @@ -1,7 +1,8 @@ +import { useRefValue } from "@renderer/hooks" import { stopPropagation } from "@renderer/lib/dom" import { cn } from "@renderer/lib/utils" import clsx from "clsx" -import { AnimatePresence, m } from "framer-motion" +import { AnimatePresence } from "framer-motion" import Fuse from "fuse.js" import { merge, throttle } from "lodash-es" import { @@ -79,6 +80,16 @@ export const Autocomplete = forwardRef( doFilter() }, [inputValue, suggestions]) + useEffect(() => { + const $input = inputRef.current + if (!$input) return + if (document.activeElement !== $input) { return } + + setIsOpen( + filterableSuggestions.length > 0, + ) + }, [filterableSuggestions]) + const [isOpen, setIsOpen] = useState(false) const [listRef, setListRef] = useState(null) @@ -102,29 +113,56 @@ export const Autocomplete = forwardRef( const inputRef = useRef(null) useImperativeHandle(forwardedRef, () => inputRef.current!) - const [inputRect, setInputRect] = useState(null) + const [currentActiveIndex, setCurrentActiveIndex] = useState(0) + const currentActiveIndexRef = useRefValue(currentActiveIndex) + const filterableSuggestionsRef = useRefValue(filterableSuggestions) + // Bind hotkey + useEffect(() => { + const $input = inputRef.current + if (!$input) return + const handleKeyDown = (e: KeyboardEvent) => { + const currentActiveIndex = currentActiveIndexRef.current + const filterableSuggestionsLength = + filterableSuggestionsRef.current.length + const filterableSuggestions = filterableSuggestionsRef.current + if (e.key === "ArrowDown" || e.key === "ArrowUp") { + e.preventDefault() + const nextIndex = + currentActiveIndex + + (e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0) + setCurrentActiveIndex( + Math.max(0, Math.min(nextIndex, filterableSuggestionsLength - 1)), + ) + } else if ( + (e.key === "Enter" || e.key === "Tab") && + currentActiveIndex >= 0 && + currentActiveIndex < filterableSuggestionsLength + ) { + e.stopPropagation() + e.preventDefault() + + onSuggestionSelected(filterableSuggestions[currentActiveIndex]) + setInputValue(filterableSuggestions[currentActiveIndex].name) + + requestAnimationFrame((() => { + requestAnimationFrame(() => { + setIsOpen(false) + }) + })) + } + } + + $input.addEventListener("keydown", handleKeyDown) + return () => { + $input.removeEventListener("keydown", handleKeyDown) + } + }, [currentActiveIndexRef, filterableSuggestionsRef, onSuggestionSelected]) + + const [inputRect, setInputRect] = useState(null) + const [dropdownPos, setDropdownPos] = useState("down") const [dropdownHeight, setDropdownHeight] = useState(maxHeight || 200) - const [dropdownPos, setDropdownPos] = useState("down") - - // useLayoutEffect(() => { - // const $list = listRef - // if (!$list) return - // const handler = () => { - // const rect = $list.getBoundingClientRect() - - // setDropdownHeight(rect.height) - // } - // const ob = new ResizeObserver(handler) - // handler() - - // // setInterval(handler, 1000) - // ob.observe($list) - // return () => { - // ob.disconnect() - // } - // }, [listRef]) useLayoutEffect(() => { const $input = inputRef.current if (!$input) return @@ -199,19 +237,18 @@ export const Autocomplete = forwardRef( {}, )} > - - {filterableSuggestions.map((suggestion) => { + {filterableSuggestions.map((suggestion, index) => { const handleClick = () => { onSuggestionSelected(suggestion) setIsOpen(false) @@ -220,16 +257,21 @@ export const Autocomplete = forwardRef( } return (
  • setCurrentActiveIndex(index)} > {renderSuggestion(suggestion)}
  • ) })} -
    + ) const handleFocus: React.FocusEventHandler = diff --git a/src/renderer/src/components/ui/modal/stacked/modal.tsx b/src/renderer/src/components/ui/modal/stacked/modal.tsx index c3ed03248..5543a0cc8 100644 --- a/src/renderer/src/components/ui/modal/stacked/modal.tsx +++ b/src/renderer/src/components/ui/modal/stacked/modal.tsx @@ -92,7 +92,7 @@ export const ModalInternal: Component<{ const animateController = useAnimationControls() useEffect(() => { - animateController.start(modalMontionConfig.animate) + requestAnimationFrame(() => animateController.start(modalMontionConfig.animate)) }, [animateController]) const noticeModal = useCallback(() => { animateController