Monday 27 January 2020

How to define design properties(attributes) in lightning web components

Hi ,

Whenever we want to provide the provision to set default values for the web components while including on different lightning page types then we use design properties. So to define design properties we have to use "@api" properties in Javascript file and we have to define design properties for the same in configuration file of lightning web component.
We can define design properties for different targets by using "targetConfig" .

Here i have configured for 2 targets
1) "lightning__AppPage"
 <targetConfig targets="lightning__AppPage">           
            <property name="title" type="string"/>
  </targetConfig>

2) "lightning__RecordPage"

 <targetConfig targets="lightning__RecordPage"> 
             <property name="title" type="string"/>         
            <property name="greeting" type="string"/>
     </targetConfig>

 The attribute "name" in <property> tag. This value must match the property name in the component’s JavaScript class.

You can define label also. It will be displayed as a label for the attribute in the tool.

Eg:
----
Calculator Example:
-----------------------
Html File:

<template>
    <lightning-card  title={title}>
        <lightning-button label="Submit" slot="actions" onclick={handleClick}></lightning-button>
        <p class="slds-p-horizontal_small">
            <lightning-input type="number" vaue={fNumber} name="fstNumber"  label="Enter First Number" onchange={handleChange} ></lightning-input>
            <lightning-input type="number" vaue={sNumber} name="scdNumber"  label="Enter Second Number" onchange={handleChange} ></lightning-input>
        </p>
        <p slot="footer">
            <b>{sum}</b>   
            <b>{greeting}</b>   
        </p>
    </lightning-card>
</template>

javascript File:
--------------------
import { LightningElement,track,api } from 'lwc';
import getSumResult from '@salesforce/apex/CalculateNumbers.getSummResult';
export default class Caclulate2Numbers extends LightningElement {
    @track fNumber;
    @track sNumber;
    @track sum;
    @track errors;
    @api title;
    @api greeting;   
    handleClick(){
        getSumResult({firstNumber:this.fNumber,secondNumber:this.sNumber})
        .then(result=>{
            this.sum = result;
        })
        .catch(error=>{
            this.errors = error;
        });
    }
    handleChange(event){
        if(event.target.name==='fstNumber'){
            this.fNumber = event.target.value;           
        }
        else if(event.target.name==='scdNumber'){
            this.sNumber = event.target.value;           
        }
    }
}

Meta File: (where we define design properties for @api variables declared in javascript)
------------
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__Tab</target> 
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>     
    </targets>   
    <targetConfigs>
        <targetConfig targets="lightning__AppPage">            
            <property name="title" type="string"/>
        </targetConfig>
<targetConfig targets="lightning__RecordPage">  
             <property name="title" type="string"/>          
            <property name="greeting" type="string"/>
     </targetConfig>
    </targetConfigs> 
 
</LightningComponentBundle>

Notes:
Targets are mandatory before you define targets in <targetConfig>.

Output:
----------
While including in app page:
----------------------------------

While including in Record Page:
-------------------------------------


Reference:
https://www.youtube.com/watch?v=G7oJDD6RMWU&list=PL-JzyFWuCbkKcFSOCRlCUk79YbET5QvZC

https://developer.salesforce.com/docs/component-library/documentation/lwc/reference_configuration_tags

5 comments:

  1. Nice article! I found many useful information in your blog, it was awesome to read,thanks for sharing this great content, keep sharing..
    Salesforce Support and Maintenance Services

    ReplyDelete
  2. The information which you have provided is very good. It is very useful who is looking for salesforce online training in Hyderabad

    ReplyDelete
  3. Thanks a lot for explaining practically. Really Appreciable!
    Integrate Salesforce with third party software seamlessly and smoothly with the help of Atocloud.

    ReplyDelete
  4. Quick! Book Your Spot for Wednesday[4th March, 11 AM(IST) | 4 PM(GST) | 11:00 AM(EST)] WEBINAR on topic: How to Perform Bulk Operation in Salesforce using BOFC App?

    You'll learn
    How to Bulk Compare Multiple Profiles and Permission Sets and Export in XLS, Manage Record Types in Bulk, Clone Multiple Custom Fields and Objects.

    ReplyDelete

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...