diff --git a/CHANGELOG.md b/CHANGELOG.md index 662221fb..07496814 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - New #388: Add `beforeRadio()` and `afterRadio()` methods to `RadioList` field (@vjik) - Enh #386: Bump minimal `yiisoft/html` version to `^4.2` (@vjik) - Enh #392: Move `.meta-storm.xml` to the project root (@Mister-42) +- New #399: Add `$themed` parameter to `ButtonGroup::buttonsData()` method (@Mister-42) ## 1.5.2 March 20, 2026 diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 709a827e..13b379f3 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -4,11 +4,17 @@ namespace Yiisoft\Form\Field; +use InvalidArgumentException; use Yiisoft\Form\Field\Base\ButtonField; use Yiisoft\Form\Field\Base\PartsField; +use Yiisoft\Form\Theme\ThemeContainer; use Yiisoft\Html\Tag\Button as ButtonTag; use Yiisoft\Html\Widget\ButtonGroup as ButtonGroupWidget; +use function array_slice; +use function is_array; +use function strtolower; + /** * Represents a button group widget. * @@ -48,11 +54,59 @@ public function buttons(ButtonTag|ButtonField ...$buttons): self * ] * ``` * @param bool $encode Whether button content should be HTML-encoded. + * @param bool $themed When true, creates buttons via {@see ButtonField} instances + * ({@see Button}, {@see ResetButton}, {@see SubmitButton}) instead of raw tag buttons. + * The `type` attribute determines which subclass is used: + * `reset` → {@see ResetButton}, `submit` → {@see SubmitButton}, default → {@see Button}. + * If label is null, the content from the theme is used. + * Theme styling only applies when the host application has configured a theme via + * {@see ThemeContainer}. Without a configured theme, the output is identical to the + * default (non-themed) behavior. + * + * In a future major version, the default value of this parameter will change to `true`. */ - public function buttonsData(array $data, bool $encode = true): self + public function buttonsData(array $data, bool $encode = true, bool $themed = false): self { $new = clone $this; - $new->widget = $this->widget->buttonsData($data, $encode); + + if (!$themed) { + $new->widget = $this->widget->buttonsData($data, $encode); + return $new; + } + + $buttonTags = []; + foreach ($data as $item) { + if (!is_array($item)) { + throw new InvalidArgumentException( + 'Invalid buttons data. A data row must be array with label as first element ' + . 'and additional name-value pairs as attributes of button.', + ); + } + + $label = $item[0] ?? null; + $attributes = array_slice($item, 1, null, true); + + $type = strtolower((string) ($attributes['type'] ?? 'button')); + unset($attributes['type']); + + $buttonWidget = match ($type) { + 'reset' => ResetButton::widget(), + 'submit' => SubmitButton::widget(), + default => Button::widget(), + }; + + if ($label !== null) { + $buttonWidget = $buttonWidget->content((string) $label); + } + + $buttonTags[] = $buttonWidget + ->encodeContent(false) + ->addButtonAttributes($attributes) + ->getButton() + ->encode($encode); + } + + $new->widget = $this->widget->buttons(...$buttonTags); return $new; } diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 1bca1e14..d452b5b2 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Form\Tests\Field; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Yiisoft\Form\Field\Button; use Yiisoft\Form\Field\ButtonGroup; @@ -297,12 +298,235 @@ public function testButtonsWithFieldButtonCustomized(): void $this->assertSame($expected, $result); } + public function testButtonsDataThemed(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + ['Reset', 'type' => 'reset', 'class' => 'default'], + ['Send >', 'type' => 'submit', 'class' => 'primary'], + ], themed: true) + ->render(); + + $expected = << + + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedDefaultType(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + ['Click', 'class' => 'btn'], + ], themed: true) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedCaseInsensitiveType(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + ['Reset', 'type' => 'RESET'], + ['Send', 'type' => 'Submit'], + ], themed: true) + ->render(); + + $expected = << + + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedIntegerType(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + ['Click', 'type' => 123], + ], themed: true) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedNoEncode(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + ['Bold', 'type' => 'submit'], + ], false, themed: true) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedEmpty(): void + { + $result = ButtonGroup::widget() + ->buttonsData([], themed: true) + ->render(); + + $expected = << + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedIntegerLabel(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + [0, 'type' => 'submit'], + ], themed: true) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedWithTheme(): void + { + ThemeContainer::initialize( + [ + 'default' => [ + 'fieldConfigs' => [ + ButtonGroup::class => [ + 'containerClass()' => ['btn-toolbar justify-content-end'], + ], + Button::class => [ + 'content()' => ['Click'], + ], + ResetButton::class => [ + 'buttonClass()' => ['btn', 'btn-secondary'], + 'content()' => ['Reset'], + ], + SubmitButton::class => [ + 'buttonClass()' => ['btn', 'btn-primary'], + 'content()' => ['Submit'], + ], + ], + ], + ], + 'default', + ); + + $result = Field::ButtonGroup() + ->buttonsData([ + [], + ['type' => 'button'], + [null, 'type' => 'reset'], + ['Send', 'type' => 'submit'], + ], themed: true) + ->render(); + + $expected = << + + + + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataDefaultNotThemedWithTheme(): void + { + ThemeContainer::initialize( + [ + 'default' => [ + 'fieldConfigs' => [ + ButtonGroup::class => [ + 'containerClass()' => ['btn-toolbar justify-content-end'], + ], + ResetButton::class => [ + 'buttonClass()' => ['btn', 'btn-secondary'], + 'content()' => ['Reset'], + ], + SubmitButton::class => [ + 'buttonClass()' => ['btn', 'btn-primary'], + 'content()' => ['Submit'], + ], + ], + ], + ], + 'default', + ); + + $result = ButtonGroup::widget() + ->buttonsData([ + ['Reset', 'type' => 'reset'], + ['Send', 'type' => 'submit'], + ]) + ->render(); + + $expected = << + + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedThrowsExceptionForNonArrayItem(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Invalid buttons data. A data row must be array with label as first element ' + . 'and additional name-value pairs as attributes of button.', + ); + ButtonGroup::widget() + ->buttonsData([ + 'invalid', + ['Send', 'type' => 'submit'], + ], themed: true) + ->render(); + } + public function testImmutability(): void { $field = ButtonGroup::widget(); $this->assertNotSame($field, $field->buttons()); $this->assertNotSame($field, $field->buttonsData([])); + $this->assertNotSame($field, $field->buttonsData([], themed: true)); $this->assertNotSame($field, $field->buttonAttributes([])); $this->assertNotSame($field, $field->addButtonAttributes([])); $this->assertNotSame($field, $field->disabled());