# SearchInput

A search input provides a specialized text field for search functionality, featuring a search icon and optional clear button. It's optimized for search queries and includes built-in state management for search field behavior. SearchInput does not represent a persistent value and cannot be connected to a form.

## When to Use

- Enable users to search through large datasets or content
- Provide filtering capabilities in lists, tables, or catalogs
- Create search bars for navigation or content discovery
- Implement autocomplete or suggestion functionality
- Offer quick search access in headers or sidebars

## Component

---

### Properties

- **`value`** `string | null` — The search value in controlled mode. `null` clears the field and keeps it controlled
- **`defaultValue`** `string` — The default search value in uncontrolled mode
- **`isBuffered`** `boolean` (default: `true`) — Whether typed text is held locally until the controlled `value` catches up. This is what keeps the caret in place when the value round-trips through a store that re-renders a pass late; `onChange` still fires on every keystroke. Set to `false` for a caller that must see the field snap back to its own `value` the moment it declines a keystroke.
- **`placeholder`** `string` — Placeholder text when input is empty
- **`icon`** `ReactElement` — Icon element rendered before the input (default: search icon)
- **`prefix`** `ReactNode` — Content rendered before the input
- **`suffix`** — Input decoration after the main input (before clear button)
- **`tooltip`** `string | TooltipProviderProps` — The input's own tooltip, opened on hover over the field box and on keyboard focus. A disabled input with a tooltip stays focusable, so the keyboard reaches it too. See [TextInput](./TextInput.md#with-a-tooltip) for the behaviour. While it is open, the first `Escape` closes it and leaves the text; the next one clears it
- **`suffixPosition`** `'before' | 'after'` (default: `after`) — Position of suffix relative to validation/loading state icons
- **`isClearable`** `boolean` (default: `false`) — Whether the search input shows a clear button while it has text. `Escape` clears the input either way
- **`size`** `'small' | 'medium' | 'large'` (default: `medium`) — Input size
- **`shape`** `'default' | 'plain'` (default: `default`) — Shape of the field box. `plain` is square with no border at rest, for a field set flush in a container that draws its edges, such as a toolbar or a panel header. Its border keeps its width and shows on focus and in a valid or invalid state, so nothing shifts
- **`autoComplete`** `string` — HTML [`autocomplete`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete) token that tells the browser what to autofill (`off` to suppress autofill on a filter field). The lowercase `autocomplete` alias is deprecated.
- **`inputProps`** `Props` — Direct props passed to the native input element, merged over the props React Aria generates: a key you set wins, and event handlers are chained rather than replaced. The input sets `enterKeyHint="search"`, `autoCapitalize="none"`, `autoCorrect="off"` and `spellCheck={false}` by default, so a phone keyboard shows a Search key and neither capitalizes nor corrects a query; set any of them as a prop or here to change it
- **`wrapperProps`** `Props` — Direct props passed to the input wrapper element
- **`inputRef`** `RefObject<HTMLInputElement>` — Ref to the native input element. It is the same element the clear button returns focus to
- **`onChange`** `function` — Callback fired when the search value changes
- **`onSubmit`** `function` — Callback fired when search is submitted
- **`onClear`** `function` — Callback fired after the text is cleared, by the clear button or by `Escape`. Not called when there was no text to clear
- **`onBlur`** `function` — Callback fired when the input loses focus
- **`onFocus`** `function` — Callback fired when the input receives focus

### Base Properties

Supports [Base properties](../../BaseProperties.md)

### Field Properties

Supports the layout-related [Field properties](../../FieldProperties.md) (`label`, `description`, `message`, `labelPosition`, etc.). Note that SearchInput is not form-connected: it does not accept `name`/`rules` and does not participate in form state or validation.

### Styling Properties

#### styles

Customizes the root wrapper element of the component.

**Sub-elements:**

- `Prefix` - Element displayed before the input (contains search icon)
- `Suffix` - Element displayed after the input (contains clear button when shown)
- `State` - Container for validation state indicators
- `InputIcon` - Icon displayed within the input area
- `ValidationIcon` - Icon displayed for validation state

#### inputStyles

Customizes the search input field specifically.

### Style Properties

These properties allow direct style application without using the `styles` prop:

- **Position:** `gridArea`, `order`, `gridColumn`, `gridRow`, `placeSelf`, `alignSelf`, `justifySelf`, `zIndex`, `margin`, `blockMargin`, `inlineMargin`, `inset`, `blockInset`, `inlineInset`, `position`, `scrollMargin`, `blockScrollMargin`, `inlineScrollMargin`, `scrollPadding`, `blockScrollPadding`, `inlineScrollPadding`
- **Dimension:** `width`, `height`, `blockSize`, `inlineSize`, `flexBasis`, `flexGrow`, `flexShrink`, `flex`

### Modifiers

The `mods` property accepts the following modifiers you can override:

- **`focused`** `boolean` — Whether the input has focus
- **`disabled`** `boolean` — Whether the search input is disabled
- **`readonly`** `boolean` — Whether the search input is read-only: a recessed fill and a softer border, except in high contrast, and no hover state
- **`invalid`** `boolean` — Whether the search input has validation errors
- **`valid`** `boolean` — Whether the search input is valid
- **`hovered`** `boolean` — Whether the search input is being hovered
- **`inside-form`** `boolean` — Whether the search input is inside a form field
- **`shape`** `'default' | 'plain'` — Set from `shape`: `plain` drops the radius, and the border colour at rest.

## Variants

### Sizes

- `small` - Compact size for dense interfaces
- `medium` - Standard size
- `large` - Emphasized size for prominent search functionality

### Shapes

- `default` - A rounded field box with a border
- `plain` - A square field box with no border at rest, for a field set flush in a container that draws its edges, such as a toolbar or a panel header. The border still marks focus and validity.

```jsx
<Block styles={{ border: 'bottom', fill: '#surface' }}>
  <SearchInput shape="plain" width="100%" aria-label="Search models" placeholder="Search models" />
</Block>
```

## Examples

### Basic Usage

```jsx
<SearchInput
  label="Search"
  placeholder="Search products..."
/>
```

### With Clear Button

```jsx
<SearchInput
  label="Filter items"
  placeholder="Type to filter..."
  isClearable={true}
/>
```

### Controlled Search

```jsx
const [searchValue, setSearchValue] = useState('');

<SearchInput
  label="Search"
  value={searchValue}
  onChange={setSearchValue}
  placeholder="Search content..."
/>
```

### With Description

```jsx
<SearchInput
  label="Search Library"
  description="Search by title, author, or ISBN"
  placeholder="Enter search terms..."
/>
```

### Small Size

```jsx
<SearchInput
  size="small"
  placeholder="Quick search..."
  aria-label="Quick search"
/>
```

### With Validation

```jsx
<SearchInput
  label="Search Query"
  isInvalid
  errorMessage="Search query too short"
  placeholder="Enter at least 3 characters"
/>
```

### Disabled State

```jsx
<SearchInput
  label="Search (Offline)"
  isDisabled
  placeholder="Search unavailable"
/>
```

### With Custom Suffix

```jsx
<SearchInput
  label="Advanced Search"
  placeholder="Search with filters..."
  suffix={<FilterButton />}
/>
```

## Accessibility

### Keyboard Navigation

- `Tab` - Moves focus to the search input. The clear button is skipped: it is not in the `Tab` order
- `Escape` - Clears the search input and calls `onClear`, whether or not `isClearable` is set, as in a native search field. With the input already empty, `Escape` is left to the surrounding UI, so a `Dialog` closes on the next press. An `onKeyDown` that prevents it keeps it at the input, as in every field; one that doesn't leaves `Escape` and `Tab` going on to the surrounding UI
- `Enter` - Submits search or triggers onSubmit callback
- All standard text input keyboard shortcuts work for text entry

### Screen Reader Support

- Component announces as "search" input type to screen readers
- Search icon and clear button have appropriate labels
- Current search value and results count are announced when available
- `isInvalid` sets `aria-invalid` on the input. The loading indicator is visual only: `isLoading` is not announced

### ARIA Properties

- `aria-label` - Provides accessible label when no visible label exists
- `aria-labelledby` - References external label elements
- `aria-describedby` - References additional descriptive text
- `aria-invalid` - Indicates validation state
- `role="searchbox"` - Identifies the input as a search field
- Clear button includes appropriate `aria-label`

## Best Practices

1. **Do**: Provide clear placeholder text indicating what can be searched

   ```jsx
   <SearchInput placeholder="Search by name, email, or department..." />
   ```

2. **Don't**: Use search input for non-search related text entry

   ```jsx
   <SearchInput label="Enter your name" /> {/* Use TextInput instead */}
   ```

3. **Performance**: Debounce search queries to avoid excessive API calls
4. **UX**: Show search results or feedback immediately when possible
5. **Accessibility**: Always provide meaningful labels or aria-label
6. **Clear**: Enable clear functionality for better user experience

## Integration with Forms

SearchInput is not a form field. Use [TextInput](./TextInput.md) when the typed text has to be captured as a form value.

## Suggested Improvements

- Add built-in debouncing for search queries
- Implement search suggestions or autocomplete dropdown
- Add search history or recent searches functionality
- Include advanced search filters integration
- Add keyboard shortcuts for common search actions

## Related Components

- [TextInput](./TextInput.md) - For general text input
- [ComboBox](./ComboBox.md) - For searchable selection with predefined options
- [Select](./Select.md) - For selection without search functionality
