From 0d1cf22f5bf06c24609d8ee090e902660a01fc1b Mon Sep 17 00:00:00 2001 From: Bugo Date: Mon, 22 Apr 2024 18:45:11 +0500 Subject: [PATCH] Refactor code --- README.md | 9 +- composer.json | 4 +- src/Fields/Icon.php | 42 ++++---- src/Providers/IconServiceProvider.php | 10 +- tests/Feature/IconServiceProviderTest.php | 2 +- tests/TestCase.php | 2 +- tests/Unit/IconFieldTest.php | 126 +++------------------- 7 files changed, 55 insertions(+), 140 deletions(-) diff --git a/README.md b/README.md index 9d2e93b..f40f3c9 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -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; /** @@ -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 diff --git a/composer.json b/composer.json index cc38fc8..a715d2b 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ }, "autoload": { "psr-4": { - "Bugo\\MoonShineHeroicons\\": "src/" + "Bugo\\MoonShine\\Heroicons\\": "src/" } }, "autoload-dev": { @@ -46,7 +46,7 @@ "extra": { "laravel": { "providers": [ - "Bugo\\MoonShineHeroicons\\Providers\\IconServiceProvider" + "Bugo\\MoonShine\\Heroicons\\Providers\\IconServiceProvider" ] } }, diff --git a/src/Fields/Icon.php b/src/Fields/Icon.php index 3d15dcb..639b6b9 100644 --- a/src/Fields/Icon.php +++ b/src/Fields/Icon.php @@ -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; @@ -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); @@ -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()); + }); + } } diff --git a/src/Providers/IconServiceProvider.php b/src/Providers/IconServiceProvider.php index b127963..783e69d 100644 --- a/src/Providers/IconServiceProvider.php +++ b/src/Providers/IconServiceProvider.php @@ -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; @@ -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']); } } } diff --git a/tests/Feature/IconServiceProviderTest.php b/tests/Feature/IconServiceProviderTest.php index 1bdf0a2..f4545cd 100644 --- a/tests/Feature/IconServiceProviderTest.php +++ b/tests/Feature/IconServiceProviderTest.php @@ -1,6 +1,6 @@ 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()); - }); });