Skip to content

Commit

Permalink
[8.x] Allow components to use custom attribute bag (#36186)
Browse files Browse the repository at this point in the history
* allow components to use custom attribute bag

* Update Component.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
rodrigopedra and taylorotwell authored Feb 8, 2021
1 parent 79b98fe commit 2f96f84
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/View/AnonymousComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function render()
*/
public function data()
{
$this->attributes = $this->attributes ?: new ComponentAttributeBag;
$this->attributes = $this->attributes ?: $this->newAttributeBag();

return $this->data + ['attributes' => $this->attributes];
}
Expand Down
15 changes: 13 additions & 2 deletions src/Illuminate/View/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function createBladeViewFromString($factory, $contents)
*/
public function data()
{
$this->attributes = $this->attributes ?: new ComponentAttributeBag;
$this->attributes = $this->attributes ?: $this->newAttributeBag();

return array_merge($this->extractPublicProperties(), $this->extractPublicMethods());
}
Expand Down Expand Up @@ -266,13 +266,24 @@ public function withName($name)
*/
public function withAttributes(array $attributes)
{
$this->attributes = $this->attributes ?: new ComponentAttributeBag;
$this->attributes = $this->attributes ?: $this->newAttributeBag();

$this->attributes->setAttributes($attributes);

return $this;
}

/**
* Get a new attribute bag instance.
*
* @param array $attributes
* @return \Illuminate\View\ComponentAttributeBag
*/
protected function newAttributeBag(array $attributes = [])
{
return new ComponentAttributeBag($attributes);
}

/**
* Determine if the component should be rendered.
*
Expand Down

0 comments on commit 2f96f84

Please sign in to comment.