Search form
A search box with a magnifier icon and a clear button; it works as a GET form that reloads the page or as a live Livewire search.
zb:search zubazar/ui · free (MIT)
Basic search
Typing shows a clear (×) button on the right; Esc clears the field too.
<zb:search name="q" placeholder="Search by customer, order no. or email" />
| Prop | Values |
|---|---|
clearable | false: turns off the clear button and Esc |
Search form that reloads the page
With action the component becomes a search form and sends the text to that address by GET, as ?q=.
<zb:search name="q" :value="request('q')" :action="route('customers.index')" button="Search" placeholder="Search customers">
<input type="hidden" name="status" value="{{ request('status') }}">
</zb:search>
| Prop | Values |
|---|---|
action | URL the form is submitted to |
method | GET (default) or POST; @csrf is added for POST when the URL is the application's own |
button | Text of the attached submit button |
Keyboard shortcut
Pressing the shortcut key anywhere on the page focuses the field.
<zb:search name="q" shortcut="/" placeholder="Search the menu" />
| Prop | Values |
|---|---|
shortcut | Key that focuses the field, for example "/" |
Live search with Livewire
With wire:model.live results arrive as the user types; a spinner replaces the magnifier while the request runs.
<zb:search wire:model.live.debounce.300ms="search" shortcut="/" placeholder="Search customers" />
| Prop | Values |
|---|---|
wire:model | The bound Livewire property; the field's id also comes from it |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
name | — | null | Field name (the query parameter in a GET form) |
placeholder | — | Search | Placeholder; without a visible label, the field's accessible name |
label | — | null | Visible label above the field |
action | — | null | When given, the component is a <form role="search">; otherwise a <div role="search"> |
method | — | GET | GET or POST; with POST, @csrf is added only for the application's own address |
button | — | null | Text of the attached submit button |
shortcut | — | null | Key that focuses the field, for example "/" |
clearable | boolean | true | Clear (×) button and clearing with Esc |
size | — | md | md (40 px) or lg (48 px) |
input-class | — | null | Classes of the field (<input>) itself |
class · style · x-show · wire:key | — | — | Added to the element around the whole search; other attributes go to the field |
Slots: The default slot is written inside the form, below the field: for hidden fields that keep other query parameters.
Advanced
Large size
size="lg" gives a larger, 48 px field for a page's main search (e.g. a help centre).
<zb:search name="question" size="lg" button="Search" placeholder="E.g. return policy, downloading invoices" />
| Prop | Values |
|---|---|
size | md (40 px, default) or lg (48 px) |
Search form with filters
Inside a form with other fields, leave out action and use the form's own submit button. label adds a visible label above the field.
<form wire:submit="search" class="grid items-end gap-4 md:grid-cols-[minmax(0,2fr)_minmax(0,1fr)_auto]">
<zb:search wire:model="term" label="Search term" placeholder="Name, phone or address" />
<zb:select wire:model="status" label="Status" :options="['' => 'All statuses', 'pending' => 'Pending', 'approved' => 'Approved']" />
<zb:button type="submit">Search</zb:button>
</form>
| Prop | Values |
|---|---|
label | Visible label above the field |
Developer notes
class,style,x-showandwire:keygo to the element around the whole search, other attributes (name,value,placeholder,autofocus…) to the field; style the field withinput-class.- Without a visible label,
placeholderis the field's name for screen readers. - To keep other query parameters in the form, write hidden fields inside the component.
- An unsafe
action(javascript:or another site's//address) is not written: the form searches the current page by GET, no CSRF token is added and a warning is logged. - The shortcut does nothing while typing in another field or with Ctrl/⌘; while the field is empty and not focused, a hint of the key shows on the right.
- The spinner shows only for this property's requests; when the server changes the value, the clear button and the shortcut hint update themselves.