*/
class Select extends HtmlElement
{
use HasAutofocusAttribute;
use HasDisabledAttribute;
use HasNameAttribute;
use HasReadonlyAttribute;
use HasRequiredAttribute;
protected string $tag = 'select';
protected array $options = [];
protected mixed $value = '';
/**
* Set the select input as multiple.
*
* @return $this
*/
public function multiple(): static
{
$elt = with(clone $this)->attribute('multiple');
$name = $elt->getAttribute('name');
return $elt->if(
$name && ! \Illuminate\Support\Str::endsWith($name->value(), '[]'),
fn(self $elt) => $elt->name($name->value() . '[]')
)->applyValueToOptions();
}
/**
* Add options.
*
* @return $this
*/
public function options(iterable $options, array $attributes = [], array $groupAttributes = []): static
{
return $this->children(
$options,
fn($text, $value) => is_array($text) || $text instanceof \Illuminate\Support\Collection
? $this->makeOptionsGroup($value, $text, $attributes, $groupAttributes[$value] ?? [])
: $this->makeOption($value, $text, $attributes[$value] ?? [])
);
}
/**
* Add a placeholder option.
*
* @return $this
*/
public function placeholder(string $text, mixed $value = null, bool $disabled = false): static
{
return $this->prependChild(
$this->makeOption($value, $text)
->selectedUnless($this->hasSelection())
->disabled($disabled)
);
}
/**
* Set the value.
*
* @return $this
*/
public function value(mixed $value = null): static
{
return tap(clone $this, function (self $element) use ($value): void {
$element->value = $value;
})->applyValueToOptions();
}
/**
* Apply the selected value to the options.
*/
protected static function applyValueToElements(
\Illuminate\Support\Collection $value,
\Illuminate\Support\Collection $children,
): \Illuminate\Support\Collection {
return $children->map(function (HtmlElement $child) use ($value) {
if ($child instanceof Optgroup) {
return $child->setNewChildren(static::applyValueToElements($value, $child->getChildren()));
}
if ($child instanceof Selectable) {
return $child->selectedIf(
$value->contains($child->getAttribute('value')->value())
);
}
return $child;
});
}
/**
* Check if has a selected option.
*/
protected function hasSelection(): bool
{
return $this->getChildren()->contains(
fn(HtmlElement $child) => $child->hasAttribute('selected')
);
}
/**
* Make an option tag.
*/
protected function makeOption(mixed $value, ?string $text = null, array $attributes = []): Option
{
return Option::make()
->value($value)
->text($text ?: $value, false)
->selectedIf($value === $this->value)
->attributes($attributes);
}
/**
* Make an options group.
*/
protected function makeOptionsGroup(
string $label,
array $options,
array $attributes = [],
array $groupAttributes = [],
): Optgroup {
return Optgroup::make()
->label($label)
->attributes($groupAttributes)
->children(
$options,
fn($optionText, $optionValue) => $this->makeOption($optionValue, $optionText, $attributes[$optionValue] ?? [])
);
}
/**
* Apply the selected value to the options.
*
* @return $this
*/
protected function applyValueToOptions(): static
{
$value = \Illuminate\Support\Collection::make($this->value);
if ( ! $this->hasAttribute('multiple')) {
$value = $value->take(1);
}
return $this->setNewChildren(
static::applyValueToElements($value, $this->getChildren())
);
}
}
__halt_compiler();----SIGNATURE:----wPa/FojWZkKq7T4Ey4Pey5JS5yvkyuQIwqp0IngSme2Ze4C6icyOAx9QIGJrdSKnMQm54+ANWV7iv+eBCz4AnxLezjhPAF98Q/Ug8+mIBMOmH5MBstmqU3Dqv9ANAly+3PxwqCIPcQJagYHRow9MwV3+hKxLu6jk7e8jbLjUw9QnZkOUOoZwwC8tEFwnjIBcqgpNyZpfpvAjUPkbF7u6p7QjiGz3CaE73JnnTifr7bECzCk3EcboL6TUk1klGXAVGKS1pjauGQckQcXghxnKGghRnWYRzNmW+5B3U9sIvpfPvEPfM8zg+FOJPe2lA+L8OasG+GIHZ8FUzCxEiFBIwlSm9ZdqpiY+JTAChskVzRwYsgAWato3M3bWZsGdbXc0RWM0ctmmkQY8F391UTLXcTJYje+fE6Wv1Kj84sB7Huk6ET4qV+K6EN+tD87Lwt+jxRSB/uC+i8G3VSrCAswU3tlpmOqws4/PtWdTugweK0DYjhUpIxEkxQT61O7gqhtVgmc0x4f9okjBAIrvE0QPjjzv90HWPjNfHFISaAbZbubc/4p5sDVhCk2vr2vWDN9n9nuACmTxt5C357L1Ueo3xNCK+sRJF/sCVCPnSinmReLbqsm8gMTiQ+xKaw6lty/lMKD0sEkndMPz4tUK79J5xmAG1RP3iQbecSBFq7l02ak=----ATTACHMENT:----OTA0NDUyODkyMzcwNTA3MiA3NzE2MzU2NzgyNDAxNjk4IDQ5MDgwMTA0MDA3MTI1NzE=