0

I have a scenario where I have toggle input and if the toggle input is changed, I have a Lightning Confirm popup and in that if the user selects okay button, the system should put whatever value the toggle input has current value in it but if the user selects cancel button, the system should put back to it's original state.

For example: Current Value of Toggle when page loads is True, If the user clicks on Toggle input, the Lightning confirm will open If the user selects okay button in the popup, the toggle should be disabled. If the user selects cancel button in the popup, the toggle should remain in it's previous state as True.

Here is the sample code I have

-----HTML---

<lightning-input type="toggle" lable="publish" onchange={handleToggleChange}  checked={isActive}></lightning-input>

-----JS-----

import { LightningElement, track } from 'lwc';
import LightningConfirm from 'lightning/confirm';

export default class MyApp extends LightningElement {


  @track isActive = false;

  async handleToggleChange() {
      const result = await LightningConfirm.open({
          message: 'this is the prompt message',
          variant: 'headerless',
      });
        
      if (result){
          //Do nothing because the user has provided consent to publish
      } else {
          this.isActive = !this.isActive;
      }
    }
}

The problem here is when the user clicks on Cancel button in the lightning prompt, toggle field is not setting back to it's original value. Am I missing something here? Any help would be appreciated! Thanks!

0

2 Answers 2

0

You need to prevent the value from being set until the confirmation occurs.

<lightning-input type="checkbox" onclick={confirmAction} ...></lightning-input>
async confirmAction(event) {
  if(event.target.checked) {
    return;
  }
  event.preventDefault();
  let result = await LightningConfirm.open({
    message: 'This is the message',
    variant: 'headerless'
  });
  if(result) {
    this.value = true;
  }
}
2
  • Thank you @sfdcfox for the response. The cancel part is still not working. Let me try to rephrase my question. If the user clicks on toggle (no matter whatever state it is in right now), the pop should open and take the consent there. If the user clicks on okay button, whatever value has been there in current toggle take it forward and if the cancel button is clicked, change it to the previous state. I hope I am clear now.
    – SilverWing
    Commented May 15 at 4:25
  • Another thing, setting the value for the toggle inside lightning confirm is NOT working.
    – SilverWing
    Commented May 15 at 4:32
0

@@Loki_72 As u expected it will be false at first when we press cancel( even though toggle is on) it will become false again(Previous state) if we press okay nothing happens.


    async handleToggleChange() {
        const result = await LightningConfirm.open({
            message: 'this is the prompt message',
            variant: 'headerless',
        });
        
     if (result){
          //Do nothing because the user has provided consent to publish
      } else {
          this.template.querySelectorAll("lightning-input")[0].checked = this.isActive;
      }
1
  • Thank you @jenifa It worked!!!
    – SilverWing
    Commented May 18 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .