# Input group

> A text field with an icon, unit, keyboard hint or small button inside its border.

Category: Inputs · Atomic level: molecule
Page: https://ui.dtheo.dev/docs/components/input-group

## Install

```bash
npx dtheo@latest add input-group
# or, with the shadcn CLI
npx shadcn@latest add https://ui.dtheo.dev/r/input-group.json
```

Dependencies: clsx, tailwind-merge. Registry dependencies: utils, input.

## Exports

`InputGroup`, `InputGroupAddon`

## Use it for

- Search fields with an icon and shortcut
- Values with a fixed unit (N·m, ms)

## Reach for something else

- Decorating with icons that add no meaning
- Units the person must type themselves: put the unit in the group, not in the value

## Accessibility

- Addons are decorative; the input still needs its own label.

## Examples

### input-group

```tsx
import * as React from 'react';
import { SearchIcon, XIcon } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Field, FieldLabel } from '@/components/ui/field';
import { Input } from '@/components/ui/input';
import { InputGroup, InputGroupAddon } from '@/components/ui/input-group';
import { Kbd } from '@/components/ui/kbd';

export default function InputGroupExample() {
  const [query, setQuery] = React.useState('');
  const searchRef = React.useRef<HTMLInputElement>(null);

  // "/" focuses the search field, unless the user is already typing somewhere.
  React.useEffect(() => {
    const onKey = (e: KeyboardEvent) => {
      const target = e.target as HTMLElement;
      if (e.key !== '/' || target.closest('input, textarea, [contenteditable="true"]')) return;
      e.preventDefault();
      searchRef.current?.focus();
    };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, []);

  return (
    <div className="flex w-full max-w-sm flex-col gap-4">
      <Field>
        <FieldLabel>Search runs</FieldLabel>
        <InputGroup>
          <InputGroupAddon>
            <SearchIcon aria-hidden="true" />
          </InputGroupAddon>
          <Input
            ref={searchRef}
            value={query}
            onValueChange={setQuery}
            placeholder="Run, bench or operator"
          />
          {query ? (
            <Button variant="ghost" size="icon-xs" aria-label="Clear search" onClick={() => setQuery('')}>
              <XIcon aria-hidden="true" />
            </Button>
          ) : (
            <InputGroupAddon>
              <Kbd>/</Kbd>
            </InputGroupAddon>
          )}
        </InputGroup>
      </Field>
      <div className="grid grid-cols-2 gap-3">
        <Field>
          <FieldLabel>Torque limit</FieldLabel>
          <InputGroup>
            <Input defaultValue="12.0" inputMode="decimal" className="tabular-nums" />
            <InputGroupAddon>N·m</InputGroupAddon>
          </InputGroup>
        </Field>
        <Field>
          <FieldLabel>Settling target</FieldLabel>
          <InputGroup>
            <Input defaultValue="250" inputMode="numeric" className="tabular-nums" />
            <InputGroupAddon>ms</InputGroupAddon>
          </InputGroup>
        </Field>
      </div>
    </div>
  );
}
```

## Used in

- Data table pattern (template): https://ui.dtheo.dev/docs/patterns/data-table
