diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index 4cd24796e..234308609 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -3,7 +3,6 @@ import { Transition } from '@headlessui/react' import { ChevronDownIcon } from '@heroicons/react/20/solid' import debounce from 'debounce-promise' import classNames from 'classnames' -import { useKeybinds } from '../keybinding' function Option({isHighlighted, onClick, onMouseEnter, text, id}) { const className = classNames('relative cursor-pointer select-none py-2 px-3', { @@ -25,7 +24,6 @@ function Option({isHighlighted, onClick, onMouseEnter, text, id}) { function scrollTo(wrapper, id) { if (wrapper) { const el = wrapper.querySelector('#' + id); - console.log(el) if (el) { el.scrollIntoView({block: 'center'}); @@ -47,12 +45,17 @@ export default function PlausibleCombobox(props) { const containerRef = useRef(null); const listRef = useRef(null); + const visibleOptions = [...options] + if (props.allowWildcard && input.length > 0) { + visibleOptions.push({value: input, label: input, freeChoice: true}) + } + function highLight(index) { let newIndex = index if (index < 0) { - newIndex = options.length - 1 - } else if (index >= options.length) { + newIndex = visibleOptions.length - 1 + } else if (index >= visibleOptions.length) { newIndex = 0 } @@ -60,46 +63,41 @@ export default function PlausibleCombobox(props) { scrollTo(listRef.current, optionId(newIndex)) } - useKeybinds(containerRef.current, (e) => { - if (e.key === 'enter') { + function onKeyDown(e) { + if (e.key === 'Enter') { if (!isOpen || loading) return null - selectOption(options[highlightedIndex]) - return false + selectOption(visibleOptions[highlightedIndex]) + e.preventDefault() } - if (e.key === 'escape') { + if (e.key === 'Escape') { if (!isOpen || loading) return null setOpen(false) searchRef.current?.focus() - return false + e.preventDefault() } - if (e.key === 'arrowdown') { + if (e.key === 'ArrowDown') { if (isOpen) { highLight(highlightedIndex + 1) } else { - fetchOptions(input) + setOpen(true) } } - if (e.key === 'arrowup') { + if (e.key === 'ArrowUp') { if (isOpen) { highLight(highlightedIndex - 1) } else { - fetchOptions(input, 'bottom') + setOpen(true) } } - }, [options, isOpen, loading, highlightedIndex, input]) + } - function fetchOptions(query, highlight = 'top') { + function fetchOptions(query) { setLoading(true) setOpen(true) return props.fetchOptions(query).then((loadedOptions) => { setLoading(false) - if (highlight === 'top') { - setHighlightedIndex(0) - } else if (highlight === 'bottom') { - setHighlightedIndex(loadedOptions.length - 1) - scrollTo(listRef.current, optionId(loadedOptions.length - 1)) - } + setHighlightedIndex(0) setOptions(loadedOptions) }) } @@ -109,10 +107,7 @@ export default function PlausibleCombobox(props) { function onInput(e) { const newInput = e.target.value setInput(newInput) - - if (!newInput.includes('*')) { - debouncedFetchOptions(newInput) - } + debouncedFetchOptions(newInput) } function toggleOpen() { @@ -153,13 +148,12 @@ export default function PlausibleCombobox(props) { return () => { document.removeEventListener("mousedown", handleClick, false); } }, []) - const isWildCard = props.allowWildcard && !loading && input.includes('*') - const matchesFound = !loading && !isWildCard && options.length > 0 - const noMatchesFound = !loading && !isWildCard && options.length === 0 + const matchesFound = !loading && visibleOptions.length > 0 + const noMatchesFound = !loading && !props.allowWildcard && options.length === 0 return ( -
-
+
+
{ props.values.map((value) => { return (
{value.label} removeOption(value, e)} className="cursor-pointer font-bold ml-1">×
@@ -191,7 +185,9 @@ export default function PlausibleCombobox(props) {
)} { matchesFound && ( - options.map((option, i) => { + visibleOptions.map((option, i) => { + const text = option.freeChoice ? `Filter by '${option.label}'` : option.label + return (
diff --git a/assets/js/dashboard/keybinding.js b/assets/js/dashboard/keybinding.js index a712a3f5a..003eb573d 100644 --- a/assets/js/dashboard/keybinding.js +++ b/assets/js/dashboard/keybinding.js @@ -1,5 +1,3 @@ -import { useEffect, useCallback } from "react"; - /** * Returns whether a keydown or keyup event should be ignored or not. * @@ -22,29 +20,6 @@ export function shouldIgnoreKeypress(event) { return modifierPressed || isTyping } -export function useKeybinds(el, callback, deps) { - const handleKeydown = useCallback((e) => { - const modifierPressed = event.ctrlKey || event.metaKey || event.altKey || event.keyCode == 229 - if (modifierPressed) return true - - e = {...e, key: e.key.toLowerCase()} - - const retValue = callback(e) - if (retValue === false) { - e.preventDefault() - e.stopPropagation() - } - }, deps) - - useEffect(() => { - if (el) { - el.addEventListener("keydown", handleKeydown); - return () => { el.removeEventListener("keydown", handleKeydown); } - } - }) -} - - /** * Returns whether the given keybinding has been pressed and should be * processed. Events can be ignored based on `shouldIgnoreKeypress(event)`.