diff --git a/src/Filament/Integration/Builders/FormBuilder.php b/src/Filament/Integration/Builders/FormBuilder.php index ee11a89..c88381c 100644 --- a/src/Filament/Integration/Builders/FormBuilder.php +++ b/src/Filament/Integration/Builders/FormBuilder.php @@ -8,18 +8,30 @@ use Filament\Schemas\Components\Grid; use Illuminate\Support\Collection; use Relaticle\CustomFields\Filament\Integration\Factories\FieldComponentFactory; +use Relaticle\CustomFields\Filament\Integration\Factories\SectionComponentFactory; use Relaticle\CustomFields\Models\CustomField; +use Relaticle\CustomFields\Models\CustomFieldSection; class FormBuilder extends BaseBuilder { + private bool $withoutSections = false; + public function build(): Grid { return FormContainer::make() ->forModel($this->explicitModel ?? null) + ->withoutSections($this->withoutSections) ->only($this->only) ->except($this->except); } + public function withoutSections(bool $withoutSections = true): static + { + $this->withoutSections = $withoutSections; + + return $this; + } + private function getDependentFieldCodes(Collection $fields): array { $dependentCodes = []; @@ -40,21 +52,29 @@ private function getDependentFieldCodes(Collection $fields): array public function values(): Collection { $fieldComponentFactory = app(FieldComponentFactory::class); + $sectionComponentFactory = app(SectionComponentFactory::class); $allFields = $this->getFilteredSections()->flatMap(fn (mixed $section) => $section->fields); $dependentFieldCodes = $this->getDependentFieldCodes($allFields); - // Return fields directly without Section/Fieldset wrappers - // This ensures the flat structure: custom_fields.{field_code} - // Note: We skip section grouping to avoid nested paths like custom_fields.{section_code}.{field_code} - // which causes issues with Filament v4's child schema nesting behavior. - // Visual grouping can be added later using alternative methods if needed. - return $allFields->map( - fn (CustomField $customField) => $fieldComponentFactory->create( - $customField, - $dependentFieldCodes, - $allFields - ) + $createField = fn (CustomField $customField) => $fieldComponentFactory->create( + $customField, + $dependentFieldCodes, + $allFields ); + + if ($this->withoutSections) { + return $allFields->map($createField); + } + + return $this->getFilteredSections() + ->map(function (CustomFieldSection $section) use ($sectionComponentFactory, $createField) { + $fields = $section->fields->map($createField); + + return $fields->isEmpty() + ? null + : $sectionComponentFactory->create($section)->schema($fields->toArray()); + }) + ->filter(); } } diff --git a/src/Filament/Integration/Builders/FormContainer.php b/src/Filament/Integration/Builders/FormContainer.php index 0656c4a..4e9f310 100644 --- a/src/Filament/Integration/Builders/FormContainer.php +++ b/src/Filament/Integration/Builders/FormContainer.php @@ -13,6 +13,8 @@ final class FormContainer extends Grid private array $only = []; + private bool $withoutSections = false; + public static function make(array|int|null $columns = 12): static { $container = new self($columns); @@ -44,6 +46,13 @@ public function only(array $fieldCodes): static return $this; } + public function withoutSections(bool $withoutSections = true): static + { + $this->withoutSections = $withoutSections; + + return $this; + } + private function generateSchema(): array { // Inline priority: explicit ?? record ?? model class @@ -57,6 +66,7 @@ private function generateSchema(): array return $builder ->forModel($model) + ->withoutSections($this->withoutSections) ->only($this->only) ->except($this->except) ->values() diff --git a/src/Filament/Integration/Factories/SectionComponentFactory.php b/src/Filament/Integration/Factories/SectionComponentFactory.php index d26c94c..6b0748e 100644 --- a/src/Filament/Integration/Factories/SectionComponentFactory.php +++ b/src/Filament/Integration/Factories/SectionComponentFactory.php @@ -16,9 +16,11 @@ public function create(CustomFieldSection $customFieldSection): Section|Fieldset { return match ($customFieldSection->type) { CustomFieldSectionType::SECTION => Section::make($customFieldSection->name) + ->columnSpanFull() ->description($customFieldSection->description) ->columns(12), CustomFieldSectionType::FIELDSET => Fieldset::make('custom_fields.'.$customFieldSection->code) + ->columnSpanFull() ->label($customFieldSection->name) ->columns(12), CustomFieldSectionType::HEADLESS => Grid::make(12), diff --git a/src/Filament/Integration/Factories/SectionInfolistsFactory.php b/src/Filament/Integration/Factories/SectionInfolistsFactory.php index ef40738..e406874 100644 --- a/src/Filament/Integration/Factories/SectionInfolistsFactory.php +++ b/src/Filament/Integration/Factories/SectionInfolistsFactory.php @@ -16,10 +16,12 @@ public function create(CustomFieldSection $customFieldSection): Section|Fieldset { return match ($customFieldSection->type) { CustomFieldSectionType::SECTION => Section::make($customFieldSection->name) + ->columnSpanFull() ->columns(12) ->description($customFieldSection->description), CustomFieldSectionType::FIELDSET => Fieldset::make($customFieldSection->name) + ->columnSpanFull() ->columns(12), CustomFieldSectionType::HEADLESS => Grid::make($customFieldSection->column_span ?? 12),