From de3f54217c64f9aabb9b8343eacdc14c48b7eecc Mon Sep 17 00:00:00 2001 From: antoine Date: Wed, 22 Jan 2025 19:20:24 +0100 Subject: [PATCH] Allow quick creation layout --- .../QuickCreate/QuickCreationCommand.php | 11 ++- src/Form/Layout/FormLayout.php | 10 ++ src/Form/Layout/FormLayoutColumn.php | 16 ++++ src/Form/Layout/FormLayoutTab.php | 8 ++ src/Form/SharpForm.php | 9 +- ...ListQuickCreationCommandControllerTest.php | 91 +++++++++++++++++++ 6 files changed, 142 insertions(+), 3 deletions(-) diff --git a/src/EntityList/Commands/QuickCreate/QuickCreationCommand.php b/src/EntityList/Commands/QuickCreate/QuickCreationCommand.php index 0ff4ffb00..cb91c00d4 100644 --- a/src/EntityList/Commands/QuickCreate/QuickCreationCommand.php +++ b/src/EntityList/Commands/QuickCreate/QuickCreationCommand.php @@ -4,6 +4,7 @@ use Code16\Sharp\EntityList\Commands\EntityCommand; use Code16\Sharp\Form\Fields\SharpFormField; +use Code16\Sharp\Form\Layout\FormLayoutColumn; use Code16\Sharp\Form\SharpForm; use Code16\Sharp\Utils\Fields\FieldsContainer; @@ -40,7 +41,15 @@ public function buildFormFields(FieldsContainer $formFields): void ) ->each(fn (SharpFormField $field) => $formFields->addField($field)); } - + + public function buildFormLayout(FormLayoutColumn &$column): void + { + $this->sharpForm->getBuiltFormLayout()->getAllColumns() + ->each(function (FormLayoutColumn $formLayoutColumn) use (&$column) { + $column->merge($formLayoutColumn); + }); + } + protected function initialData(): array { return $this->sharpForm->create(); diff --git a/src/Form/Layout/FormLayout.php b/src/Form/Layout/FormLayout.php index e0f7a734f..b7af15e71 100644 --- a/src/Form/Layout/FormLayout.php +++ b/src/Form/Layout/FormLayout.php @@ -2,6 +2,7 @@ namespace Code16\Sharp\Form\Layout; +use Illuminate\Support\Collection; use Illuminate\Support\Traits\Conditionable; class FormLayout implements HasLayout @@ -52,6 +53,15 @@ final public function setTabbed(bool $tabbed = true): self return $this; } + + /** + * @internal + */ + public function getAllColumns(): Collection + { + return collect($this->tabs) + ->flatMap(fn (FormLayoutTab $tab) => $tab->getColumns()); + } private function addTabLayout(FormLayoutTab $tab): FormLayoutTab { diff --git a/src/Form/Layout/FormLayoutColumn.php b/src/Form/Layout/FormLayoutColumn.php index 254480088..43365caea 100644 --- a/src/Form/Layout/FormLayoutColumn.php +++ b/src/Form/Layout/FormLayoutColumn.php @@ -27,4 +27,20 @@ private function addFieldsetLayout(FormLayoutFieldset $fieldset) { $this->rows[] = [$fieldset]; } + + /** + * @internal + */ + public function getRows(): array + { + return $this->rows; + } + + /** + * @internal + */ + public function merge(FormLayoutColumn $column): array + { + return $this->rows = [...$this->rows, ...$column->getRows()]; + } } diff --git a/src/Form/Layout/FormLayoutTab.php b/src/Form/Layout/FormLayoutTab.php index c7665e960..f096bca6d 100644 --- a/src/Form/Layout/FormLayoutTab.php +++ b/src/Form/Layout/FormLayoutTab.php @@ -48,4 +48,12 @@ public function addColumnLayout(FormLayoutColumn $column): FormLayoutColumn return $column; } + + /** + * @internal + */ + public function getColumns(): array + { + return $this->columns; + } } diff --git a/src/Form/SharpForm.php b/src/Form/SharpForm.php index ec34feb56..5a18e7de1 100644 --- a/src/Form/SharpForm.php +++ b/src/Form/SharpForm.php @@ -27,13 +27,18 @@ abstract class SharpForm protected ?string $createFormTitle = null; final public function formLayout(): array + { + return $this->getBuiltFormLayout()->toArray(); + } + + final public function getBuiltFormLayout(): FormLayout { if ($this->formLayout === null) { $this->formLayout = new FormLayout(); $this->buildFormLayout($this->formLayout); } - - return $this->formLayout->toArray(); + + return $this->formLayout; } public function formConfig(): array diff --git a/tests/Http/Api/Commands/ApiEntityListQuickCreationCommandControllerTest.php b/tests/Http/Api/Commands/ApiEntityListQuickCreationCommandControllerTest.php index 8c1b21ea9..fb19a2e1d 100644 --- a/tests/Http/Api/Commands/ApiEntityListQuickCreationCommandControllerTest.php +++ b/tests/Http/Api/Commands/ApiEntityListQuickCreationCommandControllerTest.php @@ -1,6 +1,9 @@ configureQuickCreationForm(); + } + }); + + fakeFormFor('person', new class() extends PersonForm + { + public function buildFormFields(FieldsContainer $formFields): void + { + $formFields + ->addField(SharpFormTextField::make('job')) + ->addField(SharpFormTextField::make('name')) + ->addField(SharpFormListField::make('collaborators') + ->addItemField(SharpFormTextField::make('name')) + ); + } + + public function buildFormLayout(FormLayout $formLayout): void + { + $formLayout->addColumn(6, function(FormLayoutColumn $column) { + $column->withField('name'); + }) + ->addColumn(6, function(FormLayoutColumn $column) { + $column->withField('job') + ->withListField('collaborators', function(FormLayoutColumn $column) { + $column->withField('name'); + }); + }); + } + }); + + $this + ->getJson( + route('code16.sharp.api.list.command.quick-creation-form.create', ['person']), + ) + ->assertOk() + ->assertJson([ + 'layout' => [ + 'tabbed' => false, + 'tabs' => [ + [ + 'columns' => [ + [ + 'fields' => [ + [ + ['key' => 'name', 'size' => 12], + ], + [ + ['key' => 'job', 'size' => 12], + ], + [ + [ + 'key' => 'collaborators', + 'size' => 12, + 'item' => [ + [ + ['key' => 'name', 'size' => 12], + ], + ] + ], + ], + ], + 'size' => 12, + ], + ], + 'title' => 'one', + ], + ], + ], + 'fields' => [ + 'name' => [ + 'key' => 'name', + ], + 'job' => [ + 'key' => 'job', + ], + 'collaborators' => [ + 'key' => 'collaborators', + ], + ], + ]); +}); + + it('allows to call a quick creation command with custom form fields', function () { fakeListFor('person', new class() extends PersonList {