22
33namespace Utilitte \Nette \UI \Component ;
44
5+ use InvalidArgumentException ;
56use Nette \Application \UI \Component ;
67use Nette \Application \UI \Control ;
78use Nette \Application \UI \IRenderable ;
9+ use Nette \Application \UI \Multiplier ;
810use Nette \Bridges \ApplicationLatte \Template ;
911use Nette \Utils \Html ;
1012use function assert ;
@@ -28,17 +30,30 @@ final class GridComponent extends Control
2830
2931 private int $ columnNumber = 1 ;
3032
33+ /** @var mixed[] */
34+ private array $ prepends = [];
35+
36+ /** @var Component[] */
37+ private array $ prependsById = [];
38+
3139 public function __construct (Component $ component )
3240 {
3341 $ this ->component = $ component ;
3442 }
3543
44+ public static function create (Component $ component ): self
45+ {
46+ return new static ($ component );
47+ }
48+
3649 /**
3750 * @phpstan-param Html<int, Html|string>|null $row
3851 */
39- public function setRow (?Html $ row ): void
52+ public function setRow (?Html $ row ): self
4053 {
4154 $ this ->row = $ row ;
55+
56+ return $ this ;
4257 }
4358
4459 /**
@@ -65,6 +80,14 @@ public function setIncrementSequence(string $prefix): self
6580 return $ this ;
6681 }
6782
83+ public function addComponentAtPosition (string $ id , int $ position , Component $ control ): self
84+ {
85+ $ this ->prependsById [$ id ] = $ control ;
86+ $ this ->prepends [$ position ][] = $ id ;
87+
88+ return $ this ;
89+ }
90+
6891 public function render (): void
6992 {
7093 $ component = $ this ['grid ' ];
@@ -86,6 +109,7 @@ public function render(): void
86109 }
87110
88111 $ col = $ this ->column ;
112+
89113 if ($ this ->incrementSequencePrefix ) {
90114 $ col = clone $ this ->column ;
91115 $ col ->appendAttribute ('class ' , $ this ->incrementSequencePrefix . $ counter );
@@ -117,6 +141,7 @@ public function render(): void
117141 return $ this ->row ->endTag ();
118142 });
119143
144+ $ controls = $ this ->mergePrepends ($ controls );
120145
121146 $ template ->render (__DIR__ . '/templates/grid.latte ' , [
122147 'controls ' => $ controls ,
@@ -130,4 +155,44 @@ protected function createComponentGrid(): Component
130155 return $ this ->component ;
131156 }
132157
158+ protected function createComponentPrepend (): Component
159+ {
160+ return new Multiplier (function (string $ index ) {
161+ if (!isset ($ this ->prependsById [$ index ])) {
162+ throw new InvalidArgumentException (sprintf ('Prepended component %d not exists ' , $ index ));
163+ }
164+
165+ return $ this ->prependsById [$ index ];
166+ });
167+ }
168+
169+ /**
170+ * @param Component[] $controls
171+ * @return Component[]
172+ */
173+ private function mergePrepends (array $ controls ): array
174+ {
175+ if (!$ this ->prepends ) {
176+ return $ controls ;
177+ }
178+
179+ $ return = [];
180+
181+ $ position = 1 ;
182+
183+ foreach ($ controls as $ control ) {
184+ if (isset ($ this ->prepends [$ position ])) {
185+ foreach ($ this ->prepends [$ position ] as $ id ) {
186+ $ return [] = $ this ->getComponent (sprintf ('prepend-%s ' , $ id ));
187+ }
188+ }
189+
190+ $ return [] = $ control ;
191+
192+ $ position ++;
193+ }
194+
195+ return $ return ;
196+ }
197+
133198}
0 commit comments