Skip to content

Commit 6751641

Browse files
sukhwinder33445nilmerg
authored andcommitted
Introduce PrimaryButtonDecorator
1 parent d6648b4 commit 6751641

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

src/Compat/CompatForm.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ipl\Html\HtmlDocument;
1414
use ipl\Html\HtmlString;
1515
use ipl\I18n\Translation;
16+
use ipl\Web\Compat\FormDecorator\PrimaryButtonDecorator;
1617
use ipl\Web\FormDecorator\IcingaFormDecorator;
1718
use ipl\Web\Compat\FormDecorator\LabelDecorator;
1819

@@ -42,8 +43,12 @@ public function applyDefaultElementDecorators(): static
4243
['ipl\\Web\\Compat\\FormDecorator', 'Decorator']
4344
]);
4445

45-
$this->getDecorators()->addDecorator('Required', LabelDecorator::class, [
46-
'uniqueName' => fn(string $name) => Icinga::app()->getRequest()->protectId($name)
46+
$this->getDecorators()->addDecorators([
47+
'PrimaryButton' => new PrimaryButtonDecorator(),
48+
'Required' => [
49+
'name' => LabelDecorator::class,
50+
'options' => ['uniqueName' => fn(string $name) => Icinga::app()->getRequest()->protectId($name)]
51+
]
4752
]);
4853

4954
$this->setDefaultElementDecorators([
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace ipl\Web\Compat\FormDecorator;
4+
5+
use ipl\Html\Attribute;
6+
use ipl\Html\Contract\DecorationResult;
7+
use ipl\Html\Contract\Form;
8+
use ipl\Html\Contract\FormDecoration;
9+
use ipl\Html\Contract\FormSubmitElement;
10+
11+
class PrimaryButtonDecorator implements FormDecoration
12+
{
13+
public function decorateForm(DecorationResult $result, Form $form): void
14+
{
15+
foreach ($form->getElements() as $element) {
16+
if ($element instanceof FormSubmitElement) {
17+
$element->getAttributes()->addAttribute(new Attribute('class', 'btn-primary'));
18+
}
19+
}
20+
}
21+
}

tests/Compat/CompatFormTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function testFormControlsDecoration(): void
314314
$expected = <<<'HTML'
315315
<form class="icinga-form icinga-controls" method="POST">
316316
<div class="control-group form-controls">
317-
<input name="foo" type="submit" value="Submit Form"/>
317+
<input name="foo" type="submit" class="btn-primary" value="Submit Form"/>
318318
</div>
319319
</form>
320320
HTML;
@@ -378,7 +378,7 @@ public function testMethodApplyDefaultElementDecorators(): void
378378
<span class="sr-only" id="desc_fooBar-id">Fieldset Description</span>
379379
</div>
380380
<div class="control-group form-controls">
381-
<input name="submit_form" type="submit" value="Submit Form"/>
381+
<input name="submit_form" type="submit" class="btn-primary" value="Submit Form"/>
382382
</div>
383383
</form>
384384
HTML;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace ipl\Tests\Web\Compat\FormDecorator;
4+
5+
use ipl\Html\FormDecoration\FormElementDecorationResult;
6+
use ipl\Tests\Web\TestCase;
7+
use ipl\Web\Compat\CompatForm;
8+
use ipl\Web\Compat\FormDecorator\PrimaryButtonDecorator;
9+
10+
class PrimaryButtonDecoratorTest extends TestCase
11+
{
12+
protected PrimaryButtonDecorator $decorator;
13+
14+
public function setUp(): void
15+
{
16+
$this->decorator = new PrimaryButtonDecorator();
17+
}
18+
19+
public function testEveryButtonIsMarkedAsPrimaryButton(): void
20+
{
21+
$form = (new CompatForm())
22+
->addElement('submit','btn-1')
23+
->addElement('submit', 'btn-2', ['class' => 'test'])
24+
->addElement('submit', 'btn-3', ['class' => 'btn-remove'])
25+
->addElement('submit', 'btn-4', ['class' => 'btn-primary']);
26+
27+
$this->decorator->decorateForm(new FormElementDecorationResult(), $form);
28+
29+
$this->assertSame(
30+
'class="btn-primary"',
31+
$form->getElement('btn-1')->getAttributes()->get('class')->render()
32+
);
33+
34+
$this->assertSame(
35+
'class="test btn-primary"',
36+
$form->getElement('btn-2')->getAttributes()->get('class')->render()
37+
);
38+
39+
$this->assertSame(
40+
'class="btn-remove btn-primary"',
41+
$form->getElement('btn-3')->getAttributes()->get('class')->render()
42+
);
43+
44+
$this->assertSame(
45+
'class="btn-primary btn-primary"',
46+
$form->getElement('btn-4')->getAttributes()->get('class')->render()
47+
);
48+
}
49+
50+
public function testElementsOtherThanSubmitButtonsAreIgnored(): void
51+
{
52+
$form = (new CompatForm())
53+
->addElement('text', 'username', ['class' => 'test'])
54+
->addElement('password', 'pwd', ['class' => 'test']);
55+
56+
$this->decorator->decorateForm(new FormElementDecorationResult(), $form);
57+
58+
$this->assertSame('class="test"', $form->getElement('username')->getAttributes()->get('class')->render());
59+
$this->assertSame('class="test"', $form->getElement('pwd')->getAttributes()->get('class')->render());
60+
}
61+
}

0 commit comments

Comments
 (0)