Merge branch 'master' into dicr-fix-register-plugin

This commit is contained in:
simialbi 2021-08-20 08:25:00 +02:00 committed by GitHub
commit 51744dc4ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 151 additions and 38 deletions

View File

@ -5,6 +5,9 @@ Yii Framework 2 bootstrap5 extension Change Log
-----------------------
- Bug #5: BootstrapWidgetTrait::registerPlugin do nothing if no clientOptions is provided (dicrtarasov)
- 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)
2.0.1 August 11, 2021

View File

@ -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

View File

@ -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

View File

@ -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
@ -494,7 +494,7 @@ class ActiveField extends \yii\widgets\ActiveField
{
$config = [
'hintOptions' => [
'tag' => 'small',
'tag' => 'div',
'class' => ['form-text', 'text-muted'],
],
'errorOptions' => [

View File

@ -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']

View File

@ -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'];
@ -73,11 +72,11 @@ abstract class BaseHtml extends \yii\helpers\Html
$value = static::getAttributeValue($model, $attribute);
}
return static::staticControl($value, $options);
return static::staticControl((string)$value, $options);
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
public static function radioList($name, $selection = null, $items = [], $options = []): string
{

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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';

View File

@ -291,7 +291,7 @@ HTML;
<div class="mb-3 field-user-username required">
<label class="form-label" for="user-username">Username</label>
<input type="text" id="user-username" class="form-control" name="User[username]" aria-required="true">
<small class="form-text text-muted">Your username must be at least 4 characters long</small>
<div class="form-text text-muted">Your username must be at least 4 characters long</div>
<div class="invalid-feedback"></div>
</div>
HTML;
@ -299,7 +299,7 @@ HTML;
<div class="mb-3 field-user-password required">
<label class="form-label" for="user-password">Password</label>
<input type="password" id="user-password" class="form-control" name="User[password]" aria-required="true">
<small class="form-text text-muted">Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.</small>
<div class="form-text text-muted">Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.</div>
<div class="invalid-feedback"></div>
</div>
HTML;
@ -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
<div class="mb-3 field-user-id">
<label class="form-label" for="user-id">Id</label>
<input type="text" class="form-control-plaintext" value="1" readonly>
<div class="invalid-feedback"></div>
</div>
HTML;
$expected2 = <<<HTML
<div class="mb-3 field-user-firstname">
<label class="form-label" for="user-firstname">First Name</label>
<input type="text" class="form-control-plaintext" value="John" readonly>
<div class="invalid-feedback"></div>
</div>
HTML;
$expected3 = <<<HTML
<div class="mb-3 field-user-lastname">
<label class="form-label" for="user-lastname">Last Name</label>
<input type="text" class="form-control-plaintext" value="Doe" readonly>
<div class="invalid-feedback"></div>
</div>
HTML;
$this->assertContainsWithoutLE($expected, $out);
$this->assertContainsWithoutLE($expected2, $out);
$this->assertContainsWithoutLE($expected3, $out);
}
/**
* Fixes #128
* @see https://github.com/yiisoft/yii2-bootstrap5/issues/128
@ -330,7 +382,7 @@ HTML;
<div class="mb-3 field-user-username required">
<label class="form-label" for="user-username">Username</label>
<input type="text" id="user-username" class="form-control is-invalid" name="User[username]" aria-required="true" aria-invalid="true">
<small class="form-text text-muted">Your username must be at least 4 characters long</small>
<div class="form-text text-muted">Your username must be at least 4 characters long</div>
<div class="invalid-feedback">Username cannot be blank.</div>
</div>
HTML;

View File

@ -48,6 +48,38 @@ HTML;
Message1
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
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 = <<<HTML
<div id="w0" class="alert-warning alert alert-dismissible" role="alert">
Low Blow: Bob Loblaw's Law Blog Lobs Law Bomb
<a class="btn btn-outline-warning" data-bs-dismiss="alert" aria-label="Close" style="position: absolute; top: .5rem; right: .5rem;">Dismiss</a>
</div>
HTML;
$this->assertEqualsWithoutLE($expectedHtml, $html);

View File

@ -23,7 +23,7 @@ class ToastTest extends TestCase
<div id="w0" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto"></strong>
<button type="button" class="btn-close" aria-label="Close" data-bs-dismiss="toast"></button>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body test" style="text-align: center;">
@ -55,7 +55,7 @@ HTML;
<div class="toast-header">
<strong class="me-auto">Toast title</strong>
<small class="text-muted">a minute ago</small>
<button type="button" class="btn-close" aria-label="Close" data-bs-dismiss="toast"></button>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Woohoo, you're reading this text in a toast!
@ -81,7 +81,7 @@ HTML;
<div class="toast-header">
<strong class="me-auto">Toast title</strong>
<small class="toast-date-time text-muted" style="text-align: right;">a minute ago</small>
<button type="button" class="btn-close" aria-label="Close" data-bs-dismiss="toast"></button>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
@ -105,7 +105,7 @@ HTML;
<div id="w0" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<h5 class="me-auto" style="text-align: left;">Toast title</h5>
<button type="button" class="btn-close" aria-label="Close" data-bs-dismiss="toast"></button>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">

View File

@ -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']