Cancel button still showing on confirmation
Hi,
I noticed that if I use default config like this one:
swal.setDefaults({
showCancelButton: true
});
the Cancel button still showing when this is executed:
swal("Success!", "Your data has been saved.", "success");

... but if I remove showCancelButton from setDefaults and add it to the swal function config like this:
swal({
title: 'Enter your name',
text: 'Enter your name',
type: "input",
showCancelButton: true,
}
the Cancel button doesn't show up anymore, which is the expected behaviour.

I guess this is a bug.
Hey @geodeveloper
Do you have a callback function after the input swal?
If so, I assume you have another swal as the success message, which has its own settings (none of which are inherited by its parent). You would have to redeclare the showCancelbutton property.
Hi @fallen-rve, Yes I have a callback function. I've took the example of input from official sweetalert website and just added the default config on top. Take a look here --> https://jsfiddle.net/a836z9xv/ so you can see the bug.
Thanks for your time.
@geodeveloper gotcha!
That is the expected behavior of setDefaults. If you set the setDefaults property, it'll apply to all of your SweetAlerts, regardless if it's the shorthand or longhand versions.
From the documentation:
If you end up using a lot of the same settings when calling SweetAlert, you can use setDefaults at the start of your program to set them once and for all!
Let me know if that clears it up.
Thanks!
As @ryanvaness pointed out, this is indeed the expected behaviour. :) Don't use setDefaults unless it applies to all your alerts!
@ryanvaness, @t4t5 thanks for the reply (and sorry for my late response),
I see what you mean and it makes sense, I was confused because I thought if I pass the showCancelButton to the swal function will override the default setting.
swal({
title: 'Enter your name',
text: 'Enter your name',
type: "input",
showCancelButton: true, <-- should override the default setting?
}
and the default setting should apply only if there is no override setting passed to swal.
But if this was the expected behaviour then I would have to change the way I use the cancel button.
Thanks again!
@geodeveloper Hmm, that should indeed override the user defaults, so if it does not then it seems like a bug. Using the the buttons option in SweetAlert 2.0 is probably more reliable now :)
@t4t5 I just checked and indeed it overrides correctly, I think I had some issues with the configuration. All works great, thanks!