Skip to main content

Understanding Slots and Events in Lightning Web Components using Base Components

 Agenda


This blog will give us a brief about how can we use slots for filling the common requirement many times and give us an idea about how can we communicate between multiple components using events (mainly focuses on the child to parent communication). And we also implement base components to understand the functionality.


What we are going to do

In this blog, we create two buttons showing details of the record using the same slot (creates a dynamical slot)and we also see that how can we communicate through events from child to parent and gives the required commands.

Slots In lightning Web Components

A slot is a placeholder for markup that a parent component passes into a component’s body. Slots are part of the Web Component specification.


For Parent Component:
<div slot="footer">
<!--markup-->
</div>

For Child Component:
<slot name="footer"></slot>

We add a slot to a component’s HTML file so a parent component can pass markup into the component


Example: Unnamed slots


<template>

    <h1>Add content to slot</h1>

    <div>

        <slot></slot>

    </div>

</template>

[Note: An unnamed slot is a placeholder for any markup that a parent component passes into the body of c-slot-demo.]


Example: Named slots


<slot name="firstName">Default first name</slot>


[Note: We can define named slots just by putting name=”<!--Name-->” inside the slot attribute. And this can only be placed by calling it using its name.]


A component can have zero or more slots.


Events in Lightning Web Component


Please refer to my previous blog for this.


Blog: Understanding Event



Creating a Generic Slot (Used to show details in a Lightning Modal):


HTML:




Here, we are using a lightning modal to show the details which are sent from the parent object and to discriminate between the header, body, and footer we named the slots accordingly.

The class and Header of the modal is defined by the API variable’s 

[Note: To expose a public property, decorate a field with @api. Public properties define the API for a component.]  which are getting values directly from the parent component.


JavaScript :




As discussed above, we are using API variables to define the class and header of the modal. And, handleClose() method is used to dispatch the event (Event Name: ‘close’) to the parent component. [To listen for events, use an HTML attribute with the syntax ‘oneventtype’, So, our event type is close we use ‘onclose’]. 


Parent Component which uses slot :


HTML Syntax






In the parent component, we are using 2 buttons (Show and Edit) to show a record data using the base component according to users input we call 2 lightning record forms (view and edit respectively). We are using boolean variables to handle the forms display. And in footer we are using a close button to close the modal directly from parent component.

And as discuss above, [To listen for events, use an HTML attribute with the syntax ‘oneventtype’, So, our event type is close we use ‘onclose’]


On Click of Show Button




We are making show variable true and using account record id and ‘lightning-record-view-form’ to display the record data.



On Click of Edit Button



We are making edit variable true and using account record id and ‘lightning-record-edit-form’ to display the record data.



JavaScript:





At JS level we are using accId which contains record id of a account and other boolean variables to handle the event accordingly. And handleClick() method handles the button click of(show and edit) and handleCancel() method handle the close event from both parent and child component.


Blog’s Conclusion:

So, this blog gives us a brief about how can we use a single slot to fullfill same looking different requirement and also how can we handle a child component from both parent and child component.


Comments

Post a Comment

Popular posts from this blog

Using Data - Attributes in Lightning Web Component (LWC)`

  Agenda      While working with Salesforce data we need to play with Id’s (Identifiers). An ID can be associated with a Record or with a specifier. While working with records when we need to make changes or view any particular record details, we need their ID’s, as I describe in my previous blog’s how can we show or edit any salesforce record with the help of Basic components of Salesforce.  To create forms that let users view, edit, and create Salesforce records, use Lightning-record-form, Lightning-record-edit-form, and Lightning-record-view form components. And To use these components we need Salesforce Record ID. Blog Details This Blog gives us a brief about how can we get a particular record Id or value after any event using Data - Attributes.  What we are going to do In this blog, we use Data - Attribute functionality from HTML5  in our Lightning Web Component and use data attribute to get...

Select - Deselect checkbox in Lightning Web Component

  Agenda In the previous blog, we are working with a single record and manipulating it as per the requirements, but when we are working for the organization we need to think to cover larger aspects. And while working with salesforce data we should be aware of the fact that we might need to work with data in bulk. So, when we need to work with more than one record we use checkbox functionality and use that checkbox to see which record we need to make changes to. Blog Details The blog gives us a brief that how we select and unselect all checkboxes from the header checkbox. What we are going to do In this blog, we use a table to show the records with the checkbox for each record and a header checkbox to select - unselect all records. Requirements: You need to have access to your org.     Should have installed the VS Code Installed in your system.    You should have some basic knowledge of Lightning Web Component. You must have some proper records ...

Displaying User Level’s Dynamically In Lightning Web Component

  Agenda I am writing this blog, to display dynamic user data beautifully, It enables each user to see the users they need to report. With this dynamic component, you can control data visibility without having to create a separate component as well, with its own running user.  Now, It’s time to show the user’s of our organization dynamically their role in a beautiful way. How does it work? You fetch the User’s record with a simple Apex SOQL query and just displaying details Dynamically over a Lightning Web Component, And place that component where ever you want. Requirements: You need to have access to your org. Should have the VS Code Installed in your system. You should have some basic knowledge of Lightning Web Component. About Lightning Web Component (LWC): Lightning Web Components are lightweight custom elements based on native HTML and Modern JavaScript. Lightning Web Compone...