2024-02-05 14:32:50 +00:00
|
|
|
import {
|
|
|
|
Combobox,
|
|
|
|
ComboboxCancel,
|
|
|
|
ComboboxGroup,
|
|
|
|
ComboboxGroupLabel,
|
|
|
|
ComboboxItem,
|
|
|
|
ComboboxItemValue,
|
|
|
|
ComboboxPopover,
|
|
|
|
ComboboxProvider,
|
|
|
|
} from '@ariakit/react'
|
|
|
|
import { ComponentType, ForwardedRef, forwardRef, startTransition, useState } from 'react'
|
|
|
|
import './Autocomplete.css'
|
|
|
|
import { Icon } from './Icon'
|
2024-02-05 20:46:07 +00:00
|
|
|
import { Spinner } from './Spinner'
|
2024-02-05 14:32:50 +00:00
|
|
|
|
2024-05-22 15:55:49 +00:00
|
|
|
export interface DropdownOption {
|
2024-02-05 14:32:50 +00:00
|
|
|
label: string
|
|
|
|
value: string
|
|
|
|
group?: string
|
|
|
|
}
|
|
|
|
|
2024-05-22 15:55:49 +00:00
|
|
|
interface AutocompleteProps {
|
2024-02-05 14:32:50 +00:00
|
|
|
customUI?: React.ReactNode
|
|
|
|
groups?: string[]
|
|
|
|
groupsToIcon?: {
|
|
|
|
[key: string]: ComponentType<{
|
|
|
|
className?: string
|
|
|
|
}>
|
|
|
|
}
|
|
|
|
groupsToLabel?: { [key: string]: string }
|
|
|
|
isLoading: boolean
|
|
|
|
options: DropdownOption[]
|
|
|
|
onChange: (value: string) => void
|
|
|
|
onInputChange: (value: string) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_GROUP = 'autocomplete-default'
|
|
|
|
|
|
|
|
const Autocomplete = forwardRef(function Autocomplete(
|
|
|
|
{
|
|
|
|
customUI,
|
|
|
|
groups = [DEFAULT_GROUP],
|
|
|
|
groupsToIcon,
|
|
|
|
groupsToLabel,
|
|
|
|
isLoading,
|
|
|
|
options,
|
|
|
|
onInputChange,
|
|
|
|
onChange,
|
|
|
|
}: AutocompleteProps,
|
|
|
|
ref: ForwardedRef<HTMLInputElement>
|
|
|
|
) {
|
|
|
|
const [open, setOpen] = useState(false)
|
|
|
|
const [value, setValue] = useState('')
|
|
|
|
|
|
|
|
const renderedGroups = groups.map((group) => {
|
|
|
|
const filteredOptions = options.filter(
|
|
|
|
({ group: optionGroup }) => optionGroup === group || group === DEFAULT_GROUP
|
|
|
|
)
|
|
|
|
if (filteredOptions.length === 0) return null
|
|
|
|
|
|
|
|
return (
|
2024-02-05 20:46:07 +00:00
|
|
|
<ComboboxGroup key={group}>
|
2024-02-05 14:32:50 +00:00
|
|
|
{groupsToLabel?.[group] && (
|
2024-02-05 20:46:07 +00:00
|
|
|
<ComboboxGroupLabel key={`${group}-group`} className="autocomplete__group">
|
|
|
|
{groupsToLabel[group]}
|
2024-02-05 14:32:50 +00:00
|
|
|
</ComboboxGroupLabel>
|
|
|
|
)}
|
|
|
|
{filteredOptions.map(({ label, value }) => {
|
|
|
|
const Icon = groupsToIcon?.[group]
|
|
|
|
return (
|
|
|
|
<ComboboxItem key={`${label}-${value}`} className="autocomplete__item" value={value}>
|
|
|
|
{Icon && <Icon className="autocomplete__item__icon" />}
|
|
|
|
<ComboboxItemValue value={label} />
|
|
|
|
</ComboboxItem>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</ComboboxGroup>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ComboboxProvider<string>
|
|
|
|
defaultSelectedValue=""
|
|
|
|
open={open}
|
|
|
|
setOpen={setOpen}
|
|
|
|
resetValueOnHide
|
|
|
|
includesBaseElement={false}
|
|
|
|
setValue={(newValue) => {
|
|
|
|
startTransition(() => setValue(newValue))
|
|
|
|
onInputChange(newValue)
|
|
|
|
}}
|
|
|
|
setSelectedValue={(newValue) => onChange(newValue)}
|
|
|
|
>
|
|
|
|
<div className="autocomplete__wrapper">
|
|
|
|
{isLoading ? (
|
|
|
|
<Spinner className="autocomplete__icon" />
|
|
|
|
) : (
|
|
|
|
<Icon className="autocomplete__icon" icon="search" small />
|
|
|
|
)}
|
|
|
|
|
2024-02-06 09:49:31 +00:00
|
|
|
<Combobox placeholder="Search…" ref={ref} className="autocomplete__input" value={value} />
|
2024-02-05 14:32:50 +00:00
|
|
|
|
|
|
|
{value && <ComboboxCancel className="autocomplete__cancel" />}
|
|
|
|
|
2024-02-06 09:49:31 +00:00
|
|
|
{value && options.length !== 0 && (
|
|
|
|
<ComboboxPopover sameWidth className="autocomplete__popover">
|
2024-02-05 14:32:50 +00:00
|
|
|
{customUI}
|
2024-02-06 09:49:31 +00:00
|
|
|
{renderedGroups}
|
2024-02-05 14:32:50 +00:00
|
|
|
</ComboboxPopover>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</ComboboxProvider>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-02-05 20:46:07 +00:00
|
|
|
export { Autocomplete }
|