Filament Form Fields as custom components

So i´m trying to develop a Custom Component which is an extension of the Filament RichEditor and add some more functionality to it and later call it from other packages through the CustomField::make()->label()->model() inside a $form->schema([]) but i´m having trouble calling any of the public functions because it´s not a livewire component, even dispatching events and listening for them inside the custom field doesnt work how do you make the field become a livewire component? or does it even have to? how can i emit an event to the field and listen to it? or call the public function directly? i want most of the configuration to be done on the custom field and only insert the name, label and a DB Model so i can call $model->save() etc.
Thanks for your time and really appreciate any help since i`m a beginner with filament / livewire
10 Replies
awcodes
awcodes2w ago
You can register listeners on fields using $this->registerListeners() in the setUp method on your custom fields.
Huemer
HuemerOP2w ago
i tried and just wanted a simple alert() executed but i can´t seem to get it to work i dispatched the event through @click=$dispatch('name'), wire:click="dispatchFormEvent('name')" also tried $wire.emit() and it´s variants, but it get´s lost somewhere, trying to execute the public functions directly results in an error saying this are not public functions of the livewire component, so basicly can´t find them using the $field variable i clearly see the field and the registered listeners but also can´t make it work through that one listening for events directly on the view from which it gets executed works fine through a window.addEvenetListener(() => alert()) so dispatching the event works fine and in the setUp function i would have defined ->registerListeners([ 'name' => [fn() => alert('test')]]) but this never executes i also tried it through the #[On('name')] above the function i want to call
on the web all i read is to wrap it inside a livewire component but can i still call the component through component::make() inside my applications filament resources $form->schema([]) call?
awcodes
awcodes2w ago
Is the method on the page component? In filament the fields aren’t livewire components, the page are. You can register the listener on the field but the page is the Lw component so the method has to exist there.
Huemer
HuemerOP2w ago
the page lives inside another app, my form field is in it`s own package and should be reused across multiple pages
i think i really just have to rebuild it completly as its own Component instead of a form field and then use the component inside the main application thought i could get most of the functionality from filaments RichText Editor instead of building it myself and only add the extra toolbar with the new buttons and their functionality
awcodes
awcodes2w ago
In that case I would use a trait that can be added to the pages
Huemer
HuemerOP2w ago
gonna have to look into traits then 🙂 definetly thanks for your advice and time
awcodes
awcodes2w ago
Just have to know what is and isn’t the livewire component. I’d have to double check the code but I also think disptchFormEvent might be deprecated, since livewire v3 changed everything to just dispatch.
Huemer
HuemerOP2w ago
oh thats definetly good to know at least one option i can delete 😄 yeah just thought it kinda off how hard it is to make the custom field react like a normal livewire component and sadly there is hardly any info on the web. i tried laracasts video courses but think i need to rewatch the advanced part again
awcodes
awcodes2w ago
Filament has its opinions but it’s still just laravel and livewire. So as long as you get the context right it will work.
Huemer
HuemerOP2w ago
thanks for your time 🙂 i will try some options and report back

Did you find this page helpful?