> Leaf UI 0.17.3 · Package: @ifhover/leaf-ui · Source: c3221834f84f2f48481a2054026b713456f8e5dc
> Browser documentation: https://ifhover.github.io/leaf-ui/en/components/input.html

> For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/en/llms-full.txt.

# Input

Enter single-line text, such as a name, email address or search query.


## Basic usage

Use `FormField` to provide an accessible label. Placeholder text is not a replacement for a label.


Project name

basic.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/basic.tsx"
import { Form, FormField, Input } from '@ifhover/leaf-ui';

export function InputBasic() {
  return (
    <Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
      <FormField label="Project name">
        <Input name="project" placeholder="Name your project" />
      </FormField>
    </Form>
  );
}
```

Expand code


## Examples

### Prefixes and suffixes

Add an icon or text with `prefix` and `suffix`. These are decorative; place interactive controls outside the input.


Search projects

Domain

.com

affixes.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/affixes.tsx"
import { Form, FormField, Input } from '@ifhover/leaf-ui';
import { Search } from 'lucide-react';

export function InputAffixes() {
  return (
    <Form layout="vertical">
      <FormField label="Search projects">
        <Input prefix={<Search />} placeholder="Enter a keyword" />
      </FormField>
      <FormField label="Domain">
        <Input suffix=".com" placeholder="example" />
      </FormField>
    </Form>
  );
}
```

Expand code

### Password input

Set `type="password"` to mask the input. Control visibility with `visible` and `onVisibleChange`, customize the toggle with `visibilityIcon`, or hide it with `visibilityToggle={false}`.


Password

Controlled password

No visibility toggle

password.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/password.tsx"
import { Form, FormField, Input } from '@ifhover/leaf-ui';
import { Eye, EyeOff } from 'lucide-react';
import { useState } from 'react';

export function InputPassword() {
  const [visible, setVisible] = useState(false);
  return (
    <Form layout="vertical">
      <FormField label="Password">
        <Input type="password" placeholder="Enter password" autoComplete="new-password" />
      </FormField>
      <FormField label="Controlled password">
        <Input
          type="password"
          defaultValue="leaf-garden"
          visible={visible}
          onVisibleChange={setVisible}
          visibilityIcon={(shown) => (shown ? <EyeOff size={16} /> : <Eye size={16} />)}
        />
      </FormField>
      <FormField label="No visibility toggle">
        <Input type="password" placeholder="No visibility toggle" visibilityToggle={false} />
      </FormField>
    </Form>
  );
}
```

Expand code

### Clear input

Set `allowClear` to show a clear button. Clearing fires `onChange` with an empty input value.


Project name

Current text: Leaf Garden

clear.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/clear.tsx"
import { Form, FormField, Input, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function InputClear() {
  const [value, setValue] = useState('Leaf Garden');

  return (
    <Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
      <FormField label="Project name">
        <Input value={value} onChange={(event) => setValue(event.target.value)} allowClear />
      </FormField>
      <Text tone="muted" role="status">
        Current text: {value || 'Empty'}
      </Text>
    </Form>
  );
}
```

Expand code

### Character count

Use `showCount` for a built-in or custom count, and `maxLength` to limit input length.


Default count

11 / 30

Custom count

4 characters

count.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/count.tsx"
import { Form, FormField, Input } from '@ifhover/leaf-ui';

export function InputCount() {
  return (
    <Form layout="vertical">
      <FormField label="Default count">
        <Input defaultValue="Leaf Garden" showCount maxLength={30} />
      </FormField>
      <FormField label="Custom count">
        <Input defaultValue="Leaf" showCount={(value) => `${value.length} characters`} />
      </FormField>
    </Form>
  );
}
```

Expand code

### Sizes

Choose from `sm`, `md` and `lg`.


Small

Medium

Large

sizes.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/sizes.tsx"
import { Form, FormField, Input } from '@ifhover/leaf-ui';

export function InputSizes() {
  return (
    <Form layout="vertical">
      <FormField label="Small">
        <Input size="sm" placeholder="Project name" />
      </FormField>
      <FormField label="Medium">
        <Input size="md" placeholder="Project name" />
      </FormField>
      <FormField label="Large">
        <Input size="lg" placeholder="Project name" />
      </FormField>
    </Form>
  );
}
```

Expand code

### States

`disabled` prevents interaction; `readOnly` keeps content readable without allowing edits. `status` changes appearance without performing validation. Use `FormField` for error messages.


Read only

Disabled

Error

Project name is required.

Warning

states.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/states.tsx"
import { Form, FormField, Input } from '@ifhover/leaf-ui';

export function InputStates() {
  return (
    <Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
      <FormField label="Read only">
        <Input defaultValue="Leaf Garden" readOnly />
      </FormField>
      <FormField label="Disabled">
        <Input disabled placeholder="Currently unavailable" />
      </FormField>
      <FormField label="Error" error="Project name is required.">
        <Input placeholder="Enter a project name" />
      </FormField>
      <FormField label="Warning">
        <Input status="warning" defaultValue="A similar name already exists" />
      </FormField>
    </Form>
  );
}
```

Expand code

### Controlled input and Enter

[](#)
Manage content with `value` and `onChange`. `onPressEnter` runs on Enter, except during IME composition or when the keyboard event has been prevented.


Enter and submit

Submitted: Waiting for input

controlled.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/controlled.tsx"
import { Form, FormField, Input, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function InputControlled() {
  const [value, setValue] = useState('');
  const [submitted, setSubmitted] = useState('');
  return (
    <Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
      <FormField label="Enter and submit">
        <Input
          value={value}
          placeholder="Type and press Enter"
          onChange={(event) => setValue(event.target.value)}
          onPressEnter={() => setSubmitted(value)}
        />
      </FormField>
      <Text tone="muted" role="status">
        Submitted: {submitted || 'Waiting for input'}
      </Text>
    </Form>
  );
}
```

Expand code

### Search input

`InputSearch` calls `onSearch` with the current value when its search button is clicked or Enter is pressed.


Search projects

Search

Search: No search yet

search.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/search.tsx"
import { Form, FormField, InputSearch, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function InputSearchDemo() {
  const [query, setQuery] = useState('');

  return (
    <Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
      <FormField label="Search projects">
        <InputSearch placeholder="Enter a keyword" enterButton="Search" onSearch={setQuery} />
      </FormField>
      <Text tone="muted" role="status">
        Search: {query || 'No search yet'}
      </Text>
    </Form>
  );
}
```

Expand code

### Input groups

Use `InputGroup` to join adjacent controls. Give each input in the group its own accessible name.


URL

group.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/input/group.tsx"
import { Form, FormField, Input, InputGroup, Select } from '@ifhover/leaf-ui';
import { useId } from 'react';

export function InputGroupDemo() {
  const protocolId = useId();
  const domainId = useId();

  return (
    <Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
      <FormField label="URL" htmlFor={domainId}>
        <InputGroup aria-label="URL">
          <Select
            id={protocolId}
            aria-label="Protocol"
            style={{ width: 110 }}
            defaultValue="https"
            options={[
              { value: 'https', label: 'https://' },
              { value: 'http', label: 'http://' },
            ]}
          />
          <Input id={domainId} aria-label="Domain" placeholder="example.com" />
        </InputGroup>
      </FormField>
    </Form>
  );
}
```

Expand code


## API

### Input API

#### Forms

Set `name` to include the value in FormData. Uncontrolled fields restore their initial value on form reset; reset controlled values in application state. See [Form](https://ifhover.github.io/leaf-ui/en/components/form.md) for labels, help and errors.

| Property                     | Type                                                            | Default  | Description                                   |
| ---------------------------- | --------------------------------------------------------------- | -------- | --------------------------------------------- |
| `size`                       | `'sm' \| 'md' \| 'lg'`                                          | `'md'`   | Control size                                  |
| `status`                     | `'error' \| 'warning'`                                          | —        | Validation appearance                         |
| `prefix` / `suffix`          | `ReactNode`                                                     | —        | Decorative content before / after the input   |
| `onPressEnter`               | `KeyboardEventHandler<HTMLInputElement>`                        | —        | Runs when Enter is pressed                    |
| `type`                       | `HTMLInputTypeAttribute`                                        | `'text'` | Input type; use `'password'` for passwords    |
| `visibilityToggle`           | `boolean`                                                       | `true`   | Show the password visibility toggle           |
| `visible` / `defaultVisible` | `boolean`                                                       | `false`  | Controlled / initial password visibility      |
| `onVisibleChange`            | `(visible: boolean) => void`                                    | —        | Password visibility change                    |
| `visibilityIcon`             | `(visible: boolean) => ReactNode`                               | —        | Custom visibility toggle icon                 |
| `value` / `defaultValue`     | `string \| number \| readonly string[]`                         | —        | Controlled value / initial uncontrolled value |
| `onChange`                   | `ChangeEventHandler<HTMLInputElement>`                          | —        | Read event.target.value                       |
| `disabled` / `readOnly`      | `boolean`                                                       | `false`  | Disabled / read-only                          |
| `ref`                        | `Ref<HTMLInputElement>`                                         | —        | DOM reference                                 |
| `allowClear`                 | `boolean`                                                       | `false`  | Show clear button                             |
| `onClear`                    | `() => void`                                                    | —        | After clearing; onChange also fires           |
| `showCount`                  | `boolean \| ((value: string, maxLength?: number) => ReactNode)` | `false`  | Show or customize the character count         |

### InputSearchProps API

Props for the submission behavior and actions of a search input.

| Property           | Type                                                            | Default | Description                                |
| ------------------ | --------------------------------------------------------------- | ------- | ------------------------------------------ |
| `size`             | `'sm' \| 'md' \| 'lg'`                                          | `'md'`  | Input size.                                |
| `status`           | `'error' \| 'warning'`                                          | —       | Display state                              |
| `prefix`           | `ReactNode`                                                     | —       | Content before the input                   |
| `suffix`           | `ReactNode`                                                     | —       | Content after the input                    |
| `allowClear`       | `boolean`                                                       | `false` | Show the clear button                      |
| `onClear`          | `() => void`                                                    | —       | Called after clearing the input            |
| `showCount`        | `boolean \| ((value: string, maxLength?: number) => ReactNode)` | `false` | Display count or a custom renderer         |
| `visibilityToggle` | `boolean`                                                       | `true`  | Show the password visibility toggle        |
| `visible`          | `boolean`                                                       | —       | Controlled password visibility             |
| `defaultVisible`   | `boolean`                                                       | `false` | Initial password visibility                |
| `onVisibleChange`  | `(visible: boolean) => void`                                    | —       | Password visibility change                 |
| `visibilityIcon`   | `(visible: boolean) => ReactNode`                               | —       | Custom password visibility icon            |
| `onPressEnter`     | `KeyboardEventHandler<HTMLInputElement>`                        | —       | Called when Enter is pressed               |
| `onSearch`         | `(value: string) => void`                                       | —       | Search or Enter callback                   |
| `loading`          | `boolean`                                                       | `false` | Loading state                              |
| `enterButton`      | `ReactNode`                                                     | —       | Search button label; omitted shows an icon |

### InputGroupProps API

Props for combining related input controls.

| Property  | Type      | Default | Description            |
| --------- | --------- | ------- | ---------------------- |
| `compact` | `boolean` | —       | Join adjacent controls |
