# Input

Capture user data with various forms of text input.

```tsx
import { Field, Input } from '@fluxui-dev/react';

<Field name="username">
    <Field.Label>Username</Field.Label>

    <Field.Description>This will be publicly displayed.</Field.Description>

    <Input />

    <Field.Error />
</Field>
```

## Shorthand

Because using the field component in its full form can be verbose and repetitive, all form controls in Flux allow you to pass a `label` and a `description` prop directly. Under the hood, they will be wrapped in a field with an error component automatically.

```tsx
<Input label="Username" description="This will be publicly displayed." />
```

## Class targeting

Unlike other form components, Flux's input component is composed of two underlying elements: an `input` element and a wrapper `div`. The wrapper div is there to add padding where icons should go.

This is typically fine, however, if you need to pass classes into the input component and have them directly applied to the input element, you can do so using the `inputClassName` prop instead of simply `className`:

```tsx
<Input className="max-w-xs" inputClassName="font-mono" />
```

## Types

Use the browser's various input types for different situations: `text`, `email`, `password`, etc.

```tsx
<Input type="email" label="Email" />
<Input type="password" label="Password" />
<Input type="date" max="2999-12-31" label="Date" />
```

## File

Flux provides a special input component for file uploads. It's a simple wrapper around the native `input[type="file"]` element.

```tsx
<Input type="file" label="Logo" />
<Input type="file" label="Attachments" multiple />
```

## Smaller

Use the `size` prop to make the input's height more compact.

```tsx
<Input size="sm" placeholder="Filter by..." />
```

## Disabled

Prevent users from interacting with an input by disabling it.

```tsx
<Input disabled label="Email" />
```

## Readonly

Useful for locking an input during a form submission.

```tsx
<Input readOnly variant="filled" />
```

## Invalid

Signal to users that the contents of an input are invalid.

```tsx
<Input invalid />
```

## Icons

Append or prepend an icon to the inside of a form input.

```tsx
import { CreditCardIcon, LoadingIcon, MagnifyingGlassIcon } from '@fluxui-dev/react/icons';

<Input icon={MagnifyingGlassIcon} placeholder="Search orders" />

<Input iconTrailing={CreditCardIcon} placeholder="4444-4444-4444-4444" />

<Input iconTrailing={LoadingIcon} placeholder="Search transactions" />
```

## Icon buttons

Pass an element such as a `Button` to `iconTrailing` to place a button inside the input.

```tsx
import { Button } from '@fluxui-dev/react';
import { EyeIcon, XMarkIcon } from '@fluxui-dev/react/icons';

<Input
    placeholder="Search orders"
    iconTrailing={<Button size="sm" variant="subtle" icon={XMarkIcon} className="-mr-1" />}
/>

<Input
    type="password"
    defaultValue="password"
    iconTrailing={<Button size="sm" variant="subtle" icon={EyeIcon} className="-mr-1" />}
/>
```

## Clearable, copyable, and viewable inputs

Flux provides three special input properties to configure common input button behaviors. `clearable` for clearing contents, `copyable` for copying contents, and `viewable` for toggling password visibility.

```tsx
import { KeyIcon } from '@fluxui-dev/react/icons';

<Input placeholder="Search orders" clearable />
<Input type="password" defaultValue="password" viewable />
<Input icon={KeyIcon} defaultValue="FLUX-1234-5678-ABCD-EFGH" readOnly copyable />
```

## Keyboard hint

Hint to users what keyboard shortcuts they can use with this input.

```tsx
<Input kbd="⌘K" icon={MagnifyingGlassIcon} placeholder="Search..." />
```

## With buttons

Attach buttons to the beginning or end of an input element.

```tsx
import { PlusIcon } from '@fluxui-dev/react/icons';

<Input.Group>
    <Input placeholder="Post title" />

    <Button icon={PlusIcon}>New post</Button>
</Input.Group>
```

## Text prefixes and suffixes

Append text inside a form input, or place it between two inputs with `Input.Group.Affix`.

```tsx
<Input.Group>
    <Input.Group.Prefix>https://</Input.Group.Prefix>

    <Input placeholder="example.com" />
</Input.Group>

<Input.Group>
    <Input placeholder="chunky-spaceship" />

    <Input.Group.Suffix>.brand.com</Input.Group.Suffix>
</Input.Group>

<Input.Group>
    <Input placeholder="Min" />

    <Input.Group.Affix>to</Input.Group.Affix>

    <Input placeholder="Max" />
</Input.Group>
```

## Input group labels

If you want to use an input group in a form field with a label, you will need to wrap the input group in a `Field` component.

```tsx
<Field name="website">
    <Field.Label>Website</Field.Label>

    <Input.Group>
        <Input.Group.Prefix>https://</Input.Group.Prefix>

        <Input placeholder="example.com" />
    </Input.Group>

    <Field.Error />
</Field>
```

## Reference

### Input

**Prop:**

- `label` - Label text displayed above the input. When provided, wraps the input in a `Field` component with an adjacent `Field.Label` component. See the [field component](https://fluxui.dev/react/components/field).
- `description` - Help text displayed above the input. When provided alongside `label`, appears between the label and input within the `Field` wrapper. See the [field component](https://fluxui.dev/react/components/field).
- `descriptionTrailing` - Help text displayed below the input. When provided alongside `label`, appears below the label and input within the `Field` wrapper. See the [field component](https://fluxui.dev/react/components/field).
- `badge` - Text displayed after the label as a badge (e.g., "Required").
- `error` - Error message displayed below the input, marking it invalid and defaulting to the validation error for the input's `name`.
- `value` - Controlled value of the input, paired with `onValueChange`.
- `defaultValue` - Initial value of the input when uncontrolled.
- `onValueChange` - Callback fired when the value changes, receiving the new value (`onChange` is also supported for the raw event).
- `type` - The HTML `type` of the input, such as `email`, `password`, `date`, or `file`. Default: `text`.
- `placeholder` - Placeholder text displayed when the input is empty.
- `size` - Size of the input. Options: `base` (default), `sm`, `xs`. A number sets the native `size` attribute.
- `variant` - Visual style variant. Options: `filled`. Default: `outline`.
- `disabled` - Prevents user interaction with the input.
- `readOnly` - Makes the input read-only.
- `invalid` - Applies error styling to the input.
- `multiple` - For file inputs, allows selecting multiple files.
- `icon` - Icon component (such as `MagnifyingGlassIcon`) or custom element displayed at the start of the input.
- `iconTrailing` - Icon component or custom element, such as a `Button`, displayed at the end of the input.
- `iconVariant` - Variant of the input's icons, including its clear, copy, view, and expand buttons: `outline`, `solid`, `mini`, or `micro`. Default: `mini`.
- `kbd` - Keyboard shortcut hint displayed at the end of the input.
- `loading` - If `true`, displays a loading spinner at the end of the input.
- `clearable` - If `true`, displays a clear button when the input has content.
- `copyable` - If `true`, displays a copy button to copy the input's content (https only).
- `viewable` - For password inputs, displays a toggle to show/hide the password.
- `className` - CSS classes applied to the wrapper element. With `label` set, they stay on the wrapper, between the label and the input; to space the whole field, [write the `Field` yourself](https://fluxui.dev/react/components/field#spacing-a-field) and pass it `className`.
- `inputClassName` - CSS classes applied directly to the input element instead of the wrapper.

**Attribute:**

- `data-flux-input` - Applied to the root element for styling and identification.

### Input.Group

**Prop:**

- `label, description, error` - Shorthand props that wrap the entire group in a single `Field`, so an error marks every input in the group invalid.

**Slot:**

- `children` - The input group content, typically containing an input and prefix/suffix elements.

### Input.Group.Prefix

**Prop:**

- `size` - Size of the prefix. Options: `base` (default), `sm`, `xs`.

**Slot:**

- `children` - Content displayed before the input (e.g., icons, text, buttons).

### Input.Group.Suffix

**Prop:**

- `size` - Size of the suffix. Options: `base` (default), `sm`, `xs`.

**Slot:**

- `children` - Content displayed after the input (e.g., icons, text, buttons).

### Input.Group.Affix

**Slot:**

- `children` - Text placed between controls in a group, such as "to" between a minimum and maximum input.

See the [field component documentation](https://fluxui.dev/react/components/field) for more information.
