Skip to content

Commit

Permalink
filament layout fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Oct 15, 2023
1 parent c8f5721 commit ce41eeb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 99 deletions.
29 changes: 13 additions & 16 deletions src/Filament/Config/FilamentLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Kiwilan\Steward\Filament\Config;

use Filament\Forms\Components\Section;
use Filament\Forms\Components\Component;
use Filament\Forms\Form;
use Kiwilan\Steward\Filament\Config\FilamentLayout\FilamentLayoutCard;
use Kiwilan\Steward\Filament\Config\FilamentLayout\FilamentLayoutColumn;
use Kiwilan\Steward\Filament\Config\FilamentLayout\FilamentLayoutSettings;
use Kiwilan\Steward\Filament\Config\FilamentLayout\FilamentLayoutSection;

class FilamentLayout
{
Expand All @@ -29,41 +28,39 @@ public static function make(Form $form, array $schema = [], int $width = 3): For
}

/**
* @param array<array<int,mixed>>|array<int,mixed> $fields
* @param FilamentLayoutSection[] $sections
*/
public static function column(array $fields = [], int $width = 2): \Filament\Forms\Components\Group
public static function column(array $sections = [], int $width = 2): \Filament\Forms\Components\Group
{
return FilamentLayoutColumn::make($fields)
return FilamentLayoutColumn::make($sections)
->width($width)
->get()
;
}

public static function card(array $fields = [], string $title = null, int $width = 2): Section
{
return FilamentLayoutCard::make($fields, $title, $width);
}

public static function setting(array $fields = [], int $width = 2, string $title = null): \Filament\Forms\Components\Group
/**
* @param Component[] $fields
*/
public static function section(array $fields = []): FilamentLayoutSection
{
return FilamentLayoutSettings::make($fields, $width, $title);
return FilamentLayoutSection::make($fields);
}

public function width(int $width = 3): self
protected function width(int $width = 3): self
{
$this->width = $width;

return $this;
}

public function schema(array $schema = []): self
protected function schema(array $schema = []): self
{
$this->schema = $schema;

return $this;
}

public function get(): Form
protected function get(): Form
{
return $this->form
->schema($this->schema)
Expand Down
68 changes: 16 additions & 52 deletions src/Filament/Config/FilamentLayout/FilamentLayoutColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

use Closure;
use Filament\Forms;
use Illuminate\Support\Str;

class FilamentLayoutColumn
{
/**
* @param FilamentLayoutSection[]|mixed[] $sections
*/
public function __construct(
protected array $fields = [],
protected array $sections = [],
protected int $width = 2,
protected bool|Closure $hidden = false,
protected bool $card = true,
protected array $titles = [],
) {
}

/**
* @param array<array<int,mixed>>|array<int,mixed> $fields
* @param FilamentLayoutSection[] $sections
*/
public static function make(array $fields = []): self
public static function make(array $sections = []): self
{
$column = new FilamentLayoutColumn();
$column->fields = $fields;
$column->sections = $sections;

return $column;
}
Expand All @@ -35,27 +35,13 @@ public function width(int $width = 2): self
return $this;
}

public function disableCard(): self
{
$this->card = false;

return $this;
}

public function hidden(bool|Closure $condition = true): self
{
$this->hidden = $condition;

return $this;
}

public function titles(string|array $titles = []): self
{
$this->titles = is_array($titles) ? $titles : [$titles];

return $this;
}

public function get()
{
$fields = $this->setFields();
Expand All @@ -74,38 +60,16 @@ private function setFields(): array
{
$fields = [];

foreach ($this->fields as $key => $field) {
if (! is_array($field)) {
$field = [$field];
}

$title = null;

if (array_key_exists($key, $this->titles)) {
$title = $this->titles[$key];
}

$group = [];

if ($title) {
$group[] = Forms\Components\Placeholder::make(Str::slug($title))
->label($title)
->columnSpan($this->width)
;
}
$group = array_merge($group, $field);
$component = $this->card ? Forms\Components\Section::make() : Forms\Components\Group::make();
foreach ($this->sections as $key => $section) {
$component = Forms\Components\Section::make();
$schema = $section instanceof FilamentLayoutSection ? $section->get() : $section;

if (! empty($group)) {
$fields[] = $component
->schema($group)
->columns([
'sm' => $this->width,
])
;
} else {
$fields[] = Forms\Components\Group::make();
}
$fields[] = $component
->schema($schema)
->columns([
'sm' => $this->width,
])
;
}

return $fields;
Expand Down
29 changes: 29 additions & 0 deletions src/Filament/Config/FilamentLayout/FilamentLayoutSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Kiwilan\Steward\Filament\Config\FilamentLayout;

use Filament\Forms\Components\Component;

class FilamentLayoutSection
{
protected function __construct(
protected array $fields = [],
) {
}

/**
* @param Component[] $fields
*/
public static function make(array $fields = []): self
{
$column = new FilamentLayoutSection();
$column->fields = $fields;

return $column;
}

public function get(): array
{
return $this->fields;
}
}
31 changes: 0 additions & 31 deletions src/Filament/Config/FilamentLayout/FilamentLayoutSettings.php

This file was deleted.

0 comments on commit ce41eeb

Please sign in to comment.