Extract RendersAlpine trait

This commit is contained in:
philipp lang 2024-12-27 15:10:35 +01:00
parent 6b505e1c02
commit 544217baf5
2 changed files with 38 additions and 29 deletions

View File

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