Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/EntityList/Commands/QuickCreate/QuickCreationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/Form/Layout/FormLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Code16\Sharp\Form\Layout;

use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Conditionable;

class FormLayout implements HasLayout
Expand Down Expand Up @@ -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
{
Expand Down
16 changes: 16 additions & 0 deletions src/Form/Layout/FormLayoutColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
}
}
8 changes: 8 additions & 0 deletions src/Form/Layout/FormLayoutTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ public function addColumnLayout(FormLayoutColumn $column): FormLayoutColumn

return $column;
}

/**
* @internal
*/
public function getColumns(): array
{
return $this->columns;
}
}
9 changes: 7 additions & 2 deletions src/Form/SharpForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use Code16\Sharp\Form\Fields\SharpFormListField;
use Code16\Sharp\Form\Fields\SharpFormTextField;
use Code16\Sharp\Form\Layout\FormLayout;
use Code16\Sharp\Form\Layout\FormLayoutColumn;
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm;
use Code16\Sharp\Tests\Fixtures\Sharp\PersonList;
Expand Down Expand Up @@ -46,6 +49,94 @@ public function buildFormFields(FieldsContainer $formFields): void
]);
});

it('allows to call a quick creation command with form layout', function () {
fakeListFor('person', new class() extends PersonList
{
public function buildListConfig(): void
{
$this->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
{
Expand Down
Loading