Skip to content

Commit 9040a5f

Browse files
committed
use PostMount hook for HasAttributesTrait to add extra data as attributes
1 parent 9704a88 commit 9040a5f

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/TwigComponent/src/HasAttributesTrait.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\TwigComponent;
1313

1414
use Symfony\UX\LiveComponent\Attribute\LiveProp;
15+
use Symfony\UX\TwigComponent\Attribute\PostMount;
1516
use Symfony\UX\TwigComponent\Attribute\PreMount;
1617

1718
/**
@@ -25,13 +26,26 @@ trait HasAttributesTrait
2526
public ComponentAttributes $attributes;
2627

2728
#[PreMount]
28-
public function mapAttributes(array $data): array
29+
public function preMountAttributes(array $data): array
2930
{
30-
$data['attributes'] = new ComponentAttributes($data['attributes'] ?? []);
31+
$data['_attributes'] = $data['attributes'] ?? [];
32+
33+
unset($data['attributes']);
3134

3235
return $data;
3336
}
3437

38+
#[PostMount]
39+
public function mountAttributes(array $data): array
40+
{
41+
$attributes = array_merge($data['_attributes'], $data);
42+
unset($attributes['_attributes']);
43+
44+
$this->attributes = new ComponentAttributes($attributes);
45+
46+
return [];
47+
}
48+
3549
public static function dehydrateAttributes(ComponentAttributes $attributes): array
3650
{
3751
return $attributes->all();

0 commit comments

Comments
 (0)