In this guide, you'll learn how to build an autocomplete UI using React.
The @francoischalifour/autocomplete-core package provides all the primitives to build an autocomplete experience, but you remain in charge of the UI output. This page will teach you how to leverage all the autocomplete capacities to build an accessible autocomplete, both for desktop and mobile.
We'll import createAutocomplete from the core package and getAlgoliaHits from the Algolia preset which is a utility function to retrieve suggestions from an Algolia index.
Prop getters are methods that return props to use in your components. These props contain accessibility features, event handlers, etc. You do not have to know exactly what they're doing, since their responsibility is to create a complete experience without understanding the complex technical parts.
The above code demonstrates that you do not need to worry about keyboard events, or tracking which item is highlighted, Autocomplete handles it with its prop getters.
At this point, you should already have a usable autocomplete input:
getFormProps will handle submit and reset events and will respectively blur and focus the input when these events happen. You need to pass the inputElement to getFormProps to leverage this functionality.
A good practice for search inputs is to display a reset button. You can conditionally display it based on if there's a query, and trigger the onReset event from getFormProps on it.
Native platforms offer better primitive for mobile search experiences than the web. Autocomplete aims at providing this extra layer for the web mobile experience to be as close as the native mobile experience.
A common feature in mobile native experiences is to close the virtual keyboard when the user starts scrolling. This makes the results more discoverable, without the user having to manually close the keyboard
The getEnvironmentProps method returns event handlers that let you create this experience.
Users will now feel a little bit closer to what they're used to on mobile apps.
You now have enough knowledge to build your own experience based on Autocomplete. If you feel like some topics weren't covered in this page, feel free to open an issue.