SearchField
A search input with a clear button. Esc clears the value and Enter submits it.
View in Storybook (opens in a new tab)
Installation
import { SearchField } from "@ginga-ui/core";Basic usage
The label is read by screen readers but not shown. A clear button appears when there is a value.
search-field.tsx
import { SearchField } from "@ginga-ui/core";
import { Search } from "lucide-react";
export function BasicExample() {
return (
<SearchField
label="Search"
placeholder="Enter keywords"
icon={<Search />}
/>
);
}Showing the label
Pass showLabel to show the label above the field. onSubmit is called when Enter is pressed.
Press Enter to search and Esc to clear
search-field.tsx
import { SearchField } from "@ginga-ui/core";
import { Search } from "lucide-react";
export function ShowLabelExample() {
return (
<SearchField
label="Search"
showLabel
placeholder="Enter keywords"
description="Press Enter to search and Esc to clear"
onSubmit={(value) => console.log("Search:", value)}
/>
);
}Icons
Use icon for a leading icon and clearIcon for the clear button.
search-field.tsx
import { SearchField } from "@ginga-ui/core";
import { Search, X } from "lucide-react";
export function IconExample() {
return (
<SearchField
label="Search components"
placeholder="Search components"
defaultValue="Ginga UI"
icon={<Search />}
clearIcon={<X />}
/>
);
}Disabled
Pass disabled to disable the field.
search-field.tsx
import { SearchField } from "@ginga-ui/core";
import { Search } from "lucide-react";
export function DisabledExample() {
return (
<SearchField
label="Search"
defaultValue="Ginga UI"
disabled
icon={<Search />}
/>
);
}Props
SearchField
| Name | Type | Default | Description |
|---|---|---|---|
label | string | - | Label of the field. It is read by screen readers and is visually hidden unless showLabel is set. Use aria-label instead when omitted. |
showLabel | boolean | false | Shows the label above the field. |
placeholder | string | - | Placeholder text of the input. |
description | ReactNode | - | Help text shown below the field. |
errorMessage | ReactNode | ((validation: ValidationResult) => ReactNode) | - | Error message shown when the value is invalid. |
disabled | boolean | false | Disables the field. |
required | boolean | false | Requires a value. |
readOnly | boolean | false | Makes the field read-only. |
invalid | boolean | false | Shows the field as invalid. |
icon | ReactNode | - | Icon shown before the input. |
clearIcon | ReactNode | - | Icon of the clear button. A cross is drawn when omitted. |
Also accepts the props of React Aria's SearchField (opens in a new tab).