# Input

> A single-line text field.

An input takes one line of text, such as a name, an email address or a search term.

## When to use

- Use it for short, free text.
- Use [Select](https://design.sosialoka.tech/components/select/) when the answer comes from a short list.
- Always pair it with a [Label](https://design.sosialoka.tech/components/label/); a placeholder is not a label.

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

### Do and don't

- **Do:** Put the label above the field, where it stays while people type.
- **Don't:** Don't use the placeholder as the label; it disappears as soon as people type.

## Anatomy

1. Label above the field
2. Field with an input border
3. Value
4. Caret

Label above the field, the field itself, then an optional hint below it. The field uses `input` for its border, `radius-md` for its corners and `ring` for focus.

## Variants

There is one style. Invalid fields set `aria-invalid`, which turns the border and the focus ring `destructive`.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `value` / `defaultValue` | `v-model` / `default-value` | `controller` / `initialValue` | string | none | The text. |
| `type` | `type` | `keyboardType` | string | `text` | Keyboard and validation hints. |
| `disabled` | `disabled` | `enabled: false` | boolean | `false` | Blocks typing. |
| `aria-invalid` | `aria-invalid` | `validator` | boolean | `false` | Marks the value as wrong. |

## Code

**React**
```tsx
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"

export default function InputDemo() {
  return (
    <div className="grid w-full max-w-sm gap-2">
      <Label htmlFor="input-demo-email">Email</Label>
      <Input id="input-demo-email" type="email" aria-describedby="input-demo-hint" />
      <p id="input-demo-hint" className="text-sm text-muted-foreground">
        Ke sini kami kirim kabar penting untukmu.
      </p>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
</script>

<template>
  <div class="grid w-full max-w-sm gap-2">
    <Label for="input-demo-email-vue">Email</Label>
    <Input id="input-demo-email-vue" type="email" aria-describedby="input-demo-hint-vue" />
    <p id="input-demo-hint-vue" class="text-sm text-muted-foreground">
      Ke sini kami kirim kabar penting untukmu.
    </p>
  </div>
</template>
```

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

class InputDemo extends StatelessWidget {
  const InputDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 384),
      child: ShadInputFormField(
        id: 'email',
        label: const Text('Email'),
        keyboardType: TextInputType.emailAddress,
        description: const Text('Ke sini kami kirim kabar penting untukmu.'),
      ),
    );
  }
}
```

## Install

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

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

## Accessibility

- Link the label with `htmlFor` (React) or `for` (Vue) and the field's `id`.
- Link the hint with `aria-describedby` so screen readers read it.
- Error text comes from the backend; show it below the field and set `aria-invalid`.
