In this module, you create a SearchBar component that allows the user to search contacts by name. You could add the search bar to the ContactList component, but that would limit the reusability of the component: depending on specific UI requirements, you may want the search bar to be directly on top of the list (like you’ll do here), integrated in the header, or somewhere else. You also want the ContactList component to be able to display a list of contacts independently of the type of search bar you use: regular input field with search button, type ahead search, etc. For these reasons, it’s a good idea to decouple the search UI, from the display UI, and create two components: ContactList and SearchBar.
Now that we decided to build SearchBar and ContactList as two separate components, we need a way for ContactList to know when the search key changes so that it can retrieve and display the matching contacts. Lightning Events enable that kind of communication between components. In this step, you create a Lightning Event used by the SearchBar component to notify other components when the search key changes.
In the Developer Console, click File > New > Lightning Event. Specify SearchKeyChange as the bundle name and click Submit
Implement the event as follows:
<aura:event type="APPLICATION">
<aura:attribute name="searchKey" type="String"/>
</aura:event>
### Code Highlights:
Click File > Save to save the file
In the Developer Console, click File > New > Lightning Component. Specify SearchBar as the bundle name and click Submit
Implement the component as follows:
<aura:component>
<div>
<input type="text" class="form-control"
placeholder="Search" onkeyup="{!c.searchKeyChange}"/>
</div>
</aura:component>
### Code Highlights:
Click File > Save to save the file
Click CONTROLLER (upper right corner in the code editor)
Implement the Controller as follows:
({
searchKeyChange: function(component, event, helper) {
var myEvent = $A.get("e.c:SearchKeyChange");
myEvent.setParams({"searchKey": event.target.value});
myEvent.fire();
}
})
### Code Highlights:
Click File > Save to save the file
Click STYLE
Implement the following styles:
.THIS {
width: 100%;
padding: 8px;
}
.THIS input {
width: 100%;
padding: 8px;
-webkit-appearance: none;
border: solid 1px #dddddd;
}
Click File > Save to save the file
In the Developer Console, go back to the ContactList component
Add an event handler for the SearchKeyChange event (right after the init handler):
<aura:handler event="c:SearchKeyChange" action="{!c.searchKeyChange}"/>
### Code Highlight:
Click CONTROLLER (upper right corner in the code editor)
Add the searchKeyChange() function implemented as follows:
searchKeyChange: function(component, event) {
var searchKey = event.getParam("searchKey");
var action = component.get("c.findByName");
action.setParams({
"searchKey": searchKey
});
action.setCallback(this, function(a) {
component.set("v.contacts", a.getReturnValue());
});
$A.enqueueAction(action);
}
Make sure you separate the doInit() and the searchKeyChange() functions with a comma
### Code Highlights:
Click File > Save to save the file
In the developer console, go back to the QuickContacts component
Add the SearchBar component before the ContactList component:
<aura:component implements="force:appHostable">
<c:SearchBar/>
<c:ContactList/>
</aura:component>
Click File > Save to save the file
Go back to the Salesforce1 app and reload Quick Contacts from the menu to see the changes. Type a few characters in the search bar to search for contacts.