import CwElement from '../../internal/cw-element.js'; import CwIcon from '../icon/icon.component.js'; import CwPopup from '../popup/popup.component.js'; import CwSpinner from '../spinner/spinner.component.js'; import CwTag from '../tag/tag.component.js'; import CwVisuallyHidden from '../visually-hidden/visually-hidden.component.js'; import type { CSSResultGroup, TemplateResult } from 'lit'; import type { CwFormControl } from '../../internal/cw-element.js'; import type CwOption from '../option/option.component.js'; /** * @summary Comboboxes combine a text input with a listbox, allowing users to filter a list of options by typing. * @documentation https://cw-elements-b0c7e5.gitlab.io/components/combobox * @status experimental * @since 1.1 * * @dependency cw-icon * @dependency cw-popup * @dependency cw-spinner * @dependency cw-tag * @dependency cw-visually-hidden * * @slot - The listbox options. Must be `` elements. You can use `` to group items visually. * @slot label - The input's label. Alternatively, you can use the `label` attribute. * @slot prefix - Used to prepend a presentational icon or similar element to the combobox. * @slot suffix - Used to append a presentational icon or similar element to the combobox. * @slot clear-icon - An icon to use in lieu of the default clear icon. * @slot expand-icon - The icon to show when the control is expanded and collapsed. Rotates on open and close. * @slot no-options - Content to show in the listbox when no options match the current filter. * @slot help-text - Text that describes how to use the input. Alternatively, you can use the `help-text` attribute. * * @event cw-change - Emitted when the control's value changes. * @event cw-clear - Emitted when the control's value is cleared. * @event cw-input - Emitted when the control receives input, including as the user types to filter options. * @event cw-focus - Emitted when the control gains focus. * @event cw-blur - Emitted when the control loses focus. * @event cw-show - Emitted when the combobox's listbox opens. * @event cw-after-show - Emitted after the combobox's listbox opens and all animations are complete. * @event cw-hide - Emitted when the combobox's listbox closes. * @event cw-after-hide - Emitted after the combobox's listbox closes and all animations are complete. * @event cw-invalid - Emitted when the form control has been checked for validity and its constraints aren't satisfied. * * @csspart form-control - The form control that wraps the label, input, and help text. * @csspart form-control-label - The label's wrapper. * @csspart form-control-input - The combobox's wrapper. * @csspart form-control-help-text - The help text's wrapper. * @csspart combobox - The container that wraps the prefix, suffix, input, tags, clear icon, and expand button. * @csspart prefix - The container that wraps the prefix slot. * @csspart suffix - The container that wraps the suffix slot. * @csspart display-input - The component's `` element. * @csspart listbox - The listbox container where options are slotted. * @csspart no-options - The container shown when no options match the current filter. * @csspart loading - The container shown while `loading` is set. * @csspart tags - The container that houses option tags when `multiple` is used. * @csspart tag - The individual tags that represent each selected option. * @csspart tag__base - The tag's base part. * @csspart tag__content - The tag's content part. * @csspart tag__remove-button - The tag's remove button. * @csspart tag__remove-button__base - The tag's remove button base part. * @csspart clear-button - The clear button. * @csspart expand-icon - The container that wraps the expand icon. */ export default class CwCombobox extends CwElement implements CwFormControl { static styles: CSSResultGroup; static dependencies: { 'cw-icon': typeof CwIcon; 'cw-popup': typeof CwPopup; 'cw-spinner': typeof CwSpinner; 'cw-tag': typeof CwTag; 'cw-visually-hidden': typeof CwVisuallyHidden; }; private readonly formControlController; private readonly hasSlotController; private readonly localize; private closeWatcher; /** A free-text value the user has typed that doesn't match any option. Only used when `allowCustomValue` is set. */ private customValue; popup: CwPopup; combobox: HTMLSlotElement; displayInput: HTMLInputElement; valueInput: HTMLInputElement; listbox: HTMLSlotElement; private hasFocus; displayLabel: string; currentOption: CwOption | undefined; selectedOptions: CwOption[]; private activeOptionId; private hasVisibleOptions; private valueHasChanged; private isFiltering; /** The name of the combobox, submitted as a name/value pair with form data. */ name: string; private _value; get value(): string | string[]; /** * The current value of the combobox, submitted as a name/value pair with form data. When `multiple` is enabled, the * value attribute will be a space-delimited list of values based on the options selected, and the value property will * be an array. **For this reason, values must not contain spaces.** */ set value(val: string | string[]); /** The default value of the form control. Primarily used for resetting the form control. */ defaultValue: string | string[]; /** The combobox's size. */ size: 'small' | 'medium' | 'large'; /** Placeholder text to show as a hint when the combobox is empty. */ placeholder: string; /** Allows more than one option to be selected. */ multiple: boolean; /** * The maximum number of selected options to show when `multiple` is true. After the maximum, "+n" will be shown to * indicate the number of additional items that are selected. Set to 0 to remove the limit. */ maxOptionsVisible: number; /** Disables the combobox control. */ disabled: boolean; /** Adds a clear button when the combobox is not empty. */ clearable: boolean; /** * When set, values typed by the user that don't match any `` are accepted as the control's value. Only * applies when `multiple` is not set. */ allowCustomValue: boolean; /** * Shows a loading indicator and "no options" message in the listbox. Useful while options are being fetched * asynchronously in response to the `cw-input` event. */ loading: boolean; /** * Indicates whether or not the listbox is open. You can toggle this attribute to show and hide the listbox, or you can * use the `show()` and `hide()` methods and this attribute will reflect the combobox's open state. */ open: boolean; /** * Enable this option to prevent the listbox from being clipped when the component is placed inside a container with * `overflow: auto|scroll`. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios. */ hoist: boolean; /** Draws a filled combobox. */ filled: boolean; /** Draws a pill-style combobox with rounded edges. */ pill: boolean; /** The combobox's label. If you need to display HTML, use the `label` slot instead. */ label: string; /** * The preferred placement of the combobox's listbox. Note that the actual placement may vary as needed to keep the * listbox inside of the viewport. */ placement: 'top' | 'bottom'; /** The combobox's help text. If you need to display HTML, use the `help-text` slot instead. */ helpText: string; /** * By default, form controls are associated with the nearest containing `
` element. This attribute allows you * to place the form control outside of a form and associate it with the form that has this `id`. The form must be in * the same document or shadow root for this to work. */ form: string; /** The combobox's required attribute. */ required: boolean; /** * A function that customizes the tags to be rendered when multiple=true. The first argument is the option, the second * is the current tag's index. The function should return either a Lit TemplateResult or a string containing trusted * HTML of the symbol to render at the specified value. */ getTag: (option: CwOption, index: number) => TemplateResult | string | HTMLElement; /** Gets the validity state object */ get validity(): ValidityState; /** Gets the validation message */ get validationMessage(): string; connectedCallback(): void; private addOpenListeners; private removeOpenListeners; private handleFocus; private handleBlur; private handleDocumentFocusIn; private handleDocumentKeyDown; private handleDocumentMouseDown; private handleLabelClick; private handleComboboxMouseDown; private handleComboboxKeyDown; private handleInput; private handleClearClick; private handleClearMouseDown; private handleOptionClick; private handleListboxMouseDown; handleDefaultSlotChange(): void; private handleTagRemove; private getAllOptions; private getVisibleOptions; private filterOptions; private setCurrentOption; private setSelectedOptions; private toggleOptionSelection; private updateCustomValue; private commitCustomValue; private selectionChanged; private selectOption; protected get tags(): TemplateResult<1>[]; private handleInvalid; handleDisabledChange(): void; attributeChangedCallback(name: string, oldVal: string | null, newVal: string | null): void; handleValueChange(): void; handleOpenChange(): Promise; /** Shows the listbox. */ show(): Promise; /** Hides the listbox. */ hide(): Promise; /** Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. */ checkValidity(): boolean; /** Gets the associated form, if one exists. */ getForm(): HTMLFormElement | null; /** Checks for validity and shows the browser's validation message if the control is invalid. */ reportValidity(): boolean; /** Sets a custom validation message. Pass an empty string to restore validity. */ setCustomValidity(message: string): void; /** Sets focus on the control. */ focus(options?: FocusOptions): void; /** Removes focus from the control. */ blur(): void; render(): TemplateResult<1>; }