Skip to content

Commit

Permalink
Merge pull request #1354 from moonshine-software/fragment-metrics
Browse files Browse the repository at this point in the history
feat: Fragment metrics
  • Loading branch information
lee-to authored Nov 29, 2024
2 parents bdb4b61 + 18ccac8 commit 66799a6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/Laravel/src/Pages/Crud/IndexPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,18 @@ protected function getMetrics(): ?ComponentContract

$metrics = $this->getResource()->getMetrics();

return $metrics
? Div::make($metrics)
->class('layout-metrics')
: null;
if ($metrics === []) {
return null;
}

$components = Div::make($metrics)->class('layout-metrics');


if (! \is_null($fragment = $this->getResource()->getFragmentMetrics())) {
return $fragment([$components]);
}

return $components;
}

/**
Expand Down
24 changes: 21 additions & 3 deletions src/Laravel/src/Resources/CrudResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MoonShine\Laravel\Resources;

use Closure;
use Illuminate\Support\Collection;
use MoonShine\Contracts\Core\CrudPageContract;
use MoonShine\Contracts\Core\CrudResourceContract;
use MoonShine\Contracts\Core\DependencyInjection\FieldsContract;
Expand All @@ -12,6 +14,7 @@
use MoonShine\Contracts\Core\TypeCasts\DataWrapperContract;
use MoonShine\Core\Resources\Resource;
use MoonShine\Core\TypeCasts\MixedDataCaster;
use MoonShine\Laravel\Components\Fragment;
use MoonShine\Laravel\Pages\Crud\DetailPage;
use MoonShine\Laravel\Pages\Crud\FormPage;
use MoonShine\Laravel\Pages\Crud\IndexPage;
Expand Down Expand Up @@ -250,7 +253,22 @@ protected function metrics(): array
*/
public function getMetrics(): array
{
return $this->metrics();
return Collection::make($this->metrics())
->ensure(Metric::class)
->toArray();
}

/**
* @return ?Closure(array $components): Fragment
*/
protected function fragmentMetrics(): ?Closure
{
return null;
}

public function getFragmentMetrics(): ?Closure
{
return $this->fragmentMetrics();
}

/**
Expand Down Expand Up @@ -279,7 +297,7 @@ public function getListComponentName(): string
return rescue(
fn (): string => $this->getIndexPage()?->getListComponentName(),
"index-table-{$this->getUriKey()}",
false
false,
);
}

Expand All @@ -295,7 +313,7 @@ public function getListEventName(?string $name = null, array $params = []): stri
return rescue(
fn (): string => AlpineJs::event($this->getIndexPage()?->getListEventName() ?? '', $name, $params),
AlpineJs::event(JsEvent::TABLE_UPDATED, $name, $params),
false
false,
);
}

Expand Down

0 comments on commit 66799a6

Please sign in to comment.