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
|
|
|
|
|
|
|
use ViewComponents\Component;
|
|
|
|
|
|
|
|
class Label extends Component
|
|
|
|
{
|
2021-09-10 16:02:25 +00:00
|
|
|
protected ?string $for = null;
|
2021-08-19 14:00:14 +00:00
|
|
|
|
2021-09-10 16:02:25 +00:00
|
|
|
protected ?string $hint = null;
|
2021-08-19 14:00:14 +00:00
|
|
|
|
|
|
|
protected bool $isOptional = false;
|
|
|
|
|
2021-08-27 10:58:22 +00:00
|
|
|
public function setIsOptional(string $value): void
|
|
|
|
{
|
|
|
|
$this->isOptional = $value === 'true';
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:00:14 +00:00
|
|
|
public function render(): string
|
|
|
|
{
|
2023-10-12 15:52:20 +00:00
|
|
|
$labelClass = 'text-sm font-semibold ' . $this->attributes['class'];
|
2021-08-19 14:00:14 +00:00
|
|
|
unset($this->attributes['class']);
|
|
|
|
|
2023-10-12 15:52:20 +00:00
|
|
|
$optionalText = $this->isOptional ? '<small class="ml-1 font-normal lowercase">(' .
|
2021-08-19 14:00:14 +00:00
|
|
|
lang('Common.optional') .
|
|
|
|
')</small>' : '';
|
2021-09-10 16:02:25 +00:00
|
|
|
$hint = $this->hint === null ? '' : hint_tooltip($this->hint, 'ml-1');
|
2021-08-19 14:00:14 +00:00
|
|
|
|
2021-09-15 15:58:21 +00:00
|
|
|
unset($this->attributes['isOptional']);
|
|
|
|
unset($this->attributes['hint']);
|
|
|
|
unset($this->attributes['slot']);
|
|
|
|
|
|
|
|
$attributes = stringify_attributes($this->attributes);
|
|
|
|
|
2021-08-27 10:58:22 +00:00
|
|
|
return <<<HTML
|
|
|
|
<label class="{$labelClass}" {$attributes}>{$this->slot}{$optionalText}{$hint}</label>
|
|
|
|
HTML;
|
2021-08-19 14:00:14 +00:00
|
|
|
}
|
|
|
|
}
|