-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add widget fields & form elements (#123)
- Loading branch information
Showing
31 changed files
with
904 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/DatasourceCustomizer/src/Decorators/Actions/BaseFormElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions; | ||
|
||
class BaseFormElement | ||
{ | ||
public function __construct( | ||
protected string $type, | ||
array $extraArguments = [] // workaround like kwargs in other languages | ||
) { | ||
foreach ($extraArguments as $key => $value) { | ||
$this->$key = $value; | ||
} | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
foreach ($this->keys() as $attribute) { | ||
if (is_callable($this->$attribute)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$result = []; | ||
foreach ($this->keys() as $attribute) { | ||
$result[$attribute] = $this->$attribute; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function keys(): array | ||
{ | ||
return array_keys(get_object_vars($this)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/DatasourceCustomizer/src/Decorators/Actions/FormLayoutElement/HtmlBlockElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\FormLayoutElement; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseFormElement; | ||
|
||
class HtmlBlockElement extends BaseFormElement | ||
{ | ||
public function __construct( | ||
protected string $content, | ||
array $extraArguments = [] | ||
) { | ||
parent::__construct('HtmlBlock', $extraArguments); | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/DatasourceCustomizer/src/Decorators/Actions/FormLayoutElement/LayoutElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\FormLayoutElement; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseFormElement; | ||
|
||
class LayoutElement extends BaseFormElement | ||
{ | ||
public function __construct( | ||
protected string $component, | ||
protected ?string $ifCondition = null, | ||
array $extraArguments = [] | ||
) { | ||
parent::__construct('Layout', $extraArguments); | ||
} | ||
|
||
public function getComponent(): string | ||
{ | ||
return $this->component; | ||
} | ||
|
||
public function getIfCondition(): ?string | ||
{ | ||
return $this->ifCondition; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/DatasourceCustomizer/src/Decorators/Actions/FormLayoutElement/PageElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\FormLayoutElement; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceToolkit\Exceptions\ForestException; | ||
|
||
class PageElement extends LayoutElement | ||
{ | ||
protected array $elements; | ||
protected ?string $nextButtonLabel; | ||
protected ?string $previousButtonLabel; | ||
|
||
public function __construct( | ||
array $elements, | ||
?string $nextButtonLabel = null, | ||
?string $previousButtonLabel = null, | ||
array $extraArguments = [] | ||
) { | ||
parent::__construct('Page', $extraArguments); | ||
|
||
$this->validateElementsPresence($elements); | ||
$this->validateNoPageElements($elements); | ||
|
||
$this->elements = $this->instantiateElements($elements); | ||
$this->nextButtonLabel = $nextButtonLabel; | ||
$this->previousButtonLabel = $previousButtonLabel; | ||
} | ||
|
||
private function validateElementsPresence(array $elements): void | ||
{ | ||
if (empty($elements)) { | ||
throw new ForestException("Using 'elements' in a 'Page' configuration is mandatory"); | ||
} | ||
} | ||
|
||
private function validateNoPageElements(array $elements): void | ||
{ | ||
foreach ($elements as $element) { | ||
if (isset($element['component']) && $element['component'] === 'Page') { | ||
throw new ForestException("'Page' component cannot be used within 'elements'"); | ||
} | ||
} | ||
} | ||
|
||
private function instantiateElements(array $elements): array | ||
{ | ||
// TODO | ||
//return FormFactory::buildElements($elements); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/DatasourceCustomizer/src/Decorators/Actions/FormLayoutElement/RowElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\FormLayoutElement; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseFormElement; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\DynamicField; | ||
use ForestAdmin\AgentPHP\DatasourceToolkit\Exceptions\ForestException; | ||
|
||
class RowElement extends BaseFormElement | ||
{ | ||
public function __construct( | ||
protected array $fields, | ||
array $extraArguments = [] | ||
) { | ||
parent::__construct('Row', $extraArguments); | ||
$this->validateFieldsPresence($extraArguments); | ||
$this->validateNoLayoutSubfields($extraArguments['fields'] ?? []); | ||
$this->fields = $this->instantiateSubfields($extraArguments['fields'] ?? []); | ||
} | ||
|
||
private function validateFieldsPresence(array $options): void | ||
{ | ||
if (! array_key_exists('fields', $options)) { | ||
throw new ForestException("Using 'fields' in a 'Row' configuration is mandatory"); | ||
} | ||
} | ||
|
||
private function validateNoLayoutSubfields(array $fields): void | ||
{ | ||
foreach ($fields as $field) { | ||
if (($field instanceof DynamicField && $field->getType() === 'Layout') || | ||
(is_array($field) && ($field['type'] ?? '') === 'Layout')) { | ||
throw new ForestException("A 'Row' form element doesn't allow layout elements as subfields"); | ||
} | ||
} | ||
} | ||
|
||
private function instantiateSubfields(array $fields): array | ||
{ | ||
return array_map(fn ($field) => new DynamicField(...$field), $fields); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/DatasourceCustomizer/src/Decorators/Actions/FormLayoutElement/SeparatorElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\FormLayoutElement; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseFormElement; | ||
|
||
class SeparatorElement extends BaseFormElement | ||
{ | ||
public function __construct(array $extraArguments = []) | ||
{ | ||
parent::__construct('Separator', $extraArguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ages/DatasourceCustomizer/src/Decorators/Actions/WidgetField/AddressAutocompleteField.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ForestAdminDatasourceCustomizer\Decorators\Action\WidgetField; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\DynamicField; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\WidgetField\Widget; | ||
|
||
class AddressAutocompleteField extends DynamicField | ||
{ | ||
use Widget; | ||
|
||
public function __construct($options) | ||
{ | ||
parent::__construct($options['type'], $options['label']); | ||
WidgetValidator::validateArg($options, 'type', ['type' => 'contains', 'value' => ['String']]); | ||
$this->widget = 'AddressAutocomplete'; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/DatasourceCustomizer/src/Decorators/Actions/WidgetField/CheckboxField.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace ForestAdminDatasourceCustomizer\Decorators\Action\WidgetField; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\DynamicField; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\FieldType; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\WidgetField\Widget; | ||
|
||
class CheckboxField extends DynamicField | ||
{ | ||
use Widget; | ||
|
||
public function __construct($options) | ||
{ | ||
parent::__construct($options['type'], $options['label']); | ||
WidgetValidator::validateArg($options, 'type', ['type' => 'contains', 'value' => [FieldType::BOOLEAN]]); | ||
$this->widget = 'Checkbox'; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/DatasourceCustomizer/src/Decorators/Actions/WidgetField/CheckboxGroupField.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace ForestAdminDatasourceCustomizer\Decorators\Action\WidgetField; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\DynamicField; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\FieldType; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\WidgetField\Widget; | ||
|
||
class CheckboxGroupField extends DynamicField | ||
{ | ||
use Widget; | ||
|
||
private array $options; | ||
|
||
public function __construct($options) | ||
{ | ||
parent::__construct($options['type'], $options['label']); | ||
WidgetValidator::validateArg($options, 'options', ['type' => 'present']); | ||
WidgetValidator::validateArg($options, 'type', ['type' => 'contains', 'value' => [FieldType::STRING_LIST, FieldType::NUMBER_LIST]]); | ||
$this->widget = 'CheckboxGroup'; | ||
$this->options = $options['options']; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/DatasourceCustomizer/src/Decorators/Actions/WidgetField/ColorPickerField.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace ForestAdminDatasourceCustomizer\Decorators\Action\WidgetField; | ||
|
||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\DynamicField; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\FieldType; | ||
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\WidgetField\Widget; | ||
|
||
class ColorPickerField extends DynamicField | ||
{ | ||
use Widget; | ||
|
||
private ?bool $enableOpacity; | ||
|
||
private ?array $quickPalette; | ||
|
||
public function __construct($options) | ||
{ | ||
parent::__construct($options['type'], $options['label']); | ||
WidgetValidator::validateArg($options, 'enable_opacity', ['type' => 'contains', 'value' => [FieldType::STRING]]); | ||
$this->widget = 'ColorPicker'; | ||
$this->enableOpacity = $options['enable_opacity'] ?? null; | ||
$this->quickPalette = $options['quick_palette'] ?? null; | ||
} | ||
|
||
public function getEnableOpacity(): ?bool | ||
{ | ||
return $this->enableOpacity; | ||
} | ||
|
||
public function getQuickPalette(): ?array | ||
{ | ||
return $this->quickPalette; | ||
} | ||
} |
Oops, something went wrong.