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-15 15:58:21 +00:00
|
|
|
class MultiSelect extends FormComponent
|
2021-08-19 14:00:14 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected array $options = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected array $selected = [];
|
|
|
|
|
2021-08-27 10:58:22 +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-08-27 10:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setSelected(string $selected): void
|
|
|
|
{
|
2022-01-05 14:58:53 +00:00
|
|
|
$this->selected = json_decode(htmlspecialchars_decode($selected), true);
|
2021-08-27 10:58:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:00:14 +00:00
|
|
|
public function render(): string
|
|
|
|
{
|
|
|
|
$defaultAttributes = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'data-class' => $this->attributes['class'],
|
|
|
|
'multiple' => 'multiple',
|
|
|
|
'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-10-12 15:52:20 +00:00
|
|
|
$this->attributes['class'] .= ' w-full bg-elevated border-3 border-contrast rounded-lg';
|
2021-08-19 14:00:14 +00:00
|
|
|
$extra = array_merge($defaultAttributes, $this->attributes);
|
|
|
|
|
2021-09-15 15:58:21 +00:00
|
|
|
return form_dropdown($this->name, $this->options, $this->selected, $extra);
|
2021-08-19 14:00:14 +00:00
|
|
|
}
|
|
|
|
}
|