AutoComplete
AutoComplete is a legacy migration surface: a text field with a dropdown of suggestions that opens while the field is focused. It filters its own options by prefix as the host feeds values back in, and it reports every selection through one callback. They are legacy migration surfaces kept for existing pages; new code should prefer Input with a suggestion list the host renders itself — the crate now ships field primitives whose popup is a host decision rather than a fixed dropdown.
- Reach for it while migrating a page that already uses this shape: a controlled value, a list of plain strings, and one callback for "the user picked something".
- Its selection semantics are deliberately simple: the callback fires on a keystroke as well as on a pick, so the host can treat it as "the value changed" and filter externally if it wants to.
- Options are matched by prefix on the lower-cased whole value, so an empty field lists every option.
Import
use *; // AutoComplete
use *; // rsx!, VNode, EventHandler
The Rust form is a plain function, AutoComplete(AutoCompleteProps { … }). Inside rsx! every prop may be omitted — an omitted prop takes its default. The examples below omit the imports the page has just shown.
Value, options and placeholder
value is the field's text and options the suggestion pool; both are required props, and the field is controlled — the host stores whatever on_select reports and passes it back in. The visible suggestions are the options the current value is a prefix of (case-insensitively), and every option is listed while the value is empty. The dropdown only exists while the field is open and has suggestions.
A snapshot cannot open the dropdown: the field starts closed, and only focus, a keystroke or a pick open it. The example therefore shows the resting state.
Clearing
allow_clear renders a small clear button at the field's trailing edge while the field holds text and is enabled; pressing it reports an empty string through the same callback and closes the dropdown. It is off by default.
Reporting the value
on_select is the one channel: it fires with the field's whole value on every keystroke, and with the picked option on a click, an Enter on the highlighted option, or the clear button (as an empty string). class is appended to the dropdown's own classes and style is written on the wrapper ahead of its positioning rule.
API
Rust
Props — AutoComplete
| Prop | Type | Default | Description |
|---|---|---|---|
| value | String | "" | — |
| options | Vec<String> | [] | — |
| on_select | EventHandler<String> | <code>EventHandler::new(|_: String| {})</code> | — |
| placeholder | String | "" | — |
| disabled | bool | false | — |
| allow_clear | bool | false | — |
| class | String | "" | — |
| style | String | "" | — |
Notes
- The field is controlled: it never stores the value it shows, and the host has to feed every report back through value. Until it does, the input keeps whatever the user typed — a snapshot always shows the seeded value.
- on_select is "the value this field should now hold", not "the user finished a selection" — a keystroke reports too. Hosts that need a distinct pick signal compare the reported value against the option list, or watch for the dropdown closing.
- Keyboard behaviour: Down and Up move the highlight cyclically through the visible suggestions, Enter picks the highlighted one, Escape closes the dropdown. None of them edit the value except Enter.
- The dropdown lists the same options the current value filters to; it is closed by blur, by Escape and by any pick.
- The options are filtered once, from the value the component was rendered with, so the dropdown follows the value the host passes back in — not the raw text sitting in the field.
- style is concatenated onto the wrapper's own position: relative rule, so a property you also set there is duplicated rather than replaced.
Accessibility
- The field is a plain <input type="text"> with placeholder only: give it a visible or programmatic label of your own, a placeholder is not an accessible name.
- The dropdown is not wired to the field as a combobox: there is no role="listbox", no aria-expanded and no aria-activedescendant, so the keyboard highlight is invisible to a screen reader. Treat keyboard picking as a pointer-user convenience.
- The clear button has no accessible name of its own; when it is the only way to empty the field, the host should offer another route.
- Options are plain text: keep them short and unambiguous, since nothing else describes them.
Related components
- Input — the current text field, with caption, hint, error line and affix slots.
- Search — the search field with a suggestions dropdown and a portal-positioned panel.
- SearchInput — the bare search field.