How to create a Rating Component in Lightning Web Component?

Hello #Trailblazers,

In this blog post, we are going to talk how to create the rating component using Lightning Web Component

There are many use cases when we need to develop the rating component to display the rating for example survey result, interview feedback.

Step1 – Create Static Resource

Create a Static resource and name it fivestar and you can get the Static Resource. Five Star Rating

Step2 – Create a Lightning Web Component

Now, once you have create the Static resource, it’s time to Create the Rating Component.

  1. Create a Lightning Web Component
  2. use the code from the Given link
  3. Link to Code

Usage

Call the component from the parent component like below

<div >
                <lightning-tile label="Salesforce" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="5" onratingchange={handleRatingChanged} ></c-five-star-rating>
                </lightning-tile>    
            </div>

Code Explanation

  1. value – In the code, you can see that I have passed one variable value which indicates what value you wanted to pass and the valid values are 1-5
  2. onratingchange – this is the event raised by the rating component which in return sends the rating value from 1-5
  3. readOnly – If you wanted your rating to be read-only then pass any value to this property like read-only=”yes”
  4. handleRatingChanged(event){ let rating=event.detail.rating; }

Step3 – Create a feedback Component

  1. Create a Lightning Web Component
  2. use the code from the Given link
  3. Link to Code
<!--
    @description       : 
    @author            : Amit Singh
    @group             : 
    @last modified on  : 12-18-2020
    @last modified by  : Amit Singh
    Modifications Log 
    Ver   Date         Author                               Modification
    1.0   12-18-2020   Amit Singh   Initial Version
-->
<template>
    <lightning-card title='Skill Set'>
        <div class="slds-m-top_medium slds-m-bottom_x-small">
            <div >
                <lightning-tile label="Salesforce" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="5" onratingchange={handleRatingChanged} ></c-five-star-rating>
                </lightning-tile>    
            </div>
            <div>
                <lightning-tile label="Integration" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="5" ></c-five-star-rating>
                </lightning-tile>    
            </div>
            <div>
                <lightning-tile label="Lightning Development" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="5" ></c-five-star-rating>
                </lightning-tile>    
            </div>
            <div >
                <lightning-tile label="Admin" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="5" ></c-five-star-rating>
                </lightning-tile>    
            </div>
            <div>
                <lightning-tile label="Communities" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="4" ></c-five-star-rating>
                </lightning-tile>    
            </div>
            <div>
                <lightning-tile label="CI/CD" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="4" ></c-five-star-rating>
                </lightning-tile>    
            </div>
            <div >
                <lightning-tile label="Project Management" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="3.5" ></c-five-star-rating>
                </lightning-tile>    
            </div>
            <div >
                <lightning-tile label="MuleSoft" href="JavaScript:void(0);" type="media">
                    <c-five-star-rating value="3" ></c-five-star-rating>
                </lightning-tile>    
            </div>
        </div>
    </lightning-card>
</template>

Step4 – Preview the Feedback Component

Now, as you have created the feedback component. You can add your component to either Aura Application, OR Lightning Pages to see the outcome. Refer below image

Thanks for reading

#Salesforce #DeveloperGeeks

Please like, share & Subscribe the Blog Post and YouTube Channel

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading