Implementing Extra Features
Even though for the scope of this project we are done with the SideBar
, I would like to show you how to implement events for pressing Enter on the SearchBar
and how to make the SideBar
labels clickable.
SearchBar Enter Event
We want the user to be able to press Enter when they have selected SearchBar
and trigger an event. This event could pull up a list of results in a context menu or new page. Feel free to implement this as an extra task.
The code to achieve this is super simple. Add the following after the .
searchable
code:
}.searchable( text: $searchText ) .onSubmit( of: .search ) { print( searchText ) }
When the user has submitted the search, the code inside of the parentheses will run. For testing, we are printing out the searchText
variable, which will print out what you type in it. Feel free to run it.