Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle cancel button action #656

Closed
Zorgatone opened this issue Jan 25, 2017 · 7 comments
Closed

How to handle cancel button action #656

Zorgatone opened this issue Jan 25, 2017 · 7 comments

Comments

@Zorgatone
Copy link

We have a callback parameter for the confirm button, but how can we use a callback for the cancel button?

@Zorgatone
Copy link
Author

Zorgatone commented Jan 25, 2017

I see in the code there's a doneFunction parameter.

I'm trying this code but seems my callback is ignored completely, what am I doing wrong?

(new Promise((resolve, reject) => {
  swal({
    title: "Delete data?",
    text: "Deleted data will be lost.",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Delete",
    closeOnConfirm: false,
    html: false,
    doneFunction(res) {
      if (res) {
        resolve();
      } else {
        reject();
      }
    }
  });
})).then(() => console.log("Resolved"), () => console.log("Rejected"))

@jeffmath
Copy link

jeffmath commented Feb 7, 2017

Same thing for me. My callback function is called when the ok button is clicked, but not for the cancel button.

@Zorgatone
Copy link
Author

@jeffmath I switched to https://github.com/limonte/sweetalert2 to solve that

@t4t5
Copy link
Owner

t4t5 commented May 13, 2017

You can find an example of how to do this in the documentation: http://t4t5.github.io/sweetalert/

"... and by passing a parameter, you can execute something else for 'Cancel'."

swal({
  title: "Are you sure?",
  text: "You will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  cancelButtonText: "No, cancel plx!",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  if (isConfirm) {
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
  } else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
  }
});

@t4t5 t4t5 closed this as completed May 13, 2017
@arpit786shukla
Copy link

@t4t5 Through isConfirm, we can check if the user response was Confirm button or something else. But could you please tell me how to check if the clicked button was a "Confirm" button or "Cancel" button or "Close" button because I'm using Cancel button also to redirect somewhere else but close button just closes the alert?

@gualopezb
Copy link

The actual issue is that in sweetalert v1.x the cancel handler is checking for the callback starts with function( https://github.com/t4t5/sweetalert/blob/v1.1.3/lib/modules/handle-click.js#L119.
So if that condition is not met, as @Zorgatone's example is using doneFunction( the callback is just going to be ignored.

@jdurand
Copy link

jdurand commented Jun 11, 2020

Good catch @gualopezb. I've had this working forever and it suddenly stopped working.

Le fix:

-    }, (code) => {
+    }, function(code) {

I'm guessing some change in my Babel configuration is at play.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants