Input elements of type search are text fields designed for the user to enter search queries into. These are functionally identical to text inputs but may be styled differently by the user agent.
Search fields have extra behaviors and use-cases that set them apart from regular text fields. This composable provides the behavior, state, and accessibility implementation for search fields.
A couple of behaviors set this apart from regular text fields:
The text content can be cleared with the clear button or a keyboard shortcut.
They are usually used without a parent form element and sometimes without a submit button. So they can be submitted with the Enter keyboard key on their own.
You can find more information about the differences here.
Features
Uses input[type="search"] element as a base.
Labeling, descriptions, and error message displays are automatically linked to input and label elements with aria-* attributes.
Support for a custom clear button.
Validation support with native HTML constraint validation or Standard Schema validation.
Support for v-model binding.
Supported Keyboard features:
Key
Description
Esc
Clears the input value.
↩Enter
If in a form, submits the form. Otherwise, submits the input with the onSubmit event or callback.
Anatomy
Label
Label
Value
Clear button
Input
Help text
Description or Error Message
Building a Search Field Component
The useSearchField composable returns binding objects for the elements shown in the anatomy. You will use v-bind to bind them to the corresponding DOM elements.
Notice that we imported the SearchFieldProps in the previous example. This is recommended to use as your component prop types. Not only do you get type safety for your component out of it, but it also handles the reactivity aspects of the props so you don’t have to. You are free to extend it with your own props or omit the ones you don’t need.
Validation
HTML Constraints
You can use the following native HTML validation properties to validate the search field:
Name
Type
Description
maxLength
number
The maximum length of characters.
minLength
number
The minimum length of characters.
required
boolean
Whether the text field is required.
pattern
string | RegExp
A regular expression for validation.
Here is an example of how to use the maxLength and minLength properties to limit the text length between 3 and 18 characters.
Assuming you have a SearchField component like the one shown above, you can use it like this:
Standard Schema
useSearchField also supports Standard Schema validation through the schema prop. This includes multiple providers like Zod, Valibot, Arktype, and more.
Mixed Validation
All search fields created with Formwerk support mixed validation, which means you can use both HTML constraints and Standard Schemas to validate the field, and they work seamlessly together.
Note that HTML constraints are validated first, so any errors from the HTML constraints will be displayed first. Then, once all HTML constraints are satisfied, the Standard Schema is validated.
This makes schemas lighter; however, we recommend sticking to one or the other per form for maintainability.
If you need to disable the native validation, you can do so by setting the disableHtmlValidation prop to true.
You can also disable it globally for all fields. For more information, check out the Validation guide.
Usage
Disabled
You can disable the search field by setting the disabled prop to true. Disabled fields are not editable, will not be validated, and will not be submitted with the form.
If you need to prevent the user from interacting with the field while still allowing it to submit, consider using readonly instead.
Readonly
Readonly fields are validated and submitted, but they do not accept user input. The field is still focusable, and the value is copyable. For more info, check the MDN.
RTL
The search field doesn’t really need much for RTL support; however, the dir prop can be used to set the direction of the field for convenience.
Submitting
Search fields can be used without a parent form element and sometimes without a submit button. The useSearchField accepts an onSubmit callback that is called when the user presses the Enter key on the field.
Note that empty search fields can be submitted, so you might want to validate the field before submitting.
API
Props
These are the properties that can be passed to the useSearchField composable.
Name
Type
Description
clearButtonLabel
The label text for the clear button.
description
The description text for the search field.
disabled
Whether the search field is disabled.
disableHtmlValidation
Whether to disable HTML5 form validation.
label
The label text for the search field.
maxLength
The maximum length of text allowed in the search field.
minLength
The minimum length of text required in the search field.
modelValue
The v-model value of the search field.
name
The name attribute for the search field input.
onSubmit
Handler called when the search field is submitted via the Enter key.
pattern
A regular expression pattern that the search field's value must match.
placeholder
Placeholder text shown when the search field is empty.
readonly
Whether the search field is readonly.
required
Whether the search field is required.
schema
Schema for search field validation.
value
The value attribute of the search field input.
Returns
These are the properties in the object returned by the useSearchField composable.