Merge pull request #17826 from filamentphp/fix/deferred-query-builder

fix: Deferred query builder
This commit is contained in:
Dan Harrin
2025-09-16 11:21:49 +01:00
committed by GitHub
7 changed files with 67 additions and 19 deletions
+21 -7
View File
@@ -97,6 +97,8 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
protected Width | string | Closure | null $blockPickerWidth = null;
protected bool | Closure $shouldPartiallyRenderAfterActionsCalled = true;
protected function setUp(): void
{
parent::setUp();
@@ -270,7 +272,7 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->livewireClickHandlerEnabled(false)
->button()
@@ -332,7 +334,7 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -373,7 +375,7 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -413,7 +415,7 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -453,7 +455,7 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -503,7 +505,7 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->livewireClickHandlerEnabled(false)
->iconButton()
@@ -680,7 +682,7 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->icon(Heroicon::Cog6Tooth)
@@ -1156,4 +1158,16 @@ class Builder extends Field implements CanConcealComponents, HasExtraItemActions
$rules["{$this->getStatePath()}.*.type"] = ['required'];
}
public function partiallyRenderAfterActionsCalled(bool | Closure $condition = true): static
{
$this->shouldPartiallyRenderAfterActionsCalled = $condition;
return $this;
}
public function shouldPartiallyRenderAfterActionsCalled(): bool
{
return (bool) $this->evaluate($this->shouldPartiallyRenderAfterActionsCalled);
}
}
+21 -7
View File
@@ -115,6 +115,8 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
protected bool $shouldMergeHydratedDefaultStateWithItemsStateAfterStateHydrated = true;
protected bool | Closure $shouldPartiallyRenderAfterActionsCalled = true;
protected function setUp(): void
{
parent::setUp();
@@ -202,7 +204,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->button()
->size(Size::Small)
@@ -279,7 +281,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->button()
->size(Size::Small)
@@ -341,7 +343,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -382,7 +384,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -422,7 +424,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -462,7 +464,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->iconButton()
->size(Size::Small)
@@ -505,7 +507,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
$component->callAfterStateUpdated();
$component->partiallyRender();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
})
->livewireClickHandlerEnabled(false)
->iconButton()
@@ -1375,4 +1377,16 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
return 1;
}
public function partiallyRenderAfterActionsCalled(bool | Closure $condition = true): static
{
$this->shouldPartiallyRenderAfterActionsCalled = $condition;
return $this;
}
public function shouldPartiallyRenderAfterActionsCalled(): bool
{
return (bool) $this->evaluate($this->shouldPartiallyRenderAfterActionsCalled);
}
}
@@ -178,6 +178,11 @@ trait HasFilters
return Arr::get($this->tableFilters, $this->parseTableFilterName($name));
}
public function getTableFilterFormState(string $name): ?array
{
return Arr::get($this->getTable()->hasDeferredFilters() ? $this->tableDeferredFilters : $this->tableFilters, $this->parseTableFilterName($name));
}
public function parseTableFilterName(string $name): string
{
if (! class_exists($name)) {
@@ -38,6 +38,11 @@ interface HasTable
*/
public function getTableFilterState(string $name): ?array;
/**
* @return array<string, mixed> | null
*/
public function getTableFilterFormState(string $name): ?array;
public function getSelectedTableRecords(bool $shouldFetchSelectedRecords = true, ?int $chunkSize = null): EloquentCollection | Collection | LazyCollection;
public function getSelectedTableRecordsQuery(bool $shouldFetchSelectedRecords = true, ?int $chunkSize = null): Builder;
@@ -33,4 +33,12 @@ trait BelongsToTable
{
return $this->getLivewire()->getTableFilterState($this->getName()) ?? [];
}
/**
* @return array<string, mixed>
*/
public function getFormState(): array
{
return $this->getLivewire()->getTableFilterFormState($this->getName()) ?? [];
}
}
+2 -3
View File
@@ -35,8 +35,7 @@ class QueryBuilder extends BaseFilter
->label($filter->getLabel())
->constraints($filter->getConstraints())
->blockPickerColumns($filter->getConstraintPickerColumns())
->blockPickerWidth($filter->getConstraintPickerWidth())
->live(onBlur: true),
->blockPickerWidth($filter->getConstraintPickerWidth()),
]);
$this->query(function (Builder $query, array $data): void {
@@ -57,7 +56,7 @@ class QueryBuilder extends BaseFilter
public function getActiveCount(): int
{
return $this->countRules($this->getState()['rules'], $this->getRuleBuilder());
return $this->countRules($this->getFormState()['rules'], $this->getRuleBuilder());
}
/**
@@ -107,7 +107,8 @@ class RuleBuilder extends Builder
->cloneable()
->reorderable(false)
->hiddenLabel()
->generateUuidUsing(fn (): string => Str::random(4)),
->generateUuidUsing(fn (): string => Str::random(4))
->partiallyRenderAfterActionsCalled($component->shouldPartiallyRenderAfterActionsCalled()),
]),
];
})
@@ -125,6 +126,8 @@ class RuleBuilder extends Builder
->expandAllAction(fn (Action $action) => $action->hidden())
->collapseAllAction(fn (Action $action) => $action->hidden())
->truncateBlockLabel(false)
->generateUuidUsing(fn (): string => Str::random(4));
->generateUuidUsing(fn (): string => Str::random(4))
->live(onBlur: true)
->partiallyRenderAfterActionsCalled(false);
}
}