adrema/app/View/Form/Label.php

60 lines
1.5 KiB
PHP

<?php
namespace App\View\Form;
use App\View\Traits\HasFormDimensions;
use Illuminate\View\Component;
use Illuminate\View\ComponentAttributeBag;
class Label extends Component
{
use HasFormDimensions;
public function __construct(
public bool $required = false,
public string $value = ''
) {
}
public function asAlpineString(ComponentAttributeBag $attributes, string $tagName): string
{
$rawTag = ':' . $tagName;
if ($attributes->has($rawTag)) {
return $attributes->get($rawTag);
}
$output = e($this->{$tagName});
return str($output)->swap([
'`' => '\\`',
'$' => '\\$',
'{' => '\\{',
'}' => '\\}',
])->wrap('`');
}
public function asAlpineBool(ComponentAttributeBag $attributes, string $tagName): string
{
$rawTag = ':' . $tagName;
if ($attributes->has($rawTag)) {
return $attributes->get($rawTag);
}
return $this->{$tagName} ? 'true' : 'false';
}
public function render()
{
return <<<'HTML'
<span class="font-semibold leading-none text-gray-400 group-[.size-default]:text-sm group-[.size-sm]:text-xs">
<span x-text="{!! $asAlpineString($attributes, 'value') !!}"></span>
<template x-if="{!! $asAlpineBool($attributes, 'required') !!}">
<span class="text-red-800">&nbsp;*</span>
</template>
</span>
HTML;
}
}