Form Field
Form Field for a useful, keyboard-friendly product interface.
Make it yours.
Install the editable source and its dependencies into your React + Tailwind project.
Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/form-field.jsonUse it
Scroll horizontally for long lines
"use client";
import { FormField } from "@/components/jez-ui/ui/form-field";
import { Input } from "@/components/jez-ui/ui/input";
export default function Example() {
return (
<>
<div className="w-full max-w-sm">
<div className="mb-7">
<h3 className="text-2xl tracking-tight">Start something new.</h3>
<p className="mt-2 text-sm leading-relaxed text-muted-foreground">
Give your workspace a name you will remember.
</p>
</div>
<FormField
label="Project name"
hint="Something you’ll recognise later."
>
<Input placeholder="Field notes" />
</FormField>
</div>
</>
);
}
Compose your own
These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.
Scroll horizontally for long lines
import { FormField, Field, FieldGroup, FieldSet, FieldLegend, FieldLabel, FieldDescription, FieldError, FieldContent, FieldRow } from "@/components/jez-ui/ui/form-field";Props & types
Scroll horizontally for long lines
Omit<
React.ComponentProps<"div">,
keyof {
label: string;
hint?: string;
error?: string;
children: React.ReactElement<{
id?: string;
"aria-describedby"?: string;
"aria-invalid"?: boolean;
}>;
className?: string;
}
> & {
label: string;
hint?: string;
error?: string;
children: React.ReactElement<{
id?: string;
"aria-describedby"?: string;
"aria-invalid"?: boolean;
}>;
className?: string;
}Source
View 2 source files
registry/ui/form-field.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export function FormField({
label,
hint,
error,
children,
className,
...rootProps
}: Omit<
React.ComponentProps<"div">,
keyof {
label: string;
hint?: string;
error?: string;
children: React.ReactElement<{
id?: string;
"aria-describedby"?: string;
"aria-invalid"?: boolean;
}>;
className?: string;
}
> & {
label: string;
hint?: string;
error?: string;
children: React.ReactElement<{
id?: string;
"aria-describedby"?: string;
"aria-invalid"?: boolean;
}>;
className?: string;
}) {
const generatedId = React.useId();
const id = children.props.id ?? generatedId;
return (
<div {...rootProps} className={cn("grid gap-2", className)}>
<label htmlFor={id} className="text-sm font-medium">
{label}
</label>
{React.cloneElement(children, {
id,
"aria-describedby":
[
children.props["aria-describedby"],
hint || error ? id + "-help" : undefined,
]
.filter(Boolean)
.join(" ") || undefined,
"aria-invalid": error ? true : children.props["aria-invalid"],
})}
{(hint || error) && (
<p
id={id + "-help"}
role={error ? "alert" : undefined}
className={cn(
"text-xs",
error ? "text-danger" : "text-muted-foreground",
)}
>
{error || hint}
</p>
)}
</div>
);
}
export function Field({ className, ...props }: React.ComponentProps<"div">) {
return <div className={cn("grid gap-2", className)} {...props} />;
}
export function FieldGroup({
className,
...props
}: React.ComponentProps<"div">) {
return <div className={cn("grid gap-6", className)} {...props} />;
}
export function FieldSet({
className,
...props
}: React.ComponentProps<"fieldset">) {
return (
<fieldset className={cn("grid min-w-0 gap-5", className)} {...props} />
);
}
export function FieldLegend({
className,
...props
}: React.ComponentProps<"legend">) {
return <legend className={cn("mb-3 font-medium", className)} {...props} />;
}
export function FieldLabel({
className,
...props
}: React.ComponentProps<"label">) {
return <label className={cn("text-sm font-medium", className)} {...props} />;
}
export function FieldDescription({
className,
...props
}: React.ComponentProps<"p">) {
return (
<p
className={cn("text-xs leading-relaxed text-muted-foreground", className)}
{...props}
/>
);
}
export function FieldError({ className, ...props }: React.ComponentProps<"p">) {
return (
<p
role="alert"
className={cn("text-xs leading-relaxed text-danger", className)}
{...props}
/>
);
}
export function FieldContent({
className,
...props
}: React.ComponentProps<"div">) {
return <div className={cn("grid min-w-0 gap-1", className)} {...props} />;
}
export function FieldRow({
className,
...props
}: React.ComponentProps<"label">) {
return (
<label
className={cn("flex items-center gap-3 text-sm font-medium", className)}
{...props}
/>
);
}
registry/ui/utils.ts
Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
return twMerge(clsx(values));
}
Dependencies
clsx@2.1.1 · tailwind-merge@3.5.0
Accessibility & behaviour
Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.
Read the accessibility and performance guide →