Autocomplete
A text field that suggests values while you type and accepts anything you enter.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsstable
- Built on native elements
- ARIA 1.2 combobox with aria-autocomplete="list", aria-expanded, aria-controls and aria-activedescendant.
- ArrowDown/ArrowUp move through suggestions, Enter picks the active one (or keeps typed text), Escape closes.
- The field value is always the typed or picked text; nothing is forced to match a suggestion.
- Styles in one file:
03-form-fields.css
Import
import '@monoline/css';
import {Autocomplete} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add autocomplete
Usage
import {Autocomplete, Field} from '@monoline/react';
const cities = ['Amsterdam', 'Berlin', 'Bangalore', 'Boston', 'Lisbon', 'London', 'Lagos'];
<div style={{maxWidth: '320px', minHeight: '220px'}}>
<Field label="City">
<Autocomplete suggestions={cities} placeholder="Start typing" />
</Field>
</div>
Examples
Props
AutocompleteProps
| Prop | Type | Default | Description |
|---|---|---|---|
suggestions required | string[] | – | Suggestions shown while typing; matched by substring, case-insensitive. |
value | string | – | The text in the field. |
defaultValue | string | '' | Initial text when the field is uncontrolled. Default empty. |
onValueChange | ((value: string) => void) | – | Called with the text on every change, and with the suggestion when one is picked. |
size | "sm" | "md" | "lg" | 'md' | `md` (default), `sm` for dense layouts or `lg` for prominent forms. Heights match Button, Input and Select at each size. |
invalid | boolean | – | Marks the field as having an error: sets `aria-invalid` and shows the error border. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[aria-checked="true"] | 03-form-fields.css |
[aria-invalid="true"] | 03-form-fields.css |
[aria-pressed="true"] | 03-form-fields.css |
[data-active] | autocomplete.css |
CSS classes
| Class | Stylesheet |
|---|---|
.field | 03-form-fields.css |
.field-hint | 03-form-fields.css |
.input | 03-form-fields.css |
.input-bare | 03-form-fields.css |
.input-end | 03-form-fields.css |
.input-lg | 03-form-fields.css |
.input-sm | 03-form-fields.css |
.input-wrap | 03-form-fields.css |
.muted | 03-form-fields.css |
.seg | 03-form-fields.css |
.seg-opt | 03-form-fields.css |
.select | 03-form-fields.css |
.setting | 03-form-fields.css |
.switch | 03-form-fields.css |
.textarea | 03-form-fields.css |
.autocomplete | autocomplete.css |
.autocomplete-list | autocomplete.css |
.autocomplete-option | autocomplete.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.field {
border-color: var(--accent);
}
.field[aria-checked="true"] {
background: var(--wash);
}
Accessibility
- ARIA 1.2 combobox with aria-autocomplete="list", aria-expanded, aria-controls and aria-activedescendant.
- ArrowDown/ArrowUp move through suggestions, Enter picks the active one (or keeps typed text), Escape closes.
- The field value is always the typed or picked text; nothing is forced to match a suggestion.
Keyboard
| Keys | Action |
|---|---|
| ArrowDown / ArrowUp | Move through suggestions |
| Enter | Picks the active one (or keeps typed text) |
| Escape | Closes |