Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@contentful/f36-autocomplete
Advanced tools
Search through a long list of options.
import { Autocomplete } from '@contentful/f36-components';
// or
import { Autocomplete } from '@contentful/f36-autocomplete';
The Autocomplete requires 3 props to work:
items
: It’s an array containing the items that will be shown as selectable options when the user types something in the TextInput
.onInputValueChange
: This function will be called every time the user types something in the input.
The component will pass the item
, which the filter method is currently iterating over, and the inputValue
prop of the TextInput
component.onSelectItem
: This function is called when the user selects one of the options of the list. The component will pass the selected item as an argument to the function.An Autocomplete
with a list of spaces will look like this:
items
We showed how to create an Autocomplete with an array of string but it’s also possible to use other types of data as items
.
A very common way of using the Autocomplete is with objects and for that, with a few changes to the previous example this can be done:
Both itemToString
and renderItem
are necessary when passing objects as items and they both will receive an "item" as an argument.
If you are using Typescript, you can tell the Autocomplete what is the type of your items to make these functions strongly typed.
You can do that by writing the component like this <Autocomplete<ItemType> {...props}/>
getStringMatch
A common use case for Autocomplete components is to highlight in each suggestion what is typed in the input.
Using the previous example, if a user types "fi" we want to show a list of suggestions where only "fi" is bold.
This is possible by using the renderItem
prop and the getStringMatch
utility function:
It is also possible to use the Autocomplete as multiselect. To improve the user experience, you can keep the dropdown open after selection by setting the "closeAfterSelect" property to false.
items
As an extension of "Use objects as items" section, you are also able to use a nested object to group your entries.
The most important part of making this work is the shape of the grouped object. The options themselves work exactly as in the object example and require the itemToString
and renderItem
functions.
Besides the correct shape of the object the Autocomplete component needs to receive the prop isGrouped
In order to use proper form validation you need to be able to control the actual input field from the autocomplete, because the search query value is not the actual selection. This is done via the selectedItem property.
Pass a custom icon to the text input, example: to indicate a search input
FAQs
Forma 36: Autocomplete component
We found that @contentful/f36-autocomplete demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.