|
4 | 4 |
|
5 | 5 | namespace App\Filament\Forms\Components; |
6 | 6 |
|
7 | | -use App\Filament\Schemas\Components\Subsection; |
8 | 7 | use App\Models\City; |
9 | 8 | use App\Models\County; |
10 | 9 | use Closure; |
|
16 | 15 | use Filament\Schemas\Components\Grid; |
17 | 16 | use Filament\Schemas\Components\Utilities\Get; |
18 | 17 | use Filament\Schemas\Components\Utilities\Set; |
19 | | -use Filament\Support\Concerns\CanBeContained; |
20 | | -use Filament\Support\Icons\Heroicon; |
21 | 18 |
|
22 | 19 | class Location extends Component implements CanEntangleWithSingularRelationships |
23 | 20 | { |
24 | 21 | use EntanglesStateWithSingularRelationship; |
25 | 22 | use CanBeValidated; |
26 | | - use CanBeContained; |
27 | 23 |
|
28 | 24 | protected string $view = 'filament-schemas::components.grid'; |
29 | 25 |
|
@@ -54,65 +50,52 @@ protected function setUp(): void |
54 | 50 | { |
55 | 51 | $this->columnSpanFull(); |
56 | 52 |
|
57 | | - $this->schema(function () { |
58 | | - $components = [ |
59 | | - Select::make($this->getCountyField()) |
60 | | - ->label($this->getCountyLabel()) |
61 | | - ->placeholder(__('placeholder.county')) |
62 | | - ->options(County::cachedList()) |
63 | | - ->searchable() |
64 | | - ->preload() |
65 | | - ->live() |
66 | | - ->required($this->isRequired()) |
67 | | - ->afterStateUpdated(function (Set $set) { |
68 | | - if (! $this->hasCity()) { |
69 | | - return; |
70 | | - } |
71 | | - |
72 | | - $set($this->getCityField(), null); |
73 | | - }) |
74 | | - ->when(! $this->hasCity(), fn (Select $component) => $component->columnSpanFull()), |
75 | | - |
76 | | - Select::make($this->getCityField()) |
77 | | - ->label($this->getCityLabel()) |
78 | | - ->placeholder(__('placeholder.city')) |
79 | | - ->allowHtml() |
80 | | - ->searchable() |
81 | | - ->required($this->isRequired()) |
82 | | - ->getSearchResultsUsing(function (string $search, Get $get) { |
83 | | - $countyId = (int) $get($this->getCountyField()); |
84 | | - |
85 | | - if (! $countyId) { |
86 | | - return []; |
87 | | - } |
88 | | - |
89 | | - return City::query() |
90 | | - ->where('county_id', $countyId) |
91 | | - ->search($search) |
92 | | - ->limit(50) |
93 | | - ->get() |
94 | | - ->mapWithKeys(fn (City $city) => [ |
95 | | - $city->getKey() => $city->formatted_name, |
96 | | - ]); |
97 | | - }) |
98 | | - ->visible(fn () => $this->hasCity()) |
99 | | - ->getOptionLabelUsing(fn (int $value) => City::find($value)->formatted_name), |
100 | | - ]; |
101 | | - |
102 | | - if (! $this->isContained()) { |
103 | | - return [ |
104 | | - Grid::make() |
105 | | - ->components($components), |
106 | | - ]; |
107 | | - } |
108 | | - |
109 | | - return [ |
110 | | - Subsection::make() |
111 | | - ->icon(Heroicon::OutlinedMapPin) |
112 | | - ->columns() |
113 | | - ->components($components), |
114 | | - ]; |
115 | | - }); |
| 53 | + $this->schema(fn () => [ |
| 54 | + Grid::make() |
| 55 | + ->components([ |
| 56 | + Select::make($this->getCountyField()) |
| 57 | + ->label($this->getCountyLabel()) |
| 58 | + ->placeholder(__('placeholder.county')) |
| 59 | + ->options(County::cachedList()) |
| 60 | + ->searchable() |
| 61 | + ->preload() |
| 62 | + ->live() |
| 63 | + ->required($this->isRequired()) |
| 64 | + ->afterStateUpdated(function (Set $set) { |
| 65 | + if (! $this->hasCity()) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $set($this->getCityField(), null); |
| 70 | + }) |
| 71 | + ->when(! $this->hasCity(), fn (Select $component) => $component->columnSpanFull()), |
| 72 | + |
| 73 | + Select::make($this->getCityField()) |
| 74 | + ->label($this->getCityLabel()) |
| 75 | + ->placeholder(__('placeholder.city')) |
| 76 | + ->allowHtml() |
| 77 | + ->searchable() |
| 78 | + ->required($this->isRequired()) |
| 79 | + ->getSearchResultsUsing(function (string $search, Get $get) { |
| 80 | + $countyId = (int) $get($this->getCountyField()); |
| 81 | + |
| 82 | + if (! $countyId) { |
| 83 | + return []; |
| 84 | + } |
| 85 | + |
| 86 | + return City::query() |
| 87 | + ->where('county_id', $countyId) |
| 88 | + ->search($search) |
| 89 | + ->limit(50) |
| 90 | + ->get() |
| 91 | + ->mapWithKeys(fn (City $city) => [ |
| 92 | + $city->getKey() => $city->formatted_name, |
| 93 | + ]); |
| 94 | + }) |
| 95 | + ->visible(fn () => $this->hasCity()) |
| 96 | + ->getOptionLabelUsing(fn (int $value) => City::findOrFail($value)->formatted_name), |
| 97 | + ]), |
| 98 | + ]); |
116 | 99 | } |
117 | 100 |
|
118 | 101 | public function getCountyField(): string |
|
0 commit comments