FilamentF
Filament12mo ago
Sven

afterStateUpdated in toggle in a custom component not firering

What I want to do
I want to have a toggle in my profilenav to switch to an expert mode for extended options. I am using the formbuilder not for its functionality but because of the consistent look and feel / easy way of getting a toggle.

What is happening
After refreshing the page the toggle is still turned off, even if I interacted with it. When toggleing I do not see any network activity, so I think that toggleing just doesnt do anything.

My code
<?php

namespace App\Livewire;

use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Livewire\Component;

class ExpertModeSwitcher extends Component implements HasForms
{
    use InteractsWithForms;

    public ?array $data = [];

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Toggle::make('active')
                    ->label('Expertenmodus')
                    ->default(session('expertMode'))
                    ->live(onBlur: true)
                    ->afterStateUpdated(fn (Set $set, ?string $state) => 
                        session(['expertMode' => $state])
                    )
            ])
            ->statePath('data');
    }

    public function render()
    {
        return view('livewire.expert-mode-switcher');
    }
}
Solution
add $this->form->fill() to the mount method
Was this page helpful?