#shadcn/ui
The registration form demo written with shadcn/ui, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and grid) and the config.propsuse shadcn/ui's own API, typed by its components. -
fields.tsx: the adapters, typed with shadcn/ui's own props minus whatuseField()controls. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
shadcn components are plain elements you own, so adapters are a few lines. The demo keeps its primitives minimal (
ui.tsx, no Radix); with the realSelect/Checkboxfromnpx shadcn add, useonValueChange/onCheckedChange(value, not event). -
Tailwind v4 scans only this demo, so its preflight does not leak into the other demos.
main.tsx only mounts the docs' demo stage (language switch, payload drawer); your app renders Form itself. See Connect your UI kit.
import type { ReactNode } from 'react'
import { defineMapping, setupForm, useArrayField } from 'react-headless-form'
import { type Translator, useT } from '../shared/i18n'
import { FORM_ID } from '../shared/mount'
import { fields } from './fields'
import { Button, Card, CardTitle } from './ui'
export type Registration = {
name: string
email: string
ticket: 'standard' | 'student'
studentId?: string
promoCode?: string
guests: { name: string }[]
invoice: boolean
billing?: { company: string; taxId?: string }
total?: number
}
export const defaultValues: Partial<Registration> = { ticket: 'standard', guests: [], invoice: false }
const PRICE = { standard: 100, student: 50, guest: 100 }
// renderRoot: the whole form, with a title and a responsive grid
function Root({ children, onSubmit }: { children: ReactNode; onSubmit: () => void }) {
const t = useT()
return (
<form id={FORM_ID} onSubmit={onSubmit} noValidate className="grid gap-4">
<h2 className="text-xl font-semibold">{t('title')}</h2>
<div className="grid gap-4 sm:grid-cols-2">{children}</div>
<Button type="submit" className="justify-self-start">
{t('action.save')}
</Button>
</form>
)
}
function Guests() {
const t = useT()
const { label, fields, renderItem, append, remove, errorMessage } = useArrayField()
return (
<Card className="sm:col-span-2">
<CardTitle>{label}</CardTitle>
{fields.map((item, index) => (
<div key={item.id} className="flex items-end gap-2">
<div className="flex-1">{renderItem(item, index)}</div>
<Button type="button" variant="ghost" onClick={() => remove(index)}>
{t('action.remove')}
</Button>
</div>
))}
{errorMessage && <p className="text-sm text-red-600">{errorMessage}</p>}
<Button type="button" variant="outline" onClick={() => append({ name: '' }, { shouldFocus: false })}>
{t('action.addGuest')}
</Button>
</Card>
)
}
export const [Form, defineConfig] = setupForm({
renderRoot: ({ children, onSubmit }) => <Root onSubmit={onSubmit}>{children}</Root>,
fieldMapping: defineMapping(fields),
})
// Labels are i18n keys; `props` are the shadcn components' own props
export const buildConfig = (t: Translator) =>
defineConfig<Registration>({
name: { type: 'text', label: 'field.name', rules: { required: true } },
email: {
type: 'text',
label: 'field.email',
rules: { required: true, pattern: '^\\S+@\\S+\\.\\S+$' },
props: { type: 'email', placeholder: 'hint.email', autoComplete: 'email' },
},
ticket: {
type: 'select',
label: 'field.ticket',
props: {
children: [
<option key="standard" value="standard">
{t('ticket.standard')}
</option>,
<option key="student" value="student">
{t('ticket.student')}
</option>,
],
},
},
studentId: {
type: 'text',
label: 'field.studentId',
visible: { when: 'ticket', eq: 'student' },
rules: { required: true },
},
promoCode: {
type: 'text',
label: 'field.promoCode',
disabled: { when: 'ticket', eq: 'student' },
props: { placeholder: 'hint.promoCode', className: 'uppercase' },
},
guests: {
type: 'array',
label: 'field.guests',
rules: { maxLength: 3 },
render: () => <Guests />,
props: { config: { name: { type: 'text', label: 'field.guestName', rules: { required: true } } } },
},
invoice: { type: 'checkbox', label: 'field.invoice' },
billing: {
type: 'section',
label: 'field.billing',
visible: { when: 'invoice', eq: true },
render: ({ label, children }) => (
<Card className="sm:col-span-2">
<CardTitle>{label}</CardTitle>
<div className="grid gap-4 sm:grid-cols-2">{children}</div>
</Card>
),
props: {
nested: true,
config: {
company: { type: 'text', label: 'field.company', rules: { required: true } },
taxId: { type: 'text', label: 'field.taxId' },
},
},
},
total: {
type: 'text',
label: 'field.total',
readOnly: true,
compute: v => PRICE[v.ticket ?? 'standard'] + PRICE.guest * (v.guests?.length ?? 0),
props: { className: 'bg-zinc-50 font-mono font-bold' },
},
})
import { useId } from 'react'
import type { ReactNode } from 'react'
import { type AdapterProps, useField } from 'react-headless-form'
import type { ComponentProps } from 'react'
import { Checkbox, Input, Label, NativeSelect } from './ui'
// Label + control + message, like shadcn's <FormItem>.
function FieldItem({ id, children }: { id: string; children: ReactNode }) {
const { label, errorMessage, required } = useField()
return (
<div className="grid gap-2">
<Label htmlFor={id}>
{label}
{required && <span className="text-red-600"> *</span>}
</Label>
{children}
{errorMessage && <p className="text-sm text-red-600">{errorMessage}</p>}
</div>
)
}
// AdapterProps: the component's own props minus what useField() controls (value,
// onChange, label, disabled, ...) and `Extra`, the props this adapter also sets.
type Extra = 'id'
export type TextFieldProps = AdapterProps<ComponentProps<typeof Input>, Extra>
function TextField(props: TextFieldProps) {
const id = useId()
const { value, onChange, onBlur, ref, errorMessage, disabled, readOnly } = useField()
return (
<FieldItem id={id}>
<Input
{...props}
id={id}
ref={ref}
value={value ?? ''}
onChange={e => onChange(e.target.value)}
onBlur={onBlur}
aria-invalid={Boolean(errorMessage)}
disabled={disabled}
readOnly={readOnly}
/>
</FieldItem>
)
}
/** A native select: the `<option>` children come from the config. */
export type SelectFieldProps = AdapterProps<ComponentProps<typeof NativeSelect>, Extra>
function SelectField(props: SelectFieldProps) {
const id = useId()
const { value, onChange, onBlur, disabled } = useField()
return (
<FieldItem id={id}>
<NativeSelect
{...props}
id={id}
value={value ?? ''}
onChange={e => onChange(e.target.value)}
onBlur={onBlur}
disabled={disabled}
/>
</FieldItem>
)
}
export type CheckboxFieldProps = AdapterProps<ComponentProps<typeof Checkbox>, Extra | 'checked' | 'type'>
function CheckboxField(props: CheckboxFieldProps) {
const id = useId()
const { value, onChange, label, disabled } = useField()
return (
<div className="flex items-center gap-2">
<Checkbox
{...props}
id={id}
checked={Boolean(value)}
onChange={e => onChange(e.target.checked)}
disabled={disabled}
/>
<Label htmlFor={id}>{label}</Label>
</div>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import './tailwind.css'
import { Button } from './ui'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// The demo stage around the form: header controls drawn with shadcn/ui
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button type="submit" form={form} className="h-8 px-3">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<div className="inline-flex gap-1 rounded-lg border border-zinc-200 bg-white p-1">
{options.map(option => (
<Button
key={option.value}
type="button"
variant={option.value === value ? 'default' : 'outline'}
className="h-7 px-3"
aria-pressed={option.value === value}
onClick={() => onChange(option.value)}
>
{option.label}
</Button>
))}
</div>
),
},
)

