2021-09-10 16:02:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Views\Components\Forms;
|
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
use App\Views\Components\Hint;
|
2024-05-29 10:24:13 +00:00
|
|
|
use Override;
|
2024-05-09 17:55:41 +00:00
|
|
|
|
2021-09-10 16:02:25 +00:00
|
|
|
class RadioButton extends FormComponent
|
|
|
|
{
|
2024-05-09 17:55:41 +00:00
|
|
|
protected array $props = ['isChecked', 'hint'];
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
protected array $casts = [
|
2024-05-12 18:38:33 +00:00
|
|
|
'isSelected' => 'boolean',
|
2024-05-09 17:55:41 +00:00
|
|
|
];
|
2021-12-24 17:55:56 +00:00
|
|
|
|
2024-05-12 18:38:33 +00:00
|
|
|
protected bool $isSelected = false;
|
2024-05-09 17:55:41 +00:00
|
|
|
|
|
|
|
protected string $hint = '';
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2024-05-29 10:24:13 +00:00
|
|
|
#[Override]
|
2021-09-10 16:02:25 +00:00
|
|
|
public function render(): string
|
|
|
|
{
|
2022-01-02 14:11:05 +00:00
|
|
|
$data = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'id' => $this->value,
|
|
|
|
'name' => $this->name,
|
2022-01-02 14:11:05 +00:00
|
|
|
'class' => 'form-radio-btn bg-elevated',
|
|
|
|
];
|
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
if ($this->isRequired) {
|
2022-01-02 14:11:05 +00:00
|
|
|
$data['required'] = 'required';
|
|
|
|
}
|
|
|
|
|
2021-09-10 16:02:25 +00:00
|
|
|
$radioInput = form_radio(
|
2022-01-02 14:11:05 +00:00
|
|
|
$data,
|
2021-09-10 16:02:25 +00:00
|
|
|
$this->value,
|
2024-05-12 18:38:33 +00:00
|
|
|
old($this->name) ? old($this->name) === $this->value : $this->isSelected,
|
2021-09-10 16:02:25 +00:00
|
|
|
);
|
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
$hint = $this->hint === '' ? '' : (new Hint([
|
|
|
|
'class' => 'ml-1 text-base',
|
|
|
|
'slot' => $this->hint,
|
|
|
|
]))->render();
|
2021-12-24 17:55:56 +00:00
|
|
|
|
2021-09-10 16:02:25 +00:00
|
|
|
return <<<HTML
|
2024-05-09 17:55:41 +00:00
|
|
|
<div {$this->getStringifiedAttributes()}">
|
2021-09-10 16:02:25 +00:00
|
|
|
{$radioInput}
|
2021-12-24 17:55:56 +00:00
|
|
|
<label for="{$this->value}">{$this->slot}{$hint}</label>
|
2021-09-10 16:02:25 +00:00
|
|
|
</div>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
}
|