Hey guys, today in this post we are going to learn about LWC how to Create horizontal tabs uses of selected radio group button value using ‘lightning-radio-group’ element and display selected radio value in Salesforce Lightning Web Component — LWC.
A lightning-radio-group component represents a group of radio buttons that permit only one button to be selected at a time. The component renders radio button input elements and assigns the same value to the name attribute for each element. The common name attribute joins the elements in a group. If you select any radio button in that group, any previously selected button in the group is deselected. To know more, click here..
To create radio buttons, pass in the following properties to the options attribute.
PROPERTY | TYPE | DESCRIPTION |
---|---|---|
label | string | The text that displays next to a radio button. |
value | string | The string that’s used to identify which radio button is selected. |
The radio group is nested in a fieldset element that contains a legend element. The legend contains the label value. The fieldset element enables grouping of related radio buttons to facilitate tabbing navigation and speech navigation for accessibility purposes. Similarly, the legend element improves accessibility by enabling a caption to be assigned to the fieldset.
lightning-radio-group is available in the Base Components Recipes GitHub repository. It’s transpiled into the c namespace so that you can use it in your own projects.
Component Styling
lightning-radio-group implements the radio button blueprint in the Salesforce Lightning Design System (SLDS).
Set type=”button” to create a component that implements the radio button group blueprint in SLDS.
Variant attribute of radio group
- label-hidden hides the radio group label but makes it available to assistive technology. This variant does not hide the option labels.
- label-inline horizontally aligns the label and radio group.
- label-stacked places the label above the radio group.
- standard is the default variant, which displays the radio group label above the options.
Files we used in this post example
lwcRadioButtonGroup.html | LWc HTML File | Template HTML file to get selected radio group button value in Salesforce Lightning Web Component (LWC) |
lwcRadioButtonGroup.js | LWC JavaScript File | Itβs hold a javascript on handlechange function to get selected radio group button value and display in lightning component. |
lwcRadioButtonGroup.js-meta.xml | XML Meta File | It is used to where this lightning web component file you want to display as like lightning__AppPage, lightning__RecordPage, lightning__HomePage. |
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps
Create Lightning Web Component HTML
Step 1:- Create Lightning Web Component HTML β‘ lwcRadioButtonGroup.html
SFDX:Lightning Web Component β‘ New β‘ lwcRadioButtonGroup.html
lwcRadioButtonGroup.html [Lightning Web Component HTML]
<template>
<lightning-card title="Get Radio Group Button Selected Value in Lightning Web Component -- LWC" icon-name="custom:custom19" size="small">
<div class="slds-p-around_medium">
<lightning-radio-group name="radioGroup"
options={options}
value={value}
type="button" onchange={handleRadioChange}>
</lightning-radio-group>
<section aria-label="Dialog title" aria-describedby="popover-body-id" class="slds-popover" style="width: 50%;">
<div class="slds-popover__body">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_12-of-12" style="background: #eee; padding: 10px;">
<template if:true={tutorialValue}>
<p class="slds-text-heading_medium slds-text-color--error"><strong>Tutorial</strong></p>
<p>An easy way to learn step-by-step online salesforce free tutorial from <strong>w3web.net</strong>, To learn <span class="readMore"><a href="https://www.w3web.net/tutorial/" target="_blank" rel="noopener noreferrer">more...</a></span></p>
</template>
<template if:true={auraCompValue}>
<p class="slds-text-heading_medium slds-text-color--error"><strong>Aura Component</strong></p>
<p>The Lightning Component framework is a UI framework for developing web apps for mobile and desktop devices. It's a modern framework for building single-page applications with dynamic, responsive user interfaces for Lightning Platform apps. To live demo project learn <span class="readMore"><a href="https://www.w3web.net/tutorial/lightning-component/" target="_blank" rel="noopener noreferrer">more...</a></span></p>
</template>
<template if:true={salesforceLwcValue}>
<p class="slds-text-heading_medium slds-text-color--error"><strong> Salesforce LWC</strong></p>
<p>Enhance your Salesforce usersβ experience with customizable Lightning Web Components. Find the Lightning Web Components that are right for you on AppExchange. Connect With Customers. Collaborate Better. Salesforce Admin Tools. To live demo project learn <span class="readMore"><a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">more...</a></span></p>
</template>
</div>
</div>
</div>
</section>
</div>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like β</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</lightning-card>
</template>
</lightning-card>
</template>
Create Lightning Web Component Javascript
Step 2:- Create Lightning Web Component Javascript β‘ lwcRadioButtonGroup.js
SFDX:Lightning Web Component β‘ New β‘ lwcRadioButtonGroup.js
lwcRadioButtonGroup.js [LWC JavaScript File]
import { LightningElement,track } from 'lwc';
export default class LwcRadioButtonGroup extends LightningElement {
value = 'Tutorial';
get options() {
return [
{ label: 'Tutorial', value: 'Tutorial' },
{ label: 'Aura Component', value: 'Aura Component' },
{ label: 'Salesforce LWC', value: 'Salesforce LWC' },
];
}
@track tutorialValue = true;
@track auraCompValue = false;
@track salesforceLwcValue = false;
handleRadioChange(event) {
const selectedOption = event.detail.value;
//alert('selectedOption ' + selectedOption);
if (selectedOption == 'Tutorial'){
this.tutorialValue = true;
}else{
this.tutorialValue = false;
}
if (selectedOption == 'Aura Component'){
this.auraCompValue = true;
}
else{
this.auraCompValue = false;
}
if (selectedOption == 'Salesforce LWC'){
this.salesforceLwcValue = true;
}
else{
this.salesforceLwcValue = false;
}
}
}
Create Lightning Web Component Meta XML
Step 3:- Create Lightning Web Component Meta XML β‘ lwcRadioButtonGroup.js-meta.xml
SFDX:Lightning Web Component β‘ New >> lwcRadioButtonGroup.js-meta.xml
lwcRadioButtonGroup.js-meta.xml [LWC Meta Data XML]
<!--?xml version="1.0" encoding="UTF-8"?-->
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Create Lightning Application
Step 4:- Create Lightning Application : lwcRadioButtonGroupApp.app
From Developer Console β‘ File β‘ New β‘ Lightning Application
lwcRadioButtonGroupApp.app [Component Application File]
<c:lwcRadioButtonGroup/>
Further post that would you like to learn in Salesforce
What is button group in radio button?
The ButtonGroup component manages the selected/unselected state for a set of buttons.
How do you make a radio button group required?
To group radio buttons should have the same value for the name attribute. It's allows us to select only one radio button at once and apply the required attribute to the whole group.
Which attribute sets the current state of radio button?
checked attribute in radio button is used to set the current state of a radio button.
Related Topics | You May Also Like
Our Free Courses β
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β
π Aura Lightning Framework π Lightning Web Component (LWC) π Rest APIs Integration |