# Input Group

> An input with buttons, icons or text attached.

An input group attaches an icon, text or a small button to an input, inside one border.

## When to use

- **Search:** a search icon before the input.
- **Password:** a button after the input that shows or hides the password. This is the Sosialoka password field; there is no separate password component.
- **Units and prefixes:** text such as `Rp` or `.id` before or after the value.
- Don't put the main action of a form inside the group; it goes in the form footer.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/input-group/_

## Anatomy

1. Leading icon
2. Input
3. Attached button

1. **Group** (`InputGroup`): one `input` border on `radius-md`, `h-9` tall. Focus on the input rings the whole group.
2. **Addon** (`InputGroupAddon`): an icon, text or button, at `inline-start` (before) or `inline-end` (after).
3. **Control** (`InputGroupInput` or `InputGroupTextarea`): the field itself, without its own border.

## Variants

| Addon position | Use it for |
| --- | --- |
| `inline-start` | A search icon, a currency such as `Rp`. |
| `inline-end` | The show-password button, a unit, a clear button. |
| `block-start` / `block-end` | A label or actions above or below an `InputGroupTextarea`. |

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `InputGroupAddon align` | `InputGroupAddon align` | `leading` / `trailing` on `ShadInput` | `inline-start`, `inline-end`, `block-start`, `block-end` | `inline-start` | Where the addon sits. |
| `InputGroupButton size` | `InputGroupButton size` | `ShadIconButton` in `trailing` | `xs`, `sm`, `icon-xs`, `icon-sm` | `xs` | A small button inside the group. |
| `type` on `InputGroupInput` | `type` on `InputGroupInput` | `obscureText` | text | `text` | `password` hides the text; `search` makes a search field. |

## Code

**React**
```tsx
import { useId, useState } from "react"
import { EyeIcon, EyeOffIcon, SearchIcon } from "lucide-react"
import { Field, FieldLabel } from "@/components/ui/field"
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group"

export default function InputGroupDemo() {
  const id = useId()
  const [visible, setVisible] = useState(false)
  return (
    <div className="grid w-full max-w-sm gap-6">
      <InputGroup>
        <InputGroupAddon>
          <SearchIcon />
        </InputGroupAddon>
        <InputGroupInput type="search" placeholder="Cari rilis" aria-label="Cari rilis" />
      </InputGroup>
      <Field>
        <FieldLabel htmlFor={`${id}-password`}>Kata sandi</FieldLabel>
        <InputGroup>
          <InputGroupInput id={`${id}-password`} type={visible ? "text" : "password"} autoComplete="current-password" />
          <InputGroupAddon align="inline-end">
            <InputGroupButton
              size="icon-xs"
              aria-label={visible ? "Sembunyikan kata sandi" : "Tampilkan kata sandi"}
              aria-pressed={visible}
              onClick={() => setVisible((v) => !v)}
            >
              {visible ? <EyeOffIcon /> : <EyeIcon />}
            </InputGroupButton>
          </InputGroupAddon>
        </InputGroup>
      </Field>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { ref, useId } from "vue"
import { EyeIcon, EyeOffIcon, SearchIcon } from "@lucide/vue"
import { Field, FieldLabel } from "@/components/ui/field"
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group"

const id = useId()
const visible = ref(false)
</script>

<template>
  <div class="grid w-full max-w-sm gap-6">
    <InputGroup>
      <InputGroupAddon>
        <SearchIcon />
      </InputGroupAddon>
      <InputGroupInput type="search" placeholder="Cari rilis" aria-label="Cari rilis" />
    </InputGroup>
    <Field>
      <FieldLabel :for="`${id}-password`">Kata sandi</FieldLabel>
      <InputGroup>
        <InputGroupInput :id="`${id}-password`" :type="visible ? 'text' : 'password'" autocomplete="current-password" />
        <InputGroupAddon align="inline-end">
          <InputGroupButton
            size="icon-xs"
            :aria-label="visible ? 'Sembunyikan kata sandi' : 'Tampilkan kata sandi'"
            :aria-pressed="visible"
            @click="visible = !visible"
          >
            <EyeOffIcon v-if="visible" />
            <EyeIcon v-else />
          </InputGroupButton>
        </InputGroupAddon>
      </InputGroup>
    </Field>
  </div>
</template>
```

**Flutter**
```dart
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class InputGroupDemo extends StatefulWidget {
  const InputGroupDemo({super.key});

  @override
  State<InputGroupDemo> createState() => _InputGroupDemoState();
}

class _InputGroupDemoState extends State<InputGroupDemo> {
  bool visible = false;

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 384),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        spacing: 24,
        children: [
          const ShadInput(
            placeholder: Text('Cari rilis'),
            leading: Icon(LucideIcons.search, size: 16),
          ),
          ShadInputFormField(
            id: 'password',
            label: const Text('Kata sandi'),
            obscureText: !visible,
            trailing: Semantics(
              label: visible ? 'Sembunyikan kata sandi' : 'Tampilkan kata sandi',
              toggled: visible,
              child: ShadIconButton.ghost(
                width: 24,
                height: 24,
                icon: Icon(visible ? LucideIcons.eyeOff : LucideIcons.eye, size: 16),
                onPressed: () => setState(() => visible = !visible),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/input-group.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/input-group.json
```

## Accessibility

- The input keeps its own label: a visible label in a Field, or an `aria-label` such as "Cari rilis" for a search field with only an icon.
- The show-password button says what it does: `aria-label` "Tampilkan kata sandi" / "Sembunyikan kata sandi", with `aria-pressed` while the password shows.
- Icons in addons are decorative and need no label.
