iced-cube

Forms

Combobox

A text field with suggestions that filter as you type.

use iced_cube::forms::combobox::{self, State, combobox};
API reference
Default: Suggestions filter as you type. The list starts open here so you can see it.Default: Suggestions filter as you type. The list starts open here so you can see it.
Static preview
Default. Suggestions filter as you type. The list starts open here so you can see it.

Usage

Use a combobox when the list is long enough that people would rather type than scroll, such as timezones or countries. For a handful of options, a select is simpler.

The app owns a combobox::State and passes every event to update. Clicking the field or pressing the down arrow opens the list; typing filters it. The selected value shows in the field while the list is closed, with the caret at its end, and as the placeholder while you type. In a field with an error, set .invalid(true) so the border matches the message, as the field example does. The list is as wide as the field, draws its rows like every other menu and list, and stays inside the window.

The combobox handles its own keys while its field has focus or its list is open, so there is no need to subscribe to key presses. Inside a dialog or popover, Escape closes the list first and leaves the dialog or popover open. The list closes when the field loses focus or when you click elsewhere; that click goes no further and the field gives up focus. Long lists show a window of rows that follows the highlight and scrolls with the mouse wheel.

Examples

Empty results

A message in the list when nothing matches the query.

Empty results: A message in the list when nothing matches the query.Empty results: A message in the list when nothing matches the query.
Static preview

In a field

A labelled combobox showing its selected value, a required one marked invalid until something is picked, and a disabled one.

In a field: A labelled combobox showing its selected value, a required one marked invalid until something is picked, and a disabled one.In a field: A labelled combobox showing its selected value, a required one marked invalid until something is picked, and a disabled one.
Static preview

Keyboard shortcuts

The default keymap with Ctrl+N and Ctrl+P added. The combobox handles its keys itself while it has focus.

Keyboard shortcuts: The default keymap with Ctrl+N and Ctrl+P added. The combobox handles its keys itself while it has focus.Keyboard shortcuts: The default keymap with Ctrl+N and Ctrl+P added. The combobox handles its keys itself while it has focus.
Static preview

API

ItemDescription
State::new(options)Holds the options, labelled with their Display text. The list starts closed with nothing selected.
.with_selected(&value) / .with_visible_rows(n)Starts with a value selected, and sets how many suggestions show at once (six by default).
state.update(Event)Input(query), Open, Close, Next, Previous, ActivateHighlighted, Highlight(index), Activate(index) or Scroll(rows). Returns the value when one is chosen.
state.selected() / state.query() / state.is_open()The chosen value, the text typed so far and whether the list is showing.
state.matches() / state.highlighted()The options that contain the query, ignoring case, in their original order, and the highlighted one.
combobox(&state)Renders the field and, while open, the floating list. It renders disabled until .on_event is set.
.on_event(f)Maps combobox events to your message, such as Message::Fruit.
.placeholder(text) / .empty(text)Text shown in the empty field, and in the list when nothing matches. The empty text defaults to "No results found."
.width(length) / .size(Size) / .id(id)Overrides the width (240 pixels by default), takes the input sizes Sm, Md (the default) or Lg, and sets the text field's widget id.
.invalid(bool)Draws the field with the destructive border, as input does, for example when a required value is missing.
.keymap(keymap)Replaces the default keymap. The combobox resolves its own key presses while its field has focus or its list is open.
combobox::default_keymap()The default shortcuts as a Keymap<Action>. Bind, unbind or clear chords to change them.
action.event(&state)Turns an Action into the Event it sends, or None when it would do nothing, for apps that route keys themselves.

Keyboard

Default shortcuts. Every one can be changed or removed with a keymap; see Keyboard shortcuts.

KeysAction
ArrowDownNext Highlights the next suggestion, opening the list if it is closed.
ArrowUpPrevious Highlights the previous suggestion, opening the list if it is closed.
EnterActivate Chooses the highlighted suggestion and closes the list.
EscapeClose Closes the list and clears the query.
Esc