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.
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>
);
}
Examples
Prefixes and suffixes
Add an icon or text with prefix and suffix. These are decorative; place interactive controls outside the input.
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>
);
}
Set type="password" to mask the input. Control visibility with visible and onVisibleChange, customize the toggle with visibilityIcon, or hide it with visibilityToggle={false}.
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>
);
}
Set allowClear to show a clear button. Clearing fires onChange with an empty input value.
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>
);
}
Character count
Use showCount for a built-in or custom count, and maxLength to limit input length.
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>
);
}
Sizes
Choose from sm, md and lg.
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>
);
}
States
disabled prevents interaction; readOnly keeps content readable without allowing edits. status changes appearance without performing validation. Use FormField for error messages.
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>
);
}
Manage content with value and onChange. onPressEnter runs on Enter, except during IME composition or when the keyboard event has been prevented.
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>
);
}
InputSearch calls onSearch with the current value when its search button is clicked or Enter is pressed.
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>
);
}
Use InputGroup to join adjacent controls. Give each input in the group its own accessible name.
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>
);
}
API
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 for labels, help and errors.
Props for the submission behavior and actions of a search input.
Props for combining related input controls.