How to use Alpine in Filament?
If I have this very basic example:
MyPage.php
my-page.blade.php
I can't understand how I can access the field state from alpine...
What if I want to change the state of
another_text_input
each time my_text_input
state changes, for example?16 Replies
Ah! This works just the same as sharing any other Livewire state with JS, with the only difference that the LW property is also written/read by Filament in addition to your own reads/writes.
Try
See https://livewire.laravel.com/docs/alpine#sharing-state-using-wireentangle
Also, I would personally not recommend creating individual properties for all of them, but just to add
public ?array $data = null
and then $form->statePath('data')
, then you will only have just one property on your Livewire. And then in Alpine you do state: $wire.entangle('state').live
.
(You are also missing a mount()
method that includes a $this->form->fill()
but perhaps you just omitted that for simplicity)Thank you but what I looking for is a way to interact with an existing TextInput or ColorPicker without extending it (if possible)
Yes, with what I shared you can
In my case I doesn't need any database connection, I try to avoid request and do all the reactive logic with Alpine only, without Livewire
wait, I'll read your message again
If you update the
my_text_input
, then Alpine is entangled with the $my_text_input
on the server. That means that if you update that property in Alpine, the $my_text_input
property also gets updated on the server. In turn, your TextInput or ColorPicker is also entangled with $my_text_input
, so if $my_text_input
updates, your TextInput or ColorPicker also updates.
And that also goes two-ways, if your TextInput or ColorPicker state updates, then the $my_text_input
gets updated, Livewire/Alpine knows that it is entangled as well with your custom x-data
and then it gets updated both
You should try, but it might even work without the .live
and without a server requestSo what you say is I need to entangle myself the fields in a div? Like that?
Yeah for example
You need to entangle it somehow
It depends on what you want to do with it again after you got access to the values
An alternative, you can also do it like this: https://livewire.laravel.com/docs/properties#accessing-properties-from-javascript
Laravel
Properties | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Then you dont need a div
Ok, I thought it was already entangled by Filament.
I cannot say I understand better 😅 , but thank you very much, I will explore all that
To add a bit of context, what I'm trying to do is exactly that: https://filament-theme-generator.charlie-etienne.fr/
so update the code when the color changes,
BUT in client only (since it doesn't need to call the server here)
maybe
$watch
?
I think you can also use extraAlpineAttributes
in the form fields like
Thank you @Leandro Ferreira , yeah I wanted to avoid extraAttributes cause I have a bunch of JS and I would prefer to extract it.
ok, I think you can also use an eventlistener like
oh, interesting, thanks
Yeah, I did it!!!
https://filament-theme-generator.charlie-etienne.fr/
Not even one server request!
I don't know if it follows the best Alpine practices, but I manage to make it work and it's FAST
Thanks to both of you @Leandro Ferreira and @ralphjsmit
Leandro Ferreira (@leandrocfe) on X
In Filament V4, a new method called afterStateUpdatedJs will be introduced, enabling state updates on the client side using Javascript 🤯
X
wow, this feature is pure gold