Skip to content

Commit 9f313bc

Browse files
committed
[Live] fixing bug where custom data-live-id would be ignored in re-render
This caused it to look like a child component with a custom data-live-id *always* needed to be re-rendered.
1 parent 2e17b4d commit 9f313bc

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

src/LiveComponent/src/EventListener/InterceptChildComponentRenderSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function preComponentCreated(PreCreateForRenderEvent $event): void
5959
$childFingerprints = $parentComponent->getExtraMetadata(self::CHILDREN_FINGERPRINTS_METADATA_KEY);
6060

6161
// get the deterministic id for this child, but without incrementing the counter yet
62-
$deterministicId = $this->deterministicTwigIdCalculator->calculateDeterministicId(increment: false);
62+
$deterministicId = $event->getProps()['data-live-id'] ?? $this->deterministicTwigIdCalculator->calculateDeterministicId(increment: false);
6363
if (!isset($childFingerprints[$deterministicId])) {
6464
// child fingerprint wasn't set, it is likely a new child, allow it to render fully
6565
return;

src/LiveComponent/tests/Fixtures/Component/TodoListComponent.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class TodoListComponent
2424
#[LiveProp]
2525
public array $items = [];
2626

27+
#[LiveProp(writable: true)]
2728
public $includeDataLiveId = false;
2829

2930
use DefaultActionTrait;

src/LiveComponent/tests/Fixtures/templates/render_todo_list_with_live_id.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
items: [
33
{ text: 'milk'},
44
{ text: 'cheese'},
5+
{ text: 'milk'},
56
],
67
includeDataLiveId: true
78
}) }}

src/LiveComponent/tests/Functional/EventListener/AddLiveAttributesSubscriberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ public function testItDoesNotOverrideDataLiveIdIfSpecified(): void
114114
$lis = $ul->children('li');
115115
// deterministic id: is not used: data-live-id was passed in manually
116116
$this->assertSame('todo-item-1', $lis->first()->attr('data-live-id'));
117-
$this->assertSame('todo-item-2', $lis->last()->attr('data-live-id'));
117+
$this->assertSame('todo-item-3', $lis->last()->attr('data-live-id'));
118118
}
119119
}

src/LiveComponent/tests/Functional/EventListener/InterceptChildComponentRenderSubscriberTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ public function testItRendersEmptyElementOnMatchingFingerprint(): void
5656
;
5757
}
5858

59+
public function testItRendersEmptyElementOnMatchingFingerprintWithCustomDataLiveId(): void
60+
{
61+
$fingerPrintsWithCustomLiveId = [];
62+
foreach (array_values(self::$actualTodoItemFingerprints) as $key => $fingerprintValue) {
63+
// creating fingerprints to match todo_list.html.twig
64+
$fingerPrintsWithCustomLiveId['todo-item-'.$key + 1] = $fingerprintValue;
65+
}
66+
67+
$this->browser()
68+
->visit($this->buildUrlForTodoListComponent($fingerPrintsWithCustomLiveId, true))
69+
->assertSuccessful()
70+
->assertHtml()
71+
// no lis (because we render a div always)
72+
->assertElementCount('ul li', 0)
73+
// because we actually slip in a div element
74+
->assertElementCount('ul div', 3)
75+
->assertNotContains('todo item')
76+
;
77+
}
78+
5979
public function testItRendersNewPropWhenFingerprintDoesNotMatch(): void
6080
{
6181
$fingerprints = self::$actualTodoItemFingerprints;
@@ -89,14 +109,15 @@ public function testItRendersNewPropWhenFingerprintDoesNotMatch(): void
89109
});
90110
}
91111

92-
private function buildUrlForTodoListComponent(array $childrenFingerprints): string
112+
private function buildUrlForTodoListComponent(array $childrenFingerprints, bool $includeLiveId = false): string
93113
{
94114
$component = $this->mountComponent('todo_list', [
95115
'items' => [
96116
['text' => 'wake up'],
97117
['text' => 'high five a friend'],
98118
['text' => 'take a nap'],
99119
],
120+
'includeDataLiveId' => $includeLiveId,
100121
]);
101122

102123
$dehydrated = $this->dehydrateComponent($component);

0 commit comments

Comments
 (0)