Skip to main content

Autocomplete

Overview

The Autocomplete component provides an input field with dropdown suggestions that filter based on user input. It supports both string arrays and object data, keyboard navigation, and custom templates.


Import

import { Autocomplete } from '@allxsmith/bestax-bulma';

Props

PropTypeDefaultDescription
dataAutocompleteItem[] | string[]The options data to display (required).
valuestringThe current input value (controlled).
selectedAutocompleteItem | string | nullThe selected item (controlled).
placeholderstringPlaceholder text for the input.
fieldstring'label'Object property to use as the display field.
clearablebooleanfalseWhether to show a clear button.
openOnFocusbooleanfalseOpen dropdown when input is focused.
keepFirstbooleanfalseKeep first option highlighted.
keepOpenbooleanfalseKeep dropdown open after selection.
selectOnClickOutsidebooleanfalseSelect highlighted item on click outside.
maxHeightnumber200Maximum dropdown height in pixels.
dropdownbooleanfalseRender as dropdown style.
loadingbooleanfalseShow loading state.
disabledbooleanfalseWhether the input is disabled.
color'primary' | 'link' | 'info' | 'success' | 'warning' | 'danger'Input color variant.
size'small' | 'medium' | 'large'Size variant.
onInput(value: string) => voidCallback when input value changes.
onSelect(item: AutocompleteItem | string | null) => voidCallback when item is selected.
onActiveChange(active: boolean) => voidCallback when dropdown active state changes.
onInfiniteScroll() => voidCallback when scrolled to bottom.
checkInfiniteScrollbooleanfalseEnables infinite scroll detection in the dropdown.
infiniteScrollDistancenumber50Distance in pixels from the bottom to trigger onInfiniteScroll.
itemTemplate(item: AutocompleteItem | string) => React.ReactNodeCustom render for items.
headerReact.ReactNodeCustom header in dropdown.
footerReact.ReactNodeCustom footer in dropdown.
emptyReact.ReactNodeContent to show when no results.
classNamestringAdditional CSS classes.
refReact.Ref<HTMLElement>Ref forwarded to the input element.
...All standard HTML and Bulma helper props(See Helper Props)

AutocompleteItem

PropTypeDescription
valuestringThe item value.
labelstringDisplay label (optional).
disabledbooleanWhether item is disabled.

Usage

Basic Autocomplete

Simple autocomplete with string array.

function example() {
  const [selected, setSelected] = useState(null);
  const fruits = [
    'Apple',
    'Banana',
    'Cherry',
    'Date',
    'Elderberry',
    'Fig',
    'Grape',
  ];

  return (
    <Block>
      <Autocomplete
        data={fruits}
        placeholder="Search fruit..."
        onSelect={setSelected}
      />
      {selected && <Paragraph mt="2">Selected: {selected}</Paragraph>}
    </Block>
  );
}


Open on Focus

Dropdown opens immediately when input is focused.

function example() {
  const [selected, setSelected] = useState(null);
  const countries = [
    'United States',
    'United Kingdom',
    'Canada',
    'Australia',
    'Germany',
    'France',
  ];

  return (
    <Autocomplete
      data={countries}
      placeholder="Select a country..."
      openOnFocus
      onSelect={setSelected}
    />
  );
}


With Clear Button

Autocomplete with clearable input.

function example() {
  const [selected, setSelected] = useState(null);
  const options = ['Option 1', 'Option 2', 'Option 3', 'Option 4'];

  return (
    <Autocomplete
      data={options}
      placeholder="Search options..."
      clearable
      onSelect={setSelected}
    />
  );
}


Color and Size Variants

Autocomplete with different colors and sizes.

<Block display="flex" flexDirection="column" gap="4">
  <Autocomplete
    data={['Apple', 'Banana', 'Cherry']}
    placeholder="Primary small"
    color="primary"
    size="small"
  />
  <Autocomplete
    data={['Apple', 'Banana', 'Cherry']}
    placeholder="Info normal"
    color="info"
  />
  <Autocomplete
    data={['Apple', 'Banana', 'Cherry']}
    placeholder="Success medium"
    color="success"
    size="medium"
  />
  <Autocomplete
    data={['Apple', 'Banana', 'Cherry']}
    placeholder="Danger large"
    color="danger"
    size="large"
  />
</Block>


With Object Data

Autocomplete with object items.

function example() {
  const [selected, setSelected] = useState(null);
  const users = [
    { value: '1', label: 'John Doe', email: '[email protected]' },
    { value: '2', label: 'Jane Smith', email: '[email protected]' },
    { value: '3', label: 'Bob Johnson', email: '[email protected]' },
  ];

  return (
    <Block>
      <Autocomplete
        data={users}
        field="label"
        placeholder="Search users..."
        openOnFocus
        onSelect={setSelected}
      />
      {selected && (
        <Paragraph mt="2">
          Selected: {selected.label} ({selected.email})
        </Paragraph>
      )}
    </Block>
  );
}


Custom Item Template

Autocomplete with custom item rendering.

function example() {
  const [selected, setSelected] = useState(null);
  const users = [
    { value: '1', label: 'John Doe', role: 'Admin' },
    { value: '2', label: 'Jane Smith', role: 'Editor' },
    { value: '3', label: 'Bob Johnson', role: 'Viewer' },
  ];

  return (
    <Autocomplete
      data={users}
      field="label"
      placeholder="Search users..."
      openOnFocus
      onSelect={setSelected}
      itemTemplate={item => (
        <Block display="flex" justifyContent="space-between">
          <Span>{item.label}</Span>
          <Tag color="info" light>
            {item.role}
          </Tag>
        </Block>
      )}
    />
  );
}


With Empty State

Custom content when no results match.

function example() {
  const options = ['Apple', 'Banana', 'Cherry'];

  return (
    <Autocomplete
      data={options}
      placeholder="Try searching 'xyz'..."
      empty={
        <Paragraph textColor="grey">
          <Icon name="search" variant="solid" mr="2" />
          No results found
        </Paragraph>
      }
    />
  );
}


Loading State

Autocomplete showing loading indicator.

<Autocomplete data={[]} placeholder="Loading..." loading />


Keep First Highlighted

First option stays highlighted while typing.

function example() {
  const fruits = [
    'Apple',
    'Apricot',
    'Avocado',
    'Banana',
    'Blackberry',
    'Blueberry',
  ];

  return (
    <Autocomplete data={fruits} placeholder="Type to filter..." keepFirst />
  );
}


Context-Aware Rendering

The Autocomplete component is context-aware: it detects whether it is already inside a Field and adjusts its rendering accordingly. This means you can use it standalone with a label prop (it wraps itself in a Field), or inside a Field (it skips rendering its own).

note

Autocomplete does not use ControlContext, so the "With Field and Control Wrappers" example below uses Field wrapping only. The Control wrapper is shown for layout consistency but does not change the component's internal rendering.

Default (with label)

The simplest usage — the component automatically renders its own Field wrapper.

<Autocomplete
  label="Fruit"
  data={['Apple', 'Banana', 'Cherry']}
  placeholder="Search fruit..."
/>


With Field Wrapper

When you need manual control over the Field layout (e.g., horizontal forms), wrap the component in Field. The component detects it's inside a Field and skips rendering its own.

function example() {
  return (
    <Field horizontal label="Fruit">
      <Field.Body>
        <Field>
          <Autocomplete
            data={['Apple', 'Banana', 'Cherry']}
            placeholder="Search fruit..."
          />
        </Field>
      </Field.Body>
    </Field>
  );
}


With Field and Control Wrappers

For full manual composition with icons, wrap in both Field and Control. Autocomplete does not consume ControlContext, but the Field wrapper is still detected and its own Field is skipped.

function example() {
  return (
    <Field horizontal label="Fruit">
      <Field.Body>
        <Field>
          <Control iconLeftName="search">
            <Autocomplete
              data={['Apple', 'Banana', 'Cherry']}
              placeholder="Search fruit..."
            />
          </Control>
        </Field>
      </Field.Body>
    </Field>
  );
}


Keyboard Navigation

KeyAction
Open dropdown / Move down
Move up
EnterSelect highlighted item
TabSelect highlighted and move focus
EscapeClose dropdown

Form Submission

Autocomplete is an HTML form element. Pass a name prop and the typed/selected text is forwarded to the inner <input> so the value submits with the surrounding form, exactly like a native text input.

PropDescription
nameForm field name. Forwarded to the inner <input>.
formOptional id of the form the input belongs to.
requiredMarks the field as required for native HTML form validation.
function AutocompleteFormDemo() {
  const [submitted, setSubmitted] = React.useState('');
  return (
    <form
      onSubmit={e => {
        e.preventDefault();
        const fd = new FormData(e.currentTarget);
        setSubmitted(JSON.stringify(Array.from(fd.entries()), null, 2));
      }}
    >
      <Autocomplete
        name="city"
        data={['New York', 'London', 'Paris', 'Tokyo', 'Sydney']}
        placeholder="Search a city…"
      />
      <div style={{ marginTop: '1rem' }}>
        <button type="submit" className="button is-primary">
          Submit
        </button>
      </div>
      {submitted && <pre style={{ marginTop: '1rem' }}>{submitted}</pre>}
    </form>
  );
}


Accessibility

  • Uses role="combobox" with aria-expanded
  • Has aria-haspopup="listbox" and aria-autocomplete="list"
  • Dropdown items use role="option" with aria-selected
  • Disabled items have aria-disabled
  • Full keyboard navigation support

  • Taginput - For multiple tag selection with autocomplete
  • Select - For simple dropdown selection
  • Input - For basic text input

Additional Resources

Pro Tip

Use keepFirst combined with Enter to quickly select the first matching result without using the mouse.