Building Our First LWC Component : In this section, we’ll start developing our Car Hub project by creating the carTile
component. This component is responsible for displaying individual car details in a concise format, which will be used within the carTileList
component to show multiple cars.
carTile.html
{car.Name}
MSRP:
carTile.js
import { LightningElement ,api} from 'lwc';
export default class CarTile extends LightningElement {
@api car={};
handleClick(event){
const carId = new CustomEvent('selected',{
detail: this.car.Id
});
this.dispatchEvent(carId);
}
}
carTile.css
img.carImage{
height: 120px;
max-width: initial;
pointer-events: none;
}
.title{
font-weight: bold;
text-transform: uppercase;
}
.content{
padding: 8px;
background-color: white;
border-radius: 0.25rem;
}
carTile.js-meta.xml
57.0
true
lightning__RecordPage
lightning__AppPage
lightning__HomePage
lightning__Tab
Adding Message Channels: CarsSelected and CarsFiltered
In addition to building the carTile
component, we’ll also set up two message channels: CarsSelected
and CarsFiltered
. These channels will facilitate event passing between components, allowing for seamless interaction and updates:
CarsSelected
: This message channel will be used to pass information about the selected car from the Cars List
component to the Car Details
component.
CarsFiltered
: This channel will handle the transmission of filter criteria from the Filter
component to the Cars List
component, ensuring that the car list updates according to the selected filters.
CarsFiltered
CarsFiltered
true
This event is fired when car filters change
filters
Current car filters
CarsSelected
CarsSelected
false
This event is fired when car is selected
carId
CarSelectedId
carTileList.html
There are no car matching your current selection
carTileList.js
import { LightningElement,wire } from 'lwc';
import getCars from '@salesforce/apex/CarController.getCars';
// Lightning Message Service and a message channel
import {publish,subscribe,MessageContext} from 'lightning/messageService';
import CARS_FILTERED_MESSAGE from '@salesforce/messageChannel/CarsFiltered__c';
import CARS_SELECTED_MESSAGE from '@salesforce/messageChannel/CarsSelected__c';
export default class CarTileList extends LightningElement {
cars={};
filters={};
carFilterSubscription;
// Load Context for LMS
@wire(MessageContext)
messageContext
@wire(getCars,{filters:'$filters'})
carsHandler({data,error}){
if(data){
console.log('car list=>',data)
this.cars = data;
}
if(error){
console.error(error);
}
}
connectedCallback() {
this.subscribeHandler();
}
subscribeHandler(){
this.carFilterSubscription = subscribe(this.messageContext,CARS_FILTERED_MESSAGE,(message)=>this.handleFilterChanges(message));
}
handleFilterChanges(message){
console.log('message=>',message.filters);
this.filters = {...message.filters};
}
handleSelected(event){
console.log('selected car id=>',event.detail);
publish(this.messageContext,CARS_SELECTED_MESSAGE,{
carId: event.detail
})
}
disconnectedCallback() {
unsubscribe(carFilterSubscription);
this.carFilterSubscription = null;
}
}
carTileList.css
c-car-tile{
min-width: 200px;
flex:1;
}
.content{
display: flex;
flex-wrap: wrap;
}
.wrapper{
background:#e3e5e4;
padding: 12px;
}
carTileList.js-meta.xml
57.0
true
Car Tile List
lightning__AppPage
That’s it for today! We’ll pick up where we left off in the next blog post.