Skip to content

Commit ba7bcc5

Browse files
author
antoine
committed
add allowEmptySearch
1 parent c129140 commit ba7bcc5

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

demo/app/Sharp/Posts/PostForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public function buildFormFields(FieldsContainer $formFields): void
138138
SharpFormAutocompleteRemoteField::make('author_id')
139139
->setReadOnly(! auth()->user()->isAdmin())
140140
->setLabel('Author')
141+
->allowEmptySearch()
141142
->setRemoteCallback(function ($search) {
142143
$users = User::orderBy('name')->limit(10);
143144

docs/guide/form-fields/autocomplete.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ SharpFormAutocompleteRemoteField::make('customer')
6666
}, linkedFields: ['country']);
6767
```
6868

69+
### `allowEmptySearch()`
70+
71+
This method allows to call the endpoint / callback with empty search (on first click on the field for example). It's equivalent to `setSearchMinChars(0)`.
6972

7073
### `setSearchMinChars(int $searchMinChars)`
7174

resources/js/form/components/fields/Autocomplete.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
class="group/item"
169169
:value="item[props.field.itemIdAttribute]"
170170
>
171-
<div class="hidden absolute top-0 -left-1 h-full border-l border-primary group-data-[state=checked]/item:block"></div>
171+
<div class="hidden absolute top-0 -left-1 h-full border-l border-2 border-primary/50 group-data-[state=checked]/item:block"></div>
172172
<div v-html="item._html"></div>
173173
</CommandItem>
174174
</template>

src/Form/Fields/SharpFormAutocompleteRemoteField.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ public function setSearchMinChars(int $searchMinChars): self
9090

9191
return $this;
9292
}
93+
94+
public function allowEmptySearch(): self
95+
{
96+
$this->searchMinChars = 0;
97+
98+
return $this;
99+
}
93100

94101
public function setDebounceDelayInMilliseconds(int $debounceDelay): self
95102
{

src/Http/Controllers/Api/ApiFormAutocompleteController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ public function index(string $entityKey, string $autocompleteFieldKey)
5959
cookies: request()->cookies->all(),
6060
), fn (Request $request) => $request->headers->set('Accept', 'application/json'))
6161
);
62-
63-
$data = Arr::get(json_decode($response->getContent(), true), $field->dataWrapper() ?: null);
62+
6463
if ($response->getStatusCode() >= 400) {
6564
abort($response);
6665
}
66+
67+
$data = Arr::get(json_decode($response->getContent(), true), $field->dataWrapper() ?: null);
6768
}
6869

6970
return response()->json([

0 commit comments

Comments
 (0)