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 Toggler extends FormComponent
|
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 $label = '';
|
|
|
|
|
|
|
|
protected string $hint = '';
|
|
|
|
|
|
|
|
protected bool $checked = false;
|
|
|
|
|
2021-08-27 10:58:22 +00:00
|
|
|
public function setChecked(string $value): void
|
|
|
|
{
|
2021-09-15 15:58:21 +00:00
|
|
|
$this->checked = $value === 'true';
|
2021-08-27 10:58:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:00:14 +00:00
|
|
|
public function render(): string
|
|
|
|
{
|
2021-08-27 10:58:22 +00:00
|
|
|
unset($this->attributes['checked']);
|
|
|
|
|
2021-09-15 15:58:21 +00:00
|
|
|
$wrapperClass = $this->class;
|
2021-08-19 14:00:14 +00:00
|
|
|
unset($this->attributes['class']);
|
|
|
|
|
2021-09-15 15:58:21 +00:00
|
|
|
$sizeClass = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'base' => 'form-switch-slider',
|
2021-09-15 15:58:21 +00:00
|
|
|
'small' => 'form-switch-slider form-switch-slider--small',
|
|
|
|
];
|
2021-08-19 14:00:14 +00:00
|
|
|
|
2021-09-15 15:58:21 +00:00
|
|
|
$this->attributes['class'] = 'form-switch';
|
2021-08-27 10:58:22 +00:00
|
|
|
|
2021-11-01 17:12:03 +00:00
|
|
|
$checkbox = form_checkbox($this->attributes, $this->value, old($this->name) === 'yes' ? true : $this->checked);
|
2021-08-27 10:58:22 +00:00
|
|
|
$hint = $this->hint === '' ? '' : hint_tooltip($this->hint, 'ml-1');
|
|
|
|
return <<<HTML
|
2021-08-19 14:00:14 +00:00
|
|
|
<label class="relative inline-flex items-center {$wrapperClass}">
|
|
|
|
{$checkbox}
|
2021-09-15 15:58:21 +00:00
|
|
|
<span class="{$sizeClass[$this->size]}"></span>
|
2021-08-27 10:58:22 +00:00
|
|
|
<span class="ml-2">{$this->slot}{$hint}</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
|
|
|
}
|
|
|
|
}
|