Data Entry

AutoComplete

An input field with a filterable suggestion dropdown. Supports keyboard navigation, custom filtering, and async data loading.

Installation

npx benflux-ui add auto-complete

Rich option objects

<AutoComplete
  options={[
    { value: "react", label: "React", description: "A JavaScript library for building UI" },
    { value: "vue", label: "Vue", description: "Progressive JavaScript Framework" },
    { value: "svelte", label: "Svelte", disabled: true },
  ]}
  placeholder="Select framework"
/>

Custom filter

// Filter only from the beginning of each option
<AutoComplete
  options={options}
  filterOption={(input, option) =>
    option.value.toLowerCase().startsWith(input.toLowerCase())
  }
/>

Async loading

"use client"
const [options, setOptions] = useState([])
const [loading, setLoading] = useState(false)

const handleSearch = async (text: string) => {
  setLoading(true)
  const results = await fetchSuggestions(text)
  setOptions(results)
  setLoading(false)
}

<AutoComplete
  options={options}
  onSearch={handleSearch}
  loading={loading}
  filterOption={false}  // disable client-side filter
/>

Props

PropTypeDefaultDescription
options*AutoCompleteOption[] | string[]List of suggestions. Strings are converted automatically.
valuestringControlled input value
defaultValuestring""Initial value (uncontrolled)
onChange(value: string) => voidCalled when the input value changes
onSelect(value: string) => voidCalled when the user selects an option
onSearch(value: string) => voidCalled on every keystroke. Use for async loading.
placeholderstring"Search..."Input placeholder
allowClearbooleanfalseShow a clear button when the input is not empty
filterOptionboolean | ((input, option) => boolean)trueClient-side filter function. Pass false to disable.
loadingbooleanfalseShows a loading indicator in the dropdown
disabledbooleanfalseDisables the input
notFoundContentReactNode"No matches"Shown when no options match the search