22
33namespace AppKit \UI \Components ;
44
5+ use AppKit \UI \Attributes \Element ;
6+ use AppKit \UI \Attributes \ExposedAsState ;
57use AppKit \UI \Attributes \Inheritable ;
68use AppKit \UI \Attributes \Slotable ;
79use AppKit \UI \ComponentBuilder ;
810use AppKit \UI \Components \Concerns \HasComponentBuilder ;
11+ use AppKit \UI \Components \Concerns \InteractsWithComponentStack ;
912use AppKit \UI \ElementAttributeBag ;
1013use AppKit \UI \Facades \UI ;
1114use AppKit \UI \Support \Attributes ;
1215use Attribute ;
16+ use Closure ;
1317use Illuminate \Support \Collection ;
18+ use Illuminate \Support \Str ;
1419use Illuminate \View \Component as BladeComponent ;
1520use Illuminate \View \ComponentAttributeBag ;
1621use ReflectionClass ;
2126abstract class BaseComponent extends BladeComponent
2227{
2328 use HasComponentBuilder;
29+ use InteractsWithComponentStack;
2430
2531 /**
2632 * The name of the view that this component renders
@@ -45,8 +51,44 @@ abstract class BaseComponent extends BladeComponent
4551 */
4652 public function withAttributes (array $ attributes )
4753 {
54+ // flag to the service provider that we are starting this component
4855 UI ::startComponent ($ this );
4956
57+ // if we don't have a view name passed in, we try to generate one based on a convention
58+ if (empty ($ this ->viewName )) {
59+ $ this ->viewName = Str::of (static ::class)
60+ ->remove ('AppKit\UI\Components \\' )
61+ ->replace ('\\' , '. ' )
62+ ->kebab ()
63+ ->replace ('.- ' , '. ' )
64+ ->prepend ('components. ' );
65+ }
66+
67+ // find parameters that are being exposed as a state
68+ Attributes::find (ExposedAsState::class)
69+ ->onConstructorParameters ()
70+ ->ofClass (static ::class)
71+ ->get ()
72+ ->keys ()
73+ ->each (fn ($ parameter ) => $ this ->exposePropertyAsState ($ parameter ));
74+
75+ Attributes::find (ExposedAsState::class)
76+ ->onMethods ()
77+ ->ofClass (static ::class)
78+ ->get ()
79+ ->keys ()
80+ ->each (fn ($ method ) => $ this ->defineState ($ method , Closure::fromCallable ([$ this , $ method ])));
81+
82+ // find the element attribute bags that we need to register
83+ Attributes::find (Element::class)
84+ ->onProperties ()
85+ ->ofClass (static ::class)
86+ ->get ()
87+ ->each (function ($ attribute , $ property ) {
88+ $ this ->{$ property } = $ this ->registerElement ($ attribute ->elementName );
89+ });
90+
91+ // create a collection of child components
5092 $ this ->childComponents = new Collection ();
5193
5294 // ensure that we have an attribute bag assigned to the component
@@ -247,6 +289,7 @@ public function render()
247289 // if we have inheritable attributes on the component (attributes passed in from another attribute bag)
248290 if (isset ($ data ['inheritedAttributes ' ])) {
249291 // we merge them in right at the end
292+ dd ($ this ->inheritedAttributes );
250293 $ data ['attributes ' ] = $ data ['attributes ' ]->merge ($ this ->inheritedAttributes );
251294 }
252295
0 commit comments