2021-08-19 14:00:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-09-02 16:34:25 +00:00
|
|
|
namespace App\Views\Components\Forms;
|
2021-08-19 14:00:14 +00:00
|
|
|
|
2021-09-10 16:02:25 +00:00
|
|
|
class Select extends FormComponent
|
2021-08-19 14:00:14 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected array $options = [];
|
|
|
|
|
2021-09-15 15:58:21 +00:00
|
|
|
protected string $selected = '';
|
2021-09-10 16:02:25 +00:00
|
|
|
|
|
|
|
public function setOptions(string $value): void
|
|
|
|
{
|
2022-01-05 14:58:53 +00:00
|
|
|
$this->options = json_decode(htmlspecialchars_decode($value), true);
|
2021-09-10 16:02:25 +00:00
|
|
|
}
|
2021-08-19 14:00:14 +00:00
|
|
|
|
|
|
|
public function render(): string
|
|
|
|
{
|
|
|
|
$defaultAttributes = [
|
2023-10-12 15:52:20 +00:00
|
|
|
'class' => 'w-full focus:border-contrast focus:ring-accent border-3 rounded-lg bg-elevated border-contrast ' . $this->class,
|
2023-06-12 14:47:38 +00:00
|
|
|
'data-class' => $this->class,
|
|
|
|
'data-select-text' => lang('Common.forms.multiSelect.selectText'),
|
|
|
|
'data-loading-text' => lang('Common.forms.multiSelect.loadingText'),
|
2021-09-22 14:50:54 +00:00
|
|
|
'data-no-results-text' => lang('Common.forms.multiSelect.noResultsText'),
|
|
|
|
'data-no-choices-text' => lang('Common.forms.multiSelect.noChoicesText'),
|
2023-06-12 14:47:38 +00:00
|
|
|
'data-max-item-text' => lang('Common.forms.multiSelect.maxItemText'),
|
2021-08-19 14:00:14 +00:00
|
|
|
];
|
2023-06-21 16:17:11 +00:00
|
|
|
unset($this->attributes['name']);
|
|
|
|
unset($this->attributes['options']);
|
|
|
|
unset($this->attributes['selected']);
|
2023-11-15 16:17:43 +00:00
|
|
|
$extra = [...$this->attributes, ...$defaultAttributes];
|
2021-08-19 14:00:14 +00:00
|
|
|
|
2021-09-20 15:45:38 +00:00
|
|
|
return form_dropdown($this->name, $this->options, old($this->name, $this->selected !== '' ? [$this->selected] : []), $extra);
|
2021-08-19 14:00:14 +00:00
|
|
|
}
|
|
|
|
}
|