Skip to content

Commit 840b46a

Browse files
Upgrade to Filament v4 (#37)
* Upgrade to Filament v4 * Fix styling * Add source tag in plugin.css + remove package.json and postcss config since they are not used * Fix link picker for Filament v4 * Use correct index for the mounted actions --------- Co-authored-by: jyrkidn <2447042+jyrkidn@users.noreply.github.com> Co-authored-by: Sander Van Damme <sander.vandamme@onlyhumans.com>
1 parent 05dc049 commit 840b46a

File tree

9 files changed

+40
-2980
lines changed

9 files changed

+40
-2980
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
"license": "MIT",
1111
"require": {
1212
"php": "^8.2",
13+
"filament/filament": "^4.0",
1314
"illuminate/contracts": "^10.0|^11.0|^12.0",
14-
"spatie/laravel-package-tools": "^1.12",
15-
"filament/filament": "^3.0"
15+
"spatie/laravel-package-tools": "^1.12"
1616
},
1717
"require-dev": {
1818
"codedor/laravel-translatable-routes": "^1.3",
19+
"filament/upgrade": "^4.0",
20+
"larastan/larastan": "^2.0|^3.0",
1921
"laravel/pint": "^1.0",
2022
"nunomaduro/collision": "^7.0|^8.0|^9.0",
21-
"larastan/larastan": "^2.0|^3.0",
2223
"orchestra/testbench": "^8.0|^9.0|^10.0",
2324
"pestphp/pest": "^2.0|^3.0",
2425
"pestphp/pest-plugin-laravel": "^2.0|^3.0",

dist/css/filament-link-picker.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

Lines changed: 0 additions & 2875 deletions
This file was deleted.

package.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

postcss.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

resources/css/plugin.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@tailwind utilities;
1+
@source '../views/**';

resources/views/filament/link-picker.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424

2525
<ul>
2626
@foreach ($routeDescription['parameters'] as $name => $value)
27-
<li>
28-
{{ ucfirst($name) }}: {{ $value }}
29-
</li>
27+
@if (filled($value))
28+
<li>
29+
{{ ucfirst($name) }}: {{ $value }}
30+
</li>
31+
@endif
3032
@endforeach
3133
</ul>
3234
@endif

src/Filament/LinkPickerInput.php

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
use Codedor\LinkPicker\Facades\LinkCollection;
66
use Codedor\LinkPicker\Link;
77
use Codedor\LocaleCollection\Facades\LocaleCollection;
8-
use Filament\Forms\Components\Actions\Action;
98
use Filament\Forms\Components\Checkbox;
10-
use Filament\Forms\Components\Component;
119
use Filament\Forms\Components\Field;
1210
use Filament\Forms\Components\Select;
13-
use Filament\Forms\Form;
14-
use Filament\Forms\Get;
15-
use Filament\Forms\Set;
11+
use Filament\Schemas\Components\Grid;
1612
use Illuminate\Database\Eloquent\Model;
1713
use Illuminate\Routing\Route;
1814
use Illuminate\Support\Arr;
@@ -31,84 +27,47 @@ protected function setUp(): void
3127
parent::setUp();
3228

3329
$this->registerActions([
34-
Action::make('link-picker-modal')
30+
\Filament\Actions\Action::make('link-picker-modal')
3531
->label(fn ($state) => $state
3632
? __('filament-link-picker::input.edit link')
3733
: __('filament-link-picker::input.select link')
3834
)
3935
->icon(fn ($state) => $state ? 'heroicon-o-pencil' : 'heroicon-o-plus')
4036
->color('gray')
4137
->iconSize('sm')
42-
->fillForm(function (Get $get, Component $component, \Livewire\Component $livewire): array {
43-
$statePath = $component->getStatePath(false);
44-
45-
$schema = $this->getFormSchemaForRoute($get("{$statePath}.route"));
46-
47-
$state = [
48-
'route' => $get("{$statePath}.route"),
49-
'newTab' => $get("{$statePath}.newTab"),
50-
'parameters' => $get("{$statePath}.parameters") ?: [],
38+
->fillForm(fn (\Filament\Schemas\Components\Component $component): array => $component->getState() ?? [])
39+
->schema(function () {
40+
return [
41+
Grid::make(1)->schema(function (\Livewire\Component $livewire) {
42+
$mountedAction = Arr::last($livewire->mountedActions);
43+
$mountedActionIndex = array_key_last($livewire->mountedActions);
44+
$schema = $this->getFormSchemaForRoute($mountedAction['data']['route'] ?? null);
45+
46+
// since the fields are dynamic we have to fill the state manually,
47+
// else validation will fail because property is not in the state
48+
data_fill($livewire, "mountedActions.{$mountedActionIndex}.data.parameters", []);
49+
$schema->each(function (Field $field) use (&$livewire, $mountedActionIndex) {
50+
data_fill(
51+
$livewire,
52+
"mountedActions.{$mountedActionIndex}.data.{$field->statePath}",
53+
null,
54+
);
55+
});
56+
57+
return $schema->toArray();
58+
}),
5159
];
52-
53-
$actionNestingIndex = array_key_last($livewire->mountedFormComponentActions);
54-
55-
$schema
56-
->each(function (Field $field) use (&$state, $statePath, $get, $actionNestingIndex, $livewire) {
57-
$fieldStatePath = $field->statePath;
58-
59-
data_fill(
60-
$state,
61-
$fieldStatePath,
62-
data_get(
63-
$livewire->mountedFormComponentActionsData[$actionNestingIndex] ?? [],
64-
"{$statePath}.{$fieldStatePath}"
65-
) ?? $get("{$statePath}.{$fieldStatePath}") ?? null
66-
);
67-
});
68-
69-
return $state;
70-
})
71-
->form(function (Get $get, Component $component, \Livewire\Component $livewire, Form $form) {
72-
$statePath = $component->getStatePath(false);
73-
74-
$actionNestingIndex = array_key_last($livewire->mountedFormComponentActions);
75-
76-
$schema = $this->getFormSchemaForRoute(
77-
$livewire->mountedFormComponentActionsData[$actionNestingIndex]['route'] ?? $get("{$statePath}.route") ?? null
78-
);
79-
80-
$state = $livewire->mountedFormComponentActionsData[$actionNestingIndex] ?? [];
81-
82-
// since the fields are dynamic we have to fill the state manually,
83-
// else validation will fail because property is not in the state
84-
$schema->each(function (Field $field) use (&$state, $statePath, $get, $actionNestingIndex, $livewire) {
85-
$fieldStatePath = $field->statePath;
86-
87-
data_fill(
88-
$state,
89-
$fieldStatePath,
90-
data_get(
91-
$livewire->mountedFormComponentActionsData[$actionNestingIndex] ?? [],
92-
"{$statePath}.{$fieldStatePath}"
93-
) ?? $get("{$statePath}.{$fieldStatePath}") ?? null
94-
);
95-
});
96-
97-
$livewire->mountedFormComponentActionsData[$actionNestingIndex] = $state;
98-
$form->fill($state);
99-
100-
return $schema->toArray();
10160
})
102-
->action(function (Set $set, array $data, Component $component) {
61+
->action(function (\Filament\Schemas\Components\Utilities\Set $set, array $data, \Filament\Schemas\Components\Component $component) {
10362
$set($component->getStatePath(false), $data);
10463
}),
10564

106-
Action::make('link-picker-clear')
65+
\Filament\Actions\Action::make('link-picker-clear')
10766
->label(__('filament-link-picker::input.remove link'))
10867
->icon('heroicon-o-trash')
10968
->iconSize('sm')
11069
->color('danger')
111-
->action(function (Set $set) {
70+
->action(function (\Filament\Schemas\Components\Utilities\Set $set) {
11271
$set($this->getStatePath(false), null);
11372
}),
11473
]);
@@ -216,8 +175,8 @@ private function getFormSchemaForRoute(?string $selectedRoute): Collection
216175

217176
$schema->add(
218177
Select::make('parameters.anchor')
219-
->hidden(fn (Get $get) => ! $get("parameters.{$anchorData['parameter']}"))
220-
->options(function (Get $get) use ($anchorData) {
178+
->hidden(fn (\Filament\Schemas\Components\Utilities\Get $get) => ! $get("parameters.{$anchorData['parameter']}"))
179+
->options(function (\Filament\Schemas\Components\Utilities\Get $get) use ($anchorData) {
221180
/**
222181
* @var Model $record
223182
*/

tests/Feature/FilamentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
use Codedor\LinkPicker\Filament\LinkPickerInput;
44
use Codedor\LinkPicker\Tests\Fixtures\Forms\Livewire;
55
use Codedor\LinkPicker\Tests\Fixtures\Models\TestModel;
6-
use Filament\Forms\ComponentContainer;
6+
use Filament\Schemas\Schema;
77

88
beforeEach(function () {
99
$this->field = LinkPickerInput::make('link')
10-
->container(ComponentContainer::make(Livewire::make()))
10+
->container(Schema::make(Livewire::make()))
1111
->model(TestModel::class);
1212
});
1313

0 commit comments

Comments
 (0)