Skip to content

Commit

Permalink
Fix: wrong namespace and add BS5 theme
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Mtangoo committed Sep 29, 2024
1 parent 881c994 commit 2089896
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": ">=8.1",
"npm-asset/select2": "^4.0.13",
"npm-asset/select2-bootstrap-5-theme": "^1.3.0",
"yii2-extensions/asset-bootstrap5": "^0.1",
"yiisoft/yii2": "^2.0.49 || ^2.2"
},
Expand Down
17 changes: 15 additions & 2 deletions src/Select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yii2\Extension\Select2;
namespace Yii2\Extensions\Select2;

use yii\helpers\Html;
use yii\helpers\Json;
Expand Down Expand Up @@ -39,7 +39,20 @@ protected function registerPlugin($name)
Select2Asset::register($view);
$id = $this->options['id'];
if ($this->clientOptions !== false) {
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
$options = empty($this->clientOptions) ? [] : $this->clientOptions;

if (!isset($options['theme'])) {
//Do we have BS5?
if (class_exists('Yii2\Asset\BootstrapAsset')) {
$view->registerCssFile('@npm/select2-bootstrap-5-theme/dist/select2-bootstrap-5-theme.min.css');
$options['theme'] = 'bootstrap-5';
} else {
$options['theme'] = 'classic';
}
}

$options = Json::encode($options);

$js = "jQuery('#$id').$name($options);";
$view->registerJs($js);
}
Expand Down
19 changes: 17 additions & 2 deletions src/Select2Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yii2\Extension\Select2;
namespace Yii2\Extensions\Select2;

use yii\web\AssetBundle;

Expand All @@ -21,9 +21,24 @@ class Select2Asset extends AssetBundle

public $depends = [
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
];

public function init()
{
parent::init();

//which BS version is installed (from highest to lowest)
if (class_exists('yii\bootstrap5\BootstrapAsset')) { //BS5 official
$this->depends[] = 'yii\bootstrap5\BootstrapAsset';
} else if (class_exists('Yii2\Asset\BootstrapAsset')) { //BS5 Yii2-Ext
$this->depends[] = 'Yii2\Asset\BootstrapAsset';
} else if (class_exists('yii\bootstrap4\BootstrapAsset')) {
$this->depends[] = 'yii\bootstrap4\BootstrapAsset';
} else {
$this->depends[] = 'yii\bootstrap\BootstrapAsset';
}
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 2089896

Please sign in to comment.