Skip to content

Commit

Permalink
fix: preview with links/badges
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Sep 21, 2023
1 parent d76b23a commit 55a576e
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 34 deletions.
6 changes: 1 addition & 5 deletions resources/views/components/fields-group.blade.php
Original file line number Diff line number Diff line change
@@ -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())
<x-moonshine::field-container :field="$fieldOrDecoration">
{!! $fieldOrDecoration->getBeforeRender() !!}
{{ $fieldOrDecoration->render() }}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/code.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) }}
>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/json.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div x-id="['json']" :id="$id('json')" {{ $element->attributes()->except('x-on:change') }}>
<div x-id="['json']" :id="$id('json')" {{ $element->attributes()->only('class') }}>
{{ $element->value(withOld: false)
->editable()
->when(
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/range.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) }}
>
<x-moonshine::form.input
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/stack.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<x-moonshine::divider :label="$field->label()" />
@endif
<div {{ $element->attributes()
->except('x-on:change')
->only('class')
->merge(['class' => 'my-2']) }}
>
{!! $field->preview() !!}
Expand Down
2 changes: 1 addition & 1 deletion src/Decorations/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function __construct(
) {
parent::__construct($label);

$this->addLink($this->label(), $link, $blank);
$this->link($link, $this->label(), blank: $blank);
}
}
5 changes: 3 additions & 2 deletions src/Fields/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -49,6 +50,6 @@ protected function resolvePreview(): string

return view('moonshine::ui.boolean', [
'value' => (bool) $this->toValue(false),
])->render();
]);
}
}
30 changes: 23 additions & 7 deletions src/Fields/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@
namespace MoonShine\Fields;

use MoonShine\Contracts\Fields\DefaultValueTypes\DefaultCanBeEnum;
use UnitEnum;

class Enum extends Select implements DefaultCanBeEnum
{
/**
* @param class-string<UnitEnum> $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;
Expand All @@ -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 ?? '';
}
}
2 changes: 1 addition & 1 deletion src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
8 changes: 5 additions & 3 deletions src/Fields/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Fields/NoInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MoonShine\Fields;

use Closure;
use Illuminate\Contracts\View\View;
use MoonShine\Support\Condition;

class NoInput extends Field
Expand All @@ -29,7 +30,7 @@ public function boolean(
return $this;
}

protected function resolvePreview(): string
protected function resolvePreview(): View|string
{
$value = $this->toFormattedValue();

Expand All @@ -40,7 +41,7 @@ protected function resolvePreview(): string
if ($this->isBoolean) {
return view('moonshine::ui.boolean', [
'value' => $value,
])->render();
]);
}

return (string) $value;
Expand Down
5 changes: 3 additions & 2 deletions src/Fields/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MoonShine\Fields;

use Illuminate\Contracts\View\View;
use MoonShine\Contracts\Fields\DefaultValueTypes\DefaultCanBeNumeric;
use MoonShine\Traits\Fields\NumberTrait;

Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/Fields/StackFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
]);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Fields/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -24,6 +26,6 @@ protected function resolvePreview(): string
'href' => $value,
'value' => $value,
'blank' => $this->isLinkBlank(),
])->render();
]);
}
}
7 changes: 7 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Fields/EnumFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,7 +33,7 @@

it('preview', function (): void {
expect($this->field->preview())
->toBe('Red');
->toBe('R');
});

it('apply', function (): void {
Expand Down

0 comments on commit 55a576e

Please sign in to comment.