generated from yii2-extensions/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Yii2 Select2 from yii2-cms
- Loading branch information
Stefano Mtangoo
committed
Sep 29, 2024
1 parent
29692b1
commit 881c994
Showing
5 changed files
with
208 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,113 @@ | ||
<p align="center"> | ||
<a href="https://github.com/yii2-extensions/template" target="_blank"> | ||
<img src="https://www.yiiframework.com/image/yii_logo_light.svg" height="100px;"> | ||
<a href="https://github.com/yii2-extensions/selectize" target="_blank"png | ||
<img src=">https://select2.org/user/pages/images/logo.png" height="100px;"> | ||
</a> | ||
<h1 align="center">Yii2-Template.</h1> | ||
<h1 align="center">Select2 widget for Yii2 framework</h1> | ||
<br> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://www.php.net/releases/8.1/en.php" target="_blank"> | ||
<img src="https://img.shields.io/badge/PHP-%3E%3D8.1-787CB5" alt="php-version"> | ||
</a> | ||
<a href="https://github.com/yiisoft/yii2/tree/2.2" target="_blank"> | ||
<img src="https://img.shields.io/badge/Yii2%20version-2.2-blue" alt="yii2-version"> | ||
<a href="https://github.com/yii2-extensions/select2/actions/workflows/build.yml" target="_blank"> | ||
<img src="https://github.com/yii2-extensions/select2/actions/workflows/build.yml/badge.svg" alt="PHPUnit"> | ||
</a> | ||
<a href="https://github.com/yii2-extensions/template/actions/workflows/build.yml" target="_blank"> | ||
<img src="https://github.com/yii2-extensions/template/actions/workflows/build.yml/badge.svg" alt="PHPUnit"> | ||
</a> | ||
<a href="https://codecov.io/gh/yii2-extensions/template" target="_blank"> | ||
<img src="https://codecov.io/gh/yii2-extensions/template/branch/main/graph/badge.svg?token=MF0XUGVLYC" alt="Codecov"> | ||
</a> | ||
<a href="https://github.com/yii2-extensions/template/actions/workflows/static.yml" target="_blank"> | ||
<img src="https://github.com/yii2-extensions/gii/actions/workflows/static.yml/badge.svg" alt="PHPStan"> | ||
</a> | ||
<a href="https://github.com/yii2-extensions/template/actions/workflows/static.yml" target="_blank"> | ||
<img src="https://img.shields.io/badge/PHPStan%20level-2-blue" alt="PHPStan level"> | ||
</a> | ||
<a href="https://github.styleci.io/repos/698621511?branch=main" target="_blank"> | ||
<img src="https://github.styleci.io/repos/698621511/shield?branch=main" alt="Code style"> | ||
<a href="https://github.com/yii2-extensions/select2/actions/workflows/compatibility.yml" target="_blank"> | ||
<img src="https://github.com/yii2-extensions/select2/actions/workflows/compatibility.yml/badge.svg" alt="PHPUnit"> | ||
</a> | ||
<a href="https://codecov.io/gh/yii2-extensions/select2" target="_blank"> | ||
<img src="https://codecov.io/gh/yii2-extensions/select2/branch/main/graph/badge.svg?token=MF0XUGVLYC" alt="Codecov"> | ||
</a> | ||
<a href="https://dashboard.stryker-mutator.io/reports/github.com/yii2-extensions/select2/main" target="_blank"> | ||
<img src="https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyii2-extensions%2Fselect2%2Fmain" alt="Infection"> | ||
</a> | ||
</p> | ||
|
||
## Requirements | ||
## Installation | ||
|
||
The preferred way to install this extension is through [composer](https://getcomposer.org/download/). | ||
|
||
The minimun version of `PHP` required by this package is `PHP 8.1`. | ||
Either run | ||
|
||
For install this package, you need [composer](https://getcomposer.org/). | ||
```shell | ||
composer require --prefer-dist yii2-extensions/select2:^0.1 | ||
``` | ||
|
||
or add | ||
|
||
```json | ||
"yii2-extensions/select2": "^0.1" | ||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
||
## Usage | ||
|
||
[Check the documentation docs](/docs/README.md) to learn about usage. | ||
### Dropdown list | ||
|
||
```php | ||
use Yii2\Extension\Select2\Select2; | ||
|
||
<?= $form->field($model, 'user_id')->widget(Select2::className(), [ | ||
'items' => ArrayHelper::map(User::find()->all(), 'id', function($model) { | ||
return $model->username . " <{$model->email}>"; | ||
}), | ||
'clientOptions' => [ | ||
'placeholder' => 'User', | ||
'allowClear' => true, | ||
], | ||
'clientEvents' => [ | ||
'change'=>'function (e) { | ||
console.log("Select2 change."); | ||
}', | ||
], | ||
]) ?> | ||
``` | ||
|
||
### Text input | ||
|
||
```php | ||
use Yii2\Extension\Select2\Select2; | ||
|
||
<?= Select2::widget([ | ||
'name' => 'InputName', | ||
'items' => ArrayHelper::map(User::find()->all(), 'id', function($model) { | ||
return $model->fio . " <{$model->email}>"; | ||
}), | ||
'clientOptions' => [ | ||
'placeholder' => 'User', | ||
'allowClear' => true, | ||
] | ||
]) ?> | ||
``` | ||
|
||
### Properties of the widget | ||
|
||
For more details see: https://github.com/select2/select2 | ||
|
||
### Special Thanks | ||
This code was initially developed by contributors at yii2-cms and hosted at repo: https://github.com/yii-cms/yii2-select2. We thank them all. For continuity and maintenance we have moved a copy here. | ||
|
||
## Quality code | ||
|
||
[![static-analysis](https://github.com/yii2-extensions/select2/actions/workflows/static.yml/badge.svg)](https://github.com/yii2-extensions/select2/actions/workflows/static.yml) | ||
[![phpstan-level](https://img.shields.io/badge/PHPStan%20level-7-blue)](https://github.com/yii2-extensions/select2/actions/workflows/static.yml) | ||
[![StyleCI](https://github.styleci.io/repos/720718108/shield?branch=main)](https://github.styleci.io/repos/720718108?branch=main) | ||
|
||
## Support versions Yii2 | ||
|
||
[![Yii20](https://img.shields.io/badge/Yii2%20version-2.0-blue)](https://github.com/yiisoft/yii2/tree/2.0.49.3) | ||
[![Yii22](https://img.shields.io/badge/Yii2%20version-2.2-blue)](https://github.com/yiisoft/yii2/tree/2.2) | ||
|
||
## Testing | ||
|
||
[Check the documentation testing](/docs/testing.md) to learn about testing. | ||
[Check the documentation testing](docs/testing.md) to learn about testing. | ||
|
||
## Our social networks | ||
|
||
[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter&logoColor=1DA1F2&labelColor=555555?style=flat)](https://twitter.com/Terabytesoftw) | ||
[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter&logoColor=1DA1F2&labelColor=555555?style=flat)](https://twitter.com/yiiupdates) | ||
|
||
## License | ||
|
||
The MIT License. Please see [License File](LICENSE.md) for more information. | ||
The MIT License. Please see [License File](LICENSE) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii2\Extension\Select2; | ||
|
||
use yii\helpers\Html; | ||
use yii\helpers\Json; | ||
use yii\widgets\InputWidget; | ||
|
||
|
||
class Select2 extends InputWidget | ||
{ | ||
public $items = []; | ||
|
||
/** | ||
* @var array | ||
* @see https://select2.github.io/options.html | ||
*/ | ||
public $clientOptions = []; | ||
|
||
public $clientEvents = []; | ||
|
||
public function run() | ||
{ | ||
$this->registerPlugin('select2'); | ||
Html::addCssClass($this->options, 'form-control'); | ||
|
||
if ($this->hasModel()) { | ||
return Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options); | ||
} else { | ||
return Html::dropDownList($this->name, $this->value, $this->items, $this->options); | ||
} | ||
} | ||
|
||
protected function registerPlugin($name) | ||
{ | ||
$view = $this->getView(); | ||
Select2Asset::register($view); | ||
$id = $this->options['id']; | ||
if ($this->clientOptions !== false) { | ||
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); | ||
$js = "jQuery('#$id').$name($options);"; | ||
$view->registerJs($js); | ||
} | ||
$this->registerClientEvents(); | ||
} | ||
|
||
protected function registerClientEvents() | ||
{ | ||
if (!empty($this->clientEvents)) { | ||
$id = $this->options['id']; | ||
$js = []; | ||
foreach ($this->clientEvents as $event => $handler) { | ||
$js[] = "jQuery('#$id').on('$event', $handler);"; | ||
} | ||
$this->getView()->registerJs(implode("\n", $js)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii2\Extension\Select2; | ||
|
||
use yii\web\AssetBundle; | ||
|
||
|
||
class Select2Asset extends AssetBundle | ||
{ | ||
public $sourcePath = '@npm/select2/dist'; | ||
|
||
public $js = [ | ||
'js/select2.full.min.js', | ||
]; | ||
|
||
public $css = [ | ||
'css/select2.min.css', | ||
]; | ||
|
||
public $depends = [ | ||
'yii\web\JqueryAsset', | ||
'yii\bootstrap\BootstrapAsset', | ||
]; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function registerAssetFiles($view) | ||
{ | ||
$language = \Yii::$app->language; | ||
|
||
if (is_file(\Yii::getAlias("{$this->sourcePath}/js/i18n/{$language}.js"))) { | ||
$this->js[] = "js/i18n/{$language}.js"; | ||
} | ||
parent::registerAssetFiles($view); | ||
} | ||
} |