#PrimeReact
The registration form demo written with PrimeReact, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and layout) and the config.propsuse PrimeReact's own API, typed by its components. -
fields.tsx: the adapters, typed with PrimeReact's own props throughAdapterProps. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
DropdownandCheckboxpasse.value/e.checked, not the DOM event. -
Import a theme (
primereact/resources/themes/lara-light-blue/theme.css) and wrap the app inPrimeReactProvider. -
PrimeReact v11 is a different, unstyled primitives library; this demo uses v10.
Tip
main.tsx only mounts the docs' demo stage (language switch, payload drawer); your app renders Form itself. See Connect your UI kit.
Live demo
form.tsx
fields.tsx
main.tsx
import { Button } from 'primereact/button'
import { Card } from 'primereact/card'
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'
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 }
/** Two columns on wide screens; sections and the guest list take a full row. */
const grid = { display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(260px, 1fr))', gap: 16 }
const fullRow = { gridColumn: '1 / -1' }
const row = { display: 'flex', gap: 8, alignItems: 'end' }
// 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>
<h2>{t('title')}</h2>
<div style={grid}>{children}</div>
<Button type="submit" label={t('action.save')} style={{ marginTop: 16 }} />
</form>
)
}
function Guests() {
const t = useT()
const { label, fields, renderItem, append, remove, errorMessage } = useArrayField()
const add = () => append({ name: '' }, { shouldFocus: false })
return (
<Card title={label} style={fullRow}>
{fields.map((item, index) => (
<div key={item.id} style={{ ...row, marginBottom: 8 }}>
<div style={{ flex: 1 }}>{renderItem(item, index)}</div>
<Button type="button" label={t('action.remove')} severity="danger" text onClick={() => remove(index)} />
</div>
))}
{errorMessage && <small className="p-error">{errorMessage}</small>}
<Button type="button" label={t('action.addGuest')} outlined onClick={add} style={{ width: '100%', marginTop: 8 }} />
</Card>
)
}
export const [Form, defineConfig] = setupForm({
renderRoot: ({ children, onSubmit }) => <Root onSubmit={onSubmit}>{children}</Root>,
fieldMapping: defineMapping(fields),
})
// Labels are i18n keys; `props` is PrimeReact's own API, typed by its components
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', keyfilter: 'email' },
},
ticket: {
type: 'select',
label: 'field.ticket',
props: {
options: [
{ value: 'standard', label: t('ticket.standard') },
{ value: 'student', label: t('ticket.student') },
],
},
},
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' },
},
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 title={label} style={fullRow}>
<div style={grid}>{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: { style: { fontWeight: 700 } },
},
})
import { useId } from 'react'
import { Checkbox, type CheckboxProps } from 'primereact/checkbox'
import { Dropdown, type DropdownProps } from 'primereact/dropdown'
import { InputText, type InputTextProps } from 'primereact/inputtext'
import type { ReactNode } from 'react'
import { type AdapterProps, useField } from 'react-headless-form'
// PrimeReact inputs carry no label or message: plain markup with its classes
function FieldShell({ id, children }: { id: string; children: ReactNode }) {
const { label, errorMessage, required } = useField()
return (
<div style={{ display: 'grid', gap: 6 }}>
<label htmlFor={id}>
{label}
{required && ' *'}
</label>
{children}
{errorMessage && <small className="p-error">{errorMessage}</small>}
</div>
)
}
// AdapterProps: PrimeReact's own props minus what useField() controls, and what is set below
export type TextFieldProps = AdapterProps<InputTextProps, 'id' | 'invalid'>
function TextField(props: TextFieldProps) {
const id = useId()
const { value, onChange, onBlur, ref, errorMessage, disabled, readOnly } = useField()
return (
<FieldShell id={id}>
<InputText
{...props}
id={id}
ref={ref}
value={value ?? ''}
onChange={e => onChange(e.target.value)}
onBlur={onBlur}
invalid={Boolean(errorMessage)}
disabled={disabled}
readOnly={readOnly}
/>
</FieldShell>
)
}
/** `options` come from the config, as `{ value, label }`. */
export type SelectFieldProps = AdapterProps<DropdownProps, 'inputId' | 'invalid'>
function SelectField(props: SelectFieldProps) {
const id = useId()
const { value, onChange, onBlur, errorMessage, disabled } = useField()
return (
<FieldShell id={id}>
<Dropdown
optionLabel="label"
optionValue="value"
{...props}
inputId={id}
value={value}
onChange={e => onChange(e.value)} // e.value, not the event
onBlur={onBlur}
invalid={Boolean(errorMessage)}
disabled={disabled}
/>
</FieldShell>
)
}
export type CheckboxFieldProps = AdapterProps<CheckboxProps, 'inputId' | 'checked'>
function CheckboxField(props: CheckboxFieldProps) {
const id = useId()
const { value, onChange, label, disabled } = useField()
return (
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
<Checkbox {...props} inputId={id} checked={Boolean(value)} onChange={e => onChange(Boolean(e.checked))} disabled={disabled} />
<label htmlFor={id}>{label}</label>
</div>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import 'primereact/resources/themes/lara-light-blue/theme.css'
import { PrimeReactProvider } from 'primereact/api'
import { Button } from 'primereact/button'
import { SelectButton } from 'primereact/selectbutton'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// The demo stage around the form: header controls drawn with PrimeReact
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button type="submit" form={form} size="small">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<SelectButton value={value} options={options} optionValue="value" allowEmpty={false} onChange={e => onChange(e.value)} />
),
},
{ Wrapper: PrimeReactProvider },
)

