Reusable React Input Component with Tailwind CSS (Icon, Label & Error)
When building forms in React, an input component is one of the most essential UI elements. A well-designed input field improves user experience and ensures validation. This guide provides a reusable input component styled with Tailwind CSS, offering multiple styles:
- With Error Handling – Displays validation errors.
- Outlined Style – A modern input with a border.
- Filled Style – A clean, minimal look with a background fill.
This component supports props like icon
, label
, and error
, making it highly customizable for various use cases.
Input Component Props
The input component is built with TypeScript and includes the following props:
1interface InputProps { 2 type: 'text' | 'number' | 'email' | 'password' 3 label?: string 4 value: string | number 5 name: string 6 placeholder: string 7 error?: string 8 disabled?: boolean 9 icon?: React.ReactNode 10 onChange: (e: ChangeEvent<HTMLInputElement>) => void 11}
Features
✅ Supports multiple input types – text
, email
, number
, and password
.
✅ Optional label & placeholder – Helps users understand what to enter.
✅ Error Handling – Displays a validation message.
✅ Optional Icon Support – Adds an icon inside the input.
✅ Disabled State – Prevents input when needed.
Tailwind CSS Input with Error
This is how the component looks with and without error:
Code Implementation
If you want to learn how to build input component step by step, read the React reusable input component tutorial.
Tailwind CSS Outlined Input
Visual Characteristics
- Transparent background
- Defined border
- Optional left icon
Code Implementation
Tailwind CSS Filled Input
Visual Characteristics
- Solid background (#F6F5F5)
- Borderless design
- Subtle focus states
Code Implementation
React Input Component API
Prop | Type | Required | Default | Description |
---|---|---|---|---|
type | 'text' | 'number' | 'email' | 'password' | Yes | 'email' | Defines the type of input field. |
label | string | No | 'Email' | Displays a label above the input field. |
value | string | number | Yes | '' | Holds the current value of the input field. |
name | string | Yes | 'email' | Specifies the name attribute of the input field. |
placeholder | string | No | 'Please enter your email' | Text displayed inside the input as a placeholder. |
error | string | No | 'Please enter a valid email.' | Displays an error message if input validation fails. |
disabled | boolean | No | false | Disables the input field when set to true. |
onChange | (e: ChangeEvent<HTMLInputElement>) => void | Yes | undefined | Function triggered when input value changes. |
icon | React.ReactNode | No | <Mail size={20} /> | Icon displayed inside the input field. |
Input Component Use Cases
- Login & Signup Forms – Collect user emails, passwords, etc.
- Search Bars – Add an icon for a better UI experience.
- User Profile Forms – Input fields for updating user information.
- E-commerce Checkout – Collect user details like name, email, and phone number.
Don't forget to let me know if it helps you.