Skip to content

Commit

Permalink
Implement Yii2 Select2 from yii2-cms
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Mtangoo committed Sep 29, 2024
1 parent 29692b1 commit 881c994
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 53 deletions.
112 changes: 86 additions & 26 deletions README.md
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.
37 changes: 23 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
{
"name": "yii2/template",
"name": "yii2-extensions/select2",
"type": "library",
"description": "_____",
"description": "select2 widget for Yii Framework 2",
"keywords": [
"_____"
"yii2",
"select2",
"widget"
],
"license": "mit",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"yiisoft/yii2": "^2.2"
"npm-asset/select2": "^4.0.13",
"yii2-extensions/asset-bootstrap5": "^0.1",
"yiisoft/yii2": "^2.0.49 || ^2.2"
},
"require-dev": {
"maglnet/composer-require-checker": "^4.6",
"phpunit/phpunit": "^10.2",
"yii2-extensions/phpstan": "dev-main"
"maglnet/composer-require-checker": "^4.7",
"php-forge/support": "^0.1",
"phpunit/phpunit": "^10.5",
"roave/infection-static-analysis-plugin": "^1.34",
"symplify/easy-coding-standard": "^12.1",
"yii2-extensions/phpstan": "^0.1"
},
"autoload": {
"psr-4": {
"yii\\template\\": "src"
"Yii2\\Extensions\\Select2\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"yii\\template\\tests\\": "tests"
"Yii2\\Extensions\\Select2\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-main": "1.0.x-dev"
"dev-main": "0.1-dev"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"yiisoft/yii2-composer": true
"yiisoft/yii2-composer": true,
"composer/installers": true,
"oomphinc/composer-installers-extender": true,
"infection/extension-installer": true
}
},
"scripts": {
"check-dependencies": "composer-require-checker",
"ecs": "ecs check",
"mutation": "roave-infection-static-analysis-plugin",
"phpstan": "phpstan",
"test": "phpunit"
Expand All @@ -50,4 +59,4 @@
"url": "https://asset-packagist.org"
}
]
}
}
13 changes: 0 additions & 13 deletions src/Example.php

This file was deleted.

60 changes: 60 additions & 0 deletions src/Select2.php
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));
}
}
}
39 changes: 39 additions & 0 deletions src/Select2Asset.php
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);
}
}

0 comments on commit 881c994

Please sign in to comment.