@@ -15,6 +15,9 @@ class Input implements Control
1515 protected string $ type = 'text ' ;
1616 protected string $ placeholder = '' ;
1717
18+ /** @var array<string|int,string> $options */
19+ protected array $ options = [];
20+
1821 protected bool $ disabled = false ;
1922 protected bool $ readonly = false ;
2023 protected bool $ required = false ;
@@ -85,6 +88,15 @@ public function placeholder(string $placeholder): self
8588 return $ this ;
8689 }
8790
91+ /**
92+ * @param array<string|int,string> $options
93+ */
94+ public function options (array $ options ): self
95+ {
96+ $ this ->options = $ options ;
97+ return $ this ;
98+ }
99+
88100 /**
89101 * @param array<string, string|string[]|null> $data
90102 */
@@ -154,6 +166,21 @@ public function renderDom(DOMDocument $doc): DOMElement
154166 if ($ this ->autocomplete ) {
155167 $ input ->setAttribute ('autocomplete ' , 'on ' );
156168 }
169+ if ($ this ->options ) {
170+ $ input ->setAttribute ('list ' , $ this ->name . '-options ' );
171+ $ datalist = $ doc ->createElement ('datalist ' );
172+ $ datalist ->setAttribute ('id ' , $ this ->name . '-options ' );
173+ foreach ($ this ->options as $ value => $ label ) {
174+ $ option = $ doc ->createElement ('option ' , $ label );
175+ if (is_int ($ value )) {
176+ $ option ->setAttribute ('value ' , $ label );
177+ } else {
178+ $ option ->setAttribute ('value ' , $ value );
179+ }
180+ $ datalist ->appendChild ($ option );
181+ }
182+ $ doc ->appendChild ($ datalist );
183+ }
157184 return $ input ;
158185 }
159186}
0 commit comments