diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index 7a727d422..fc5e99bc4 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -129,17 +129,24 @@ export default function PlausibleCombobox(props) { function selectOption(option) { if (isDisabled(option)) return - props.onSelect([...props.values, option]) + if (props.singleOption) { + props.onSelect([option]) + } else { + searchRef.current.focus() + props.onSelect([...props.values, option]) + } + setOpen(false) setInput('') - searchRef.current.focus() } function removeOption(option, e) { e.stopPropagation() const newValues = props.values.filter((val) => val.value !== option.value) props.onSelect(newValues) - searchRef.current.focus() + if (!searchBoxHidden) { + searchRef.current.focus() + } setOpen(false) } @@ -155,19 +162,38 @@ export default function PlausibleCombobox(props) { return () => { document.removeEventListener("mousedown", handleClick, false); } }, []) + useEffect(() => { + if (props.singleOption && props.values.length === 0) { + searchRef.current.focus() + } + }, [props.values.length === 0]) + const matchesFound = !loading && visibleOptions.length > 0 const noMatchesFound = !loading && visibleOptions.length === 0 + + const searchBoxHidden = !!props.singleOption && props.values.length === 1 + const searchBoxClass = classNames('border-none py-1 px-1 p-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm', { + 'hidden': searchBoxHidden + }) + + const containerClass = classNames('relative w-full', { + [props.className]: !!props.className, + 'opacity-20 cursor-default pointer-events-none': props.isDisabled + }) return ( -