jantinnerezo / livewire-alert

SweetAlert2 wrapper for Livewire
https://livewire-alert.jantinnerezo.com
MIT License
682 stars 73 forks source link

Please add new functionality #146

Open szajens opened 4 months ago

szajens commented 4 months ago

Hello, perhaps in the next version, a function similar to the one I wrote yesterday could be added:

namespace App\Traits;
use Jantinnerezo\LivewireAlert\LivewireAlert;
use Livewire\Attributes\On;

/**
 * Class Helper.
 */
trait Helper
{

    use LivewireAlert;

    public function confirmMethod($method, $title, $text = null, $id = null){
        $this->confirm($title, [
            'onConfirmed' => 'runMethod',
            'data' => [
                'idFromConfirm' => $id,
                'confirmMethod' => $method
            ],
            'text' => $text,
        ]);
    }

    #[On('runMethod')]
    public function runMethod($data){
        $id = $data['idFromConfirm'];
        $methodName = $data['confirmMethod'];

        if($id === null){
            $this->{$methodName}();
        } else {
            $this->{$methodName}($id);
        }

    }

How to use it in Blade?

<button wire:click="confirmMethod('delete', 'Do you want to delete?', '', $id)" >Run function</button> 

This will execute $this->delete($id)

No need to use protected $listeners = ['delete']; or #[On('delete')]