Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/2.x' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Jul 3, 2024
2 parents 261dfef + 19c94fc commit 455be3b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Components/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function __construct(
protected bool $isSearchable = false,
public string $placement = 'bottom-start',
) {
if(empty($this->searchPlaceholder)){
$this->searchPlaceholder = __('moonshine::ui.search');
if(empty($this->searchPlaceholder)) {
$this->searchPlaceholder = __('moonshine::ui.search');
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/Http/Controllers/RelationModelFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ public function search(RelationModelFieldRequest $request): Response
}

$query = $model->newModelQuery();
$term = $request->input('query');

if (is_closure($field->asyncSearchQuery())) {
$query = value(
$field->asyncSearchQuery(),
$query,
$request,
$field
$field,
$term
);
}

$term = $request->input('query');
$values = $request->input($field->column(), '') ?? '';

$except = is_array($values)
Expand All @@ -78,7 +79,7 @@ public function search(RelationModelFieldRequest $request): Response
$offset = $request->input('offset', 0);

$query->when(
$term,
(is_null($field->asyncSearchQuery()) || $field->isAsyncSearchReplaceQuery() === false) && $term,
fn (Builder $q) => $q->where(
$searchColumn,
DBOperators::byModel($q->getModel())->like(),
Expand Down
14 changes: 12 additions & 2 deletions src/Traits/Fields/WithAsyncSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ trait WithAsyncSearch

protected ?Closure $asyncSearchQuery = null;

protected bool $asyncSearchReplaceQuery = false;

protected ?Closure $asyncSearchValueCallback = null;

protected array $withImage = [];
Expand Down Expand Up @@ -111,6 +113,11 @@ public function asyncSearchQuery(): ?Closure
return $this->asyncSearchQuery;
}

public function isAsyncSearchReplaceQuery(): bool
{
return $this->asyncSearchReplaceQuery;
}

public function asyncSearchValueCallback(): ?Closure
{
return $this->asyncSearchValueCallback;
Expand Down Expand Up @@ -169,6 +176,7 @@ public function asyncSearch(
?Closure $asyncSearchValueCallback = null,
?string $associatedWith = null,
?string $url = null,
bool $replaceQuery = false,
): static {
$this->asyncSearch = true;
$this->searchable = true;
Expand All @@ -178,6 +186,7 @@ public function asyncSearch(
$this->asyncSearchValueCallback = $asyncSearchValueCallback ?? $this->formattedValueCallback();
$this->associatedWith = $associatedWith;
$this->asyncUrl = $url;
$this->asyncSearchReplaceQuery = $replaceQuery;

if ($this->associatedWith) {
$this->customAttributes([
Expand All @@ -199,13 +208,14 @@ public function asyncSearch(
/**
* @param ?Closure(Builder $query, mixed $value, self $field): Builder $asyncSearchQuery
*/
public function associatedWith(string $column, ?Closure $asyncSearchQuery = null): static
public function associatedWith(string $column, ?Closure $asyncSearchQuery = null, bool $replaceQuery = false): static
{
$searchQuery = static fn (Builder $query, Request $request) => $query->where($column, $request->input($column));

return $this->asyncSearch(
asyncSearchQuery: is_null($asyncSearchQuery) ? $searchQuery : $asyncSearchQuery,
associatedWith: $column
associatedWith: $column,
replaceQuery: $replaceQuery
);
}
}

0 comments on commit 455be3b

Please sign in to comment.