opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Inputs
  4. Text Field Input
Back to Inputs

Components / Inputs

Text Field Input

Free Inputs React component — TextFieldInput. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Standard labeled text field with hint and error states. Supports all native input types, required marker, disabled, and full a11y wiring.

Preview

As shown on your government ID.

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/inputs/text-field-input.tsx in your project.

  4. 4

    Import and render:

    Example

    import { TextFieldInput } from "@/components/inputs/text-field-input";

    <TextFieldInput label="Email" type="email" required error={!valid} errorMessage="Invalid email." />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/inputs/text-field-input.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use client";
 
import {
forwardRef,
useId,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
 
export type TextFieldInputProps = Readonly<
{
label?: string;
hint?: string;
error?: boolean;
errorMessage?: string;
containerClassName?: string;
} & Omit<ComponentPropsWithoutRef<"input">, "size">
>;
 
export const TextFieldInput = forwardRef<HTMLInputElement, TextFieldInputProps>(
function TextFieldInput(
{
className,
containerClassName,
id,
label = "Label",
hint,
error = false,
errorMessage = "This field is required.",
disabled,
required,
type = "text",
...props
},
ref,
) {
const generatedId = useId();
const inputId = id ?? generatedId;
const hintId = `${inputId}-hint`;
const errorId = `${inputId}-error`;
 
return (
<div
data-slot="text-field-input"
data-error={error || undefined}
className={cn("w-full max-w-sm font-sans", containerClassName)}
>
<label
htmlFor={inputId}
className="mb-1.5 block text-sm font-medium text-neutral-900"
>
{label}
{required ? (
<span className="ml-0.5 text-rose-500" aria-hidden>
*
</span>
) : null}
</label>
 
<input
ref={ref}
id={inputId}
type={type}
disabled={disabled}
required={required}
aria-invalid={error || undefined}
aria-describedby={
error ? errorId : hint ? hintId : undefined
}
className={cn(
"h-10 w-full rounded-lg border bg-white px-3.5 font-sans text-sm text-neutral-900 outline-none ring-0 transition-[border-color,background-color] duration-200 placeholder:text-neutral-400 focus:ring-0 disabled:cursor-not-allowed disabled:bg-neutral-50 disabled:text-neutral-400",
error
? "border-rose-300 focus:border-rose-400"
: "border-neutral-200 focus:border-neutral-900",
className,
)}
{...props}
/>
 
{error ? (
<p id={errorId} role="alert" className="mt-1.5 text-xs text-rose-600">
{errorMessage}
</p>
) : hint ? (
<p id={hintId} className="mt-1.5 text-xs text-neutral-500">
{hint}
</p>
) : null}
</div>
);
},
);
 
TextFieldInput.displayName = "TextFieldInput";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/inputs/text-field-input.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.