Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Apr 22, 2024
1 parent 02ace98 commit 0d1cf22
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 140 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ Convenient Heroicons selection field for [MoonShine](https://github.com/moonshin

```bash
composer require bugo/moonshine-heroicons-field
php artisan vendor:publish --tag=blade-heroicons --force
```

## Configuration

You can specify desired style for icons:
You can specify desired default style for icons:

`.env`:

Expand Down Expand Up @@ -52,7 +51,7 @@ declare(strict_types=1);

namespace App\MoonShine\Resources;

use Bugo\MoonShineHeroicons\Fields\Icon;
use Bugo\MoonShine\Heroicons\Fields\Icon;
use MoonShine\Resources\ModelResource;

/**
Expand All @@ -73,6 +72,10 @@ class CustomResource extends ModelResource

All use cases of [Blade Heroicons](https://github.com/blade-ui-kit/blade-heroicons#usage) are also available for you.

## Caching

When using icons in Blade templates, be sure to enable [Caching](https://github.com/blade-ui-kit/blade-icons?tab=readme-ov-file#caching).

## Contributing

Pull requests are welcome. For major changes, please open an issue first
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"autoload": {
"psr-4": {
"Bugo\\MoonShineHeroicons\\": "src/"
"Bugo\\MoonShine\\Heroicons\\": "src/"
}
},
"autoload-dev": {
Expand All @@ -46,7 +46,7 @@
"extra": {
"laravel": {
"providers": [
"Bugo\\MoonShineHeroicons\\Providers\\IconServiceProvider"
"Bugo\\MoonShine\\Heroicons\\Providers\\IconServiceProvider"
]
}
},
Expand Down
42 changes: 21 additions & 21 deletions src/Fields/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @copyright 2024 Bugo
* @license https://opensource.org/licenses/MIT MIT
*
* @version 0.2.1
* @version 0.3
*/

namespace Bugo\MoonShineHeroicons\Fields;
namespace Bugo\MoonShine\Heroicons\Fields;

use Closure;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -46,25 +46,6 @@ public function optionProperties(array|Closure $data): static
return $this;
}

public function getCustomOptions(): array
{
return Cache::rememberForever("heroicons-$this->style-field-options", function () {
$items = glob(public_path("vendor/blade-heroicons/$this->style-*.svg"));
$items = array_map(fn($item) => substr(basename($item, '.svg'), 2), $items);

return array_combine($items, $items);
});
}

public function getCustomOptionProperties(): array
{
return Cache::rememberForever("heroicons-$this->style-field-option-properties", function () {
$link = asset("vendor/blade-heroicons/$this->style-%s.svg");

return array_map(fn($item) => ['image' => sprintf($link, $item)], $this->getCustomOptions());
});
}

public function style(string $style): static
{
$this->style = $this->getShortStyle($style);
Expand Down Expand Up @@ -106,4 +87,23 @@ protected function resolvePreview(): string
return (string) Preview::make(formatted: static fn() => implode('', $result))
->setAttribute('class', 'flex items-center');
}

private function getCustomOptions(): array
{
return Cache::rememberForever("heroicons-$this->style-field-options", function () {
$items = glob(public_path("vendor/blade-heroicons/$this->style-*.svg"));
$items = array_map(fn($item) => substr(basename($item, '.svg'), 2), $items);

return array_combine($items, $items);
});
}

private function getCustomOptionProperties(): array
{
return Cache::rememberForever("heroicons-$this->style-field-option-properties", function () {
$link = asset("vendor/blade-heroicons/$this->style-%s.svg");

return array_map(fn($item) => ['image' => sprintf($link, $item)], $this->getCustomOptions());
});
}
}
10 changes: 7 additions & 3 deletions src/Providers/IconServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @copyright 2024 Bugo
* @license https://opensource.org/licenses/MIT MIT
*
* @version 0.2.1
* @version 0.3
*/

namespace Bugo\MoonShineHeroicons\Providers;
namespace Bugo\MoonShine\Heroicons\Providers;

use Illuminate\Support\ServiceProvider;

Expand All @@ -32,7 +32,11 @@ public function boot(): void

$this->publishes([
__DIR__ . '/../../public' => public_path('vendor/moonshine-heroicons-field'),
], ['moonshine-heroicons-field-assets', 'laravel-assets']);
], ['moonshine-heroicons-field', 'laravel-assets']);

$this->publishes([
base_path() . '/vendor/blade-ui-kit/blade-heroicons/resources/svg' => public_path('vendor/blade-heroicons'),
], ['blade-heroicons', 'laravel-assets']);
}
}
}
2 changes: 1 addition & 1 deletion tests/Feature/IconServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

use Bugo\MoonShineHeroicons\Providers\IconServiceProvider;
use Bugo\MoonShine\Heroicons\Providers\IconServiceProvider;
use Tests\TestCase;

uses(TestCase::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests;

use Bugo\MoonShineHeroicons\Providers\IconServiceProvider;
use Bugo\MoonShine\Heroicons\Providers\IconServiceProvider;
use MoonShine\Commands\InstallCommand;
use MoonShine\Providers\MoonShineServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
Expand Down
126 changes: 17 additions & 109 deletions tests/Unit/IconFieldTest.php
Original file line number Diff line number Diff line change
@@ -1,132 +1,40 @@
<?php declare(strict_types=1);

use Bugo\MoonShineHeroicons\Fields\Icon;
use Bugo\MoonShine\Heroicons\Fields\Icon;
use Illuminate\Database\Eloquent\Model;
use Tests\TestCase;

uses(TestCase::class)->group('fields');
uses(TestCase::class);

beforeEach(function (): void {
$this->field = Icon::make('Icon');

$this->selectOptions = $this->field->getCustomOptions();
$this->selectOptionProperties = $this->field->getCustomOptionProperties();

$this->fieldMultiple = Icon::make('Icon multiple')
->multiple();

$this->item = new class () extends Model {
public string $icon = 'archive-box-arrow-down';
public array $icon_multiple = ['archive-box-arrow-down', 'archive-box-arrow-up'];

protected $casts = [
'icon_multiple' => 'json',
];
public string $icon = 'moon';
};

fillFromModel($this->field, $this->item);
fillFromModel($this->fieldMultiple, $this->item);
});

describe('basic methods', function () {
it('type', function (): void {
expect($this->field->type())
->toBeEmpty();
});

it('view', function (): void {
expect($this->field->getView())
->toBe('moonshine::fields.select');
});

it('preview', function (): void {
expect($this->field->preview())
->toBe('')
->and((string) $this->fieldMultiple)
->toBe(
view('moonshine::fields.select', $this->fieldMultiple->toArray())->render()
);
});

it('change preview', function () {
expect($this->field->changePreview(static fn () => 'changed'))
->preview()
->toBe('changed');
});

it('default value', function () {
$field = Icon::make('Icon')->default('archive-box-arrow-down');

expect($field->toValue())
->toBe('archive-box-arrow-down');
});

it('multiple', function (): void {
expect($this->field->isMultiple())
->toBeFalse()
->and($this->fieldMultiple->isMultiple())
->toBeTrue();
});

it('searchable', function (): void {
expect($this->fieldMultiple)
->isSearchable()
->toBeFalse()
->and($this->fieldMultiple->searchable())
->isSearchable()
->toBeTrue();
});

it('options', function (): void {
expect($this->fieldMultiple)
->values()
->toBe($this->selectOptions);
});

it('option properties', function (): void {
describe('updated methods', function () {
it('overrides option and optionProperties', function (): void {
expect(
$this->field->options(['1' => '2'])
->optionProperties(['1' => ['image' => 'https://some.site/1.svg']])
->optionProperties(['1' => ['image' => 'https://www.svgrepo.com/show/397179/panda.svg']])
->getOptionProperties('1')
)->toBe([]);
});
});

it('is selected correctly', function (): void {
expect($this->fieldMultiple)
->isSelected('archive-box-arrow-down')
->toBeTrue();
});

it('is selected invalid', function (): void {
expect($this->fieldMultiple)
->isSelected($this->item, 'archive-box-arrow-up')
->toBeFalse();
});

it('names single', function (): void {
expect($this->field)
->name()
->toBe('icon')
->name('archive-box-arrow-down')
->toBe('icon');
});

it('names multiple', function (): void {
expect($this->fieldMultiple)
->name()
->toBe('icon_multiple[]')
->name('archive-box-arrow-up')
->toBe('icon_multiple[archive-box-arrow-up]');
describe('new methods', function () {
it('adds style method', function(): void {
expect($this->field->style('s')->toValue())
->toBe($this->field->style('solid')->toValue())
->and($this->field->style('o')->toValue())
->toBe($this->field->style('outline')->toValue())
->and($this->field->style('m')->toValue())
->toBe($this->field->style('mini')->toValue())
->and($this->field->style('c')->toValue())
->toBe($this->field->style('micro')->toValue());
});

it('style', function(): void {
expect($this->field->style('s')->toValue())
->toBe($this->field->style('solid')->toValue())
->and($this->field->style('o')->toValue())
->toBe($this->field->style('outline')->toValue())
->and($this->field->style('m')->toValue())
->toBe($this->field->style('mini')->toValue())
->and($this->field->style('c')->toValue())
->toBe($this->field->style('micro')->toValue());
});
});

0 comments on commit 0d1cf22

Please sign in to comment.