How to access form state in Custom Livewire component
I have a form with:
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')->live(),
TextInput::make('slug')->live(),
RichEditor::make('content')->live(),
Livewire::make(MyDynamicComponent::class, ['bar' => 'baz']);
]);
}
but MyDynamicComponent need access to the form state as it need to render infos based on the latest form fields (preview).
the Livewire::getData() is only called one time during initialization and updated values are not passed to the component
7 Replies
Do you absolutely need a Livewire component? Reactivity in Livewire is not that elaborate and in combination with Filament probably worse.
How would you do this?
I need to render a preview of some data. It is a ChartJS.
(basically I have a lot of repeaters with many data on it)
I'm using a Filament Widget wrapped into a Livewire component.
The only way I've found to properly is to use the ->key(fn() => uniqid()) so that the component is detroied and re-rendered each time.
I'm open to any other alternative solution if there are better way to do so.
Unfortunately boss is a PITA. And decided to use filament fore more than crud input.
Then you can just use a
ViewField
which can access the form data or the record via $getRecord()
Filament Widgets are already Livewire components. So you wrap a LW component into another one
The only way I've found to properly is to use the ->key(fn() => uniqid()) so that the component is detroied and re-rendered each time.So, it's working? 🤔 You could get the current record from the page/url. But the form data is more complex and probably needs to be passed around via Livewire events
MyDynamicComponent extends FilamentChat widget.
So I'm only wrapping it once.
It is working for now but we are planning to move to v4 in the future and we have created a lot of edge cases where we pushed filament to the limit.
And this is one of the cases. so I wanted to know if there is a better way.
I cannot use $record as the data must change real time.
is it must depend of $livewire->state array.
Why do you need the form state for the chat?
And that works with your approach? 🤔
LEt me give you the full-ish example
The chart component:
This is how I'm using it.,
Oh nice. Honestly, I didn't know you can pass a Closure for the 2nd param.