[//]: # (This file is auto-generated by the docs:generate-skills script, do not edit it manually)

# SearchField

Import: `import { SearchField } from '@neo4j-ndl/react'`

## Props

### SearchField

| Prop | Type | Required | Default | Description |
|------|------|----------|---------|-------------|
| `debounceDelay` | `number` |  | `500` | Delay in milliseconds before calling `onSearch` after the query changes. |
| `errorText` | `ReactNode` |  |  | Error message displayed below the input. When provided, it overrides `helpText` and shows an error icon |
| `helpText` | `ReactNode` |  |  | Assistive text displayed below the input |
| `isDisabled` | `boolean` |  |  | Whether the input is disabled |
| `isFluid` | `boolean` |  |  | Whether the input should take the full available width |
| `isLoading` | `boolean` |  |  | Whether to show the loading spinner |
| `isReadOnly` | `boolean` |  |  | Whether the input is read-only |
| `isSkeletonLoading` | `boolean` |  |  | Whether to render skeletons instead of content |
| `label` | `string` | ✅ |  | Accessible name for the search field. The label is announced by assistive technologies but is not rendered visually. |
| `onChange` | `ChangeEventHandler<HTMLInputElement, HTMLInputElement>` |  |  | Callback function triggered when the input value changes |
| `onSearch` | `((query: string) => void)` |  |  | Called with the search query after typing pauses for `debounceDelay`, and immediately when the user presses Enter or clears the field. |
| `placeholder` | `string` |  |  | Placeholder text displayed when the input is empty |
| `ref` | `Ref<HTMLInputElement>` |  |  | A ref to apply to the root element. |
| `size` | `'large' \| 'medium' \| 'small'` |  |  | Size of the input |
| `skeletonProps` | `(SkeletonProps & { as?: ElementType<any, keyof IntrinsicElements>; } & BaseProps<ElementType<any, keyof IntrinsicElements>>)` |  |  | Additional props forwarded to the underlying Skeleton components |
| `value` | `string \| number \| readonly string[]` |  |  | The current value of the input |

SearchField renders a fixed search icon and uses the native `search` input
type. `onChange` is called immediately, while `onSearch` is debounced until
typing pauses. Pressing Enter or clearing the field calls `onSearch`
immediately.

## Accessibility

## Implementation guidelines

### Name and search landmark

The required `label` prop provides the search field's accessible name through `aria-label`; it is not displayed visually. Use a concise label that identifies the search scope or purpose, such as “Search documentation” rather than only “Search,” when context is needed. Do not pass a separate `aria-label` through `htmlAttributes`.

SearchField uses the native `search` input type and is exposed with the `searchbox` role.

Wrap the field in an element with `role="search"` when it provides page- or site-wide search. If a page has multiple search landmarks, give each landmark a distinct accessible name with `aria-label` or `aria-labelledby`.

### Results and status messages

Present results with appropriate page structure, such as a heading followed by a list of links. Keep focus in the search field when results update; do not automatically move focus into the results. This lets keyboard and screen reader users decide when to navigate to them.

Announce the completed result count in a persistent, polite live region, for example, “7 results for Neo4j” or “No results for Neo4j.” A `role="status"` region with `aria-atomic="true"` supports WCAG 2.2 SC 4.1.3 Status Messages (Level AA). Mount the empty status region before a search starts, update only its text, and do not place interactive content inside it. 

For asynchronous searches, set `aria-busy="true"` on the results container while it is updating and remove it when the update completes. Communicate loading, failure, and recovery without clearing existing results prematurely. Avoid having multiple live regions announce the same update.

SearchField is not an autocomplete or combobox. If typing opens a selectable suggestions popup, use Combobox or implement the complete combobox keyboard and ARIA pattern rather than adding only `aria-expanded` to SearchField.

### Interaction

SearchField is always clearable. Pressing Escape clears a non-empty search field without moving focus. The same action is available with a pointer from the clear button.

`onChange` runs immediately so controlled fields can update their visible value as the user types. `onSearch` is debounced while typing, but runs immediately when the field is cleared or the user presses Enter.

If a consumer prevents the default Enter key behavior in `htmlAttributes.onKeyDown`, `onSearch` is not called.

## Examples

### Default

```tsx
import '@neo4j-ndl/base/lib/neo4j-ds-styles.css';

import { SearchField } from '@neo4j-ndl/react';

const Component = () => {
  return <SearchField label="Search" placeholder="Search for something" />;
};

export default Component;
```

### Debounced

```tsx
import '@neo4j-ndl/base/lib/neo4j-ds-styles.css';

import { SearchField, Typography } from '@neo4j-ndl/react';
import { useState } from 'react';

const Component = () => {
  const [query, setQuery] = useState('');

  return (
    <div>
      <SearchField
        label="Search"
        placeholder="Search for something"
        onSearch={setQuery}
      />
      <Typography as="p" variant="body-medium">
        Latest query: {query || 'None'}
      </Typography>
    </div>
  );
};

export default Component;
```

### Results

```tsx
import '@neo4j-ndl/base/lib/neo4j-ds-styles.css';

import { SearchField, Typography } from '@neo4j-ndl/react';
import { useEffect, useState } from 'react';

const documentationPages = [
  {
    description: 'Connect an application and run your first query.',
    href: '#getting-started',
    title: 'Getting started',
  },
  {
    description: 'Learn how nodes, relationships, and properties work.',
    href: '#graph-data-model',
    title: 'Graph data model',
  },
  {
    description: 'Read and write graph data with the Cypher query language.',
    href: '#cypher-manual',
    title: 'Cypher manual',
  },
  {
    description: 'Configure drivers, authentication, and connection pooling.',
    href: '#drivers',
    title: 'Neo4j drivers',
  },
  {
    description: 'Deploy and operate Neo4j in production.',
    href: '#operations',
    title: 'Operations manual',
  },
];

const Component = () => {
  const [isLoading, setIsLoading] = useState(false);
  const [query, setQuery] = useState<string | null>(null);
  const [resultMessage, setResultMessage] = useState(
    `${documentationPages.length} results`,
  );
  const [results, setResults] = useState(documentationPages);

  useEffect(() => {
    if (query === null) {
      return;
    }

    setIsLoading(true);

    const timeout = window.setTimeout(() => {
      const normalizedQuery = query.trim().toLocaleLowerCase();
      const nextResults = documentationPages.filter(({ description, title }) =>
        `${title} ${description}`.toLocaleLowerCase().includes(normalizedQuery),
      );
      const querySuffix = query ? ` for ${query}` : '';

      setResults(nextResults);
      setResultMessage(
        nextResults.length === 0
          ? `No results${querySuffix}`
          : `${nextResults.length} ${
              nextResults.length === 1 ? 'result' : 'results'
            }${querySuffix}`,
      );
      setIsLoading(false);
    }, 400);

    return () => {
      window.clearTimeout(timeout);
    };
  }, [query]);

  return (
    <main
      style={{
        display: 'grid',
        gap: '24px',
        maxWidth: '640px',
        width: '100%',
      }}
    >
      <div role="search" aria-label="Documentation">
        <SearchField
          label="Search documentation"
          placeholder="Search documentation"
          isLoading={isLoading}
          onSearch={setQuery}
        />
      </div>

      <section aria-labelledby="search-results-heading">
        <Typography
          as="h2"
          variant="title-3"
          htmlAttributes={{ id: 'search-results-heading' }}
        >
          Documentation
        </Typography>
        <Typography
          as="p"
          variant="body-medium"
          htmlAttributes={{ 'aria-atomic': 'true', role: 'status' }}
        >
          {resultMessage}
        </Typography>
        <div aria-busy={isLoading || undefined}>
          {results.length > 0 ? (
            <ul
              style={{
                display: 'grid',
                gap: '16px',
                listStyle: 'none',
                margin: '16px 0 0',
                padding: 0,
              }}
            >
              {results.map(({ description, href, title }) => (
                <li key={href}>
                  <Typography
                    as="a"
                    variant="body-medium"
                    htmlAttributes={{ href }}
                  >
                    {title}
                  </Typography>
                  <Typography
                    as="p"
                    variant="body-small"
                    style={{ margin: '4px 0 0' }}
                  >
                    {description}
                  </Typography>
                </li>
              ))}
            </ul>
          ) : null}
        </div>
      </section>
    </main>
  );
};

export default Component;
```
