Skip to content

Commit 87a7333

Browse files
committed
Fix component attributes
1 parent 8b728ff commit 87a7333

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

packages/edge/src/Component/AbstractComponent.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,26 @@ public function withName(string $name): static
240240
*
241241
* @return static
242242
*
243-
* @deprecated 5.0 Use new method to merge attributes.
243+
* @deprecated 5.0 This method is not immutable, use new method to merge attributes.
244244
*/
245245
public function withAttributes(array $attributes, array|ComponentAttributes $binding = []): static
246246
{
247247
// if ($binding instanceof ComponentAttributes) {
248248
// $binding = $binding->getAttributes();
249249
// }
250250

251-
$this->attributes = $this->attributes ?: $this->newAttributeBag();
251+
$this->getComponentAttributes();
252252

253-
$this->attributes->setAttributes(
254-
[
255-
...$this->attributes->getAttributes(),
256-
...$attributes
257-
]
258-
);
253+
$this->attributes->merge($attributes);
254+
255+
return $this;
256+
}
257+
258+
public function mergeAttributes(array $attributes): static
259+
{
260+
$this->getComponentAttributes();
261+
262+
$this->attributes = $this->attributes->withMerge($attributes);
259263

260264
return $this;
261265
}

packages/edge/src/Component/ComponentAttributes.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function class(mixed $classList): static
253253
}
254254
}
255255

256-
return $this->merge(['class' => implode(' ', $classes)]);
256+
return $this->withMerge(['class' => implode(' ', $classes)]);
257257
}
258258

259259
/**
@@ -264,7 +264,12 @@ public function class(mixed $classList): static
264264
*
265265
* @return static
266266
*/
267-
public function merge(array $attributeDefaults = [], bool $escape = true)
267+
public function withMerge(array $attributeDefaults = [], bool $escape = true): static
268+
{
269+
return (clone $this)->merge($attributeDefaults, $escape);
270+
}
271+
272+
public function merge(array $attributeDefaults = [], bool $escape = true): static
268273
{
269274
$attributeDefaults = array_map(
270275
function ($value) use ($escape) {
@@ -296,7 +301,9 @@ function ($value, $key) use ($attributeDefaults, $escape) {
296301
}
297302
)->merge($nonAppendableAttributes)->dump();
298303

299-
return new static(array_merge($attributeDefaults, $attributes));
304+
$this->attributes = array_merge($attributeDefaults, $attributes);
305+
306+
return $this;
300307
}
301308

302309
/**
@@ -377,7 +384,7 @@ public function setAttributes(array $attributes): void
377384

378385
unset($attributes['attributes']);
379386

380-
$attributes = $parentBag->merge($attributes, $escape = false)->getAttributes();
387+
$attributes = $parentBag->withMerge($attributes, $escape = false)->getAttributes();
381388
}
382389

383390
$this->attributes = $attributes;
@@ -402,7 +409,7 @@ public function toHtml(): string
402409
*/
403410
public function __invoke(array $attributeDefaults = []): string
404411
{
405-
return (string) $this->merge($attributeDefaults);
412+
return (string) $this->withMerge($attributeDefaults);
406413
}
407414

408415
/**

packages/edge/test/tmpl/components/foo-component.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
?>
1010
@props(['type' => 'info', 'message' => 'unknown message'])
1111

12-
<div id="foo" {!! $attributes->merge(['class' => 'alert alert-' . $type]) !!}>
12+
<div id="foo" {!! $attributes->withMerge(['class' => 'alert alert-' . $type]) !!}>
1313
Foo Component: {{ $type }} - Message: {{ $message }}
1414

1515
{!! $slot() !!}

0 commit comments

Comments
 (0)