Skip to content

Commit ab4c2a1

Browse files
committed
Attempt to describe filter forms and handle form filters
1 parent cbb2de2 commit ab4c2a1

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/DescribeFilamentResourceTool.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,22 @@ public function mapFilterType(BaseFilter $filter): string
217217
};
218218
}
219219

220-
public function mapFormComponent(Component $component, Resource $resource): ?array
220+
public function mapFormComponent(Component $component, ?Resource $resource = null): ?array
221221
{
222222
$baseInfo = [
223223
'name' => $component->getName(),
224224
'type' => $this->mapComponentType($component),
225225
'label' => $component->getLabel(),
226226
'required' => method_exists($component, 'isRequired') ? $component->isRequired() : null,
227-
'disabled' => method_exists($component, 'isDisabled') ? $component->isDisabled() : null,
227+
// 'disabled' => method_exists($component, 'isDisabled') ? $component->isDisabled() : null, // Needs container to be instantiated
228228
// 'nullable' => method_exists($component, 'isNullable') ? $component->isNullable() : null, // Needs checking validation rules
229229
];
230230

231231
if ($component instanceof TextInput) {
232232
$baseInfo['maxLength'] = $component->getMaxLength();
233233
}
234234

235-
if ($component instanceof Select && $component->getRelationshipName()) {
235+
if ($resource && $component instanceof Select && $component->getRelationshipName()) {
236236
$modelClass = $resource::getModel();
237237
$modelInstance = app($modelClass);
238238
$relationshipDefinition = $modelInstance->{$component->getRelationshipName()}();
@@ -293,6 +293,21 @@ public function mapTableFilter(BaseFilter $filter): array
293293
$baseInfo['optionsSource'] = $options;
294294
}
295295
}
296+
297+
if ($filter->hasFormSchema()) {
298+
/** @var Component $component */
299+
$fields = collect($filter->getFormSchema())
300+
->reject(fn (Component $component) => $component instanceof Grid || $component instanceof Fieldset)
301+
->map(fn (Component $component) => $this->mapFormComponent($component))
302+
->filter()
303+
->values()
304+
->all();
305+
306+
if ($fields) {
307+
$baseInfo['fields'] = $fields;
308+
}
309+
}
310+
296311
// Add more specific filter type mappings here if needed
297312

298313
return $baseInfo;

src/GetFilamentResourceDataTool.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Exception;
66
use Filament\Tables\Columns\Column;
7+
use Filament\Tables\Concerns\InteractsWithTable;
8+
use Filament\Tables\Filters\SelectFilter;
79
use Illuminate\Database\Eloquent\Model;
810
use Illuminate\Support\Facades\Log;
911
use JsonException;
@@ -32,6 +34,8 @@ public function build(): PrismTool
3234
try {
3335
$listPageClass = $resource::getPages()['index'];
3436
$component = $listPageClass->getPage();
37+
38+
/** @var InteractsWithTable $listPage */
3539
$listPage = new $component;
3640
$listPage->bootedInteractsWithTable();
3741
$table = $listPage->getTable();
@@ -44,17 +48,29 @@ public function build(): PrismTool
4448
$listPage->tableSearch = $filters[$column->getName()];
4549
});
4650

51+
$listPage->resetTableFiltersForm();
52+
4753
foreach ($listPage->getTable()->getFilters() as $filter) {
48-
if (method_exists($filter, 'isMultiple') && $filter->isMultiple()) {
49-
$listPage->tableFilters[$filter->getName()] = [
50-
'values' => isset($filters[$filter->getName()])
51-
? (array) $filters[$filter->getName()]
52-
: null,
53-
];
54+
$value = $filters[$filter->getName()] ?? null;
55+
56+
if (blank($value)) {
57+
continue;
58+
}
59+
60+
if ($filter instanceof SelectFilter) {
61+
if (method_exists($filter, 'isMultiple') && $filter->isMultiple()) {
62+
$listPage->tableFilters[$filter->getName()] = [
63+
'values' => isset($filters[$filter->getName()])
64+
? (array) $filters[$filter->getName()]
65+
: null,
66+
];
67+
} else {
68+
$listPage->tableFilters[$filter->getName()] = [
69+
'value' => $filters[$filter->getName()] ?? null,
70+
];
71+
}
5472
} else {
55-
$listPage->tableFilters[$filter->getName()] = [
56-
'value' => $filters[$filter->getName()] ?? null,
57-
];
73+
$listPage->tableFilters[$filter->getName()] = $value;
5874
}
5975
}
6076

0 commit comments

Comments
 (0)