1313
1414use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1515use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
16+ use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
1617use Symfony \UX \LiveComponent \Util \ModelBindingParser ;
1718use Symfony \UX \TwigComponent \ComponentStack ;
1819use Symfony \UX \TwigComponent \Event \PreMountEvent ;
20+ use Symfony \UX \TwigComponent \MountedComponent ;
1921
2022/**
2123 * Parses the "data-model" key, which triggers extra props to be passed in.
@@ -55,7 +57,7 @@ public function onPreMount(PreMountEvent $event): void
5557 $ data ['data-model ' ] = $ dataModel ;
5658
5759 // the parent is still listed as the "current" component at this point
58- $ parentMountedComponent = $ this ->componentStack -> getCurrentComponent ( );
60+ $ parentMountedComponent = $ this ->getCurrentLiveComponent ( $ this -> componentStack );
5961 if (null === $ parentMountedComponent ) {
6062 throw new \LogicException ('You can only pass "data-model" when rendering a component when you \'re rendering inside of a parent component. ' );
6163 }
@@ -76,4 +78,20 @@ public static function getSubscribedEvents(): array
7678 PreMountEvent::class => 'onPreMount ' ,
7779 ];
7880 }
81+
82+ private function getCurrentLiveComponent (ComponentStack $ componentStack ): ?MountedComponent
83+ {
84+ foreach ($ componentStack as $ mountedComponent ) {
85+ if ($ this ->isLiveComponent ($ mountedComponent ->getComponent ()::class)) {
86+ return $ mountedComponent ;
87+ }
88+ }
89+
90+ return null ;
91+ }
92+
93+ private function isLiveComponent (string $ classname ): bool
94+ {
95+ return [] !== (new \ReflectionClass ($ classname ))->getAttributes (AsLiveComponent::class);
96+ }
7997}
0 commit comments