|
3 | 3 | namespace Relaticle\CustomFields\Filament\Integration\Builders; |
4 | 4 |
|
5 | 5 | use Filament\Forms\Components\Field; |
6 | | -use Filament\Schemas\Components\Component; |
| 6 | +use Filament\Schemas\Components\Grid; |
7 | 7 | use Illuminate\Database\Eloquent\Model; |
8 | 8 |
|
9 | | -final class InfolistContainer extends Component |
| 9 | +final class InfolistContainer extends Grid |
10 | 10 | { |
11 | | - protected string $view = 'filament-schemas::components.grid'; |
| 11 | + private Model|string|null $explicitModel = null; |
12 | 12 |
|
13 | | - protected Model|string|null $recordModel; |
| 13 | + private array $except = []; |
14 | 14 |
|
15 | | - protected array $except = []; |
16 | | - |
17 | | - protected array $only = []; |
| 15 | + private array $only = []; |
18 | 16 |
|
19 | 17 | private bool $hiddenLabels = false; |
20 | 18 |
|
21 | 19 | private bool $visibleWhenFilled = false; |
22 | 20 |
|
23 | 21 | private bool $withoutSections = false; |
24 | 22 |
|
25 | | - public function __construct() |
| 23 | + public static function make(array|int|null $columns = 1): static |
26 | 24 | { |
27 | | - // Defer schema generation until we can safely access the record |
28 | | - $this->schema(fn() => $this->generateSchema()); |
29 | | - } |
| 25 | + $container = new self($columns); |
30 | 26 |
|
31 | | - public static function make(): static |
32 | | - { |
33 | | - return app(self::class); |
| 27 | + // Defer schema generation until component is in container |
| 28 | + $container->schema(fn (): array => $container->generateSchema()); |
| 29 | + |
| 30 | + return $container; |
34 | 31 | } |
35 | 32 |
|
36 | 33 | public function forModel(Model|string|null $model): static |
37 | 34 | { |
38 | | - $this->recordModel = $model; |
| 35 | + $this->explicitModel = $model; |
39 | 36 |
|
40 | 37 | return $this; |
41 | 38 | } |
@@ -78,19 +75,23 @@ public function withoutSections(bool $withoutSections = true): static |
78 | 75 | /** |
79 | 76 | * @return array<int, Field> |
80 | 77 | */ |
81 | | - protected function generateSchema(): array |
| 78 | + private function generateSchema(): array |
82 | 79 | { |
83 | | - $model = $this->recordModel ?? $this->getRecord() ?? $this->getModel(); |
| 80 | + // Inline priority: explicit ?? record ?? model class |
| 81 | + $model = $this->explicitModel ?? $this->getRecord() ?? $this->getModel(); |
84 | 82 |
|
85 | | - $builder = app(InfolistBuilder::class); |
| 83 | + if ($model === null) { |
| 84 | + return []; // Graceful fallback |
| 85 | + } |
86 | 86 |
|
87 | | - return $builder |
| 87 | + $builder = app(InfolistBuilder::class) |
| 88 | + ->forModel($model) |
88 | 89 | ->only($this->only) |
89 | 90 | ->except($this->except) |
90 | 91 | ->hiddenLabels($this->hiddenLabels) |
91 | 92 | ->visibleWhenFilled($this->visibleWhenFilled) |
92 | | - ->withoutSections($this->withoutSections) |
93 | | - ->values($model) |
94 | | - ->toArray(); |
| 93 | + ->withoutSections($this->withoutSections); |
| 94 | + |
| 95 | + return $builder->values()->toArray(); |
95 | 96 | } |
96 | 97 | } |
0 commit comments