Skip to content

Commit

Permalink
feat: Table itemsResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Feb 11, 2025
1 parent c61c69e commit cb8c78f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/src/MenuManager/MenuElementContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ interface MenuElementContract extends
{
public function isActive(): bool;

/** @param ?Closure(static $ctx): bool $condition */
/** @param null|Closure(static $ctx): bool $condition */
public function topMode(?Closure $condition = null): static;
}
6 changes: 3 additions & 3 deletions src/Contracts/src/UI/FormBuilderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function submit(string $label, array $attributes = []): self;

/**
* @param Closure(mixed $values, FieldsContract $fields): bool $apply
* @param ?Closure(FieldContract $field): void $default
* @param ?Closure(mixed $values): mixed $before
* @param ?Closure(mixed $values): void $after
* @param null|Closure(FieldContract $field): void $default
* @param null|Closure(mixed $values): mixed $before
* @param null|Closure(mixed $values): void $after
*/
public function apply(
Closure $apply,
Expand Down
5 changes: 5 additions & 0 deletions src/Laravel/src/Pages/Crud/IndexPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ protected function getItemsComponent(iterable $items, Fields $fields): Component
})
->when($this->getResource()->isColumnSelection(), function (TableBuilderContract $table): void {
$table->columnSelection();
})
->when(!\is_null($this->getResource()->getItemsResolver()), function (TableBuilderContract $table): void {
$table->itemsResolver(
$this->getResource()->getItemsResolver()
);
});
}

Expand Down
11 changes: 10 additions & 1 deletion src/Laravel/src/Resources/CrudResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use MoonShine\Contracts\Core\PageContract;
use MoonShine\Contracts\Core\TypeCasts\DataCasterContract;
use MoonShine\Contracts\Core\TypeCasts\DataWrapperContract;
use MoonShine\Contracts\UI\TableBuilderContract;
use MoonShine\Core\Resources\Resource;
use MoonShine\Core\TypeCasts\MixedDataCaster;
use MoonShine\Laravel\Components\Fragment;
Expand Down Expand Up @@ -299,7 +300,7 @@ public function getMetrics(): array
}

/**
* @return ?Closure(array $components): Fragment
* @return null|Closure(array $components): Fragment
*/
protected function fragmentMetrics(): ?Closure
{
Expand Down Expand Up @@ -362,6 +363,14 @@ public function getListEventName(?string $name = null, array $params = []): stri
);
}

/**
* @return null|Closure(iterable $items, TableBuilderContract $table): iterable
*/
public function getItemsResolver(): ?Closure
{
return null;
}

/**
* @param TData $item
*/
Expand Down
6 changes: 3 additions & 3 deletions src/UI/src/Components/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ public function onBeforeFieldsRender(Closure $callback): self

/**
* @param Closure(mixed $values, FieldsContract $fields): bool $apply
* @param ?Closure(FieldContract $field): void $default
* @param ?Closure(mixed $values): mixed $before
* @param ?Closure(mixed $values): void $after
* @param null|Closure(FieldContract $field): void $default
* @param null|Closure(mixed $values): mixed $before
* @param null|Closure(mixed $values): void $after
* @throws Throwable
*/
public function apply(
Expand Down
27 changes: 26 additions & 1 deletion src/UI/src/Components/IterableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MoonShine\UI\Components;

use Closure;
use Illuminate\Support\Collection;
use MoonShine\Contracts\Core\Paginator\PaginatorContract;
use MoonShine\Contracts\Core\TypeCasts\DataCasterContract;
Expand All @@ -30,6 +31,20 @@ abstract class IterableComponent extends MoonShineComponent implements HasCaster

protected iterable $buttons = [];

protected ?Closure $itemsResolver = null;

protected bool $itemsResolved = false;

/**
* @param Closure(iterable $items, static $ctx): iterable $resolver
*/
public function itemsResolver(Closure $resolver): static
{
$this->itemsResolver = $resolver;

return $this;
}

public function items(iterable $items = []): static
{
$this->items = $items;
Expand All @@ -51,7 +66,17 @@ protected function resolvePaginator(): void

public function getItems(): Collection
{
return $this->items = collect($this->items)->filter();
if($this->itemsResolved) {
return new Collection($this->items);
}

if(!\is_null($this->itemsResolver)) {
$this->items = \call_user_func($this->itemsResolver, $this->items, $this);
}

$this->itemsResolved = true;

return $this->items = (new Collection($this->items))->filter();
}

public function paginator(PaginatorContract $paginator): static
Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Fields/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ protected function setFormattedValueCallback(Closure $formattedValueCallback): v
$this->formattedValueCallback = $formattedValueCallback;
}

/** @return ?Closure(mixed $original, int $index, static $ctx): mixed */
/** @return null|Closure(mixed $original, int $index, static $ctx): mixed */
public function getFormattedValueCallback(): ?Closure
{
return $this->formattedValueCallback;
Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Traits/Fields/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function customName(Closure $name): static
}

/**
* @return ?Closure(mixed $file, static $ctx): string
* @return null|Closure(mixed $file, static $ctx): string
*/
public function getCustomName(): ?Closure
{
Expand Down

0 comments on commit cb8c78f

Please sign in to comment.