Skip to content

Commit

Permalink
Add options limit for attach/associate
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Nov 30, 2023
1 parent e2a707f commit 09d226a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/tables/src/Actions/AssociateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function getRecordSelect(): Select
{
$table = $this->getTable();

$getOptions = function (?string $search = null, ?array $searchColumns = []) use ($table): array {
$getOptions = function (int $optionsLimit, ?string $search = null, ?array $searchColumns = []) use ($table): array {
/** @var HasMany | MorphMany $relationship */
$relationship = Relation::noConstraints(fn () => $table->getRelationship());

Expand All @@ -188,6 +188,10 @@ public function getRecordSelect(): Select
]) ?? $relationshipQuery;
}

if (! isset($relationshipQuery->getQuery()->limit)) {
$relationshipQuery->limit($optionsLimit);
}

$titleAttribute = $this->getRecordTitleAttribute();
$titleAttribute = filled($titleAttribute) ? $relationshipQuery->qualifyColumn($titleAttribute) : null;

Expand Down Expand Up @@ -263,9 +267,9 @@ public function getRecordSelect(): Select
->label(__('filament-actions::associate.single.modal.fields.record_id.label'))
->required()
->searchable($this->getRecordSelectSearchColumns() ?? true)
->getSearchResultsUsing(static fn (Select $component, string $search): array => $getOptions(search: $search, searchColumns: $component->getSearchColumns()))
->getSearchResultsUsing(static fn (Select $component, string $search): array => $getOptions(optionsLimit: $component->getOptionsLimit(), search: $search, searchColumns: $component->getSearchColumns()))
->getOptionLabelUsing(fn ($value): string => $this->getRecordTitle(Relation::noConstraints(fn () => $table->getRelationship())->getQuery()->find($value)))
->options(fn (): array => $this->isRecordSelectPreloaded() ? $getOptions() : [])
->options(fn (Select $component): array => $this->isRecordSelectPreloaded() ? $getOptions(optionsLimit: $component->getOptionsLimit()) : [])
->hiddenLabel();

if ($this->modifyRecordSelectUsing) {
Expand Down
10 changes: 7 additions & 3 deletions packages/tables/src/Actions/AttachAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function getRecordSelect(): Select
{
$table = $this->getTable();

$getOptions = function (?string $search = null, ?array $searchColumns = []) use ($table): array {
$getOptions = function (int $optionsLimit, ?string $search = null, ?array $searchColumns = []) use ($table): array {
/** @var BelongsToMany $relationship */
$relationship = Relation::noConstraints(fn () => $table->getRelationship());

Expand All @@ -192,6 +192,10 @@ public function getRecordSelect(): Select
]) ?? $relationshipQuery;
}

if (! isset($relationshipQuery->getQuery()->limit)) {
$relationshipQuery->limit($optionsLimit);
}

$titleAttribute = $this->getRecordTitleAttribute();
$titleAttribute = filled($titleAttribute) ? $relationshipQuery->qualifyColumn($titleAttribute) : null;

Expand Down Expand Up @@ -261,15 +265,15 @@ public function getRecordSelect(): Select
->label(__('filament-actions::attach.single.modal.fields.record_id.label'))
->required()
->searchable($this->getRecordSelectSearchColumns() ?? true)
->getSearchResultsUsing(static fn (Select $component, string $search): array => $getOptions(search: $search, searchColumns: $component->getSearchColumns()))
->getSearchResultsUsing(static fn (Select $component, string $search): array => $getOptions(optionsLimit: $component->getOptionsLimit(), search: $search, searchColumns: $component->getSearchColumns()))
->getOptionLabelUsing(function ($value) use ($table): string {
$relationship = Relation::noConstraints(fn () => $table->getRelationship());

$relationshipQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints($relationship);

return $this->getRecordTitle($relationshipQuery->find($value));
})
->options(fn (): array => $this->isRecordSelectPreloaded() ? $getOptions() : [])
->options(fn (Select $component): array => $this->isRecordSelectPreloaded() ? $getOptions(optionsLimit: $component->getOptionsLimit()) : [])
->hiddenLabel();

if ($this->modifyRecordSelectUsing) {
Expand Down

0 comments on commit 09d226a

Please sign in to comment.