From 55a576e5b28653d5b1fa0eec36c0e530486d48c9 Mon Sep 17 00:00:00 2001 From: LT Date: Thu, 21 Sep 2023 11:22:53 +0300 Subject: [PATCH] fix: preview with links/badges --- .../views/components/fields-group.blade.php | 6 +--- resources/views/fields/code.blade.php | 2 +- resources/views/fields/json.blade.php | 2 +- resources/views/fields/range.blade.php | 2 +- resources/views/fields/stack.blade.php | 2 +- src/Decorations/Button.php | 2 +- src/Fields/Checkbox.php | 5 ++-- src/Fields/Enum.php | 30 ++++++++++++++----- src/Fields/Field.php | 2 +- src/Fields/Json.php | 8 +++-- src/Fields/NoInput.php | 5 ++-- src/Fields/Number.php | 5 ++-- src/Fields/StackFields.php | 5 ++-- src/Fields/Url.php | 6 ++-- src/helpers.php | 7 +++++ tests/Unit/Fields/EnumFieldTest.php | 5 ++-- 16 files changed, 60 insertions(+), 34 deletions(-) diff --git a/resources/views/components/fields-group.blade.php b/resources/views/components/fields-group.blade.php index e16bd6546..7665543e4 100644 --- a/resources/views/components/fields-group.blade.php +++ b/resources/views/components/fields-group.blade.php @@ -1,9 +1,5 @@ -@php - use MoonShine\Fields\Field; -@endphp - @foreach($components as $fieldOrDecoration) - @if($fieldOrDecoration instanceof Field && $fieldOrDecoration->hasWrapper()) + @if(is_field($fieldOrDecoration) && $fieldOrDecoration->hasWrapper()) {!! $fieldOrDecoration->getBeforeRender() !!} {{ $fieldOrDecoration->render() }} diff --git a/resources/views/fields/code.blade.php b/resources/views/fields/code.blade.php index 3d8a96422..d8fe6b425 100644 --- a/resources/views/fields/code.blade.php +++ b/resources/views/fields/code.blade.php @@ -5,7 +5,7 @@ readonly: {{ $element->isReadonly() ? 'true' : 'false' }}, })" {{ $element->attributes() - ->except('x-on:change') + ->only('class') ->merge(['class' => 'w-100 min-h-[300px] relative']) }} > diff --git a/resources/views/fields/json.blade.php b/resources/views/fields/json.blade.php index ca9ca45b1..f316c43ce 100644 --- a/resources/views/fields/json.blade.php +++ b/resources/views/fields/json.blade.php @@ -1,4 +1,4 @@ -
attributes()->except('x-on:change') }}> +
attributes()->only('class') }}> {{ $element->value(withOld: false) ->editable() ->when( diff --git a/resources/views/fields/range.blade.php b/resources/views/fields/range.blade.php index 02a7443eb..67b65318d 100644 --- a/resources/views/fields/range.blade.php +++ b/resources/views/fields/range.blade.php @@ -3,7 +3,7 @@ range_to_{{ $element->id() }}: '{{ $element->value()[$element->toField] ?? '' }}' }" {{ $element->attributes() - ->except('x-on:change') + ->only('class') ->merge(['class' => 'form-group form-group-inline']) }} > @endif
attributes() - ->except('x-on:change') + ->only('class') ->merge(['class' => 'my-2']) }} > {!! $field->preview() !!} diff --git a/src/Decorations/Button.php b/src/Decorations/Button.php index fd5a702c7..f4fa653eb 100644 --- a/src/Decorations/Button.php +++ b/src/Decorations/Button.php @@ -25,6 +25,6 @@ public function __construct( ) { parent::__construct($label); - $this->addLink($this->label(), $link, $blank); + $this->link($link, $this->label(), blank: $blank); } } diff --git a/src/Fields/Checkbox.php b/src/Fields/Checkbox.php index 8bc5ceba5..a82ac60fc 100644 --- a/src/Fields/Checkbox.php +++ b/src/Fields/Checkbox.php @@ -4,6 +4,7 @@ namespace MoonShine\Fields; +use Illuminate\View\View; use MoonShine\Contracts\Fields\DefaultValueTypes\DefaultCanBeBool; use MoonShine\Contracts\Fields\DefaultValueTypes\DefaultCanBeNumeric; use MoonShine\Contracts\Fields\DefaultValueTypes\DefaultCanBeString; @@ -39,7 +40,7 @@ protected function resolveValue(): mixed return parent::resolveValue(); } - protected function resolvePreview(): string + protected function resolvePreview(): View|string { if ($this->isRawMode()) { return (string) ($this->toValue(false) @@ -49,6 +50,6 @@ protected function resolvePreview(): string return view('moonshine::ui.boolean', [ 'value' => (bool) $this->toValue(false), - ])->render(); + ]); } } diff --git a/src/Fields/Enum.php b/src/Fields/Enum.php index d6085fbfe..5b504e7e9 100644 --- a/src/Fields/Enum.php +++ b/src/Fields/Enum.php @@ -5,18 +5,26 @@ namespace MoonShine\Fields; use MoonShine\Contracts\Fields\DefaultValueTypes\DefaultCanBeEnum; +use UnitEnum; class Enum extends Select implements DefaultCanBeEnum { + /** + * @param class-string $class + * @return $this + */ public function attach(string $class): static { - /* @var UnitEnum $class ; */ + $values = collect($class::cases()); + $this->options( - array_column( - $class::cases(), - 'name', - 'value' - ) + $values->mapWithKeys(function($value) { + return [ + $value->name => method_exists($value, 'toString') + ? $value->toString() + : $value->value + ]; + })->toArray() ); return $this; @@ -26,10 +34,18 @@ protected function resolvePreview(): string { $value = $this->toFormattedValue(); + if(is_scalar($value)) { + return $value; + } + if(method_exists($value, 'getColor')) { $this->badge($value->getColor()); } - return data_get($this->values(), $value?->value, ''); + if(method_exists($value, 'toString')) { + return $value->toString(); + } + + return $value?->value ?? ''; } } diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 65b8d8921..b7dcf6222 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -274,7 +274,7 @@ protected function resolvePreview(): View|string private function previewDecoration(View|string $value): View|string { if ($value instanceof View) { - return $value; + return $value->render(); } if ($this->hasLink()) { diff --git a/src/Fields/Json.php b/src/Fields/Json.php index 23d0a2a74..048f0595b 100644 --- a/src/Fields/Json.php +++ b/src/Fields/Json.php @@ -6,6 +6,7 @@ use Closure; use Illuminate\Contracts\Database\Eloquent\Builder; +use Illuminate\Contracts\View\View; use Illuminate\Support\Collection; use Illuminate\Support\Stringable; use MoonShine\Components\TableBuilder; @@ -207,15 +208,16 @@ protected function preparedFields(): Fields }); } - protected function resolvePreview(): string + protected function resolvePreview(): View|string { if ($this->isRawMode()) { return (string) $this->toFormattedValue(); } - return (string) $this->resolveValue() + return $this->resolveValue() ->simple() - ->preview(); + ->preview() + ->render(); } protected function reformatFilledValue(mixed $data): mixed diff --git a/src/Fields/NoInput.php b/src/Fields/NoInput.php index 5a8b51648..3cff368ee 100644 --- a/src/Fields/NoInput.php +++ b/src/Fields/NoInput.php @@ -5,6 +5,7 @@ namespace MoonShine\Fields; use Closure; +use Illuminate\Contracts\View\View; use MoonShine\Support\Condition; class NoInput extends Field @@ -29,7 +30,7 @@ public function boolean( return $this; } - protected function resolvePreview(): string + protected function resolvePreview(): View|string { $value = $this->toFormattedValue(); @@ -40,7 +41,7 @@ protected function resolvePreview(): string if ($this->isBoolean) { return view('moonshine::ui.boolean', [ 'value' => $value, - ])->render(); + ]); } return (string) $value; diff --git a/src/Fields/Number.php b/src/Fields/Number.php index 51c4f5627..8730baccc 100644 --- a/src/Fields/Number.php +++ b/src/Fields/Number.php @@ -4,6 +4,7 @@ namespace MoonShine\Fields; +use Illuminate\Contracts\View\View; use MoonShine\Contracts\Fields\DefaultValueTypes\DefaultCanBeNumeric; use MoonShine\Traits\Fields\NumberTrait; @@ -37,12 +38,12 @@ public function withStars(): bool return $this->stars; } - protected function resolvePreview(): string + protected function resolvePreview(): View|string { if (! $this->isRawMode() && $this->withStars()) { return view('moonshine::ui.rating', [ 'value' => parent::resolvePreview(), - ])->render(); + ]); } return parent::resolvePreview(); diff --git a/src/Fields/StackFields.php b/src/Fields/StackFields.php index 11b6b7ece..0daaa6a6b 100644 --- a/src/Fields/StackFields.php +++ b/src/Fields/StackFields.php @@ -5,6 +5,7 @@ namespace MoonShine\Fields; use Closure; +use Illuminate\Contracts\View\View; use MoonShine\Contracts\Fields\HasFields; use MoonShine\Traits\WithFields; use Throwable; @@ -65,12 +66,12 @@ static function (mixed $item) use ($field): mixed { }; } - protected function resolvePreview(): string + protected function resolvePreview(): View|string { return view($this->getView(), [ 'element' => $this, 'indexView' => true, - ])->render(); + ]); } /** diff --git a/src/Fields/Url.php b/src/Fields/Url.php index e72ddb5d0..3804cb990 100644 --- a/src/Fields/Url.php +++ b/src/Fields/Url.php @@ -4,11 +4,13 @@ namespace MoonShine\Fields; +use Illuminate\Contracts\View\View; + class Url extends Text { protected string $type = 'url'; - protected function resolvePreview(): string + protected function resolvePreview(): View|string { $value = parent::resolvePreview(); @@ -24,6 +26,6 @@ protected function resolvePreview(): string 'href' => $value, 'value' => $value, 'blank' => $this->isLinkBlank(), - ])->render(); + ]); } } diff --git a/src/helpers.php b/src/helpers.php index 93f2df164..d66a69ff1 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -106,6 +106,13 @@ function is_closure(mixed $variable): bool } } +if (! function_exists('is_field')) { + function is_field(mixed $variable): bool + { + return $variable instanceof Field; + } +} + if (! function_exists('moonshineAssets')) { function moonshineAssets(): AssetManager { diff --git a/tests/Unit/Fields/EnumFieldTest.php b/tests/Unit/Fields/EnumFieldTest.php index f93235627..4ecefafbc 100644 --- a/tests/Unit/Fields/EnumFieldTest.php +++ b/tests/Unit/Fields/EnumFieldTest.php @@ -12,9 +12,8 @@ beforeEach(function (): void { $this->field = Enum::make('Enum') + ->fill(TestEnumColor::Red) ->attach(TestEnumColor::class); - - $this->field->resolveFill(['enum' => TestEnumColor::Red]); }); it('select field is parent', function (): void { @@ -34,7 +33,7 @@ it('preview', function (): void { expect($this->field->preview()) - ->toBe('Red'); + ->toBe('R'); }); it('apply', function (): void {