Skip to content

Commit 6d8f258

Browse files
committed
update
1 parent fcc7ebd commit 6d8f258

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/Input.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Input implements Control
1515
protected string $type = 'text';
1616
protected string $placeholder = '';
1717

18-
/** @var array<string|int,string> $options */
18+
/** @var array<string> $options */
1919
protected array $options = [];
2020

2121
protected bool $disabled = false;
@@ -89,7 +89,7 @@ public function placeholder(string $placeholder): self
8989
}
9090

9191
/**
92-
* @param array<string|int,string> $options
92+
* @param array<string> $options
9393
*/
9494
public function options(array $options): self
9595
{
@@ -170,11 +170,8 @@ public function renderDom(DOMDocument $doc): DOMElement
170170
$input->setAttribute('list', $this->name . '-options');
171171
$datalist = $doc->createElement('datalist');
172172
$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', $value);
177-
}
173+
foreach ($this->options as $value) {
174+
$option = $doc->createElement('option', $value);
178175
$datalist->appendChild($option);
179176
}
180177
$wrapper = $doc->createElement('div');

src/Select.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Select implements Control
1010
use HtmlElement;
1111

1212
protected string $name = '';
13-
/** @var array<string|int,string> $options */
13+
/** @var array<string,string> $options */
1414
protected array $options = [];
1515
/** @var string[] $values */
1616
protected array $values = [];
@@ -81,7 +81,13 @@ public function autocomplete(string $value): self
8181
*/
8282
public function options(array $options): self
8383
{
84-
$this->options = $options;
84+
$this->options = [];
85+
foreach ($options as $key => $value) {
86+
if (is_int($key)) {
87+
$key = $value;
88+
}
89+
$this->options[$key] = $value ?: '...';
90+
}
8591
if (!isset($this->options[''])) {
8692
$this->options = ['' => '...'] + $this->options; // Add empty option if not present
8793
}
@@ -158,7 +164,7 @@ public function renderDom(\DOMDocument $doc): \DOMElement
158164
}
159165
foreach ($this->options as $key => $value) {
160166
$option = $doc->createElement('option', $value);
161-
$option->setAttribute('value', strval($key));
167+
$option->setAttribute('value', $key);
162168
if (in_array($key, $this->values)) {
163169
$option->setAttribute('selected', 'selected');
164170
}

src/SelectOrType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ public function renderDom(\DOMDocument $doc): \DOMElement
162162
$caption = $value ?: '...';
163163
$isLast = $i == count($this->options) - 1;
164164
$option = $doc->createElement('option', $caption);
165-
if (!$isLast) {
165+
if ($isLast) {
166+
$option->setAttribute('value', '');
167+
} else {
166168
$option->setAttribute('value', $value);
167169
if ($value == $this->value) {
168170
$option->setAttribute('selected', 'selected');

0 commit comments

Comments
 (0)