Skip to content

Commit

Permalink
FieldsetElement: Propagate decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Oct 17, 2022
1 parent cac0a5a commit 91a5932
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/FormElement/FieldsetElement.php
Expand Up @@ -5,6 +5,8 @@
use ipl\Html\Attribute;
use ipl\Html\Attributes;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecorator;
use ipl\Html\Contract\Wrappable;
use LogicException;
use ReflectionProperty;

Expand Down Expand Up @@ -55,6 +57,19 @@ public function getValueAttribute()
return null;
}

public function prependWrapper(Wrappable $wrapper)
{
// TODO(lippserd): Revise decorator implementation to properly implement decorator propagation
if (
! $this->hasDefaultElementDecorator()
&& $wrapper instanceof FormElementDecorator
) {
$this->setDefaultElementDecorator($wrapper);
}

return parent::prependWrapper($wrapper);
}

protected function onElementRegistered(FormElement $element)
{
$element->getAttributes()->registerAttributeCallback('name', function () use ($element) {
Expand Down
23 changes: 23 additions & 0 deletions tests/FormElement/FieldsetElementTest.php
Expand Up @@ -5,6 +5,7 @@
use ipl\Html\Form;
use ipl\Html\FormElement\FieldsetElement;
use ipl\Tests\Html\TestCase;
use ipl\Tests\Html\TestDummy\SimpleFormElementDecorator;

class FieldsetElementTest extends TestCase
{
Expand Down Expand Up @@ -120,4 +121,26 @@ public function testOriginalElementNames(): void
$outer->getElement('inner')->getElement('test_select')->getName()
);
}

public function testDecoratorPropagation(): void
{
// Order is important because addElement() calls decorate(). Could be fixed.
$fieldset = (new FieldsetElement('test_fieldset'));
(new Form())
->setDefaultElementDecorator(new SimpleFormElementDecorator())
->addElement($fieldset);
$fieldset->addElement('select', 'test_select');

$expected = <<<'HTML'
<div class="simple-decorator">
<fieldset name="test_fieldset">
<div class="simple-decorator">
<select name="test_fieldset[test_select]"></select>
</div>
</fieldset>
</div>
HTML;

$this->assertHtml($expected, $fieldset);
}
}

0 comments on commit 91a5932

Please sign in to comment.