Components
HeroUI v3 Autocomplete is a searchable select that filters a ListBox of options as you type, supporting single and multiple selection with removable tags, grouped sections, disabled options, descriptions, form validation, controlled state, custom trigger values with avatars, and full-width layouts inside a Surface. Built on the real @heroui/react package with @heroui/styles, it composes with SearchField, ListBox, TagGroup, Label, Description, FieldError and useFilter. A thin re-export of the upstream component so consumers install and render the genuine HeroUI primitive.
npx shadcn@latest add https://21st.dev/r/hero_ui/heroui-autocompleteLoading preview...
"use client"
import type { Key } from "@heroui/react"
import {
EmptyState,
Label,
ListBox,
SearchField,
Tag,
TagGroup,
useFilter,
} from "@heroui/react"
import { useState } from "react"
import { Autocomplete } from "@/components/ui/heroui-autocomplete"
export function TagGroupSelection() {
const tags = [
{ id: "react", name: "React" },
{ id: "typescript", name: "TypeScript" },
{ id: "javascript", name: "JavaScript" },
{ id: "nodejs", name: "Node.js" },
{ id: "python", name: "Python" },
{ id: "vue", name: "Vue" },
{ id: "angular", name: "Angular" },
{ id: "nextjs", name: "Next.js" },
]
const [selectedKeys, setSelectedKeys] = useState<Key[]>([])
const { contains } = useFilter({ sensitivity: "base" })
const onRemoveTags = (keys: Set<Key>) => {
setSelectedKeys((prev) => prev.filter((key) => !keys.has(key)))
}
return (
<Autocomplete
className="w-[256px]"
placeholder="Select tags"
selectionMode="multiple"
value={selectedKeys}
onChange={(keys) => setSelectedKeys(keys as Key[])}
>
<Label>Tags</Label>
<Autocomplete.Trigger>
<Autocomplete.Value>
{({ defaultChildren, isPlaceholder, state }) => {
if (isPlaceholder || state.selectedItems.length === 0) {
return defaultChildren
}
const selectedItemsKeys = state.selectedItems.map((item) => item.key)
return (
<TagGroup size="sm" onRemove={onRemoveTags}>
<TagGroup.List>
{selectedItemsKeys.map((selectedItemKey) => {
const tag = tags.find((t) => t.id === selectedItemKey)
if (!tag) return null
return (
<Tag key={tag.id} id={tag.id}>
{tag.name}
</Tag>
)
})}
</TagGroup.List>
</TagGroup>
)
}}
</Autocomplete.Value>
<Autocomplete.ClearButton />
<Autocomplete.Indicator />
</Autocomplete.Trigger>
<Autocomplete.Popover>
<Autocomplete.Filter filter={contains}>
<SearchField autoFocus name="search" variant="secondary">
<SearchField.Group>
<SearchField.SearchIcon />
<SearchField.Input placeholder="Search tags..." />
<SearchField.ClearButton />
</SearchField.Group>
</SearchField>
<ListBox renderEmptyState={() => <EmptyState>No tags found</EmptyState>}>
{tags.map((tag) => (
<ListBox.Item key={tag.id} id={tag.id} textValue={tag.name}>
{tag.name}
<ListBox.ItemIndicator />
</ListBox.Item>
))}
</ListBox>
</Autocomplete.Filter>
</Autocomplete.Popover>
</Autocomplete>
)
}
export default TagGroupSelection
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...
Loading preview...