Skip to content

Commit

Permalink
Merge pull request #370 from lee-to/fix-export-values
Browse files Browse the repository at this point in the history
fix(fields): ExportViewValue
  • Loading branch information
lee-to authored Jul 3, 2023
2 parents 6bb3464 + 4524e6f commit ae31cb9
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 46 deletions.
11 changes: 11 additions & 0 deletions src/Fields/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ class Checkbox extends Field implements

public function indexViewValue(Model $item, bool $container = true): string
{
if (! $container) {
return parent::indexViewValue($item, $container);
}

return view('moonshine::ui.boolean', [
'value' => (bool) $this->formViewValue($item),
])->render();
}

public function exportViewValue(Model $item): string
{
return (string) ($this->formViewValue($item)
? $this->onValue
: $this->offValue);
}
}
4 changes: 4 additions & 0 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function indexViewValue(Model $item, bool $container = true): string
->toArray()
: [$this->pathWithDir($item->{$this->field()})];

if (!$container) {
return implode(';', array_filter($files));
}

return view('moonshine::components.files', [
'files' => $files,
'download' => $this->canDownload(),
Expand Down
9 changes: 4 additions & 5 deletions src/Fields/ID.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ class ID extends Text

public function indexViewValue(Model $item, bool $container = true): string
{
if (!$container) {
return parent::indexViewValue($item, $container);
}

return view('moonshine::ui.badge', [
'value' => parent::indexViewValue($item, $container),
'color' => 'purple',
])->render();
}

public function exportViewValue(Model $item): string
{
return (string) $item->{$this->field()};
}

public function save(Model $item): Model
{
if ($this->requestValue()) {
Expand Down
25 changes: 14 additions & 11 deletions src/Fields/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MoonShine\Fields;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

class Image extends File
{
Expand All @@ -16,18 +17,20 @@ public function indexViewValue(Model $item, bool $container = true): string
return '';
}

if ($this->isMultiple()) {
return view('moonshine::ui.image', [
'values' => collect($item->{$this->field()})
->map(
fn ($value): string => $this->pathWithDir($value ?? '')
)
->toArray(),
])->render();
$files = $this->isMultiple()
? collect($item->{$this->field()})
->map(fn ($value): string => $this->pathWithDir($value ?? ''))
->toArray()
: [$this->pathWithDir($item->{$this->field()})];

if (! $container) {
return implode(';', array_filter($files));
}

return view('moonshine::ui.image', [
'value' => $this->pathWithDir($item->{$this->field()}),
])->render();
$viewData = $this->isMultiple()
? ['values' => $files]
: ['value' => Arr::first($files)];

return view('moonshine::ui.image', $viewData)->render();
}
}
17 changes: 6 additions & 11 deletions src/Fields/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,19 @@ public function stars(): static
return $this;
}

public function withStars(): bool
{
return $this->stars;
}

public function indexViewValue(Model $item, bool $container = true): string
{
if ($this->withStars()) {
if ($container && $this->withStars()) {
return view('moonshine::ui.rating', [
'value' => $item->{$this->field()},
])->render();
}

return parent::indexViewValue($item, $container);
}

public function withStars(): bool
{
return $this->stars;
}

public function exportViewValue(Model $item): string
{
return (string) $item->{$this->field()};
}
}
5 changes: 0 additions & 5 deletions src/Fields/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class Password extends Text
'required',
];

public function exportViewValue(Model $item): string
{
return '***';
}

public function indexViewValue(Model $item, bool $container = true): string
{
return '***';
Expand Down
17 changes: 11 additions & 6 deletions src/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ public function indexViewValue(Model $item, bool $container = true): string
json_decode($value, true, 512, JSON_THROW_ON_ERROR)
: $value;

return collect($value)->map(
fn ($v): string => view('moonshine::ui.badge', [
'color' => 'purple',
'value' => $this->values()[$v] ?? false,
])->render()
)->implode(',');
return collect($value)
->when(
$container,
fn ($collect) => $collect->map(
fn ($v): string => view('moonshine::ui.badge', [
'color' => 'purple',
'value' => $this->values()[$v] ?? false,
])->render()
)
)
->implode(',');
}

return (string) ($this->values()[$value] ?? '');
Expand Down
5 changes: 0 additions & 5 deletions src/Fields/SwitchBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,4 @@ public function indexViewValue(Model $item, bool $container = true): string
'item' => $item,
])->render();
}

public function exportViewValue(Model $item): string
{
return (string) $item->{$this->field()};
}
}
4 changes: 4 additions & 0 deletions src/Fields/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function indexViewValue(Model $item, bool $container = true): string
return '';
}

if (! $container) {
return $value;
}

return view('moonshine::ui.url', [
'href' => $value,
'value' => $value,
Expand Down
10 changes: 7 additions & 3 deletions src/Traits/WithFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function indexViewValue(Model $item, bool $container = false): string
&& ! $this instanceof HasOne
&& $this->inLine
) {
return $value?->implode(function ($item) {
return $value?->implode(function ($item) use($container) {
$implodeValue = $item->{$this->resourceTitleField()} ?? false;

if ($this->inLineBadge) {
Expand All @@ -89,12 +89,12 @@ public function indexViewValue(Model $item, bool $container = false): string
'',
);

return view('moonshine::ui.badge', [
return $container ? view('moonshine::ui.badge', [
'color' => 'purple',
'link' => $link,
'value' => $implodeValue,
'margin' => true,
])->render();
])->render() : $implodeValue;
}

return $implodeValue;
Expand Down Expand Up @@ -165,6 +165,10 @@ function ($in) use ($data, $item): void {
$values = [];
}

if (!$container) {
return '';
}

return view('moonshine::ui.table', [
'columns' => $columns,
'values' => $values,
Expand Down

0 comments on commit ae31cb9

Please sign in to comment.