From d8331835555750f758ecd709eb4fbabc4afd1c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=A2=D0=B0=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=BE=D0=B2?= Date: Fri, 13 Aug 2021 05:46:42 +0500 Subject: [PATCH 1/7] fix BaseHtml::activeStaticControl value type --- src/BaseHtml.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseHtml.php b/src/BaseHtml.php index e307a1d..d72e242 100644 --- a/src/BaseHtml.php +++ b/src/BaseHtml.php @@ -73,7 +73,7 @@ abstract class BaseHtml extends \yii\helpers\Html $value = static::getAttributeValue($model, $attribute); } - return static::staticControl($value, $options); + return static::staticControl((string)$value, $options); } /** From c4013bfde7adc26bc07ea0e69d4e5996f032ba2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=A2=D0=B0=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=BE=D0=B2?= Date: Sat, 14 Aug 2021 06:24:19 +0500 Subject: [PATCH 2/7] fix default ActiveField::hintOptions --- src/ActiveField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveField.php b/src/ActiveField.php index 8cc8655..261d9c9 100644 --- a/src/ActiveField.php +++ b/src/ActiveField.php @@ -156,7 +156,7 @@ class ActiveField extends \yii\widgets\ActiveField /** * {@inheritdoc} */ - public $hintOptions = ['class' => ['widget' => 'form-text', 'text-muted'], 'tag' => 'small']; + public $hintOptions = ['class' => ['widget' => 'form-text', 'text-muted'], 'tag' => 'div']; /** * @var null|array CSS grid classes for horizontal layout. This must be an array with these keys: * - 'offset' the offset grid class to append to the wrapper if no label is rendered From a65c89425e3ad5280e6f6234238412052bd56e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=A2=D0=B0=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=BE=D0=B2?= Date: Sat, 14 Aug 2021 19:52:47 +0500 Subject: [PATCH 3/7] fix default ActiveField::hintOptions --- src/ActiveField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveField.php b/src/ActiveField.php index 261d9c9..353df0d 100644 --- a/src/ActiveField.php +++ b/src/ActiveField.php @@ -494,7 +494,7 @@ class ActiveField extends \yii\widgets\ActiveField { $config = [ 'hintOptions' => [ - 'tag' => 'small', + 'tag' => 'div', 'class' => ['form-text', 'text-muted'], ], 'errorOptions' => [ From 9fc991c2dbfbf651856e6d7df3ecdea23b3e82be Mon Sep 17 00:00:00 2001 From: simialbi Date: Mon, 16 Aug 2021 09:09:10 +0200 Subject: [PATCH 4/7] Update test to div rendering --- tests/ActiveFormTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ActiveFormTest.php b/tests/ActiveFormTest.php index 7782109..4abfd3f 100644 --- a/tests/ActiveFormTest.php +++ b/tests/ActiveFormTest.php @@ -291,7 +291,7 @@ HTML;
-Your username must be at least 4 characters long +
Your username must be at least 4 characters long
HTML; @@ -299,7 +299,7 @@ HTML;
-Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji. +
Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
HTML; @@ -330,7 +330,7 @@ HTML;
-Your username must be at least 4 characters long +
Your username must be at least 4 characters long
Username cannot be blank.
HTML; From 68be634fb9c857db5b0f744496f5e5051cf2c96d Mon Sep 17 00:00:00 2001 From: simialbi Date: Mon, 16 Aug 2021 09:11:46 +0200 Subject: [PATCH 5/7] Updated CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f3fdf1..fc1895e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.2 under development ----------------------- -- no changes in this release. +- Bug #9: fixed default ActiveField::hintOptions (dicrtarasov) 2.0.1 August 11, 2021 From 7c6dbc295dabbb0999e758d3de73bcf062373e2a Mon Sep 17 00:00:00 2001 From: Simon Karlen Date: Mon, 16 Aug 2021 09:48:14 +0200 Subject: [PATCH 6/7] added test, updated changelog, removed unnecessary typecasting (already done by method definition) --- CHANGELOG.md | 2 +- src/BaseHtml.php | 5 ++-- tests/ActiveFormTest.php | 52 ++++++++++++++++++++++++++++++++++++++++ tests/data/User.php | 3 +++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f3fdf1..8641e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.2 under development ----------------------- -- no changes in this release. +- Bug #6: yii\bootstrap5\BaseHtml::staticControl(): Argument #1 ($value) must be of type string, int given (dicrtarasov) 2.0.1 August 11, 2021 diff --git a/src/BaseHtml.php b/src/BaseHtml.php index d72e242..1bb7213 100644 --- a/src/BaseHtml.php +++ b/src/BaseHtml.php @@ -49,7 +49,6 @@ abstract class BaseHtml extends \yii\helpers\Html public static function staticControl(string $value, array $options = []): string { static::addCssClass($options, 'form-control-plaintext'); - $value = (string)$value; $options['readonly'] = true; return static::input('text', null, $value, $options); @@ -64,7 +63,7 @@ abstract class BaseHtml extends \yii\helpers\Html * @return string generated HTML * @see staticControl() */ - public static function activeStaticControl(Model $model, string $attribute, $options = []): string + public static function activeStaticControl(Model $model, string $attribute, array $options = []): string { if (isset($options['value'])) { $value = $options['value']; @@ -77,7 +76,7 @@ abstract class BaseHtml extends \yii\helpers\Html } /** - * {@inheritdoc} + * {@inheritDoc} */ public static function radioList($name, $selection = null, $items = [], $options = []): string { diff --git a/tests/ActiveFormTest.php b/tests/ActiveFormTest.php index 7782109..7e0ad82 100644 --- a/tests/ActiveFormTest.php +++ b/tests/ActiveFormTest.php @@ -310,6 +310,58 @@ HTML; $this->assertContainsWithoutLE($expected4, $out); } + public function testStaticControlRendering() + { + ActiveForm::$counter = 0; + ob_start(); + $model = new User(); + $model->setAttributes([ + 'id' => 1, + 'firstName' => 'John', + 'lastName' => 'Doe' + ]); + $form = ActiveForm::begin([ + 'action' => '/some-action', + 'layout' => ActiveForm::LAYOUT_DEFAULT + ]); + echo $form->field($model, 'id')->staticControl(); + echo $form->field($model, 'firstName')->staticControl(); + echo $form->field($model, 'lastName')->staticControl(); + echo $form->field($model, 'username')->staticControl(); + ActiveForm::end(); + $out = ob_get_clean(); + + $expected = << + + + +
+ +HTML; + + $expected2 = << + + + +
+ +HTML; + $expected3 = << + + + +
+ +HTML; + + $this->assertContainsWithoutLE($expected, $out); + $this->assertContainsWithoutLE($expected2, $out); + $this->assertContainsWithoutLE($expected3, $out); + } + /** * Fixes #128 * @see https://github.com/yiisoft/yii2-bootstrap5/issues/128 diff --git a/tests/data/User.php b/tests/data/User.php index 0e068c4..ccda0e6 100644 --- a/tests/data/User.php +++ b/tests/data/User.php @@ -11,6 +11,7 @@ use yii\base\Model; class User extends Model { + public $id; public $firstName; public $lastName; public $username; @@ -22,6 +23,8 @@ class User extends Model public function rules() { return [ + ['id', 'integer'], + [['firstName', 'lastName'], 'string'], ['username', 'string', 'min' => 4], ['password', 'string', 'min' => 8, 'max' => '20'], [['username', 'password'], 'required'] From 80966912b870ab809c2ca9ad7bbffe8f409aa1e7 Mon Sep 17 00:00:00 2001 From: Simon Karlen Date: Wed, 18 Aug 2021 15:41:26 +0200 Subject: [PATCH 7/7] fixed #11 --- CHANGELOG.md | 1 + docs/guide-de/migrating-yii2-bootstrap.md | 7 +++-- docs/guide/migrating-yii2-bootstrap.md | 3 +++ src/Alert.php | 7 +++-- src/Modal.php | 13 +++++---- src/Offcanvas.php | 13 +++++---- src/Toast.php | 31 ++++++++++++++-------- tests/AlertTest.php | 32 +++++++++++++++++++++++ tests/ToastTest.php | 8 +++--- 9 files changed, 86 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61410a5..3429f78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.2 under development ----------------------- +- Enh #11: Brought back close button api (simialbi) - Bug #6: yii\bootstrap5\BaseHtml::staticControl(): Argument #1 ($value) must be of type string, int given (dicrtarasov) - Bug #9: fixed default ActiveField::hintOptions (dicrtarasov) diff --git a/docs/guide-de/migrating-yii2-bootstrap.md b/docs/guide-de/migrating-yii2-bootstrap.md index 3a7eaee..bf7bace 100644 --- a/docs/guide-de/migrating-yii2-bootstrap.md +++ b/docs/guide-de/migrating-yii2-bootstrap.md @@ -2,12 +2,15 @@ Migration von yii2-bootstrap4 ============================= yii2-bootstrap4 ist eine komplette Überarbeitung des Projekts (siehe den Bootstrap 4 von Bootstrap 3 Migrationsguide). -Die grössten Änderungen finden Sie hier zusammengefasst: +Die größten Änderungen finden Sie hier zusammengefasst: ## Allgemein * Der Namespace ist nun `yii\bootstrap5` anstatt `yii\bootstrap4` * Die PHP Kompatibilität **ist beschränkt auf** `>=7.0` +* Der "Schließen"-Button von Widgets wie [[yii\bootstrap5\Alert|Alert]] oder [[yii\bootstrap5|Modal|Modal]] wird neu + via CSS dargestellt und hat keinen Inhalt mehr. Es ist daher beim Überschreiben sicherzustellen, dass die `btn-close` + Klasse entfernt wird und die benötigten Stile manuell gesetzt werden. ## Widgets / Klassen @@ -37,7 +40,7 @@ Es gibt eine neue Konstante [[yii\bootstrap5\ActiveForm::LAYOUT_FLOATING]]. Sie ### NavBar Es gibt nun die Möglichkeit der Erstellung einer [Offcanvas Navbar](https://getbootstrap.com/docs/5.1/components/navbar/#offcanvas). -Dies ist zu erreichen indem man die Eigentschaft `$collapseOptions` des Widgets [[yii\bootstrap5\NavBar|Navbar]] auf `false` +Dies ist zu erreichen, indem man die Eigenschaft `$collapseOptions` des Widgets [[yii\bootstrap5\NavBar|Navbar]] auf `false` und die Eigenschaft `$offcanvasOptions` auf ein array setzt (auch wenn leer). ### Tabs diff --git a/docs/guide/migrating-yii2-bootstrap.md b/docs/guide/migrating-yii2-bootstrap.md index f593f1f..b3eafd8 100644 --- a/docs/guide/migrating-yii2-bootstrap.md +++ b/docs/guide/migrating-yii2-bootstrap.md @@ -8,6 +8,9 @@ The most notable changes are summarized below: * The namespace is `yii\bootstrap5` instead of `yii\bootstrap4` * The php compatibility **is limited to** `>=7.0` +* The close buttons of widgets like [[yii\bootstrap5\Alert|Alert]] or [[yii\bootstrap5|Modal|Modal]] now gets rendered + via CSS and does not have any content anymore. So be sure to remove `btn-close` class and set appropriate styles yourself + if you override it. ## Widgets / Classes diff --git a/src/Alert.php b/src/Alert.php index 6052907..03068b6 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -108,8 +108,12 @@ class Alert extends Widget { if (($closeButton = $this->closeButton) !== false) { $tag = ArrayHelper::remove($closeButton, 'tag', 'button'); + $label = ArrayHelper::remove($closeButton, 'label', ''); + if ($tag === 'button' && !isset($closeButton['type'])) { + $closeButton['type'] = 'button'; + } - return Html::tag($tag, '', $closeButton); + return Html::tag($tag, $label, $closeButton); } else { return null; } @@ -125,7 +129,6 @@ class Alert extends Widget if ($this->closeButton !== false) { $this->closeButton = array_merge([ - 'type' => 'button', 'class' => ['widget' => 'btn-close'], 'data' => ['bs-dismiss' => 'alert'], 'aria' => ['label' => 'Close'] diff --git a/src/Modal.php b/src/Modal.php index 9276d27..342f0bc 100644 --- a/src/Modal.php +++ b/src/Modal.php @@ -252,8 +252,12 @@ class Modal extends Widget { if (($closeButton = $this->closeButton) !== false) { $tag = ArrayHelper::remove($closeButton, 'tag', 'button'); + $label = ArrayHelper::remove($closeButton, 'label', ''); + if ($tag === 'button' && !isset($closeButton['type'])) { + $closeButton['type'] = 'button'; + } - return Html::tag($tag, '', $closeButton); + return Html::tag($tag, $label, $closeButton); } else { return null; } @@ -284,10 +288,9 @@ class Modal extends Widget if ($this->closeButton !== false) { $this->closeButton = array_merge([ - 'data-bs-dismiss' => 'modal', - 'class' => 'btn-close', - 'type' => 'button', - 'aria-label' => 'Close' + 'class' => ['widget' => 'btn-close'], + 'data' => ['bs-dismiss' => 'modal'], + 'aria' => ['label' => 'Close'] ], $this->closeButton); } diff --git a/src/Offcanvas.php b/src/Offcanvas.php index 2e24f67..fb5f7f6 100644 --- a/src/Offcanvas.php +++ b/src/Offcanvas.php @@ -204,8 +204,12 @@ class Offcanvas extends Widget { if (($closeButton = $this->closeButton) !== false) { $tag = ArrayHelper::remove($closeButton, 'tag', 'button'); + $label = ArrayHelper::remove($closeButton, 'label', ''); + if ($tag === 'button' && !isset($closeButton['type'])) { + $closeButton['type'] = 'button'; + } - return Html::tag($tag, '', $closeButton); + return Html::tag($tag, $label, $closeButton); } else { return null; } @@ -233,10 +237,9 @@ class Offcanvas extends Widget if ($this->closeButton !== false) { $this->closeButton = array_merge([ - 'data-bs-dismiss' => 'offcanvas', - 'class' => 'btn-close text-reset', - 'type' => 'button', - 'aria-label' => 'Close' + 'class' => ['widget' => 'btn-close text-reset'], + 'data' => ['bs-dismiss' => 'offcanvas'], + 'aria' => ['label' => 'Close'] ], $this->closeButton); } diff --git a/src/Toast.php b/src/Toast.php index 9b74d23..2ba5abd 100644 --- a/src/Toast.php +++ b/src/Toast.php @@ -64,7 +64,7 @@ class Toast extends Widget */ public $dateTime = false; /** - * @var array the options for rendering the close button tag. + * @var array|false the options for rendering the close button tag. * The close button is displayed in the header of the toast. Clicking on the button will hide the toast. * * The following special options are supported: @@ -180,13 +180,21 @@ class Toast extends Widget /** * Renders the close button. - * @return string the rendering result + * @return string|null the rendering result */ - protected function renderCloseButton(): string + protected function renderCloseButton() { - $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button'); + if (($closeButton = $this->closeButton) !== false) { + $tag = ArrayHelper::remove($closeButton, 'tag', 'button'); + $label = ArrayHelper::remove($closeButton, 'label', ''); + if ($tag === 'button' && !isset($closeButton['type'])) { + $closeButton['type'] = 'button'; + } - return Html::tag($tag, '', $this->closeButton); + return Html::tag($tag, $label, $closeButton); + } else { + return null; + } } /** @@ -197,12 +205,13 @@ class Toast extends Widget { Html::addCssClass($this->options, ['widget' => 'toast']); - $this->closeButton = array_merge([ - 'aria' => ['label' => 'Close'], - 'data' => ['bs-dismiss' => 'toast'], - 'class' => ['widget' => 'btn-close'], - 'type' => 'button', - ], $this->closeButton); + if ($this->closeButton !== false) { + $this->closeButton = array_merge([ + 'class' => ['widget' => 'btn-close'], + 'data' => ['bs-dismiss' => 'toast'], + 'aria' => ['label' => 'Close'] + ], $this->closeButton); + } if (!isset($this->options['role'])) { $this->options['role'] = 'alert'; diff --git a/tests/AlertTest.php b/tests/AlertTest.php index 38abe3a..87bd178 100644 --- a/tests/AlertTest.php +++ b/tests/AlertTest.php @@ -48,6 +48,38 @@ HTML; Message1 + +HTML; + $this->assertEqualsWithoutLE($expectedHtml, $html); + } + + /** + * @see https://github.com/yiisoft/yii2-bootstrap5/issues/11 + */ + public function testDismissibleAlertCustomButton() + { + Alert::$counter = 0; + $html = Alert::widget([ + 'body' => "Low Blow: Bob Loblaw's Law Blog Lobs Law Bomb", + 'options' => ['class' => 'alert-warning'], + 'closeButton' => [ + 'label' => 'Dismiss', + 'tag' => 'a', + 'class' => ['widget' => 'btn btn-outline-warning'], + 'style' => [ + 'position' => 'absolute', + 'top' => '.5rem', + 'right' => '.5rem' + ], + ] + ]); + + $expectedHtml = << + +Low Blow: Bob Loblaw's Law Blog Lobs Law Bomb +Dismiss + HTML; $this->assertEqualsWithoutLE($expectedHtml, $html); diff --git a/tests/ToastTest.php b/tests/ToastTest.php index bb160f9..5352f91 100644 --- a/tests/ToastTest.php +++ b/tests/ToastTest.php @@ -20,7 +20,7 @@ class ToastTest extends TestCase