The input field is an important part of the form element that can be used to create interactive controls to accept data from the user based on multiple input types, such as text, email, number, password, URL, phone number, and more.
For validated forms, prefer FormInput inside a parent Form. FormInput requires Form context and accepts name + label plus all Input props.
Setup
pnpm add @cailabs/components.forms <script>
import { Input, Label } from '@cailabs/components.forms';
</script> See Installation.
Default input
<script>
import { Input, Label } from '.';
</script>
<form>
<div class="mb-6 grid gap-6 md:grid-cols-2">
<div>
<Label for="first_name" class="mb-2">First name</Label>
<Input type="text" id="first_name" placeholder="John" required />
</div>
</div>
</form> Sizes
size accepts sm | md (default) | lg.
<script>
import { Input } from '.';
</script>
<Input size="sm" placeholder="Small" />
<Input size="md" placeholder="Medium" />
<Input size="lg" placeholder="Large" /> Colors
Theme colors: default (default), tinted, primary, secondary, gray, red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose.
<script>
import { Input } from '.';
</script>
<Input color="default" placeholder="Default" />
<Input color="primary" placeholder="Primary" />
<Input color="green" placeholder="Green" />
<Input color="red" placeholder="Red" /> Clearable
This example shows how to create an input field where the user can remove the text with one click.
<script>
import { Input } from '.';
</script>
<Input clearable value="Clearable input" /> Clear event
<script>
import { Input } from '.';
</script>
<Input
clearable
clearableOnClick={() => {
alert('Clicked close button!');
}}
class="my-4"
/> Left and right snippets
Use left / right snippets for icons or adornments. Adjust input padding with classes like ps-8.
<script>
import { Input, Label } from '.';
</script>
<Label class="space-y-2">
<div>Small input - left icon</div>
<Input type="email" placeholder="name@flowbite.com" size="sm" class="ps-8">
{#snippet left()}
<iconify-icon icon="mynaui:envelope-solid"></iconify-icon>
{/snippet}
</Input>
</Label> Combobox
Pass data (string array) for suggestions. Use onSelect when a suggestion is chosen. Optional: maxSuggestions, comboClass, comboItemClass.
<script>
import { Input } from '.';
const carMakers = ['Toyota', 'Ford', 'Honda', 'Chevrolet'];
</script>
<Input data={carMakers} placeholder="Type to search cars..." />
<Input data={carMakers} clearable placeholder="Clearable" onSelect={(item) => console.log(item)} /> Events
Native-style handlers: oninput, onfocus, onblur, onkeydown. Combobox selection uses onSelect. Clear button uses clearableOnClick.
<script>
import { Input } from '.';
let value = $state('');
</script>
<Input
bind:value
oninput={(e) => console.log('input', e)}
onfocus={() => console.log('focus')}
onblur={() => console.log('blur')}
onkeydown={(e) => console.log('keydown', e.key)}
/> Accessibility
- Pair with
Labelusing matchingfor/id. - Use
aria-describedbywithHelperfor hints or errors. - Disabled inputs use native
disabledand reduced opacity styling. - Clear button is keyboard-reachable when
clearableis set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | number | string[] | — | Bindable value |
type | native input type | text | HTML type |
size | sm | md | lg | md | Control size |
color | theme color | default | Border/focus/bg token |
left / right | Snippet | — | Leading / trailing adornments |
leftClass / rightClass | ClassValue | — | Classes for adornment slots |
divClass / wrapperClass | ClassValue | — | Wrapper classes |
children | Snippet | — | Custom input render (advanced) |
elementRef | HTMLInputElement | — | Bindable input element |
clearable | boolean | false | Show clear button |
clearableOnClick | () => void | — | Extra clear callback |
clearableClass / clearableSvgClass | ClassValue | — | Clear button styling |
clearableColor | CloseButton color | — | Clear icon color |
data | string[] | — | Combobox suggestion list |
maxSuggestions | number | — | Max combobox items |
onSelect | (item: string) => void | — | Combobox selection |
comboClass / comboItemClass | ClassValue | — | Combobox list styling |
oninput / onfocus / onblur / onkeydown | event handlers | — | Input events |
class | ClassValue | — | Extra input classes |
...rest | HTML input attrs | — | placeholder, required, disabled, name, etc. |
FormInput
FormInput extends InputProps with name (required), label?, and contextKey?. Must be a child of Form.