renderOption
This an example of the default option rendering function. Based on that you can create your own renderOption function.
See Highlighter for more details on how to highlight search term.
import { useAutocomplete } from '../context'
import { AutocompleteOptionDefault, AutocompleteProps } from '../types'
import Highlighter from 'react-highlight-words'
export const _renderOption = (props: RenderOptionProps, option: AutocompleteOptionDefault) => {
const { getOptionLabel, autoHighlight } = useAutocomplete()
const label = getOptionLabel!(option)
return (
<li {...props}>
{autoHighlight ? <Highlighter searchWords={[props.query]} textToHighlight={label} /> : label}
</li>
)
}Custom example
type Character = {
id: string
name: string
image: string
species: string
//... rest
}
const renderOption = (props: AutocompleteOptionProps, option: Character) => (
<div {...props} className={clsx(props.className, 'h-fit w-fit p-2')}>
<div className='flex gap-2 items-center'>
<Avatar size='sm' src={option.image} />
<div className='flex flex-col'>
<span className='font-medium'>{option.name}</span>
<span className='text-info text-xs'>{option.species}</span>
</div>
</div>
</div>
)Caveats
The props of renderOption carry className that has all interaction on hover and focus.
You can either preserve this like in example or skip altogether if you want to have your own styles for things like selected, hover or disabled.
Please refer to type definition to find out what you can base your styles on.
Highlighting of search term is lost in custom option component, but you can implement it yourself, e.g. using react-highlight-words library.
Component API
type of RenderOptionProps
| Prop | Default | Description |
key | — | stringKey of the option |
tabIndex | — | numberTab index of the option |
aria-selected | — | booleanAria-selected attribute of the option |
aria-disabled | — | booleanAria-disabled attribute of the option |
aria-label | — | stringAria-label attribute of the option |
role | — | stringRole attribute of the option |
className | — | stringClass name of the option |
ref | — | (node: any) => voidref passed to the option |
active | — | booleanIs option being hovered or tabbed on? |
selected | — | booleanIs option currently selected? |
disabled | — | booleanIs option disabled? |
label | — | stringLabel of the option. Result of |