Merge pull request #39 from WinterSilence/patch-1

Update BaseHtml
This commit is contained in:
simialbi 2022-05-06 08:20:21 +02:00 committed by GitHub
commit 46aa6a76f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 29 deletions

View File

@ -3,8 +3,11 @@ Yii Framework 2 bootstrap5 extension Change Log
2.0.4 under development 2.0.4 under development
----------------------- -----------------------
- Enh #39: Add inline mode to `BaseHtml::checkboxList()` and `BaseHtml::radioList()` (WinterSilence)
- Enh #40: Breadcrumbs refactoring (WinterSilence) - Enh #40: Breadcrumbs refactoring (WinterSilence)
2.0.3 April 22, 2022 2.0.3 April 22, 2022
-------------------- --------------------

View File

@ -28,14 +28,10 @@ abstract class BaseHtml extends \yii\helpers\Html
*/ */
public static $autoIdPrefix = 'i'; public static $autoIdPrefix = 'i';
/** /**
* @var bool whether to removes duplicate class names in tag attribute `class` (fix strange yii2 behavior since 2.0.44) * @inheritDoc
* @see mergeCssClasses()
* @see renderTagAttributes()
* @since 2.0.3
*/ */
public static $normalizeClassAttribute = true; public static $normalizeClassAttribute = true;
/** /**
* Renders Bootstrap static form control. * 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. * Generates a Bootstrap static form control for the given model attribute.
*
* @param Model $model the model object. * @param Model $model the model object.
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
* about attribute expression. * about attribute expression.
@ -76,24 +73,33 @@ abstract class BaseHtml extends \yii\helpers\Html
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
* 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 public static function radioList($name, $selection = null, $items = [], $options = []): string
{ {
$inline = ArrayHelper::remove($options, 'inline', false);
if (!isset($options['item'])) { if (!isset($options['item'])) {
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::getValue($options, 'encode', true); static::addCssClass($itemOptions, ['bootstrap' => 'form-check-input']);
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) { if (!isset($itemOptions['labelOptions'])) {
unset($index); $itemOptions['labelOptions'] = ['class' => 'form-check-label'];
$options = array_merge( } else {
[ static::addCssClass($itemOptions['labelOptions'], ['bootstrap' => 'form-check-label']);
'class' => 'form-check-input', }
'label' => $encode ? static::encode($label) : $label,
'labelOptions' => ['class' => 'form-check-label'],
'value' => $value,
], $itemOptions);
return '<div class="form-check">' . static::radio($name, $checked, $options) . '</div>'; $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} * {@inheritdoc}
* 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 public static function checkboxList($name, $selection = null, $items = [], $options = []): string
{ {
$inline = ArrayHelper::remove($options, 'inline', false);
if (!isset($options['item'])) { if (!isset($options['item'])) {
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::getValue($options, 'encode', true); static::addCssClass($itemOptions, 'form-check-input');
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) { if (!isset($itemOptions['labelOptions'])) {
unset($index); $itemOptions['labelOptions'] = ['class' => 'form-check-label'];
$options = array_merge( } else {
[ static::addCssClass($itemOptions['labelOptions'], 'form-check-label');
'class' => 'form-check-input', }
'label' => $encode ? static::encode($label) : $label,
'labelOptions' => ['class' => 'form-check-label'],
'value' => $value,
], $itemOptions);
return '<div class="form-check">' . Html::checkbox($name, $checked, $options) . '</div>'; $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);
}; };
} }
@ -138,7 +153,7 @@ abstract class BaseHtml extends \yii\helpers\Html
} }
/** /**
* @inheritdoc * {@inheritdoc}
*/ */
protected static function booleanInput($type, $name, $checked = false, $options = []): string protected static function booleanInput($type, $name, $checked = false, $options = []): string
{ {