The Lightning Web Component(LWC) framework is very popular because it provides better performance to your applications, it adheres to web standards, it is easy to learn, and so much more. Salesforce provides out-of-the-box components, called base LWC, that add a LOT of functionality to your applications. Let’s talk about using these base components to complement your custom LWC application.
Salesforce has recently opened these base LWC’s source code to the public, which is incredibly exciting and an amazing opportunity to learn about their design patterns; however, this might seduce us in trying to enhance every base LWC when we can use the standard functionality as it is. Sometimes, it is necessary, but keep in mind that once you create your own LWC, you are responsible for maintaining, enhancing, and testing the code FOREVER.
Instead, let’s take a look at an example that takes advantage of base LWC while doing some very custom stuff.
Create two currency fields in the Contact object:
Set up development tools
Create the components
lightning-layout
and lightning-record-edit-form
. lightning-datatable
, I will work on showing this on a future blog post in order to compare the solutions.relatedContacts.js-meta.xml
<target>lightning__RecordPage</target>
to allow showing the component in a record page.relatedContacts.html
lightning-card
and lightning-layout
tags in order to design the layout. This makes it a lot easier since these tags take care of all the styling. <template if:true={contacts}>
to show the rest of the rows only if there are contacts to show. <template for:each={contacts} for:item="contactId" for:index="index">
will iterate through all of the components of the contacts arrayrelatedContacts.js
file.onloading, ondoneloading, ondelete, onsuccess
. These events will be dispatched from the editContactRow
components and handled in the relatedContacts
component. <c-error-panel>
tag, which uses the errorPanel
component imported from the LWC recipes, used to display errors whenever there is one.<c-modal>
which used the modal component imported from the LWC recipes, used to verify if the user Really wants to delete the selected contact.relatedContacts.js
editContactRow.html
<lightning-record-edit-form>
in order to display every contact record. I don’t have to write any code to pull the contacts data. This is a perfect example of how you can use base LWC to save you a lot of work.<lightning-input-field>
and its attribute disabled to show as editable or not, depending on the user clicking on the edit button.<lightning-input-field>
components are listening to the onchange
event on the Tax_Paid__c and on Pre_Tax_Income__c fields. When these change, I can get their values, add them up, and update the totalIncome
property.<lightning-input-field>
components are listening to the isReadOnly
property to know if the row is in edit mode or not. If it is in edit mode it will show the fields enabled for editing and also the Save and Cancel buttons. If it is not in edit mode, the fields will show in read only mode and it will show the edit and delete buttons.editContactRow.js
connectedCallback
function, I am setting the edit mode ON, if the contactId is blank, meaning that a new row was added.handleUpdateTotals
will be called every time the currency fields are updated, and will update the totalIncome
property.handleOnDelete
will dispatch a customEvent so that the parent component can take care of deleting the contact.This is a perfect example of how you can utilize base components in your custom applications. Here, we used the base component lightning-record-edit-form
to create a quick Contact creation table. The table had custom functionality like auto calculation of totals, deletion of records, and addition of rows. I w able to achieve this by creating only two custom components lowering the application's maintenance cost. I hope this inspires you to create sustainable applications, using all of the out-of-the-box functionality that the salesforce platform provides.