diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index 08682771c..f2d8e78ab 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -4,10 +4,28 @@ import { ChevronDownIcon } from '@heroicons/react/20/solid' import debounce from 'debounce-promise' import classNames from 'classnames' +function Option({isHighlighted, onClick, onMouseEnter, text}) { + 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, + }) + + return ( +
  • + {text} +
  • + ) +} + export default function PlausibleCombobox(props) { const [options, setOptions] = useState([]) const [loading, setLoading] = useState(false) const [isOpen, setOpen] = useState(false); + const [input, setInput] = useState(''); const [highlightedIndex, setHighlightedIndex] = useState(0); const searchRef = useRef(null); const containerRef = useRef(null); @@ -25,16 +43,22 @@ export default function PlausibleCombobox(props) { const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), []) - function onInput() { - debouncedFetchOptions(searchRef.current.value) + function onInput(e) { + const newInput = e.target.value + setInput(newInput) + + if (!newInput.includes('*')) { + debouncedFetchOptions(newInput) + } } function toggleOpen() { if (!isOpen) { - fetchOptions(searchRef.current.value) + fetchOptions(input) searchRef.current.focus() setOpen(true) } else { + setInput('') setOpen(false) } } @@ -42,7 +66,7 @@ export default function PlausibleCombobox(props) { function selectOption(option) { props.onChange([...props.values, option]) setOpen(false) - searchRef.current.value = '' + setInput('') searchRef.current.focus() } @@ -57,6 +81,7 @@ export default function PlausibleCombobox(props) { const handleClick = useCallback((e) => { if (containerRef.current && containerRef.current.contains(e.target)) return; + setInput('') setOpen(false) }) @@ -65,8 +90,9 @@ export default function PlausibleCombobox(props) { return () => { document.removeEventListener("mousedown", handleClick, false); } }, []) - const noMatchesFound = !loading && options.length === 0 - const matchesFound = !loading && options.length > 0 + const isWildCard = !loading && input.includes('*') + const matchesFound = !loading && !isWildCard && options.length > 0 + const noMatchesFound = !loading && !isWildCard && options.length === 0 return (
    @@ -77,7 +103,7 @@ export default function PlausibleCombobox(props) { ) }) } - +
    {!loading && } {loading && } @@ -103,24 +129,24 @@ export default function PlausibleCombobox(props) { )} { matchesFound && ( options.map((option, i) => { - const isHighlighted = highlightedIndex === i - 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, - }) - return ( -
  • selectOption(option)} onMouseEnter={() => setHighlightedIndex(i)} - > - {option.label} -
  • + text={option.label} + /> ) }) )} + { isWildCard && ( +