From 8b85ae6128f6fc401c52885c8fc93ff5264ac584 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 27 Dec 2024 00:51:40 +0100 Subject: [PATCH] Add alpineJS Support for label field --- app/View/Form/Editor.php | 4 ++-- app/View/Form/Label.php | 38 ++++++++++++++++++++++++++---- app/View/Form/Lever.php | 4 ++-- app/View/Form/Select.php | 4 ++-- app/View/Form/Text.php | 4 ++-- tests/Unit/View/Form/LabelTest.php | 31 ++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 tests/Unit/View/Form/LabelTest.php diff --git a/app/View/Form/Editor.php b/app/View/Form/Editor.php index 8a608370..99c5b24f 100644 --- a/app/View/Form/Editor.php +++ b/app/View/Form/Editor.php @@ -27,7 +27,7 @@ class Editor extends Component return <<<'HTML'
@if ($label) - {{$label}} + @endif
@@ -41,7 +41,7 @@ class Editor extends Component @updated="$wire.{{$attributes->wire('model')->value}} = $event.detail" x-data="editor($wire.{{$attributes->wire('model')->value}})" id="{{$id}}" - {{$attributes}} + {{$attributes->except(['label', ':label'])}} >
@if($hint) diff --git a/app/View/Form/Label.php b/app/View/Form/Label.php index 33c98724..ea2a320f 100644 --- a/app/View/Form/Label.php +++ b/app/View/Form/Label.php @@ -4,6 +4,7 @@ namespace App\View\Form; use App\View\Traits\HasFormDimensions; use Illuminate\View\Component; +use Illuminate\View\ComponentAttributeBag; class Label extends Component { @@ -12,17 +13,46 @@ class Label extends Component 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' - {{ $slot }} - @if ($required) -  * - @endif + + HTML; } diff --git a/app/View/Form/Lever.php b/app/View/Form/Lever.php index 7c0a3ed0..f55c1832 100644 --- a/app/View/Form/Lever.php +++ b/app/View/Form/Lever.php @@ -29,10 +29,10 @@ class Lever extends Component return <<<'HTML'