Skip to content

Commit

Permalink
Enhacements (#9)
Browse files Browse the repository at this point in the history
* Add the ability to add an icon to Dropdown::class.
* Enhancement tests without composer-plugin.
* clean more code.
  • Loading branch information
terabytesoftw committed Oct 2, 2020
1 parent fdfa949 commit 1077426
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 102 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/static.yml
@@ -1,17 +1,13 @@
name: static analysis

on:
- pull_request
- push

name: static analysis

jobs:
mutation:
name: PHP ${{ matrix.php-version }}-${{ matrix.os }}

env:
extensions: ast
update: true

runs-on: ${{ matrix.os }}

strategy:
Expand All @@ -30,15 +26,14 @@ jobs:
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
extensions: ${{ env.extensions }}
tools: composer:v2, cs2pr
coverage: none

- name: Determine composer cache directory
run: echo "::set-env name=COMPOSER_CACHE_DIR::$(composer config cache-dir)"

- name: Cache dependencies installed with composer
uses: actions/cache@v2
uses: actions/cache@v1
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
Expand All @@ -49,4 +44,4 @@ jobs:
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: Static analysis with phan
run: vendor/bin/phan --no-progress-bar --output-mode checkstyle | cs2pr --graceful-warnings --colorize
run: vendor/bin/psalm --shepherd --stats --output-format=checkstyle | cs2pr --graceful-warnings --colorize
6 changes: 2 additions & 4 deletions composer.json
Expand Up @@ -28,11 +28,9 @@
},
"require-dev": {
"infection/infection": "^0.16.3",
"phan/phan": "^3.0",
"phpunit/phpunit": "^9.3",
"yiisoft/composer-config-plugin": "^1.0@dev",
"yiisoft/di": "^3.0@dev",
"yiisoft/log": "^3.0@dev"
"vimeo/psalm": "^3.15",
"yiisoft/di": "^3.0@dev"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 15 additions & 0 deletions psalm.xml
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
20 changes: 11 additions & 9 deletions src/Breadcrumbs.php
Expand Up @@ -4,11 +4,13 @@

namespace Yiisoft\Yii\Bulma;

use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
use Yiisoft\Widget\Exception\InvalidConfigException;

use function array_key_exists;
use function array_merge;
use function is_array;
use function strtr;

Expand Down Expand Up @@ -53,7 +55,7 @@ protected function run(): string
*
* @param bool $value
*
* @return self
* @return $this
*/
public function encodeLabels(bool $value): self
{
Expand All @@ -71,7 +73,7 @@ public function encodeLabels(bool $value): self
*
* @param array $value
*
* @return self
* @return $this
*/
public function homeLink(array $value): self
{
Expand All @@ -85,7 +87,7 @@ public function homeLink(array $value): self
*
* @param string $value
*
* @return self
* @return $this
*/
public function itemTemplate(string $value): self
{
Expand All @@ -99,7 +101,7 @@ public function itemTemplate(string $value): self
*
* @param string $value
*
* @return self
* @return $this
*/
public function activeItemTemplate(string $value): self
{
Expand All @@ -121,7 +123,7 @@ public function activeItemTemplate(string $value): self
*
* @param array $value
*
* @return self
* @return $this
*/
public function links(array $value): self
{
Expand All @@ -134,7 +136,7 @@ public function links(array $value): self
*
* @param array $value
*
* @return self
* @return $this
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
Expand All @@ -149,7 +151,7 @@ public function options(array $value): self
*
* @param array $value
*
* @return self
* @return $this
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
Expand Down Expand Up @@ -194,9 +196,9 @@ private function renderIcon(string $icon, array $iconOptions): string
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the
* link.
*
* @return string the rendering result
* @throws InvalidConfigException|JsonException if `$link` does not have "label" element.
*
* @throws InvalidConfigException if `$link` does not have "label" element.
* @return string the rendering result
*/
private function renderItem(array $link, string $template): string
{
Expand Down
58 changes: 42 additions & 16 deletions src/Dropdown.php
Expand Up @@ -4,12 +4,15 @@

namespace Yiisoft\Yii\Bulma;

use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Factory\Exceptions\InvalidConfigException;
use Yiisoft\Html\Html;
use Yiisoft\Widget\Exception\InvalidConfigException;

use function array_key_exists;
use function array_merge;
use function implode;
use function is_array;

final class Dropdown extends Widget
{
Expand Down Expand Up @@ -41,7 +44,7 @@ protected function run(): string
*
* @param string $value
*
* @return self
* @return $this
*/
public function buttonLabel(string $value): self
{
Expand All @@ -54,7 +57,7 @@ public function buttonLabel(string $value): self
*
* @param array $value
*
* @return self
* @return $this
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
Expand All @@ -69,7 +72,7 @@ public function buttonLabelOptions(array $value): self
*
* @param $value
*
* @return self
* @return $this
*/
public function dividerClass(string $value): self
{
Expand All @@ -82,7 +85,7 @@ public function dividerClass(string $value): self
*
* @param $value
*
* @return self
* @return $this
*/
public function itemClass(string $value): self
{
Expand All @@ -95,7 +98,7 @@ public function itemClass(string $value): self
*
* @param $value
*
* @return self
* @return $this
*/
public function itemsClass(string $value): self
{
Expand All @@ -108,7 +111,7 @@ public function itemsClass(string $value): self
*
* @param bool $value
*
* @return self
* @return $this
*/
public function encodeLabels(bool $value): self
{
Expand All @@ -121,7 +124,7 @@ public function encodeLabels(bool $value): self
*
* @param $value
*
* @return self
* @return $this
*/
public function encloseByContainer(bool $value): self
{
Expand All @@ -146,7 +149,7 @@ public function encloseByContainer(bool $value): self
*
* @param array $value
*
* @return self
* @return $this
*/
public function items(array $value): self
{
Expand All @@ -159,7 +162,7 @@ public function items(array $value): self
*
* @param array $value
*
* @return self
* @return $this
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
Expand All @@ -174,7 +177,7 @@ public function options(array $value): self
*
* @param array $value
*
* @return self
* @return $this
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
Expand All @@ -189,7 +192,7 @@ public function buttonOptions(array $value): self
*
* @param array $value
*
* @return self
* @return $this
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
Expand All @@ -204,7 +207,7 @@ public function itemsOptions(array $value): self
*
* @param array $value
*
* @return self
* @return $this
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
Expand Down Expand Up @@ -269,9 +272,9 @@ private function buildOptions(): void
* @param array $items the menu items to be rendered
* @param array $itemsOptions the container HTML attributes
*
* @return string the rendering result.
* @throws InvalidConfigException|JsonException if the label option is not specified in one of the items.
*
* @throws InvalidConfigException if the label option is not specified in one of the items.
* @return string the rendering result.
*/
private function renderItems(array $items, array $itemsOptions = []): string
{
Expand All @@ -295,6 +298,16 @@ private function renderItems(array $items, array $itemsOptions = []): string
$label = $item['label'];
}

$iconOptions = [];

$icon = $item['icon'] ?? '';

if (array_key_exists('iconOptions', $item) && is_array($item['iconOptions'])) {
$iconOptions = $this->addOptions($iconOptions, 'icon');
}

$label = $this->renderIcon($label, $icon, $iconOptions);

$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
$active = ArrayHelper::getValue($item, 'active', false);
$disabled = ArrayHelper::getValue($item, 'disabled', false);
Expand All @@ -305,6 +318,7 @@ private function renderItems(array $items, array $itemsOptions = []): string
Html::addCssStyle($linkOptions, 'opacity:.65; pointer-events:none;');
}

/** @psalm-suppress ConflictingReferenceConstraint */
if ($active) {
Html::addCssClass($linkOptions, 'is-active');
}
Expand All @@ -316,7 +330,7 @@ private function renderItems(array $items, array $itemsOptions = []): string
} else {
$lines[] = Html::a($label, $url, array_merge($this->linkOptions, $linkOptions));

$lines[] = Dropdown::widget()
$lines[] = self::widget()
->dividerClass($this->dividerClass)
->itemClass($this->itemClass)
->itemsClass($this->itemsClass)
Expand All @@ -332,4 +346,16 @@ private function renderItems(array $items, array $itemsOptions = []): string
implode("\n", $lines) . "\n" .
Html::endTag('div');
}

private function renderIcon(string $label, string $icon, array $iconOptions): string
{
if ($icon !== '') {
$label = Html::beginTag('span', $iconOptions) .
Html::tag('i', '', ['class' => $icon]) .
Html::endTag('span') .
Html::tag('span', $label);
}

return $label;
}
}

0 comments on commit 1077426

Please sign in to comment.