diff --git a/src/Base/AbstractField.php b/src/Base/AbstractField.php index 2e58661..a87c7ca 100644 --- a/src/Base/AbstractField.php +++ b/src/Base/AbstractField.php @@ -18,6 +18,8 @@ Html\Concern\HasPrefixCollection, Html\Concern\HasSuffixCollection, Html\Concern\HasTemplate, + Html\Field\Concern\CanBeEnclosedByLabel, + Html\Field\Concern\HasClass, Html\Field\Concern\HasError, Html\Field\Concern\HasHint, Html\Field\Concern\HasInputContainer, @@ -55,7 +57,9 @@ */ abstract class AbstractField extends Element { + use CanBeEnclosedByLabel; use HasAttributes; + use HasClass; use HasContainerCollection; use HasError; use HasHint; @@ -71,9 +75,6 @@ abstract class AbstractField extends Element use HasValue; protected array $attributes = []; - protected string $class = ''; - protected bool $classOverride = false; - protected bool $enclosedByLabel = false; private InputInterface $widget; /** @@ -94,40 +95,6 @@ public function __construct( parent::__construct($definitions); } - /** - * Set the `CSS` `HTML` field class attribute. - * - * @param string $value The `CSS` attribute of the widget. - * @param bool $override If `true` the value will be overridden. - * - * @return static A new instance of the current class with the specified class value. - * - * @link https://html.spec.whatwg.org/#classes - */ - public function class(string $value, bool $override = false): static - { - $new = clone $this; - $new->class = $value; - $new->classOverride = $override; - - return $new; - } - - /** - * Set the current instance as being enclosed by a label. - * - * @param bool $value The value to set. - * - * @return self A new instance of of the current class with the specified enclosed by label property. - */ - public function enclosedByLabel(bool $value): self - { - $new = clone $this; - $new->enclosedByLabel = $value; - - return $new; - } - public function input(InputInterface $widget): self { /** @psalm-var array $definitionProperty */ @@ -306,7 +273,7 @@ private function renderField(InputInterface $widget): string ); } - public function renderHintTag(): string + private function renderHintTag(): string { return $this->renderTag($this->hintAttributes, $this->hintContent, $this->hintTag, $this->hintId); } diff --git a/src/Concern/CanBeEnclosedByLabel.php b/src/Concern/CanBeEnclosedByLabel.php new file mode 100644 index 0000000..f995385 --- /dev/null +++ b/src/Concern/CanBeEnclosedByLabel.php @@ -0,0 +1,26 @@ +enclosedByLabel = true; + + return $new; + } +} diff --git a/src/Concern/HasClass.php b/src/Concern/HasClass.php new file mode 100644 index 0000000..76943db --- /dev/null +++ b/src/Concern/HasClass.php @@ -0,0 +1,33 @@ +class = $value; + $new->classOverride = $override; + + return $new; + } +} diff --git a/tests/Concern/CanBeEnclosedByLabelTest.php b/tests/Concern/CanBeEnclosedByLabelTest.php new file mode 100644 index 0000000..d6e3ffb --- /dev/null +++ b/tests/Concern/CanBeEnclosedByLabelTest.php @@ -0,0 +1,19 @@ +assertNotSame($instance, $instance->enclosedByLabel()); + } +} diff --git a/tests/Concern/HasClassTest.php b/tests/Concern/HasClassTest.php new file mode 100644 index 0000000..d578662 --- /dev/null +++ b/tests/Concern/HasClassTest.php @@ -0,0 +1,19 @@ +assertNotSame($instance, $instance->class('')); + } +} diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index db7a946..720d7c8 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -25,4 +25,17 @@ public function testRender(): void Field::widget(new ConfigForm(), 'name')->render() ); } + + public function testRenderWithDefinitions(): void + { + Assert::equalsWithoutLE( + << + + + + HTML, + Field::widget(new ConfigForm(), 'name', ['id()' => ['custom-id']])->render() + ); + } }