import { type ComboboxProps as HeadlessComboboxProps, type ComboboxButtonProps as HeadlessComboboxButtonProps, type ComboboxInputProps as HeadlessComboboxInputProps, type ComboboxOptionProps as HeadlessComboboxOptionProps, type ComboboxOptionsProps as HeadlessComboboxOptionsProps, Label } from '@headlessui/react'; import React, { type ChangeEventHandler, type ElementType, type KeyboardEventHandler, type ReactNode } from 'react'; import type { ExtractProps, IconOrContent } from '../../util/utility-types'; import type { Status } from '../../util/variant-types'; import type { PopoverListItemProps } from '../PopoverListItem/PopoverListItem'; type ComboboxValue = string | { [k: string]: unknown; }; /** * The field holds one value normally, and a list of them when `multiple` is set. */ type ComboboxSelection = ComboboxValue | ComboboxValue[]; export type ComboboxProps = Omit, 'by' | 'defaultValue' | 'onChange' | 'value'> & { /** * Compare option values by a named key, or by a comparison function, instead of by reference. * Useful when the selected value and the option come from different places. * * See: https://headlessui.com/react/combobox#binding-objects-as-values */ by?: string | ((a: ComboboxValue, z: ComboboxValue) => boolean); /** * The default value of the combobox field (when uncontrolled) */ defaultValue?: ComboboxSelection; /** * Fires when a value is selected. Passes the selected value, or the list of selected values * when `multiple` is set. */ onChange?: (value: ComboboxSelection | null) => void; /** * The value of the combobox field (when controlled) */ value?: ComboboxSelection; /** * Screen-reader text for the combobox's label. * * When possible, use a visible label by passing a into `children`. * In rare cases where there's no visible label, you must provide an `aria-label` for screen readers. */ 'aria-label'?: string; /** * Optional className for additional styling. */ className?: string; /** * Name of the form element, which triggers the generation of hidden key/value form fields (e.g. `name=$name[$key]`). * * See: https://headlessui.com/react/combobox#using-with-html-forms */ name?: string; /** * Optional className for additional options menu styling. * * If optionsClassName is provided please include the width property to define * the options menu width. */ optionsClassName?: string; /** * Indicates that field is required for form to be successfully submitted */ required?: boolean; /** * Text under the field used to provide validation hints or error message to describe the input error. */ fieldNote?: ReactNode; /** * Visible text label for the component. */ label?: string; /** * Whether the label is adjacent to the field (horizontal) or above the field (vertical) * * **Default is `"vertical"`**. */ labelLayout?: 'vertical' | 'horizontal'; /** * Whether it should show the field hint or not * * **Default is `"false"`**. */ showHint?: boolean; /** * Status for the field state * * **Default is `"default"`**. */ status?: 'default' | Extract; /** * Add additional descriptive text for the field name */ subLabel?: ReactNode; }; type ComboboxLabelProps = ExtractProps & { disabled?: boolean; required?: boolean; showHint?: boolean; /** * Add additional descriptive text for the field name */ subLabel?: ReactNode; }; type ComboboxOptionsProps = HeadlessComboboxOptionsProps<'div'> & { /** * Where the option list sits relative to the field. When left unset, the list lines up with * the field's bottom left edge and is at least as wide as the field, however many chips it * holds. A width class on the list can make it wider. * * Setting it hands positioning back to HeadlessUI, which measures from the text field itself * rather than the field's border. * * See: https://headlessui.com/react/combobox#positioning-the-options */ anchor?: HeadlessComboboxOptionsProps<'div'>['anchor']; /** * Text shown in place of the options when none are passed in, such as when a query matches * nothing. * * **Default is `"No matches found"`**. */ noMatchesText?: ReactNode; /** * The list element. Merged with the ref used to position the list against the field. */ ref?: React.Ref; }; type ComboboxOptionProps = HeadlessComboboxOptionProps<'div', ComboboxValue> & Pick & { optionClassName?: string; }; type ComboboxButtonProps = HeadlessComboboxButtonProps<'button'> & { /** * Screen-reader text for the toggle, used when the field has no visible label. When there * is one, HeadlessUI names the toggle from that label and this is ignored. * * **Default is `"Show options"`**. */ 'aria-label'?: string; }; type ComboboxInputProps = Omit, 'displayValue' | 'onChange'> & { /** * Screen-reader text for the field. Prefer a visible label on `` (or a * ``); this is the escape hatch when there's no visible label. * * Inherited from `` when not set here. */ 'aria-label'?: string; /** * Optional className for additional styling of the field wrapper. */ className?: string; /** * Maps the currently selected value to the text shown in the text field. Receives `null` * when nothing is selected. * * See: https://headlessui.com/react/combobox#binding-objects-as-values */ displayValue?: (item: ComboboxValue | null) => string; /** * Optional className for additional styling of the inner `` element. */ inputClassName?: string; /** * Fires on every keystroke in the text field. Use it to filter the options passed * to `Combobox.Options`. */ onChange?: ChangeEventHandler; /** * Fires on key down in the text field. Runs before the component's own handling, so calling * `preventDefault()` here opts out of backspace-to-remove. */ onKeyDown?: KeyboardEventHandler; /** * Maps a selected value to the text shown on its chip, when `multiple` is set. * * **Defaults to the value's `label`**, or the value itself when it's a string. */ chipLabel?: (item: ComboboxValue) => string; /** * Leading glyph (icon) or content for a selected value's chip, when `multiple` is set. */ chipLeadingComponent?: (item: ComboboxValue) => IconOrContent; /** * Whether we should truncate the text displayed in the combobox field */ shouldTruncate?: boolean; /** * Whether selected values appear as removable chips inside the field, when `multiple` is set. * Turn it off to surface the selection yourself. Also governs whether backspace on an empty * field removes the last chip, and whether adding a chip selects the query text. * * **Default is `"true"`**. */ showChips?: boolean; }; type ComboboxInputWrapperProps = { /** * The text field (and any other content) placed inside the field's border. */ children?: ReactNode; /** * Optional className for additional styling. */ className?: string; /** * Whether the field is holding selection chips, which lets its contents wrap onto more * than one line. */ hasChips?: boolean; /** * Status for the field state * * **Default is `"default"`**. */ status?: 'default' | Extract; }; /** * ## Usage * * A text field paired with a list of options. Typing filters the list, so use it where a * `Select` would be unwieldy. Supports controlled and uncontrolled behavior. * * | Type/Use | Description | Example | * |----------|-------------|---------| * | Standard | Type to filter a list of options; one can be selected. | Country selector. Assignee picker. | * | Multi-select | Type to filter, and select more than one option from the list. | Filter panels. Role or permission assignments. | * | Disabled | Non-interactive; used to show unavailable or inactive states. | Feature-gated selections. Incomplete forms. | * | Preselected | Default selection appears in the field before user interacts. | Recommended settings. "Most common" default. | * * Filtering is left to the consumer, since only the consumer knows how the option data is * shaped and where it comes from. Use `onChange` on `Combobox.Input` to capture the query, * then pass the filtered list to `Combobox.Options`. * * ### Best Practices * * * Use `Combobox` for long lists where typing is faster than scrolling. For 3-10 options, use `Select`. * * Order the menu options logically to make it easier for users to find the option they want. Default to alphabetical order. * * Keep the option list the same width as the field that triggered it. This is the default. * * When the filtered list is empty, `Combobox.Options` says "No matches found" so the field doesn't look broken. Use `noMatchesText` to change the wording. * * When only so many values may be selected, say so in the `subLabel` (e.g., "Choose up to 3"). If the user selects more than that, set `status` to `"critical"` and explain in the `fieldNote`. Don't disable the remaining options: it hides the choices without telling the user why. * * ## Interaction * * The field is always editable, so users can either type to narrow the list or open the full * list with the toggle button. In single-select mode, only one selection can be made. In * multi-select mode, one or more selections can be made from the list, and each one shows in * the field as a chip. Adding a chip selects the query text that found it, so the next keystroke * begins a new search. A chip goes away either through its own close button or by pressing * backspace in an empty field. * * ## Content & Accessibility * * ### Do's * * * Use short, precise labels whenever possible. * * Avoid truncated items. * * In short lists, order from most common to least common choices. * * In longer lists use alphabetical order, but if there are 2 or 3 very common selections, consider repeating them at the top of the list. * * Use sentence case. * * ### Don'ts * * * Use periods at the end of labels. * * Place placeholder text within the field; it can cause accessibility issues with color contrast, inconsistent screen-reader behavior, and text disappearing as users type. * * ## Resources * * * https://headlessui.com/react/combobox */ export declare function Combobox({ 'aria-label': ariaLabel, by, children, className, disabled, fieldNote, label, labelLayout, name, optionsClassName, required, showHint, status, onChange: theirOnChange, subLabel, ...other }: ComboboxProps): React.JSX.Element; export declare namespace Combobox { var displayName: string; var Button: { (props: ComboboxButtonProps): React.JSX.Element; displayName: string; }; var Input: { (props: ComboboxInputProps): React.JSX.Element; displayName: string; }; var InputWrapper: React.ForwardRefExoticComponent>; var Label: { ({ children: label, required, className, disabled, showHint, subLabel, }: ComboboxLabelProps): React.JSX.Element; displayName: string; }; var Option: { (props: ComboboxOptionProps): React.JSX.Element; displayName: string; }; var Options: { (props: ComboboxOptionsProps): React.JSX.Element; displayName: string; }; } /** * The component providing the field border, background, and the toggle button, with space * for the text field. Use it directly when you need to render your own `ComboboxInput` but * want to keep the EDS field appearance. */ export declare const ComboboxInputWrapper: React.ForwardRefExoticComponent>; export {};