From e4582bdde9d49e029a010e896fb02126fb1f6f86 Mon Sep 17 00:00:00 2001 From: Anton Date: Sat, 23 Apr 2022 02:47:47 +0300 Subject: [PATCH 1/4] Update BaseHtml Replace PHPDoc of `BaseHtml::$normalizeClassAttribute` Add inline mode in `BaseHtml::checkboxList()` Add inline mode in `BaseHtml::radioList()` --- src/BaseHtml.php | 77 ++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/src/BaseHtml.php b/src/BaseHtml.php index 1baf6df..2081786 100644 --- a/src/BaseHtml.php +++ b/src/BaseHtml.php @@ -28,14 +28,10 @@ abstract class BaseHtml extends \yii\helpers\Html */ public static $autoIdPrefix = 'i'; /** - * @var bool whether to removes duplicate class names in tag attribute `class` (fix strange yii2 behavior since 2.0.44) - * @see mergeCssClasses() - * @see renderTagAttributes() - * @since 2.0.3 + * @inheritDoc */ public static $normalizeClassAttribute = true; - /** * Renders Bootstrap static form control. * @@ -56,6 +52,7 @@ abstract class BaseHtml extends \yii\helpers\Html /** * Generates a Bootstrap static form control for the given model attribute. + * * @param Model $model the model object. * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format * about attribute expression. @@ -76,24 +73,33 @@ abstract class BaseHtml extends \yii\helpers\Html } /** - * {@inheritDoc} + * {@inheritdoc} + * Pass `true` in `$options['inline']` to generate {@see https://getbootstrap.com/docs/5.1/forms/checks-radios/#inline inline list}. */ public static function radioList($name, $selection = null, $items = [], $options = []): string { + $inline = ArrayHelper::remove($options, 'inline', false); + if (!isset($options['item'])) { $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); - $encode = ArrayHelper::getValue($options, 'encode', true); - $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) { - unset($index); - $options = array_merge( - [ - 'class' => 'form-check-input', - 'label' => $encode ? static::encode($label) : $label, - 'labelOptions' => ['class' => 'form-check-label'], - 'value' => $value, - ], $itemOptions); + static::addCssClass($itemOptions, ['bootstrap' => 'form-check-input']); + if (!isset($itemOptions['labelOptions'])) { + $itemOptions['labelOptions'] = ['class' => 'form-check-label']; + } else { + static::addCssClass($itemOptions['labelOptions'], ['bootstrap' => 'form-check-label']); + } - return '
' . static::radio($name, $checked, $options) . '
'; + $wrapperOptions = $inline ? ['class' => 'form-check form-check-inline'] : ['class' => 'form-check']; + + $encode = ArrayHelper::getValue($options, 'encode', true); + + $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $wrapperOptions, $encode) { + $itemOptions['value'] = $value; + if (!isset($itemOptions['label'])) { + $itemOptions['label'] = $encode ? static::encode($label) : $label; + } + + return static::tag('div', static::radio($name, $checked, $itemOptions), $wrapperOptions); }; } @@ -102,23 +108,32 @@ abstract class BaseHtml extends \yii\helpers\Html /** * {@inheritdoc} + * Pass `true` in `$options['inline']` to generate {@see https://getbootstrap.com/docs/5.1/forms/checks-radios/#inline inline list}. */ public static function checkboxList($name, $selection = null, $items = [], $options = []): string { + $inline = ArrayHelper::remove($options, 'inline', false); + if (!isset($options['item'])) { $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); - $encode = ArrayHelper::getValue($options, 'encode', true); - $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) { - unset($index); - $options = array_merge( - [ - 'class' => 'form-check-input', - 'label' => $encode ? static::encode($label) : $label, - 'labelOptions' => ['class' => 'form-check-label'], - 'value' => $value, - ], $itemOptions); + static::addCssClass($itemOptions, 'form-check-input'); + if (!isset($itemOptions['labelOptions'])) { + $itemOptions['labelOptions'] = ['class' => 'form-check-label']; + } else { + static::addCssClass($itemOptions['labelOptions'], 'form-check-label'); + } - return '
' . Html::checkbox($name, $checked, $options) . '
'; + $wrapperOptions = $inline ? ['class' => 'form-check form-check-inline'] : ['class' => 'form-check']; + + $encode = ArrayHelper::getValue($options, 'encode', true); + + $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $wrapperOptions, $encode) { + $itemOptions['value'] = $value; + if (!isset($itemOptions['label'])) { + $itemOptions['label'] = $encode ? static::encode($label) : $label; + } + + return static::tag('div', static::checkbox($name, $checked, $itemOptions), $wrapperOptions); }; } @@ -130,15 +145,13 @@ abstract class BaseHtml extends \yii\helpers\Html */ public static function error($model, $attribute, $options = []): string { - if (!array_key_exists('class', $options)) { - $options['class'] = ['invalid-feedback']; - } + static::addCssClass($options, 'invalid-feedback'); return parent::error($model, $attribute, $options); } /** - * @inheritdoc + * {@inheritdoc} */ protected static function booleanInput($type, $name, $checked = false, $options = []): string { From d20467183ac764211bba771ad315a2fece2b3373 Mon Sep 17 00:00:00 2001 From: Anton Date: Sat, 23 Apr 2022 02:49:22 +0300 Subject: [PATCH 2/4] Update BaseHtml.php --- src/BaseHtml.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BaseHtml.php b/src/BaseHtml.php index 2081786..d01e1b4 100644 --- a/src/BaseHtml.php +++ b/src/BaseHtml.php @@ -74,7 +74,7 @@ abstract class BaseHtml extends \yii\helpers\Html /** * {@inheritdoc} - * Pass `true` in `$options['inline']` to generate {@see https://getbootstrap.com/docs/5.1/forms/checks-radios/#inline inline list}. + * Pass `true` in `$options['inline']` to generate [inline list](https://getbootstrap.com/docs/5.1/forms/checks-radios/#inline). */ public static function radioList($name, $selection = null, $items = [], $options = []): string { @@ -108,7 +108,7 @@ abstract class BaseHtml extends \yii\helpers\Html /** * {@inheritdoc} - * Pass `true` in `$options['inline']` to generate {@see https://getbootstrap.com/docs/5.1/forms/checks-radios/#inline inline list}. + * Pass `true` in `$options['inline']` to generate [inline list](https://getbootstrap.com/docs/5.1/forms/checks-radios/#inline). */ public static function checkboxList($name, $selection = null, $items = [], $options = []): string { From a32e10561bd37037b71e6ee003dce31eec40eaad Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 3 May 2022 20:14:38 +0300 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6d0644..bfce411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.4 under development ----------------------- +- Enh #39: Add inline mode to `BaseHtml::checkboxList()` and `BaseHtml::radioList()` (WinterSilence) + 2.0.3 April 22, 2022 -------------------- From 2e42954f2860f26a654d4497b6b3ca05945f7da2 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 3 May 2022 20:26:05 +0300 Subject: [PATCH 4/4] Update BaseHtml.php --- src/BaseHtml.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/BaseHtml.php b/src/BaseHtml.php index d01e1b4..fba0fff 100644 --- a/src/BaseHtml.php +++ b/src/BaseHtml.php @@ -145,7 +145,9 @@ abstract class BaseHtml extends \yii\helpers\Html */ public static function error($model, $attribute, $options = []): string { - static::addCssClass($options, 'invalid-feedback'); + if (!array_key_exists('class', $options)) { + $options['class'] = ['invalid-feedback']; + } return parent::error($model, $attribute, $options); }