Skip to content

Commit

Permalink
feat: Added configuration file for global defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Frey <[email protected]>
  • Loading branch information
lukas-frey committed Oct 8, 2022
1 parent 669638b commit 4a9bd1b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 28 deletions.
36 changes: 15 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,29 @@ You can install the package via composer:
composer require guava/filament-icon-picker
```

[//]: # ()
[//]: # (You can publish the config file with:)

[//]: # ()
[//]: # (```bash)
You can publish the config file with:

[//]: # (php artisan vendor:publish --tag=":package_slug-config")

[//]: # (```)
```bash
php artisan vendor:publish --tag="filament-icon-picker-config")
```

[//]: # (Optionally, you can publish the views using)
This is the contents of the published config file:

[//]: # ()
[//]: # (```bash)
```php

[//]: # (php artisan vendor:publish --tag=":package_slug-views")
<?php
return [

[//]: # (```)
'sets' => null,

[//]: # (This is the contents of the published config file:)
'columns' => 1,

[//]: # ()
[//]: # (```php)
'layout' => \Guava\FilamentIconPicker\Layout::FLOATING,

[//]: # (return [)
];

[//]: # (];)

[//]: # (```)
```

## Usage

Expand Down Expand Up @@ -103,7 +97,7 @@ public static function table(Table $table): Table

#### Columns
By default, a single-column icon picker will be displayed.
You can customize the amount of columns using `->columns()` like this:
You can customize the amount of columns via the `icon-picker.columns` configuration or using the `->columns()` option like this:
```php
// Display 3 columns from lg and above
IconPicker::make('icon')
Expand All @@ -125,7 +119,7 @@ IconPicker::make('icon')


#### Sets
By default, the plugin will use the heroicons set. If you have installed [additional icon sets](https://github.com/blade-ui-kit/blade-icons#icon-packages), you add them using this option:
By default, the plugin will use all available [blade icon sets](https://github.com/blade-ui-kit/blade-icons#icon-packages) installed. If you want to use only specific icon sets, you can change the default via the `icon-picker.sets` configuration or on a case-by-case basis:
```php
// Search both herocions and fontawesome icons
IconPicker::make('icon')
Expand Down
63 changes: 63 additions & 0 deletions config/icon-picker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
return [

/*
|--------------------------------------------------------------------------
| Default Sets
|--------------------------------------------------------------------------
|
| This configures the icon sets the plugin should use by default for
| every Icon Picker.
|
| You can pass a string with the icon set's name or an array
| of multiple icon set names.
|
| When set to null (default), every installed icon set will be used.
|
*/
'sets' => null,
// example:
// 'sets' => 'heroicons',
// 'sets' => [
// 'heroicons',
// 'fontawesome-solid',
// ],

/*
|--------------------------------------------------------------------------
| Default Columns
|--------------------------------------------------------------------------
|
| This is the default value for the columns configuration. It is used by
| every icon picker, when not set explicitly.
|
| Can be either an integer from 1 - 12 or an array of integers
| with breakpoints (default, sm, md, lg, xl, 2xl) as the key.
|
*/
'columns' => 1,
// example:
// 'columns' => [
// 'default' => 1,
// 'lg' => 3,
// '2xl' => 5,
// ],

/*
|--------------------------------------------------------------------------
| Default Layout
|--------------------------------------------------------------------------
|
| This is the default value for the layout configuration. It is used by
| every icon picker, when not set explicitly.
|
| FLOATING: The select will behave the same way as the default filament
| select. It will show when selected and hide when clicked outside.
|
| ON_TOP: The select options will always be visible in a catalogue-like
| grid view.
|
*/
'layout' => \Guava\FilamentIconPicker\Layout::FLOATING,

];
1 change: 1 addition & 0 deletions src/FilamentIconPickerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function configurePackage(Package $package): void
$package
->name('filament-icon-picker')
->hasViews()
->hasConfigFile('icon-picker')
;
}

Expand Down
20 changes: 13 additions & 7 deletions src/Forms/IconPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IconPicker extends Select

protected string $view = 'filament-icon-picker::forms.icon-picker';

protected array|Closure|null $sets = ['heroicons'];
protected array|Closure|null $sets = null;
protected array|Closure|null $allowedIcons = null;
protected array|Closure|null $disallowedIcons = null;

Expand All @@ -36,6 +36,10 @@ protected function setUp(): void
{
parent::setUp();

$this->sets(config('icon-picker.sets', null));
$this->columns(config('icon-picker.columns', 1));
$this->layout(config('icon-picker.layout', Layout::FLOATING));

$this->getSearchResultsUsing = function (IconPicker $component, string $search, Collection $icons) {
return collect($icons)
->filter(fn(string $icon) => str_contains($icon, $search))
Expand Down Expand Up @@ -64,14 +68,14 @@ protected function setUp(): void
* @param array|string|Closure|null $sets
* @return $this
*/
public function sets(array|Closure|string|null $sets): static
public function sets(array|Closure|string|null $sets = null): static
{
$this->sets = is_array($sets) ? $sets : [$sets];
$this->sets = $sets ? (is_string($sets) ? [$sets] : $sets) : null;

return $this;
}

public function getSets(): array
public function getSets(): ?array
{
return $this->evaluate($this->sets);
}
Expand Down Expand Up @@ -206,9 +210,11 @@ public function multiple(bool|Closure $condition = true): static
private function loadIcons(): Collection
{
$iconsFactory = App::make(IconFactory::class);
$sets = collect($iconsFactory->all())
->filter(fn($value, $key) => in_array($key, $this->getSets()));

$availableSets = $this->getSets();
$sets = collect($iconsFactory->all());
if ($availableSets) {
$sets = $sets->filter(fn($value, $key) => in_array($key, $availableSets));
}
$icons = [];

$allowedIcons = $this->getAllowedIcons();
Expand Down

0 comments on commit 4a9bd1b

Please sign in to comment.