'use client' import React, { useCallback, useMemo, useRef, useState } from 'react' import type { PopupProps } from '../Popup/index.js' import { SearchIcon } from '../../icons/Search/index.js' import { useTranslation } from '../../providers/Translation/index.js' import { matchesSearchQuery } from '../../utilities/matchesSearchQuery.js' import { Popup, PopupList } from '../Popup/index.js' import './index.css' const baseClass = 'combobox' /** * @internal * @experimental */ export type ComboboxEntry = { Component: React.ReactNode name: string } /** * @internal * @experimental */ export type ComboboxProps = { /** Accessible label for the list of entries */ 'aria-label'?: string entries: ComboboxEntry[] /** * Pinned below the entries, outside the scroll area and unaffected by search. * For actions that belong to the list as a whole, such as "create new". */ footer?: (args: { close: () => void }) => React.ReactNode /** * Pinned above the search, outside the scroll area and unaffected by it. For * the one entry the list is about, such as the item already selected. */ header?: (args: { close: () => void }) => React.ReactNode /** Minimum number of entries required to show search */ minEntriesForSearch?: number onSelect?: (entry: ComboboxEntry) => void searchPlaceholder?: string /** * Whether to say so when a search matches nothing. * * Only shown for a nonempty query against a nonempty list. */ showEmptyMessage?: boolean } & Omit /** * A wrapper on top of Popup + PopupList.ButtonGroup that adds search functionality. * * @internal - this component may be removed or receive breaking changes in minor releases. * @experimental */ export const Combobox: React.FC = (props) => { const { 'aria-label': ariaLabel, entries, footer, header, minEntriesForSearch = 8, onSelect, onToggleClose, onToggleOpen, searchPlaceholder = 'Search...', showEmptyMessage = true, ...popupProps } = props const { t } = useTranslation() const [searchValue, setSearchValue] = useState('') const isOpenRef = useRef(false) const searchInputRef = useRef(null) const contentRef = useRef(null) const filteredEntries = useMemo( () => entries.filter((entry) => matchesSearchQuery({ label: entry.name, query: searchValue })), [entries, searchValue], ) const showSearch = entries.length >= minEntriesForSearch const hasResults = filteredEntries.length > 0 const [lockedWidth, setLockedWidth] = useState(null) const handleToggleOpen = useCallback( (active: boolean) => { isOpenRef.current = active if (active && showSearch) { setTimeout(() => { searchInputRef.current?.focus() }, 100) } onToggleOpen?.(active) }, [showSearch, onToggleOpen], ) const handleToggleClose = useCallback(() => { isOpenRef.current = false setSearchValue('') setLockedWidth(null) onToggleClose?.() }, [onToggleClose]) return ( (
{header &&
{header({ close })}
} {showSearch && (
{ // Measure before filtering removes the entries that determine the width. if (e.target.value && lockedWidth === null) { setLockedWidth(contentRef.current?.offsetWidth ?? null) } else if (!e.target.value) { setLockedWidth(null) } setSearchValue(e.target.value) }} placeholder={searchPlaceholder} ref={searchInputRef} type="text" value={searchValue} />
)} {hasResults ? (
{filteredEntries.map((entry, index) => { const handleClick = () => { if (onSelect) { onSelect(entry) } close() } return (
{ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() handleClick() } }} role="menuitem" tabIndex={0} > {entry.Component}
) })}
) : ( showEmptyMessage && entries.length > 0 && searchValue.trim() !== '' && (
{t('general:noMatchesFound')}
) )} {footer &&
{footer({ close })}
}
)} /> ) }