> Markdown version of [Combo Box](https://vaadin.com/docs/v23/components/combo-box). Section index: [llms.txt](https://vaadin.com/docs/v23/components/llms.txt)

# Combo Box

Combo Box allows the user to choose a value from a filterable list of options presented in an overlay. It supports lazy loading and can be configured to accept custom typed values.

**TypeScript** — `combo-box-basic.ts`

```html
<vaadin-combo-box
  label="Country"
  item-label-path="name"
  item-value-path="id"
  .items="${this.items}"
></vaadin-combo-box>
```

**TypeScript** — `Country.ts`

```typescript
/**
 * This module is generated from com.vaadin.demo.domain.Country.
 * All changes to this file are overridden. Consider editing the corresponding Java file if necessary.
 * @see {@link file:///srv/jenkins/workspace/docs/docs-site-v23/docs/src/main/java/com/vaadin/demo/domain/Country.java}
 */
export default interface Country {
  name: string;
  abbreviation: string;
  id: number;
}
```

**Java** — `ComboBoxBasic.java`

```java
ComboBox<Country> comboBox = new ComboBox<>("Country");
comboBox.setItems(DataService.getCountries());
comboBox.setItemLabelGenerator(Country::getName);
add(comboBox);
```

**Java** — `Country.java`

```java
public class Country {

    @Nonnull
    private String name;

    @Nonnull
    private String abbreviation;

    @Nonnull
    private Integer id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public void setAbbreviation(String abbreviation) {
        this.abbreviation = abbreviation;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Country)) {
            return false;
        }
        Country other = (Country) obj;
        return id == other.id;
    }
}
```

The overlay opens when the user clicks the field using a pointing device. Using the Up/Down arrow keys or typing a character (found in at least one of the options) when the field is focused also opens the popup.

## <a id="common-input-field-features"></a>Common Input Field Features

Combo Box includes all Text Field and shared input field features.

- [See Text Field features](https://vaadin.com/docs/v23/components/text-field.md)

- [See shared input field features](https://vaadin.com/docs/v23/components/input-fields.md)

## <a id="custom-value-entry"></a>Custom Value Entry

Combo Box can be configured to allow entering custom values that aren’t included in the list of options.

**TypeScript** — `combo-box-custom-entry-1.ts`

```html
<vaadin-combo-box
  allow-custom-value
  label="Browser"
  helper-text="Select or type a browser"
  .items="${this.items}"
></vaadin-combo-box>
```

**Java** — `ComboBoxCustomEntry1.java`

```java
ComboBox<String> comboBox = new ComboBox<>("Browser");
comboBox.setAllowCustomValue(true);
add(comboBox);
```

Allowing custom entry is useful when you need to present the most common choices but still give users the freedom to enter their own options.

Custom values can also be stored and added to the list of options:

**TypeScript** — `combo-box-custom-entry-2.ts`

```html
<vaadin-combo-box
  allow-custom-value
  label="Browser"
  helper-text="Select or type a browser"
  .items="${this.items}"
  @custom-value-set="${(event: ComboBoxCustomValueSetEvent) => {
    this.items = [...this.items, event.detail];
  }}"
></vaadin-combo-box>
```

**Java** — `ComboBoxCustomEntry2.java`

```java
ComboBox<String> comboBox = new ComboBox<>("Browser");
comboBox.setAllowCustomValue(true);
comboBox.addCustomValueSetListener(e -> {
    String customValue = e.getDetail();
    items.add(customValue);
    comboBox.setItems(items);
    comboBox.setValue(customValue);
});
add(comboBox);
```

## <a id="custom-item-presentation"></a>Custom Item Presentation

See [Select, Custom Item Presentation.](https://vaadin.com/docs/v23/components/select.md#custom-item-presentation)

Items can be customized to display more information than a single line of text.

**TypeScript** — `combo-box-presentation.ts`

```html
<vaadin-combo-box
  label="Choose doctor"
  item-label-path="displayName"
  .filteredItems="${this.filteredItems}"
  style="--vaadin-combo-box-overlay-width: 16em"
  @filter-changed="${this.filterChanged}"
  ${comboBoxRenderer(this.renderer, [])}
></vaadin-combo-box>

...

// NOTE
// We are using inline styles here to keep the example simple.
// We recommend placing CSS in a separate style sheet and
// encapsulating the styling in a new component.

private renderer: ComboBoxLitRenderer<Person> = (person) => html`
  <div style="display: flex;">
    <img
      style="height: var(--lumo-size-m); margin-right: var(--lumo-space-s);"
      src="${person.pictureUrl}"
      alt="Portrait of ${person.firstName} ${person.lastName}"
    />
    <div>
      ${person.firstName} ${person.lastName}
      <div style="font-size: var(--lumo-font-size-s); color: var(--lumo-secondary-text-color);">
        ${person.profession}
      </div>
    </div>
  </div>
`;
```

**TypeScript** — `Person.ts`

```typescript
import Address from './Address';

/**
 * This module is generated from com.vaadin.demo.domain.Person.
 * All changes to this file are overridden. Consider editing the corresponding Java file if necessary.
 * @see {@link file:///srv/jenkins/workspace/docs/docs-site-v23/docs/src/main/java/com/vaadin/demo/domain/Person.java}
 */
export default interface Person {
  firstName: string;
  lastName: string;
  email: string;
  birthday: string;
  id: number;
  subscriber: boolean;
  membership: string;
  pictureUrl: string;
  profession: string;
  address: Address;
  managerId?: number;
  manager: boolean;
  status: string;
}
```

**Java** — `ComboBoxPresentation.java`

```java
ComboBox<Person> comboBox = new ComboBox<>("Choose doctor");
comboBox.setItems(filter, DataService.getPeople());
comboBox.setItemLabelGenerator(
        person -> person.getFirstName() + " " + person.getLastName());
comboBox.setRenderer(createRenderer());
comboBox.getStyle().set("--vaadin-combo-box-overlay-width", "16em");
add(comboBox);

...

// NOTE
// We are using inline styles here to keep the example simple.
// We recommend placing CSS in a separate style sheet and to
// encapsulating the styling in a new component.

private Renderer<Person> createRenderer() {
    StringBuilder tpl = new StringBuilder();
    tpl.append("<div style=\"display: flex;\">");
    tpl.append(
            "  <img style=\"height: var(--lumo-size-m); margin-right: var(--lumo-space-s);\" src=\"${item.pictureUrl}\" alt=\"Portrait of ${item.firstName} ${item.lastName}\" />");
    tpl.append("  <div>");
    tpl.append("    ${item.firstName} ${item.lastName}");
    tpl.append(
            "    <div style=\"font-size: var(--lumo-font-size-s); color: var(--lumo-secondary-text-color);\">${item.profession}</div>");
    tpl.append("  </div>");
    tpl.append("</div>");

    return LitRenderer.<Person> of(tpl.toString())
            .withProperty("pictureUrl", Person::getPictureUrl)
            .withProperty("firstName", Person::getFirstName)
            .withProperty("lastName", Person::getLastName)
            .withProperty("profession", Person::getProfession);
}
```

**Java** — `Person.java`

```java
public class Person {

    @Nonnull
    private String firstName;

    @Nonnull
    private String lastName;

    @Nonnull
    private String email;

    @Nonnull
    private Date birthday;

    @Nonnull
    private Integer id;

    @Nonnull
    private Boolean subscriber;

    @Nonnull
    private String membership;

    @Nonnull
    private String pictureUrl;

    @Nonnull
    private String profession;

    @Nonnull
    private Address address;

    private Integer managerId;

    @Nonnull
    private Boolean manager;

    @Nonnull
    private String status;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFullName() {
        return firstName + " " + lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isSubscriber() {
        return subscriber;
    }

    public void setSubscriber(boolean subscriber) {
        this.subscriber = subscriber;
    }

    public String getMembership() {
        return membership;
    }

    public void setMembership(String membership) {
        this.membership = membership;
    }

    public String getPictureUrl() {
        return pictureUrl;
    }

    public void setPictureUrl(String pictureUrl) {
        this.pictureUrl = pictureUrl;
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Person)) {
            return false;
        }
        Person other = (Person) obj;
        return id == other.id;
    }

    public Integer getManagerId() {
        return managerId;
    }

    public void setManagerId(Integer managerId) {
        this.managerId = managerId;
    }

    public boolean isManager() {
        return manager;
    }

    public void setManager(boolean manager) {
        this.manager = manager;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
```

Use a custom filter to allow the user to search by the rendered properties. It’s recommended to make filtering case insensitive.

## <a id="auto-open"></a>Auto Open

The overlay opens automatically when the field is focused using a pointer (mouse or touch), or when the user types in the field. You can disable that to only open the overlay when the toggle button or Up/Down arrow keys are pressed.

**TypeScript** — `combo-box-auto-open.ts`

```html
<vaadin-combo-box
  auto-open-disabled
  label="Country"
  item-label-path="name"
  item-value-path="id"
  .items="${this.items}"
></vaadin-combo-box>
```

**TypeScript** — `Country.ts`

```typescript
/**
 * This module is generated from com.vaadin.demo.domain.Country.
 * All changes to this file are overridden. Consider editing the corresponding Java file if necessary.
 * @see {@link file:///srv/jenkins/workspace/docs/docs-site-v23/docs/src/main/java/com/vaadin/demo/domain/Country.java}
 */
export default interface Country {
  name: string;
  abbreviation: string;
  id: number;
}
```

**Java** — `ComboBoxAutoOpen.java`

```java
ComboBox<Country> comboBox = new ComboBox<>("Country");
comboBox.setAutoOpen(false);
add(comboBox);
```

**Java** — `Country.java`

```java
public class Country {

    @Nonnull
    private String name;

    @Nonnull
    private String abbreviation;

    @Nonnull
    private Integer id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public void setAbbreviation(String abbreviation) {
        this.abbreviation = abbreviation;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Country)) {
            return false;
        }
        Country other = (Country) obj;
        return id == other.id;
    }
}
```

## <a id="popup-width"></a>Popup Width

The width of the popup is, by default, the same width as the input field. The popup width can be overridden to any fixed width in cases where the default width is too narrow.

**TypeScript** — `combo-box-popup-width.ts`

```html
<vaadin-combo-box
  style="--vaadin-combo-box-overlay-width: 350px"
  label="Employee"
  item-label-path="displayName"
  item-value-path="id"
  .items="${this.items}"
></vaadin-combo-box>
```

**TypeScript** — `Person.ts`

```typescript
import Address from './Address';

/**
 * This module is generated from com.vaadin.demo.domain.Person.
 * All changes to this file are overridden. Consider editing the corresponding Java file if necessary.
 * @see {@link file:///srv/jenkins/workspace/docs/docs-site-v23/docs/src/main/java/com/vaadin/demo/domain/Person.java}
 */
export default interface Person {
  firstName: string;
  lastName: string;
  email: string;
  birthday: string;
  id: number;
  subscriber: boolean;
  membership: string;
  pictureUrl: string;
  profession: string;
  address: Address;
  managerId?: number;
  manager: boolean;
  status: string;
}
```

**Java** — `ComboBoxPopupWidth.java`

```java
ComboBox<Person> comboBox = new ComboBox<>("Employee");
comboBox.getStyle().set("--vaadin-combo-box-overlay-width", "350px");
add(comboBox);
```

**Java** — `Person.java`

```java
public class Person {

    @Nonnull
    private String firstName;

    @Nonnull
    private String lastName;

    @Nonnull
    private String email;

    @Nonnull
    private Date birthday;

    @Nonnull
    private Integer id;

    @Nonnull
    private Boolean subscriber;

    @Nonnull
    private String membership;

    @Nonnull
    private String pictureUrl;

    @Nonnull
    private String profession;

    @Nonnull
    private Address address;

    private Integer managerId;

    @Nonnull
    private Boolean manager;

    @Nonnull
    private String status;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFullName() {
        return firstName + " " + lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isSubscriber() {
        return subscriber;
    }

    public void setSubscriber(boolean subscriber) {
        this.subscriber = subscriber;
    }

    public String getMembership() {
        return membership;
    }

    public void setMembership(String membership) {
        this.membership = membership;
    }

    public String getPictureUrl() {
        return pictureUrl;
    }

    public void setPictureUrl(String pictureUrl) {
        this.pictureUrl = pictureUrl;
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Person)) {
            return false;
        }
        Person other = (Person) obj;
        return id == other.id;
    }

    public Integer getManagerId() {
        return managerId;
    }

    public void setManagerId(Integer managerId) {
        this.managerId = managerId;
    }

    public boolean isManager() {
        return manager;
    }

    public void setManager(boolean manager) {
        this.manager = manager;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
```

## <a id="placeholder"></a>Placeholder

Use the placeholder feature to provide an inline text prompt for the field. Don’t create, or use, a separate item for this purpose.

**TypeScript** — `combo-box-placeholder.ts`

```html
<vaadin-combo-box
  placeholder="Select employee"
  label="Employee"
  item-label-path="displayName"
  item-value-path="id"
  .items="${this.items}"
></vaadin-combo-box>
```

**Java** — `ComboBoxPlaceholder.java`

```java
ComboBox<Person> comboBox = new ComboBox<>("Employee");
comboBox.setPlaceholder("Select employee");
add(comboBox);
```

## <a id="custom-filtering"></a>Custom Filtering

Combo Box’s filtering, by default, is configured to only show items that contain the entered value:

`combo-box-filtering-1.ts`

```html
<vaadin-combo-box
  label="Country"
  item-label-path="name"
  item-value-path="id"
  .items="${this.items}"
></vaadin-combo-box>
```

Custom filtering is also possible. For example, if you only want to show items that start with the user’s input:

**TypeScript** — `combo-box-filtering-2.ts`

```typescript
@customElement('combo-box-filtering-2')
export class Example extends LitElement {
  protected override createRenderRoot() {
    const root = super.createRenderRoot();
    // Apply custom theme (only supported if your app uses one)
    applyTheme(root);
    return root;
  }

  @state()
  private allItems: Country[] = [];

  @state()
  private filteredItems: Country[] = [];

  protected override async firstUpdated() {
    const countries = await getCountries();
    this.allItems = countries;
    this.filteredItems = countries;
  }

  protected override render() {
    return html`
      <vaadin-combo-box
        label="Country"
        item-label-path="name"
        item-value-path="id"
        .filteredItems="${this.filteredItems}"
        @filter-changed="${this.filterChanged}"
      ></vaadin-combo-box>
    `;
  }

  private filterChanged(event: ComboBoxFilterChangedEvent) {
    const filter = event.detail.value;
    this.filteredItems = this.allItems.filter(({ name }) =>
      name.toLowerCase().startsWith(filter.toLowerCase())
    );
  }
}
```

**Java** — `ComboBoxFiltering2.java`

```java
ComboBox<Country> comboBox = new ComboBox<>("Country");

ItemFilter<Country> filter = (country, filterString) -> country
        .getName().toLowerCase().startsWith(filterString.toLowerCase());
comboBox.setItems(filter, DataService.getCountries());

add(comboBox);
```

## <a id="usage-as-autocomplete-field"></a>Usage as Autocomplete Field

As the user is typing, the Combo Box filters out the options that don’t match. Once the correct value has been found, the user can use the Up/Down arrow keys to navigate the list and the Enter key to set the value, essentially using the Combo Box as an autocomplete field.

## <a id="best-practices"></a>Best Practices

Combo Box supports lazy loading for large datasets. It reduces the initial load time, consumes less bandwidth and resources.

> **Note: Don’t use as a menu**
>
> Combo Box is an input field component, not a generic menu component. Use the Menu Bar component to create overlays for actions.

## <a id="related-components"></a>Related Components

| Component                                                              | Usage recommendations                                                                     |
| ---------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| [Select](https://vaadin.com/docs/v23/components/select.md)             | Simpler overlay selection field without filtering, lazy loading or custom value entry.    |
| [Radio Button](https://vaadin.com/docs/v23/components/radio-button.md) | Better accessibility than Combo Box, as all options are visible without user interaction. |
| [List Box](https://vaadin.com/docs/v23/components/list-box.md)         | Scrollable inline list of options. Supports single and multi-select.                      |
| [Menu Bar](https://vaadin.com/docs/v23/components/menu-bar.md)         | Overlay menus for items that trigger actions.                                             |

`1DFB67E2-2A7B-4339-8C20-FC546C664BB2`
