Skip to content

Commit b29b6db

Browse files
MartinMystikJonasf3l1x
authored andcommitted
Button is btn-primary only if it is only button in form
1 parent d71fa5a commit b29b6db

20 files changed

+144
-25
lines changed

src/Rendering/AbstractBootstrapHorizontalRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Nette\Forms\Rendering\DefaultFormRenderer;
66

7-
class AbstractBootstrapHorizontalRenderer extends DefaultFormRenderer
7+
abstract class AbstractBootstrapHorizontalRenderer extends DefaultFormRenderer
88
{
99

1010
/** @var int */

src/Rendering/Bootstrap3HorizontalRenderer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,17 @@ class Bootstrap3HorizontalRenderer extends AbstractBootstrapHorizontalRenderer
6565
*/
6666
public function render(Form $form, $mode = null): string
6767
{
68-
$usedPrimary = false;
69-
7068
$form->getElementPrototype()->setNovalidate(true);
7169

7270
$form->getElementPrototype()->addClass('form-horizontal');
7371

72+
$onlyButton = Helpers::onlyOneButton($form);
73+
7474
foreach ($form->getControls() as $control) {
7575
switch (true) {
7676
case $control instanceof Controls\Button:
7777
if (!Helpers::htmlClassContains($control->getControlPrototype(), 'btn')) {
78-
$control->getControlPrototype()->addClass($usedPrimary === false ? 'btn btn-primary' : 'btn btn-default');
79-
$usedPrimary = true;
78+
$control->getControlPrototype()->addClass($onlyButton ? 'btn btn-primary' : 'btn btn-default');
8079
}
8180

8281
break;

src/Rendering/Bootstrap3InlineRenderer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@ class Bootstrap3InlineRenderer extends DefaultFormRenderer
6666
*/
6767
public function render(Form $form, $mode = null): string
6868
{
69-
$usedPrimary = false;
70-
7169
$form->getElementPrototype()->addClass('form-inline');
7270

71+
$onlyButton = Helpers::onlyOneButton($form);
72+
7373
foreach ($form->getControls() as $control) {
7474
switch (true) {
7575
case $control instanceof Controls\Button:
7676
if (!Helpers::htmlClassContains($control->getControlPrototype(), 'btn')) {
77-
$control->getControlPrototype()->addClass($usedPrimary === false ? 'btn btn-primary' : 'btn btn-default');
78-
$usedPrimary = true;
77+
$control->getControlPrototype()->addClass($onlyButton ? 'btn btn-primary' : 'btn btn-default');
7978
}
8079

8180
break;

src/Rendering/Bootstrap3VerticalRenderer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@ class Bootstrap3VerticalRenderer extends DefaultFormRenderer
6666
*/
6767
public function render(Form $form, $mode = null): string
6868
{
69-
$usedPrimary = false;
70-
7169
$form->getElementPrototype()->setNovalidate(true);
7270

71+
$onlyButton = Helpers::onlyOneButton($form);
72+
7373
foreach ($form->getControls() as $control) {
7474
switch (true) {
7575
case $control instanceof Controls\Button:
7676
if (!Helpers::htmlClassContains($control->getControlPrototype(), 'btn')) {
77-
$control->getControlPrototype()->addClass($usedPrimary === false ? 'btn btn-primary' : 'btn btn-default');
78-
$usedPrimary = true;
77+
$control->getControlPrototype()->addClass($onlyButton ? 'btn btn-primary' : 'btn btn-default');
7978
}
8079

8180
break;

src/Rendering/Bootstrap4HorizontalRenderer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ class Bootstrap4HorizontalRenderer extends AbstractBootstrapHorizontalRenderer
6666
*/
6767
public function render(Form $form, $mode = null): string
6868
{
69-
$usedPrimary = false;
70-
7169
$form->getElementPrototype()->setNovalidate(true);
7270

71+
$onlyButton = Helpers::onlyOneButton($form);
72+
7373
foreach ($form->getControls() as $control) {
7474
if ($control instanceof Controls\BaseControl
7575
&& !($control instanceof Controls\Checkbox)
@@ -81,8 +81,7 @@ public function render(Form $form, $mode = null): string
8181
switch (true) {
8282
case $control instanceof Controls\Button:
8383
if (!Helpers::htmlClassContains($control->getControlPrototype(), 'btn')) {
84-
$control->getControlPrototype()->addClass($usedPrimary === false ? 'btn btn-primary' : 'btn btn-secondary');
85-
$usedPrimary = true;
84+
$control->getControlPrototype()->addClass($onlyButton ? 'btn btn-primary' : 'btn btn-default');
8685
}
8786

8887
break;

src/Rendering/Bootstrap4InlineRenderer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ class Bootstrap4InlineRenderer extends DefaultFormRenderer
6565
*/
6666
public function render(Form $form, $mode = null): string
6767
{
68-
$usedPrimary = false;
69-
7068
$form->getElementPrototype()->addClass('form-inline');
7169

70+
$onlyButton = Helpers::onlyOneButton($form);
71+
7272
foreach ($form->getControls() as $control) {
7373
if ($control instanceof Controls\BaseControl) {
7474
$control->getLabelPrototype()->addClass('col-form-label');
7575
}
7676

7777
switch (true) {
78+
case $control instanceof Controls\Button:
7879
case $control instanceof Controls\Button:
7980
if (!Helpers::htmlClassContains($control->getControlPrototype(), 'btn')) {
80-
$control->getControlPrototype()->addClass($usedPrimary === false ? 'btn btn-primary' : 'btn btn-secondary');
81-
$usedPrimary = true;
81+
$control->getControlPrototype()->addClass($onlyButton ? 'btn btn-primary' : 'btn btn-default');
8282
}
8383

8484
break;

src/Rendering/Bootstrap4VerticalRenderer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ class Bootstrap4VerticalRenderer extends DefaultFormRenderer
6565
*/
6666
public function render(Form $form, $mode = null): string
6767
{
68-
$usedPrimary = false;
69-
7068
$form->getElementPrototype()->setNovalidate(true);
7169

70+
$onlyButton = Helpers::onlyOneButton($form);
71+
7272
foreach ($form->getControls() as $control) {
7373
if ($control instanceof Controls\BaseControl) {
7474
$control->getLabelPrototype()->addClass('col-form-label');
@@ -77,8 +77,7 @@ public function render(Form $form, $mode = null): string
7777
switch (true) {
7878
case $control instanceof Controls\Button:
7979
if (!Helpers::htmlClassContains($control->getControlPrototype(), 'btn')) {
80-
$control->getControlPrototype()->addClass($usedPrimary === false ? 'btn btn-primary' : 'btn btn-secondary');
81-
$usedPrimary = true;
80+
$control->getControlPrototype()->addClass($onlyButton ? 'btn btn-primary' : 'btn btn-default');
8281
}
8382

8483
break;

src/Rendering/Helpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Contributte\Forms\Rendering;
44

5+
use Nette\Forms\Controls\Button;
6+
use Nette\Forms\Form;
57
use Nette\Utils\Html;
68

79
class Helpers
@@ -18,4 +20,16 @@ public static function htmlClassContains(Html $html, string $contains): bool
1820
return $class !== null && mb_strpos($class, $contains) !== false;
1921
}
2022

23+
public static function onlyOneButton(Form $form): bool
24+
{
25+
$count = 0;
26+
foreach ($form->getControls() as $control) {
27+
if ($control instanceof Button) {
28+
$count++;
29+
}
30+
}
31+
32+
return $count === 1;
33+
}
34+
2135
}

tests/cases/Rendering/Bootstrap3HorizontalRenderer.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ test(function (): void {
3232
$renderer->setColumns(2, 10);
3333
Assert::matchFile(__DIR__ . '/expected/bootstrap3horizontal.cols.html', $renderer->render($form));
3434
});
35+
36+
test(function (): void {
37+
$form = new Form();
38+
$form->addSubmit('button1', 'Button 1')->setHtmlAttribute('class', 'my-button');
39+
$form->addSubmit('button2', 'Button 2')->setHtmlAttribute('class', 'btn btn-danger');
40+
$renderer = new Bootstrap3HorizontalRenderer();
41+
Assert::matchFile(__DIR__ . '/expected/bootstrap3horizontal.buttons.html', $renderer->render($form));
42+
});

tests/cases/Rendering/Bootstrap3InlineRenderer.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ test(function (): void {
2020
$renderer = new Bootstrap3InlineRenderer();
2121
Assert::matchFile(__DIR__ . '/expected/bootstrap3inline.html', $renderer->render($form));
2222
});
23+
24+
test(function (): void {
25+
$form = new Form();
26+
$form->addSubmit('button1', 'Button 1')->setHtmlAttribute('class', 'my-button');
27+
$form->addSubmit('button2', 'Button 2')->setHtmlAttribute('class', 'btn btn-danger');
28+
$renderer = new Bootstrap3InlineRenderer();
29+
Assert::matchFile(__DIR__ . '/expected/bootstrap3inline.buttons.html', $renderer->render($form));
30+
});

0 commit comments

Comments
 (0)