DocsBlog
  • 0.25.3

  • light

    dark

    system

    Switch mode
  • Cerberus

    Acheron

    Elysium

Get Started
Components
Styling
Theming

Concepts

OverviewCompositionTesting

Layout

Aspect RatioBleedBoxCenterContainerDividerFlexFloatGridLink OverlayScrollableStackWrap

Components

AccordionAdmonitionAvatarButtonCarouselCheckboxClipboardCollapsibleComboboxConfirm ModalCTAModalDate PickerDialogFieldFieldsetFile UploaderIcon ButtonInputLoading StatesMenuNotificationsNumber InputPin InputProgressPrompt ModalRadioRatingSelectSplit ButtonSwitchTableTabsTagTextTextareaToggleTooltip

Utilities

Client OnlyDownload TriggerEnvironmentFeature FlagsFocus TrapForFormat ByteFormat NumberFormat Relative TimeFrameHighlightJSON Tree ViewLocaleLocal StoragePortalPresenceShowsplitPropsTheme

Combobox

A filterable select component that allows users to search and select options.

  • npm
  • source
  • recipe
  • Ark
import {
Combobox,
ComboItemGroup,
ComboItemWithIndicator,
ComboItemText
} from '@cerberus/react'

Note

Notice the naming convention of ComboItemGroup (the abstraction) and ComboboxItemGroup the primitive.

Usage

The Combobox component is an abstraction component that provides a fully functional combobox. It is built on top of the Select component and allows additional functionality such as filtering.

Hades
Persephone
Zeus
Poseidon
Hera
Copy
'use client'
import {
For,
Combobox,
ComboItemWithIndicator,
ComboItemText,
useStatefulCollection,
} from '@cerberus/react'
import { Box, VStack } from 'styled-system/jsx'
import { useMemo, useState } from 'react'
const initialItems = [
{ label: 'Hades', value: 'hades' },
{ label: 'Persephone', value: 'persephone' },
{ label: 'Zeus', value: 'zeus', disabled: true },
{ label: 'Poseidon', value: 'poseidon' },
{ label: 'Hera', value: 'hera' },
]
function BasicPreview() {
const { collection, handleInputChange } =
useStatefulCollection(initialItems)
return (
<Box w="1/2">
<Combobox
collection={collection}
label="Select Relative"
onInputValueChange={handleInputChange}
placeholder="Choose option"
>
<For
each={collection.items}
fallback={
<VStack paddingBlock="6" w="full">
<Text textAlign="center" textStyle="label-sm">
No results found
</Text>
</VStack>
}
>
{(item) => (
<ComboItemWithIndicator key={item.value} item={item}>
<ComboItemText>{item.label}</ComboItemText>
</ComboItemWithIndicator>
)}
</For>
</Combobox>
</Box>
)
}

With Start Icon

To add an icon to the start position of the input, use the startIcon prop to pass in your ReactNode.

Hades
Persephone
Zeus
Poseidon
Hera
Copy
'use client'
import {
For,
Combobox,
ComboItemWithIndicator,
ComboItemText,
useStatefulCollection,
} from '@cerberus/react'
import { Box, VStack } from 'styled-system/jsx'
import { useMemo, useState } from 'react'
import { Search } from '@carbon/icons-react'
const initialItems = [
{ label: 'Hades', value: 'hades' },
{ label: 'Persephone', value: 'persephone' },
{ label: 'Zeus', value: 'zeus', disabled: true },
{ label: 'Poseidon', value: 'poseidon' },
{ label: 'Hera', value: 'hera' },
]
function StartIconPreview() {
const { collection, handleInputChange } =
useStatefulCollection(initialItems)
return (
<Box w="1/2">
<Combobox
collection={collection}
label="Select Relative"
onInputValueChange={handleInputChange}
openOnClick
placeholder="Choose option"
startIcon={<Search />}
>
<For
each={collection.items}
fallback={
<VStack paddingBlock="6" w="full">
<Text textAlign="center" textStyle="label-sm">
No results found
</Text>
</VStack>
}
>
{(item) => (
<ComboItemWithIndicator key={item.value} item={item}>
<ComboItemText>{item.label}</ComboItemText>
</ComboItemWithIndicator>
)}
</For>
</Combobox>
</Box>
)
}

Grouped Items

To group items, use the ComboItemGroup component (not ComboboxItemGroup).

The fam
Hades
Persephone
Zeus
Poseidon
Hera
Copy
import {
For,
Combobox,
ComboItemGroup,
ComboItemWithIndicator,
ComboItemText,
useStatefulCollection,
} from '@cerberus/react'
import { Box } from 'styled-system/jsx'
function GroupedItemsPreview() {
const { collection, handleInputChange } =
useStatefulCollection(comboInitialItems)
return (
<Box w="1/2">
<Combobox
collection={collection}
label="Select Relative"
onInputValueChange={handleInputChange}
placeholder="Choose option"
startIcon={<Search />}
>
<ComboItemGroup label="The fam">
<For
each={collection.items}
fallback={
<VStack paddingBlock="6" w="full">
<Text textAlign="center" textStyle="label-sm">
No results found
</Text>
</VStack>
}
>
{(item) => (
<ComboItemWithIndicator key={item.value} item={item}>
<ComboItemText>{item.label}</ComboItemText>
</ComboItemWithIndicator>
)}
</For>
</ComboItemGroup>
</Combobox>
</Box>
)
}

With Field

Wrap the Combobox component with the Field component to add additional functionality such as error and helper text when using it in a form.

Hades
Persephone
Zeus
Poseidon
Hera
This is saved to your profile
Copy
'use client'
import {
For,
Field,
Combobox,
ComboItemWithIndicator,
ComboItemText,
useStatefulCollection,
} from '@cerberus/react'
import { Box } from 'styled-system/jsx'
function FieldPreview() {
const { collection, handleInputChange } =
useStatefulCollection(comboInitialItems)
return (
<Box w="1/2">
<Field
label="Select Relative"
helperText="This is saved to your profile"
errorText="You must select something."
required
>
<Combobox
collection={collection}
onInputValueChange={handleInputChange}
placeholder="Choose option"
form="myForm"
>
<For
each={collection.items}
fallback={
<VStack paddingBlock="6" w="full">
<Text textAlign="center" textStyle="label-sm">
No results found
</Text>
</VStack>
}
>
{(item) => (
<ComboItemWithIndicator key={item.value} item={item}>
<ComboItemText>{item.label}</ComboItemText>
</ComboItemWithIndicator>
)}
</For>
</Combobox>
</Field>
</Box>
)
}

Primitives

You can utilize the primitive components or the css prop to customize the Combobox.

ComponentDescription
ComboboxRootThe context provider for the combobox family
ComboboxLabelThe label that appears above the combobox input
ComboboxControlThe wrapper to the combobox trigger that opens the dropdown
ComboboxInputThe input field of the combobox
ComboboxTriggerhe trigger that opens the dropdown
ComboboxClearTriggerThe trigger that clears the selected value
ComboboxPositionerThe wrapper that positions the dropdown
ComboboxContentThe content of the dropdown (i.e. the container itself)
ComboboxItemGroupThe group of options in the dropdown
ComboboxItemGroupLabelThe label for the group of options
ComboboxItemThe option in the dropdown
ComboboxItemTextThe text label of the option
ComboboxItemIndicatorThe indicator shown when the option is selected
Hades
🔥
Persephone
🔥
Zeus
🔥
Poseidon
🔥
Hera
🔥
Copy
'use client'
import {
For,
Portal,
ComboboxParts,
useStatefulCollection,
} from '@cerberus/react'
import { Box } from 'styled-system/jsx'
function CustomDemo() {
const { collection, handleInputChange } =
useStatefulCollection(comboInitialItems)
return (
<Box w="1/2">
<ComboboxParts.Root
collection={collection}
onInputValueChange={handleInputChange}
css={{
transform: 'skewX(-10deg)',
}}
>
<ComboboxParts.Label
css={{
textTransform: 'uppercase',
}}
>
Custom label
</ComboboxParts.Label>
<ComboboxParts.Control>
<ComboboxParts.Input />
<ComboboxParts.Trigger>
<ChevronDownOutline />
</ComboboxParts.Trigger>
</ComboboxParts.Control>
<Portal>
<ComboboxParts.Positioner>
<ComboboxParts.Content>
<For
each={collection.items}
fallback={
<VStack paddingBlock="6" w="full">
<Text textAlign="center" textStyle="label-sm">
No results found
</Text>
</VStack>
}
>
{(item) => (
<ComboboxParts.Item key={item.value} item={item}>
<ComboboxParts.ItemText css={{ fontSize: 'xl' }}>
{item.label}
</ComboboxParts.ItemText>
<ComboboxParts.ItemIndicator>
🔥
</ComboboxParts.ItemIndicator>
</ComboboxParts.Item>
)}
</For>
</ComboboxParts.Content>
</ComboboxParts.Positioner>
</Portal>
</ComboboxParts.Root>
</Box>
)
}

API

Combobox

The Combobox component is an abstraction of our primitives and accepts the following props:

NameDefaultDescription
sizemdThe size of the combobox.
startIconThe icon to display at the start of the input.

The Combobox component also accepts all the props of the ComboboxRoot primitive props

ItemWithIndicator

The ItemWithIndicator component is an abstraction of our primitives and accepts all the props of the ComboboxItem primitive props

ComboItemGroup

The ComboItemGroup component is an abstraction of our primitives and accepts the following props:

NameDefaultDescription
labelThe label of the group.

The ComboItemGroup component is an abstraction of our primitives and accepts all the props of the ComboboxItemGroup primitive props

useStatefulCollection

The useStatefulCollection function is a utility hook that creates a collection of options and filters the list based on the user input.

function useStatefulCollection(
initialItems: SelectCollectionItem[] = [],
): StatefulCollectionReturn

Returns

NameDescription
collectionThe collection of options.
filterCharsThe filter value split into an Array of chars.
handleInputChangeThe function to pass to onInputValueChange.

Parts

The ComboboxParts API is an Object containing the full family of components.

Note

It is best to only use the ComboboxParts if you are building a custom solution. Importing Object based components will ship every property it includes into your bundle, regardless if you use it or not.

NameDescription
RootThe ComboboxRoot component which is the Provider for the family.
LabelThe ComboboxLabel component which displays the label.
ControlThe ComboboxControl component which is the container for the visual field.
InputThe ComboboxInput component which is the visual field.
TriggerThe ComboboxTrigger component which is the trigger for the dropdown.
ClearTriggerThe ComboboxClearTrigger component which is the trigger to clear the value.
PositionerThe ComboboxPositioner component which is controls the positioning for the dropdown.
ContentThe ComboboxContent component which is the dropdown itself.
ItemGroupThe ComboboxItemGroup component which is the group of options in the dropdown.
ItemGroupLabelThe ComboboxItemGroupLabel component which is the label for the group of options.
ItemThe ComboboxItem component which is the option in the dropdown.
ItemTextThe ComboboxItemText component which is the text label of the option.
ItemIndicatorThe ComboboxItemIndicator component which displays based on the checked state.

On this page

  • Edit this page on Github
Cerberus Design System | Docs