From 67f45886ea281ce25d72b142a03272cde6ba8ff6 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 27 Dec 2024 15:10:35 +0100 Subject: [PATCH] Extract RendersAlpine trait --- app/View/Form/Label.php | 31 ++------------------------ app/View/Traits/RendersAlpine.php | 36 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 app/View/Traits/RendersAlpine.php diff --git a/app/View/Form/Label.php b/app/View/Form/Label.php index ea2a320f..3a3c1bf9 100644 --- a/app/View/Form/Label.php +++ b/app/View/Form/Label.php @@ -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() { diff --git a/app/View/Traits/RendersAlpine.php b/app/View/Traits/RendersAlpine.php new file mode 100644 index 00000000..959c8b9b --- /dev/null +++ b/app/View/Traits/RendersAlpine.php @@ -0,0 +1,36 @@ +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'; + } +}