# SearchBar

## Introduction

A search input component combining a text field and a search button. Optionally includes a category Select that lets users narrow results by keyword type. Hovering over the input while it has a value reveals a clear (✕) button to reset the field. Sizes to its content by default (`inline-flex`) and accepts all `Box` props for layout control.

```tsx
<SearchBar value={value} onChange={setValue} />
```

| Field       | Description | Default |
| ----------- | ----------- | ------- |
| showSelect  | —           | —       |
| options     | —           | —       |
| placeholder | —           | —       |
| value       | —           | —       |
| onChange    | —           | —       |
| onSearch    | —           | —       |

## Usage

```tsx
import { SearchBar } from '@ceed/cds';

// Basic
<SearchBar value={query} onChange={setQuery} onSearch={({ inputValue }) => fetch(inputValue)} />

// With category Select
<SearchBar
  showSelect
  options={[
    { label: 'Account #', value: 'account', placeholder: 'e.g. 1234567' },
    { label: 'Jira Issue #', value: 'jira', placeholder: 'e.g. PROC-1234' },
  ]}
  value={query}
  onChange={setQuery}
  onSearch={({ selectValue, inputValue }) => fetch(selectValue, inputValue)}
/>
```

## With Select

Use `showSelect` together with `options` to display a category Select to the left of the text input. The selected category is passed as `selectValue` in the `onSearch` callback.

```tsx
<SearchBar showSelect options={SAMPLE_OPTIONS} value={value} onChange={setValue} />
```

## Clearable Input

When the input has a value, hovering over the component reveals a clear (✕) button at the right of the text field. Clicking it calls `onChange('')` without triggering `onSearch`.

```tsx
<Stack alignItems="flex-start" gap={2}>
    <Typography level="body-sm">Hover over the input to reveal the clear (✕) button.</Typography>
    <SearchBar value={value} onChange={setValue} />
  </Stack>
```

## Placeholder

Each `SearchBarOption` can include a `placeholder` field that is shown in the text input while that category is selected. Pass a `placeholder` prop directly to override the option-level placeholder for all categories.

```tsx
<Stack alignItems="flex-start" gap={3}>
    <Stack alignItems="flex-start" gap={1}>
      <Typography level="body-xs" fontWeight="md">
        No placeholder prop — uses the active option's placeholder
      </Typography>
      <SearchBar showSelect options={SAMPLE_OPTIONS} value={value} onChange={setValue} />
    </Stack>
    <Stack alignItems="flex-start" gap={1}>
      <Typography level="body-xs" fontWeight="md">
        placeholder="Search by keyword" — overrides option-level placeholder
      </Typography>
      <SearchBar
        showSelect
        options={SAMPLE_OPTIONS}
        placeholder="Search by keyword"
        value={value}
        onChange={setValue}
      />
    </Stack>
  </Stack>
```

## onSearch

`onSearch` fires when the search button is clicked or the Enter key is pressed. It receives `inputValue` (always present) and `selectValue` (only present when `showSelect` is `true`).

```tsx
<Stack alignItems="flex-start" gap={2}>
    <SearchBar
      showSelect
      options={SAMPLE_OPTIONS}
      value={value}
      onChange={setValue}
      onSearch={setLastSearch}
    />
    <Typography level="body-sm">value: "{value}"</Typography>
    {lastSearch && (
      <Typography level="body-sm">
        onSearch: [{lastSearch.selectValue}] "{lastSearch.inputValue}"
      </Typography>
    )}
  </Stack>
```

When `showSelect` is `false`, `selectValue` is omitted from the payload entirely — not `undefined` as a key, but absent.

```tsx
<Stack alignItems="flex-start" gap={2}>
    <Typography level="body-sm">
      When <code>showSelect</code> is <code>false</code>, <code>selectValue</code> is omitted from
      the <code>onSearch</code> payload.
    </Typography>
    <SearchBar value={value} onChange={setValue} onSearch={setLastSearch} />
    {lastSearch && (
      <Stack alignItems="flex-start" gap={0.5}>
        <Typography level="body-sm">inputValue: "{lastSearch.inputValue}"</Typography>
        <Typography
          level="body-sm"
          sx={{
            color: lastSearch.selectValue === undefined ? "success.500" : "danger.500"
          }}
        >
          selectValue:{" "}
          {lastSearch.selectValue === undefined ? "undefined ✅" : `"${lastSearch.selectValue}" ❌`}
        </Typography>
      </Stack>
    )}
  </Stack>
```

## Width

SearchBar sizes to its content by default. Pass any `Box` width prop to constrain or stretch it.

```tsx
<Stack alignItems="flex-start" gap={3}>
    <Stack alignItems="flex-start" gap={1}>
      <Typography level="body-xs" fontWeight="md">
        width="100%" — stretches to parent width
      </Typography>
      <Box
        sx={{
          border: "1px dashed",
          borderColor: "neutral.300",
          padding: 1
        }}
      >
        <SearchBar width="100%" value={value} onChange={setValue} />
      </Box>
    </Stack>
    <Stack alignItems="flex-start" gap={1}>
      <Typography level="body-xs" fontWeight="md">
        width={400} — fixed 400px
      </Typography>
      <SearchBar width={400} value={value} onChange={setValue} />
    </Stack>
    <Stack alignItems="flex-start" gap={1}>
      <Typography level="body-xs" fontWeight="md">
        No width prop — sizes to content (inline-flex default)
      </Typography>
      <SearchBar value={value} onChange={setValue} />
    </Stack>
  </Stack>
```

## Props and Customization

| Prop          | Type                                                             | Default | Description                                                                         |
| ------------- | ---------------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------- |
| `value`       | `string`                                                         | —       | Current value of the text input. Required.                                          |
| `onChange`    | `(value: string) => void`                                        | —       | Called when the text input value changes. Required.                                 |
| `onSearch`    | `(params: { selectValue?: string; inputValue: string }) => void` | —       | Called on search button click or Enter key.                                         |
| `showSelect`  | `boolean`                                                        | `false` | Show the category Select alongside the input.                                       |
| `options`     | `SearchBarOption[]`                                              | —       | Category options for the Select. Required when `showSelect` is `true`.              |
| `placeholder` | `string`                                                         | —       | Placeholder text for the input. Takes priority over the option-level `placeholder`. |

> **Note**: Also accepts all `Box` props (`width`, `sx`, `className`, `style`, etc.).

### SearchBarOption

| Field         | Type     | Required | Description                                                                                                                             |
| ------------- | -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `label`       | `string` | ✓        | Display text shown in the Select dropdown.                                                                                              |
| `value`       | `string` | ✓        | Identifier passed as `selectValue` in the `onSearch` callback.                                                                          |
| `placeholder` | `string` | —        | Hint text shown in the input while this category is selected (e.g. `"e.g. PROC-1234"`). Overridden by the top-level `placeholder` prop. |

## Best Practices

- Always provide `options` when `showSelect` is `true` — the Select renders nothing if `options` is omitted.
- Add a `placeholder` to each option to show the expected input format for that category.
- Use the top-level `placeholder` prop only when a single hint applies regardless of category.
- Prefer handling `onSearch` over `onChange` for server-side queries — fire the request once on explicit submission rather than on every keystroke.

## Accessibility

- The search button has `aria-label="Search"` and the clear button has `aria-label="Clear"`.
- The text input is a native `<input>` element; pressing Enter triggers `onSearch`.
- The clear button uses `onMouseDown` with `e.preventDefault()` to prevent the input from losing focus when clicked.
