Nowadays, most of the web or mobile applications are using typeahead search rapidly in their application and there are many libraries that are built over time, and believe me, those are working great. But there is always a need for some customization and then using the library to sort out problems will be a pian for the developer as well as for the business.
Many of us have a question "What is the difference between Autocomplete search and Typeahead search?". And the answer is both are the same. The jquery developers are calling it Autocomplete and Twitter developers are calling it Typeahead. Sometimes it is also called Suggestions Search Bar.
The typeahead search is required when we have a large number of data and that can not be accomodate in dropdown menu. To simplify the UI/UX, typeahead search idea had implemented a long back. It has a simple input field and whatever user type on it, the data set will filter it and display it to the end user.
There are several open-source examples are out there like -
But in this blog, let's create it by yourself without using any library which is specific to typeahead. It will reduce your bundle size by at least a few bites. First, have a look at how it works -
Using this, you can get rid of the following error -
If you use the dynamic manner, the component will be enabled, if the user wants to use the search options or not.
The benefit of dynamically adding component: The component will not be fixed, it will add to the DOM whenever it is required and once the work is done it will get removed from the DOM as well. For more information about the dynamic loading component you can follow the angular tutorial.
You should use the data source to be deep copied before passing the value to the search component so that your original object will not be changed.
Once, a user selects the search item, the respective object must be sent to the parent component using the @output decorator. So, the parent component can get the selected object and do the respective work to perform.
The complete code has been created without using any CSS library, for good user experience and responsive nature bootstrap, material bootstrap, etc can be used and improve the user experience.
Thanks & Happy Coding!
- Lazy Panda Tech