Extract RendersAlpine trait

This commit is contained in:
philipp lang 2024-12-27 15:10:35 +01:00
parent 8b85ae6128
commit 67f45886ea
2 changed files with 38 additions and 29 deletions

View File

@ -3,13 +3,13 @@
namespace App\View\Form;
use App\View\Traits\HasFormDimensions;
use App\View\Traits\RendersAlpine;
use Illuminate\View\Component;
use Illuminate\View\ComponentAttributeBag;
class Label extends Component
{
use HasFormDimensions;
use RendersAlpine;
public function __construct(
public bool $required = false,
@ -17,33 +17,6 @@ class Label extends Component
) {
}
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()
{

View File

@ -0,0 +1,36 @@
<?php
namespace App\View\Traits;
use Illuminate\View\ComponentAttributeBag;
trait RendersAlpine
{
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';
}
}