Displaying items with Templates
Templates let you customize the display of your autocomplete's items.
Once you've set up your data sources, you need to define how they display in your autocomplete experience. It encompasses the structure for each item and the way they look.
Autocomplete provides a Templates API to let you fully customize the render of each item.
#
Usage#
Rendering each itemThe rendering system of Autocomplete uses an agnostic virtual DOM implementation. You can return anything from each template as long as they're valid virtual DOM elements (VNodes).
For example, templates can return a string:
Or a Preact component:
info
Autocomplete uses Preact 10 to render templates by default. It isn't compatible with earlier versions.
#
Returning HTMLNative HTML elements aren't valid VNodes, which means you can't return a template string that contains HTML, or an HTML element. But if you're not using a virtual DOM implementation in your app, there are still two ways you can return HTML.
createElement
#
Using Each template function provides access to createElement
and Fragment
to create VNodes.
You can also leverage dangerouslySetInnerHTML
if you need to provide more complex HTML without having to create nested VNodes.
By default, createElement
and Fragment
default to Preact's preact.createElement
(or h
) and preact.Fragment
. You can customize these and provide the virtual DOM implementation you prefer.
htm
library#
Using the If you're not using a transpiler to build your app, you can still use Autocomplete with the htm library, which lets you use a JSX-like syntax directly in the browser.
#
Rendering a header and footerIn addition to rendering items, you can customize what to display before and after the list of items using the header
and footer
templates.
#
Rendering a no results stateWhen there are no results, you might want to display a message to inform users or let them know what to do next. You can do this with the noResults
template.
#
Styling itemsSince you're fully controlling the rendered HTML, you can style it the way you want or use any class-based CSS library.
For example, if you're using Bootstrap:
Or Tailwind CSS:
#
Templatestemplates
#
AutocompleteTemplates
A set of templates to customize how items are displayed. You can also provide templates for header and footer elements around the list of items.
You must define templates
within your sources.
See template for what to return.
#
Templateheader
#
(params: { state: AutocompleteState<TItem>, source: AutocompleteSource<TItem>, items: TItem[], createElement: Pragma, Fragment: PragmaFrag }) => VNode | string
A function that returns the template for the header (before the list of items).
item
#
(params: { item: TItem, state: AutocompleteState<TItem>, createElement: Pragma, Fragment: PragmaFrag }) => VNode | string
A function that returns the template for each item of the source.
footer
#
(params: { state: AutocompleteState<TItem>, source: AutocompleteSource<TItem>, items: TItem[], createElement: Pragma, Fragment: PragmaFrag }) => VNode | string
A function that returns the template for the footer (after the list of items).
noResults
#
(params: { state: AutocompleteState<TItem>, source: AutocompleteSource<TItem>, createElement: Pragma, Fragment: PragmaFrag }) => VNode | string
A function that returns the template for when there are no items.