[7.x] Make ComponentAttributeBag Macroable#33354
Conversation
I am looking to PR a macro to Livewire to create a method on the ComponentAttributeBag that easily allows the user to do the following: <x-text-input wire:model="name" /> but I would need this class to be Macroable first.
|
Can you tell me more about how this helps your situation? I have put wire:model on a Blade component before already. |
|
Currently, I can hardcode Inside Livewire Blade Template <x-text-input model-name="firstName" />Inside Blade Component (components/text-input.blade.php) <input wire:model="{{$modelName}}" />
@error($modelName)
{{$message}}
@enderrorThe problem is I lose the ability to add a modifier to wire:model like The implementation in the gist solves that issue, and it would be cleaner if we could take that attribute bag iterator logic out of the component itself and put it into a macro. Then in the blade component (after the PR to livewire), we could do the following inside the blade component: Usage Inside Livewire Blade Template <x-text-input wire:model.lazy="firstName" />Inside Blade Component (components/text-input.blade.php) <input {{$attributes->wireModel()}} /> // $attributes->wireModel() = 'wire:model.lazy="firstName"'
@error($attributes->wireModelName()) // $attributes->wireModelName() = 'firstName'
{{$message}}
@enderror |
|
I've also added So you may no longer need the macro after that. |
|
Also added |
I think the ComponentAttributeBag should be macroable in general, but specifically because I'd like to submit a PR to Livewire to create a method on the ComponentAttributeBag that easily allows the user to do the following:
<x-text-input wire:model="name" />. Once this class is macroable, I can submit the PR to Livewire to make it easier to integrate blade components with Livewire.You can reference this gist to see how the implementation with Livewire would work, but this would be cleaner to pull out the code in the @php blade directive and put it into a macro.
https://gist.github.com/iAmKevinMcKee/c116282dbbbe34dcd5b3bda4242a3d7a
I've spoken with Caleb and he would accept the PR to Livewire to add this macro once the class is macroable.