Stop using JavaScript prompt()
immediately because you can use the brand
new LWC/Aura component LightningPrompt
instead of that. In this post, I
will show you how to create this beautiful and elegant prompt dialog in
Lightning web components.
Use Case
You need to show a popup dialog to the user and ask for any input. So you can use the LightningPrompt component instead of using the SLDS modal or a prompt.
The LightningPrompt Component
This component shows a modal popup with a header, message, and input box. There are three different parameters to this component.
message
- the message you want to display on dialog.label
- this is the header label for the dialog.-
defaultValue
- this is the initial input value, you can skip this if you don't want the default value.
Steps for creating an alert using LightningPrompt
-
Import the lightning prompt component.
import LightningPrompt from "lightning/prompt";
-
Write a function to open LightningPrompt. Set the message, label, and
defaultValue attributes as per your requirement.
handlePromptClick() { LightningPrompt.open({ message: "this is the prompt message", //theme defaults to "default" label: "Please Respond", // this is the header text defaultValue: "initial input value" //this is optional }).then((value) => { if (value) { // do something with the value } }); }
- Call the function
handlePromptClick
.
Complete Code Sample for LightningPrompt
lightningPromptDemo.js
import { LightningElement } from "lwc"; import LightningPrompt from "lightning/prompt"; import LightningAlert from "lightning/alert"; export default class LightningPromptDemo extends LightningElement { handlePromptClick() { LightningPrompt.open({ message: "this is the prompt message", //theme defaults to "default" label: "Please Respond", // this is the header text defaultValue: "initial input value" //this is optional }).then((value) => { if (value) { // do something with the value this.handleSuccessAlertClick(value); } else { this.handleErrorAlertClick(); } }); } async handleSuccessAlertClick(value) { await LightningAlert.open({ message: "You entered:" + value, theme: "success", label: "Success!" }); } async handleErrorAlertClick() { await LightningAlert.open({ message: "You entered nothing!", theme: "error", label: "Error!" }); } }
lightningPromptDemo.html
<template> <lightning-button variant="brand" onclick={handlePromptClick} label="Show Prompt Dialog" > </lightning-button> </template>
Code Output
Related Posts
- Creating standard Alert, Confirm, Prompt dialogs in LWC
- How to create an Alert using LightningAlert LWC?
- How to create a Confirm dialog using LightningConfirm LWC?
You can find all the code from this post on this GitHub repo: Lightning Dialogs.
I hope this was helpful. Did you ever build something like this? What did you use? Let me know in the comments. Thanks for reading!
No comments :
Post a Comment
Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com