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
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
use App\Views\Components\Hint;
|
|
|
|
|
2021-09-15 15:58:21 +00:00
|
|
|
class Toggler extends FormComponent
|
2021-08-19 14:00:14 +00:00
|
|
|
{
|
2024-05-09 17:55:41 +00:00
|
|
|
protected array $props = ['size', 'hint', 'isChecked'];
|
|
|
|
|
|
|
|
protected array $casts = [
|
|
|
|
'isChecked' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2021-08-19 14:00:14 +00:00
|
|
|
/**
|
2021-09-15 15:58:21 +00:00
|
|
|
* @var 'base'|'small
|
2021-08-19 14:00:14 +00:00
|
|
|
*/
|
2021-09-15 15:58:21 +00:00
|
|
|
protected string $size = 'base';
|
2021-08-19 14:00:14 +00:00
|
|
|
|
|
|
|
protected string $hint = '';
|
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
protected bool $isChecked = false;
|
2021-08-27 10:58:22 +00:00
|
|
|
|
2021-08-19 14:00:14 +00:00
|
|
|
public function render(): string
|
|
|
|
{
|
2024-05-09 17:55:41 +00:00
|
|
|
$sizeClass = match ($this->size) {
|
|
|
|
'small' => 'form-switch-slider form-switch-slider--small',
|
|
|
|
default => 'form-switch-slider',
|
|
|
|
};
|
2021-08-27 10:58:22 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
$this->mergeClass('relative justify-between inline-flex items-center gap-x-2');
|
2021-08-19 14:00:14 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
$checkbox = form_checkbox([
|
|
|
|
'class' => 'form-switch',
|
|
|
|
], 'yes', old($this->name) === 'yes' ? true : $this->isChecked);
|
2021-08-19 14:00:14 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
$hint = $this->hint === '' ? '' : (new Hint([
|
|
|
|
'class' => 'ml-1',
|
|
|
|
'slot' => $this->hint,
|
|
|
|
]))->render();
|
2021-08-27 10:58:22 +00:00
|
|
|
|
|
|
|
return <<<HTML
|
2024-05-09 17:55:41 +00:00
|
|
|
<label {$this->getStringifiedAttributes()}>
|
|
|
|
<span class="">{$this->slot}{$hint}</span>
|
2021-08-19 14:00:14 +00:00
|
|
|
{$checkbox}
|
2024-05-09 17:55:41 +00:00
|
|
|
<span class="{$sizeClass}"></span>
|
2021-08-19 14:00:14 +00:00
|
|
|
</label>
|
2021-08-27 10:58:22 +00:00
|
|
|
HTML;
|
2021-08-19 14:00:14 +00:00
|
|
|
}
|
|
|
|
}
|