@ -4,12 +4,10 @@ import { ChevronDownIcon } from '@heroicons/react/20/solid'
import debounce from 'debounce-promise'
import classNames from 'classnames'
function Option ( { isHighlighted , isDisabled , onClick , onMouseEnter , text , id } ) {
const className = classNames ( 'relative select-none py-2 px-3' , {
'cursor-pointer' : ! isDisabled ,
'text-gray-300 dark:text-gray-600' : isDisabled ,
'text-gray-900 dark:text-gray-300' : ! isDisabled && ! isHighlighted ,
'bg-indigo-600 text-white' : ! isDisabled && isHighlighted ,
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 ,
} )
return (
@ -25,10 +23,10 @@ function Option({isHighlighted, isDisabled, onClick, onMouseEnter, text, id}) {
}
function scrollTo ( wrapper , id ) {
if ( wrapper ) {
const el = wrapper . querySelector ( '#' + id ) ;
const el = wrapper . querySelector ( '#' + id )
if ( el ) {
el . scrollIntoView ( { block : 'center' } ) ;
el . scrollIntoView ( { block : 'center' } )
}
}
}
@ -40,12 +38,12 @@ function optionId(index) {
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 ) ;
const listRef = useRef ( null ) ;
const [ isOpen , setOpen ] = useState ( false )
const [ input , setInput ] = useState ( '' )
const [ highlightedIndex , setHighlightedIndex ] = useState ( 0 )
const searchRef = useRef ( null )
const containerRef = useRef ( null )
const listRef = useRef ( null )
const visibleOptions = [ ... options ]
if ( props . freeChoice && input . length > 0 && options . every ( option => option . value !== input ) ) {
@ -108,7 +106,7 @@ export default function PlausibleCombobox(props) {
} )
}
const debouncedFetchOptions = useCallback ( debounce ( fetchOptions , 200 ) , [ ] )
const debouncedFetchOptions = useCallback ( debounce ( fetchOptions , 200 ) , [ fetchOptions ] )
function onInput ( e ) {
const newInput = e . target . value
@ -127,8 +125,6 @@ export default function PlausibleCombobox(props) {
}
function selectOption ( option ) {
if ( isDisabled ( option ) ) return
if ( props . singleOption ) {
props . onSelect ( [ option ] )
} else {
@ -144,22 +140,20 @@ export default function PlausibleCombobox(props) {
e . stopPropagation ( )
const newValues = props . values . filter ( ( val ) => val . value !== option . value )
props . onSelect ( newValues )
if ( ! searchBoxHidden ) {
searchRef . current . focus ( )
}
searchRef . current . focus ( )
setOpen ( false )
}
const handleClick = useCallback ( ( e ) => {
if ( containerRef . current && containerRef . current . contains ( e . target ) ) return ;
if ( containerRef . current && containerRef . current . contains ( e . target ) ) { return }
setInput ( '' )
setOpen ( false )
} )
useEffect ( ( ) => {
document . addEventListener ( "mousedown" , handleClick , false ) ;
return ( ) => { document . removeEventListener ( "mousedown" , handleClick , false ) ; }
document . addEventListener ( "mousedown" , handleClick , false )
return ( ) => { document . removeEventListener ( "mousedown" , handleClick , false ) }
} , [ ] )
useEffect ( ( ) => {
@ -167,23 +161,47 @@ export default function PlausibleCombobox(props) {
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 searchBoxClass = 'border-none py-1 px-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm'
const containerClass = classNames ( 'relative w-full' , {
[ props . className ] : ! ! props . className ,
'opacity-20 cursor-default pointer-events-none' : props . isDisabled
} )
return (
< div onKeyDown = { onKeyDown } ref = { containerRef } className = { containerClass } >
< div onClick = { toggleOpen } className = { classNames ( 'pl-2 pr-8 py-1 w-full dark:bg-gray-900 dark:text-gray-300 rounded-md shadow-sm border border-gray-300 dark:border-gray-700 focus-within:border-indigo-500 focus-within:ring-1 focus-within:ring-indigo-500' , { 'border-indigo-500 ring-1 ring-indigo-500' : isOpen , '' : ! isOpen } ) } >
function renderSingleOptionContent ( ) {
const itemSelected = props . values . length === 1
const placeholder = itemSelected ? '' : props . placeholder
return (
< div className = 'flex items-center truncate' >
{ itemSelected && renderSingleSelectedItem ( ) }
< input
className = { searchBoxClass }
ref = { searchRef }
value = { input }
style = { { backgroundColor : "inherit" } }
placeholder = { placeholder }
type = "text"
onChange = { onInput } >
< / i n p u t >
< / d i v >
)
}
function renderSingleSelectedItem ( ) {
if ( input === '' ) {
return (
< span className = "dark:text-gray-300 text-sm w-0" >
{ props . values [ 0 ] . label }
< / s p a n >
)
}
}
function renderMultiOptionContent ( ) {
return (
< >
{ props . values . map ( ( value ) => {
return (
< div key = { value . value } className = "bg-indigo-100 dark:bg-indigo-600 flex justify-between w-full rounded-sm px-2 py-0.5 m-0.5 text-sm" >
@ -194,12 +212,63 @@ export default function PlausibleCombobox(props) {
} )
}
< input className = { searchBoxClass } ref = { searchRef } value = { input } style = { { backgroundColor : "inherit" } } placeholder = { props . placeholder } type = "text" onChange = { onInput } > < / i n p u t >
< / >
)
}
function renderDropDownContent ( ) {
const matchesFound = visibleOptions . length > 0 && visibleOptions . some ( option => ! isDisabled ( option ) )
if ( loading ) {
return < div className = "relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300" > Loading options ... < / d i v >
}
if ( matchesFound ) {
return visibleOptions
. filter ( option => ! isDisabled ( option ) )
. map ( ( option , i ) => {
const text = option . freeChoice ? ` Filter by ' ${ option . label } ' ` : option . label
return (
< Option
key = { option . value }
id = { optionId ( i ) }
isHighlighted = { highlightedIndex === i }
onClick = { ( ) => selectOption ( option ) }
onMouseEnter = { ( ) => setHighlightedIndex ( i ) }
text = { text }
/ >
)
} )
}
if ( props . freeChoice ) {
return < div className = "relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300" > Start typing to apply filter < / d i v >
}
return (
< div className = "relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300" >
No matches found in the current dashboard . Try selecting a different time range or searching for something different
< / d i v >
)
}
const defaultBoxClass = 'pl-2 pr-8 py-1 w-full dark:bg-gray-900 dark:text-gray-300 rounded-md shadow-sm border border-gray-300 dark:border-gray-700 focus-within:border-indigo-500 focus-within:ring-1 focus-within:ring-indigo-500'
const boxClass = classNames ( props . boxClass || defaultBoxClass , {
'border-indigo-500 ring-1 ring-indigo-500' : isOpen ,
} )
return (
< div onKeyDown = { onKeyDown } ref = { containerRef } className = { containerClass } >
< div onClick = { toggleOpen } className = { boxClass } >
{ props . singleOption && renderSingleOptionContent ( ) }
{ ! props . singleOption && renderMultiOptionContent ( ) }
< div className = "cursor-pointer absolute inset-y-0 right-0 flex items-center pr-2" >
{ ! loading && < ChevronDownIcon className = "h-4 w-4 text-gray-500" / > }
{ loading && < Spinner / > }
< / d i v >
< / d i v >
< Transition
{ isOpen && < Transition
as = { Fragment }
leave = "transition ease-in duration-100"
leaveFrom = "opacity-100"
@ -207,42 +276,11 @@ export default function PlausibleCombobox(props) {
show = { isOpen }
>
< ul ref = { listRef } className = "z-50 absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900" >
{ loading && (
< div className = "relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300" >
Loading options ...
< / d i v >
) }
{ noMatchesFound && ! props . freeChoice && (
< div className = "relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300" >
No matches found in the current dashboard . Try selecting a different time range or searching for something different
< / d i v >
) }
{ noMatchesFound && props . freeChoice && (
< div className = "relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300" >
Start typing to apply filter
< / d i v >
) }
{ matchesFound && (
visibleOptions . map ( ( option , i ) => {
const text = option . freeChoice ? ` Filter by ' ${ option . label } ' ` : option . label
return (
< Option
key = { option . value }
id = { optionId ( i ) }
isHighlighted = { highlightedIndex === i }
isDisabled = { isDisabled ( option ) }
onClick = { ( ) => selectOption ( option ) }
onMouseEnter = { ( ) => setHighlightedIndex ( i ) }
text = { text }
/ >
)
} )
) }
{ renderDropDownContent ( ) }
< / u l >
< / T r a n s i t i o n >
< / T r a n s i t i o n > }
< / d i v >
) ;
)
}
function Spinner ( ) {