From 4ed62cb0e31cd6906c001736f10f981caa386b3b Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Mon, 20 Mar 2023 13:15:19 +0200 Subject: [PATCH] Implement keybindings for combobox --- assets/js/dashboard/components/combobox.js | 76 ++++++++++++++++++++-- assets/js/dashboard/keybinding.js | 26 +++++++- assets/js/dashboard/stats/modals/filter.js | 11 ++-- assets/js/dashboard/stats/modals/modal.js | 3 +- 4 files changed, 102 insertions(+), 14 deletions(-) diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index f2d8e78ab..4cd24796e 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -3,8 +3,9 @@ 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}) { +function Option({isHighlighted, onClick, onMouseEnter, text, id}) { const className = classNames('relative cursor-pointer select-none py-2 px-3', { 'text-gray-900 dark:text-gray-300': !isHighlighted, 'bg-indigo-600 text-white': isHighlighted, @@ -13,6 +14,7 @@ function Option({isHighlighted, onClick, onMouseEnter, text}) { return (
  • @@ -20,6 +22,20 @@ function Option({isHighlighted, onClick, onMouseEnter, text}) {
  • ) } +function scrollTo(wrapper, id) { + if (wrapper) { + const el = wrapper.querySelector('#' + id); + console.log(el) + + if (el) { + el.scrollIntoView({block: 'center'}); + } + } +} + +function optionId(index) { + return `plausible-combobox-option-${index}` +} export default function PlausibleCombobox(props) { const [options, setOptions] = useState([]) @@ -29,14 +45,61 @@ export default function PlausibleCombobox(props) { const [highlightedIndex, setHighlightedIndex] = useState(0); const searchRef = useRef(null); const containerRef = useRef(null); + const listRef = useRef(null); + + function highLight(index) { + let newIndex = index + + if (index < 0) { + newIndex = options.length - 1 + } else if (index >= options.length) { + newIndex = 0 + } + + setHighlightedIndex(newIndex) + scrollTo(listRef.current, optionId(newIndex)) + } + + useKeybinds(containerRef.current, (e) => { + if (e.key === 'enter') { + if (!isOpen || loading) return null + selectOption(options[highlightedIndex]) + return false + } + if (e.key === 'escape') { + if (!isOpen || loading) return null + setOpen(false) + searchRef.current?.focus() + return false + } + if (e.key === 'arrowdown') { + if (isOpen) { + highLight(highlightedIndex + 1) + } else { + fetchOptions(input) + } + } + if (e.key === 'arrowup') { + if (isOpen) { + highLight(highlightedIndex - 1) + } else { + fetchOptions(input, 'bottom') + } + } + }, [options, isOpen, loading, highlightedIndex, input]) - function fetchOptions(query) { + function fetchOptions(query, highlight = 'top') { setLoading(true) setOpen(true) return props.fetchOptions(query).then((loadedOptions) => { setLoading(false) - setHighlightedIndex(0) + if (highlight === 'top') { + setHighlightedIndex(0) + } else if (highlight === 'bottom') { + setHighlightedIndex(loadedOptions.length - 1) + scrollTo(listRef.current, optionId(loadedOptions.length - 1)) + } setOptions(loadedOptions) }) } @@ -90,7 +153,7 @@ export default function PlausibleCombobox(props) { return () => { document.removeEventListener("mousedown", handleClick, false); } }, []) - const isWildCard = !loading && input.includes('*') + const isWildCard = props.allowWildcard && !loading && input.includes('*') const matchesFound = !loading && !isWildCard && options.length > 0 const noMatchesFound = !loading && !isWildCard && options.length === 0 @@ -99,7 +162,7 @@ export default function PlausibleCombobox(props) {
    { props.values.map((value) => { return ( -
    {value.label}
    +
    {value.label} removeOption(value, e)} className="cursor-pointer font-bold ml-1">×
    ) }) } @@ -116,7 +179,7 @@ export default function PlausibleCombobox(props) { leaveTo="opacity-0" show={isOpen} > -