Skip to content

Commit ad38d4d

Browse files
committed
update repeater field config and add iconPicker
1 parent eccdf43 commit ad38d4d

File tree

4 files changed

+148
-9
lines changed

4 files changed

+148
-9
lines changed

config/inspirecms.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@
365365

366366
Fields\Configs\ContentPicker::class,
367367
Fields\Configs\MediaPicker::class,
368+
369+
Fields\Configs\IconPicker::class,
368370
],
369371
],
370372

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace SolutionForest\InspireCms\Fields\Configs\Concerns;
4+
5+
use Filament\Forms;
6+
7+
trait HasColumnsLayoutConfig
8+
{
9+
public array $columnsLayout = [];
10+
11+
protected static function getHasColumnsLayoutConfigComponent()
12+
{
13+
return Forms\Components\KeyValue::make('columnsLayout')
14+
->label('Columns')
15+
->keyLabel('Field')
16+
->valueLabel('Width')
17+
->keyPlaceholder('e.g. default, sm, md, lg, xl')
18+
->valuePlaceholder('e.g. 1, 2, 3, 4, etc.')
19+
->helperText(str('The columns for this field. Use **`default`** for the default layout, **`sm`** for small screens, **`md`** for medium screens, etc.')->markdown()->toHtmlString());
20+
}
21+
}

src/Fields/Configs/IconPicker.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace SolutionForest\InspireCms\Fields\Configs;
4+
5+
use Filament\Forms;
6+
use Guava\FilamentIconPicker\Forms\IconPicker as FormsIconPicker;
7+
use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName;
8+
use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType;
9+
use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent;
10+
use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Contracts\FieldTypeConfig;
11+
use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\FieldTypeBaseConfig;
12+
use SolutionForest\InspireCms\Fields\Configs\Concerns\HasColumnsLayoutConfig;
13+
14+
#[ConfigName('iconPicker', 'Icon Picker', 'Picker', 'heroicon-o-cog')]
15+
#[FormComponent(FormsIconPicker::class)]
16+
#[DbType('mysql', 'varchar')]
17+
#[DbType('sqlite', 'text')]
18+
class IconPicker extends FieldTypeBaseConfig implements FieldTypeConfig
19+
{
20+
use HasColumnsLayoutConfig;
21+
22+
public function getFormSchema(): array
23+
{
24+
return [
25+
Forms\Components\Tabs::make('tabs')
26+
->tabs([
27+
Forms\Components\Tabs\Tab::make('Presentation')
28+
->schema([
29+
static::getHasColumnsLayoutConfigComponent(),
30+
]),
31+
]),
32+
];
33+
}
34+
35+
public function applyConfig(Forms\Components\Component $component): void
36+
{
37+
if ($component instanceof FormsIconPicker) {
38+
if (is_array($this->columnsLayout) && ($filterColumns = $this->filterColumnsData($this->columnsLayout)) && ! empty($filterColumns)) {
39+
$component->columns($filterColumns);
40+
}
41+
}
42+
}
43+
44+
private function filterColumnsData(array $data): array
45+
{
46+
return collect($data)
47+
->filter(fn ($value, $key) => is_numeric($value) && $value > 0 && in_array($key, ['default', 'sm', 'md', 'lg', 'xl']))
48+
->toArray();
49+
}
50+
}

src/Fields/Configs/Repeater.php

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\FieldTypeBaseConfig;
1111
use SolutionForest\InspireCms\Fields\Configs\Attributes\Converter;
1212
use SolutionForest\InspireCms\Fields\Configs\Attributes\Translatable;
13+
use SolutionForest\InspireCms\Fields\Configs\Concerns\HasColumnsLayoutConfig;
1314
use SolutionForest\InspireCms\Fields\Configs\Concerns\HasInnerField;
1415
use SolutionForest\InspireCms\Fields\Converters\RepeaterConverter;
1516
use SolutionForest\InspireCms\Helpers\FieldTypeHelper;
@@ -23,18 +24,27 @@
2324
class Repeater extends FieldTypeBaseConfig implements FieldTypeConfig
2425
{
2526
use HasInnerField;
27+
use HasColumnsLayoutConfig;
2628

2729
public array $fields = [];
2830

29-
public bool $collapsible = false;
31+
public ?string $itemLabel = null;
32+
33+
public ?int $defaultItems = null;
3034

3135
public bool $cloneable = false;
3236

37+
public bool $collapsible = false;
3338
public bool $defaultCollapsed = false;
3439

35-
public ?string $itemLabel = null;
40+
public bool $reorderable = false;
41+
public bool $reorderableWithButtons = false;
42+
public bool $reorderableWithButtonsreorderableWithDragAndDrop = false;
3643

37-
public ?int $defaultItems = null;
44+
public ?int $minItems = null;
45+
public ?int $maxItems = null;
46+
47+
public array $gridLayout = [];
3848

3949
public function getFormSchema(): array
4050
{
@@ -43,16 +53,46 @@ public function getFormSchema(): array
4353
->tabs([
4454
Forms\Components\Tabs\Tab::make('Presentation')
4555
->schema([
46-
Forms\Components\Toggle::make('collapsible'),
47-
Forms\Components\Toggle::make('cloneable'),
48-
Forms\Components\Toggle::make('defaultCollapsed'),
56+
Forms\Components\Grid::make(2)
57+
->schema([
58+
Forms\Components\Toggle::make('cloneable'),
59+
]),
60+
Forms\Components\Grid::make(2)
61+
->schema([
62+
Forms\Components\Toggle::make('collapsible'),
63+
Forms\Components\Toggle::make('defaultCollapsed'),
64+
]),
65+
Forms\Components\Grid::make(2)
66+
->schema([
67+
Forms\Components\Toggle::make('reorderable')->default(true),
68+
Forms\Components\Toggle::make('reorderableWithButtons')->default(true),
69+
Forms\Components\Toggle::make('reorderableWithDragAndDrop')->default(false),
70+
]),
71+
4972
Forms\Components\TextInput::make('itemLabel')
5073
->inlineLabel()
5174
->placeholder('e.g. title, key, etc.')
5275
->helperText(str('The label for each item in the repeater. Using **`Name`** in the **Fields**')->markdown()->toHtmlString()),
76+
77+
78+
Forms\Components\KeyValue::make('gridLayout')
79+
->keyLabel('Column')
80+
->keyLabel('Width')
81+
->keyPlaceholder('e.g. default, sm, md, lg, xl')
82+
->valuePlaceholder('e.g. 1, 2, 3, 4, etc.')
83+
->helperText(str('The grid layout for the repeater. Use **`default`** for the default layout, **`sm`** for small screens, **`md`** for medium screens, etc.')->markdown()->toHtmlString()),
84+
85+
static::getHasColumnsLayoutConfigComponent(),
86+
5387
]),
5488
Forms\Components\Tabs\Tab::make('Fields')
5589
->schema([
90+
Forms\Components\TextInput::make('minItems')
91+
->inlineLabel()
92+
->integer(),
93+
Forms\Components\TextInput::make('maxItems')
94+
->inlineLabel()
95+
->integer(),
5696
Forms\Components\TextInput::make('defaultItems')
5797
->inlineLabel()
5898
->placeholder('e.g. 1, 2, 3, etc.')
@@ -93,12 +133,17 @@ public function applyConfig(Forms\Components\Component $component): void
93133
}
94134
$component->schema(array_filter($components));
95135

96-
$component->collapsible($this->collapsible);
97-
136+
//
98137
$component->cloneable($this->cloneable);
99-
138+
139+
$component->collapsible($this->collapsible);
100140
$component->collapsed($this->defaultCollapsed ?? false);
101141

142+
$component->reorderable($this->reorderable ?? false);
143+
$component->reorderableWithButtons($this->reorderableWithButtons ?? false);
144+
$component->reorderableWithDragAndDrop($this->reorderableWithDragAndDrops ?? false);
145+
146+
//
102147
$component->itemLabel(function ($state) {
103148
if (is_array($state) && filled($this->itemLabel)) {
104149
return $state[$this->itemLabel] ?? null;
@@ -110,6 +155,27 @@ public function applyConfig(Forms\Components\Component $component): void
110155
if ($this->defaultItems != null) {
111156
$component->defaultItems($this->defaultItems);
112157
}
158+
159+
if ($this->minItems != null) {
160+
$component->minItems($this->minItems);
161+
}
162+
if ($this->maxItems != null) {
163+
$component->maxItems($this->maxItems);
164+
}
165+
166+
if (is_array($this->gridLayout) && ($filterGrid = $this->filterColumnsData($this->gridLayout)) && ! empty($filterGrid)) {
167+
$component->grid($filterGrid);
168+
}
169+
if (is_array($this->columnsLayout) && ($filterColumns = $this->filterColumnsData($this->columnsLayout)) && ! empty($filterColumns)) {
170+
$component->columns($filterColumns);
171+
}
113172
}
114173
}
174+
175+
private function filterColumnsData(array $data): array
176+
{
177+
return collect($data)
178+
->filter(fn ($value, $key) => is_numeric($value) && $value > 0 && in_array($key, ['default', 'sm', 'md', 'lg', 'xl']))
179+
->toArray();
180+
}
115181
}

0 commit comments

Comments
 (0)