From 181de8225ed941fd2e83ce2aaffe92df61e1e051 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 9 Mar 2023 10:23:15 +0200 Subject: [PATCH] Implement Combobox from scratch with no libs --- assets/js/dashboard/components/combobox.js | 151 +++++++++++---------- assets/js/dashboard/stats/modals/filter.js | 2 +- 2 files changed, 83 insertions(+), 70 deletions(-) diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index ebfbfeb7f..abaae6132 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -1,5 +1,5 @@ -import React, { Fragment, useState, useCallback } from 'react' -import { Combobox, Transition } from '@headlessui/react' +import React, { Fragment, useState, useCallback, useEffect, useRef } from 'react' +import { Transition } from '@headlessui/react' import { ChevronDownIcon } from '@heroicons/react/20/solid' import debounce from 'debounce-promise' @@ -15,9 +15,13 @@ function Spinner() { export default function PlausibleCombobox(props) { const [options, setOptions] = useState([]) const [loading, setLoading] = useState(false) + const [isOpen, setOpen] = useState(false); + const searchRef = useRef(null); + const containerRef = useRef(null); function fetchOptions(query) { setLoading(true) + setOpen(true) return props.fetchOptions(query).then((loadedOptions) => { setLoading(false) @@ -27,80 +31,89 @@ export default function PlausibleCombobox(props) { const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), []) - function onOpen() { - setOptions([]) - fetchOptions(props.selection.label) + function onInput() { + debouncedFetchOptions(searchRef.current.value) } - function onBlur(e) { - !props.strict && props.onChange({ - value: e.target.value, - label: e.target.value - }) + function toggleOpen() { + if (!isOpen) { + debouncedFetchOptions(searchRef.current.value) + setLoading(true) + } + setOpen(!isOpen) } + function selectOption(option) { + props.onChange(option) + searchRef.current.value = '' + searchRef.current.focus() + } - function renderOptions() { - if (loading) { - return ( -
- Loading options... -
- ) - } else if (!loading && options.length === 0) { - return ( -
- No matches found in the current dashboard. Try selecting a different time range or searching for something different -
- ) - - } else { - return options.map((option) => { - return ( - - `relative cursor-default select-none py-2 px-3 ${active ? 'bg-indigo-600 text-white' : 'text-gray-900 dark:text-gray-300' - }` - } - value={option} - > - {option.label} - - ) - }) - } + function removeOption() { + props.onChange({value: '', label: ''}) + searchRef.current.focus() } + const handleClick = useCallback((e) => { + if (containerRef.current && containerRef.current.contains(e.target)) return; + + setOpen(false) + }) + + useEffect(() => { + document.addEventListener("mousedown", handleClick, false); + return () => { document.removeEventListener("mousedown", handleClick, false); } + }, []) + + const noMatchesFound = !loading && options.length === 0 + const matchesFound = !loading && options.length > 0 + const placeholder = props.value.label ? '' : props.placeholder + return ( - props.onChange(val)}> -
- - item && item.label} - onChange={(event) => debouncedFetchOptions(event.target.value)} - onBlur={onBlur} - /> -
- {!loading && } - {loading && } -
-
- - - {renderOptions()} - - +
+
+ { props.value.label && ( + {props.value.label} + )} + fetchOptions(e.target.value)} placeholder={placeholder} type="text" onChange={onInput}> +
+ {!loading && } + {loading && } +
- - ) + +
    + { loading && ( +
    + Loading options... +
    + )} + { noMatchesFound && ( +
    + No matches found in the current dashboard. Try selecting a different time range or searching for something different +
    + )} + { matchesFound && ( + options.map((option) => { + return ( +
  • selectOption(option)} + > + {option.label} +
  • + ) + }) + )} +
+
+
+ ); } diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index 5b30e0aa5..cc7b330b6 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -184,7 +184,7 @@ class FilterModal extends React.Component { renderSearchBox(filter) { const isStrict = this.state.selectedFilterGroup === 'location' - return + return } renderFilterInputs() {