Skip to content

Commit

Permalink
Improve grid() method to accept closure that can be evaluated. This
Browse files Browse the repository at this point in the history
will make possible to set Repeater::grid('auto').
  • Loading branch information
tjodalv committed Oct 17, 2024
1 parent 7378911 commit b9a24ce
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions packages/forms/src/Components/Concerns/HasContainerGridLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@

namespace Filament\Forms\Components\Concerns;

use Closure;

trait HasContainerGridLayout
{
/**
* @var array<string, int | string | null> | null
*/
protected ?array $gridColumns = null;
protected array | int | string | Closure | null $gridColumns = null;

/**
* @param array<string, int | string | null> | int | string | null $columns
*/
public function grid(array | int | string | null $columns = 2): static
public function grid(array | int | string | Closure | null $columns = 2): static
{
if (! is_array($columns)) {
$columns = [
'lg' => $columns,
];
if (is_string($columns) && $columns === 'auto') {
$this->gridColumns = fn ($component) => count($component->getState());
return $this;
}

$this->gridColumns = [
...($this->gridColumns ?? []),
...$columns,
];
$this->gridColumns = $columns;

return $this;
}
Expand All @@ -33,7 +31,15 @@ public function grid(array | int | string | null $columns = 2): static
*/
public function getGridColumns(?string $breakpoint = null): array | int | string | null
{
$columns = $this->gridColumns ?? [
$gridColumns = $this->evaluate($this->gridColumns);

if (! is_array($gridColumns)) {
$gridColumns = [
'lg' => $gridColumns,
];
}

$columns = $gridColumns ?? [
'default' => 1,
'sm' => null,
'md' => null,
Expand Down

0 comments on commit b9a24ce

Please sign in to comment.