-
-
Notifications
You must be signed in to change notification settings - Fork 185
refact!: FieldClass with named props
#7608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v6/refact/field-mixins
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| namespace Kirby\Form; | ||
|
|
||
| use Kirby\Cms\HasSiblings; | ||
| use Kirby\Cms\ModelWithContent; | ||
| use Kirby\Exception\NotFoundException; | ||
| use Kirby\Toolkit\HasI18n; | ||
|
|
||
| /** | ||
|
|
@@ -39,40 +41,54 @@ abstract class FieldClass | |
| use Mixin\When; | ||
| use Mixin\Width; | ||
|
|
||
| protected Fields $siblings; | ||
|
|
||
| public function __construct( | ||
| protected array $params = [] | ||
| array|string|null $after = null, | ||
| bool $autofocus = false, | ||
| array|string|null $before = null, | ||
| mixed $default = null, | ||
| bool $disabled = false, | ||
| array|string|null $help = null, | ||
| string|null $icon = null, | ||
| array|string|null $label = null, | ||
| ModelWithContent|null $model = null, | ||
| string|null $name = null, | ||
| array|string|null $placeholder = null, | ||
| bool $required = false, | ||
| protected Fields|null $siblings = null, | ||
| bool $translate = true, | ||
| $value = null, | ||
| array|null $when = null, | ||
| string|null $width = null | ||
| ) { | ||
| $this->setAfter($params['after'] ?? null); | ||
| $this->setAutofocus($params['autofocus'] ?? false); | ||
| $this->setBefore($params['before'] ?? null); | ||
| $this->setDefault($params['default'] ?? null); | ||
| $this->setDisabled($params['disabled'] ?? false); | ||
| $this->setHelp($params['help'] ?? null); | ||
| $this->setIcon($params['icon'] ?? null); | ||
| $this->setLabel($params['label'] ?? null); | ||
| $this->setModel($params['model'] ?? null); | ||
| $this->setName($params['name'] ?? null); | ||
| $this->setPlaceholder($params['placeholder'] ?? null); | ||
| $this->setRequired($params['required'] ?? false); | ||
| $this->setSiblings($params['siblings'] ?? null); | ||
| $this->setTranslate($params['translate'] ?? true); | ||
| $this->setWhen($params['when'] ?? null); | ||
| $this->setWidth($params['width'] ?? null); | ||
|
|
||
| if (array_key_exists('value', $params) === true) { | ||
| $this->fill($params['value']); | ||
| $this->setAfter($after); | ||
| $this->setAutofocus($autofocus); | ||
| $this->setBefore($before); | ||
| $this->setDefault($default); | ||
| $this->setDisabled($disabled); | ||
| $this->setHelp($help); | ||
| $this->setIcon($icon); | ||
| $this->setLabel($label); | ||
| $this->setModel($model); | ||
| $this->setName($name); | ||
| $this->setPlaceholder($placeholder); | ||
| $this->setRequired($required); | ||
| $this->setSiblings($siblings); | ||
| $this->setTranslate($translate); | ||
| $this->setWhen($when); | ||
| $this->setWidth($width); | ||
|
|
||
| if ($value !== null) { | ||
distantnative marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->fill($value); | ||
| } | ||
| } | ||
|
|
||
| public function __call(string $param, array $args): mixed | ||
| { | ||
| if (isset($this->$param) === true) { | ||
| if (property_exists($this, $param) === true) { | ||
| return $this->$param; | ||
| } | ||
|
|
||
| return $this->params[$param] ?? null; | ||
| throw new NotFoundException(message: 'Method or option "' . $param . '" does not exist for field type "' . $this->type() . '"'); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bastianallgeier I was asking the other day about the magic
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to bring back this undocumented option for input fields, we should really do that in a way that sets a validate property and gets rid of the magic anyway. |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -91,6 +107,19 @@ public function drawers(): array | |
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new field instance from a $props array | ||
| * @since 6.0.0 | ||
| */ | ||
| public static function factory( | ||
| array $props, | ||
| Fields|null $siblings = null | ||
| ): static { | ||
| $props['siblings'] = $siblings; | ||
| unset($props['type']); | ||
| return new static(...$props); | ||
| } | ||
|
|
||
| public function id(): string | ||
| { | ||
| return $this->name(); | ||
|
|
@@ -101,14 +130,6 @@ public function isHidden(): bool | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Returns all original params for the field | ||
| */ | ||
| public function params(): array | ||
| { | ||
| return $this->params; | ||
| } | ||
|
|
||
| /** | ||
| * Define the props that will be sent to | ||
| * the Vue component | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.