shadcn.io is not affiliated with official shadcn/ui
Form examples · Basic Forms
Search Input Form for React
A search form with one search input that can't be submitted empty.
An empty search wastes a request and returns nothing useful. This form requires at least one character with Zod and uses type="search", which gives the field the browser's clear button. A Controller connects the Input to React Hook Form inside a Field, with a FieldDescription and a FieldError for "Please enter a search query." Use it for site search, product filters or documentation lookups.
Preview
Installation
FAQ
What counts as an empty search?
query is z.string().min(1, "Please enter a search query."). Any one character passes, a space included. There is no maximum. Add .trim() before .min(1) to reject spaces only.
Why is the input type search?
type="search" tells browsers and assistive tech it's a search field, and many browsers add a clear button. The label is "Search" and the placeholder "Search for anything...".
What does the description say?
FieldDescription reads "Enter keywords to search our database." FieldError is the separate line under it for the empty-query message.
How does the query reach onSubmit?
defaultValues sets query to "". The Controller named query spreads field onto the Input. Search is a type="submit" button, so Enter in the field submits too, and the handler logs the values.
Related
- Search Filter FormA product search form with a query, a
- Simple Text Input FormA form with one username text input that
- Search InputA search field with a magnifier icon
- Inline FormAn inline newsletter form with an email
- Email Input FormA newsletter form with one email input
- URL Input FormA form with one website input that only