t4t5 / sweetalert

A beautiful replacement for JavaScript's "alert"
https://sweetalert.js.org
MIT License
22.4k stars 2.84k forks source link

Clicking cancel also returns true #941

Open adnanafzal565 opened 3 years ago

adnanafzal565 commented 3 years ago

Clicking cancel also returns true

swal({ title: "Are you sure?", text: "Once deleted, all its sub-products will be deleted as well!", icon: "danger", buttons: true, dangerMode: true, showCancelButton: true }) .then((sdkfjdsfdsfsdfwillDelete) => { if (sdkfjdsfdsfsdfwillDelete) { console.log("here"); } });

loganpoynterdev commented 3 years ago

Testing your code I don't see it returning true, is there more context you can give on what issue you're running into? Cancel returns null for me and I only see true in the console if I click ok:

swal({
            title: "Confirm Action",
            icon: "info",
            text: "Are you sure you want to confirm this action",
            buttons: true,
            showCancelButton: true
        })
            .then((value) => {
                console.log(value);

However, from a warning that you should see in your console, "showCancelButton" option has been deprecated and you should make use of buttons like so which will allow you to check specifically for true/false instead of true/null:

swal({
            title: "Confirm Action",
            icon: "info",
            text: "Are you sure you want to confirm this action",
            buttons: {
                cancel: {
                    text: "Cancel",
                    value: false,
                    visible: true,
                },
                confirm: {
                    text: "Ok",
                    value: true,
                    visible: true,
                }
            }
        })
            .then((value) => {
                console.log(value)