-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Resolve component class with $attributes property #32505
Conversation
This small change allows one to type hint the $attributes parameter in the class constructor. This parameter contains an array of all attributes that were passed to this component.
Thanks for the PR, however the tests are failing. |
@GrahamCampbell I've fixed the tests. |
You can access $this->attributes from within the |
@taylorotwell here is a reproducible demo showing that that is not the case, |
@panda-madness you commited your custom changes together with the framework skeleton which makes it super hard to find out what's wrong. |
@driesvints Sorry about that. I've added some changes in a single commit to mark every file I modified, I'll know to separate my changes in the future. |
Bump in the night, @taylorotwell |
@panda-madness I've actually stumbled onto this myself in the meantime. I'll have a look at this today. |
@driesvints something related to blade svg I guess? :) |
@panda-madness heh yeah. I discovered that you can still use attributes in your render method like this actually: public function render()
{
return <<<'blade'
<div class="<div {{ $attributes }}">
Foo
</div>
blade;
} Is there a special reason why you need to access the attributes? It's best that you provide a real-life use case. I'm having a look tomorrow again so let me know. |
@driesvints I was trying to make a sort of dynamic component that would render other components and pass along the attributes that it receives, sort of like in Vue. For that I needed to extract 1 defined attribute (the component name), and pass everything else in the template. I actually made attributes work the way Taylor described at some point while digging this issue, by moving this |
@panda-madness I'm looking into doing that exact thing but you have to be aware that What I would do in your case is dynamically render different views which have these components defined. Then you don't need to define them in your render method. |
@panda-madness I've looked further into this but was unable to find a clean solution. I think it's a no-go on attributes being available in the render method for now. |
@driesvints would you consider petitioning Taylor to re-open this PR then? I suspect he might have unsubscribed from the discussion here. |
No, because I don't think this is the right solution either to mix data with attributes. |
@driesvints I might be greatly misunderstanding how component tag compilation works, so please correct me if I'm wrong. From what I'm seeing, I don't really see where we're mixing anything that's not supposed to be mixed, as far as I can see we are only operating on the attributes that are passed to the component tag. Again, if I'm mistaken I'm sorry to have taken so much of your time on this. |
Technically your change will work momentarily I believe but I think it'll collide with changes made in #32576 for the next major release. |
Is there a way to trigger the test suite against the new master branch? Apart from creating a new PR. |
Yeah, run them locally. |
This should have been merged. Attributes and slots should be accessible through the class constructor. Here is a use case example for a component using Livewire: class Input extends Component
{
public $model;
public function __construct()
{
$this->model = $this->attributes->whereStartsWith('wire:model')->first();
}
public function render()
{
return <<<'HTML'
<div class="mb-4 space-y-1">
<input {{
$attributes->class([
'w-full placeholder-gray-400 rounded-lg',
'border-gray-400' => !$errors->has($model),
'border-red-600' => $errors->has($model),
])->merge([
'type' => 'text',
])
}}>
@error($model)
<p class="text-sm text-red-600">{{ $message }}</p>
@enderror
</div>
HTML;
}
} |
This small change allows one to type hint the $attributes parameter in the class constructor. This parameter contains an array of all attributes that were passed to this component.
As this change reserves the
$attribute
parameter name it's technically breaking, although it's pretty small, so not sure whether this is major or minor feature.I will adjust the tests if this PR gets a green light.