#Ant Design
The registration form demo written with Ant Design, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and grid) and the config.propsuse Ant Design's own API, typed by its components. -
fields.tsx: the adapters, typed with Ant Design's own props minus whatuseField()controls. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
Form.Itemis used for layout only (label, help, status); values and errors come fromuseField(), so no antdForminstance is involved. -
Selectpasses the value toonChange;Input/Checkboxpass events.
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, Card, Flex, Typography } from 'antd'
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))', columnGap: 16 }
const fullRow = { gridColumn: '1 / -1', marginBottom: 16 }
// 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>
<Typography.Title level={3}>{t('title')}</Typography.Title>
<div style={grid}>{children}</div>
<Button type="primary" htmlType="submit">
{t('action.save')}
</Button>
</form>
)
}
function Guests() {
const t = useT()
const { label, fields, renderItem, append, remove, errorMessage } = useArrayField()
return (
<Card title={label} size="small" style={fullRow}>
{fields.map((item, index) => (
<Flex key={item.id} gap={8} align="end">
<div style={{ flex: 1 }}>{renderItem(item, index)}</div>
<Button danger type="link" style={{ marginBottom: 12 }} onClick={() => remove(index)}>
{t('action.remove')}
</Button>
</Flex>
))}
{errorMessage && <Typography.Text type="danger">{errorMessage}</Typography.Text>}
<Button type="dashed" block 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` is Ant Design's own API (InputProps, SelectProps, ...)
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', prefix: '@' },
},
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', allowClear: true },
},
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} size="small" 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: { variant: 'filled', prefix: '$' },
},
})
import { useId } from 'react'
import { Checkbox, Form, Input, Select } from 'antd'
import { type AdapterProps, useField } from 'react-headless-form'
import type { CheckboxProps, InputProps, SelectProps } from 'antd'
// Form.Item is used for layout only; values and errors come from useField().
function Item({ id, children }: { id: string; children: React.ReactNode }) {
const { label, errorMessage, required } = useField()
return (
<Form.Item
label={label}
htmlFor={id}
required={required}
validateStatus={errorMessage ? 'error' : undefined}
help={errorMessage}
layout="vertical"
style={{ marginBottom: 12 }}
>
{children}
</Form.Item>
)
}
// AdapterProps: the library's own props minus what useField() controls (value,
// onChange, label, disabled, ...) and `Extra`, the props this adapter also sets.
// So `props` in the config is typed (and autocompleted) as Ant Design's props.
type Extra = 'id' | 'status'
export type TextFieldProps = AdapterProps<InputProps, Extra>
function TextField(props: TextFieldProps) {
const id = useId()
const { value, onChange, onBlur, errorMessage, disabled, readOnly } = useField()
return (
<Item id={id}>
<Input
{...props}
id={id}
value={value ?? ''}
onChange={e => onChange(e.target.value)}
onBlur={onBlur}
status={errorMessage ? 'error' : undefined}
disabled={disabled}
readOnly={readOnly}
/>
</Item>
)
}
/** `options` come from the config, in antd's own shape. */
export type SelectFieldProps = AdapterProps<SelectProps<string>, Extra>
function SelectField(props: SelectFieldProps) {
const id = useId()
const { value, onChange, onBlur, errorMessage, disabled } = useField()
return (
<Item id={id}>
{/* antd passes the value, not an event */}
<Select
{...props}
id={id}
value={value}
onChange={next => onChange(next)}
onBlur={onBlur}
status={errorMessage ? 'error' : undefined}
disabled={disabled}
/>
</Item>
)
}
export type CheckboxFieldProps = AdapterProps<CheckboxProps, Extra | 'checked'>
function CheckboxField(props: CheckboxFieldProps) {
const { value, onChange, label, disabled } = useField()
return (
<Form.Item style={{ marginBottom: 12 }}>
<Checkbox {...props} checked={Boolean(value)} onChange={e => onChange(e.target.checked)} disabled={disabled}>
{label}
</Checkbox>
</Form.Item>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import { Button, ConfigProvider, Segmented } from 'antd'
import type { ReactNode } from 'react'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// antd inserts a space between two CJK characters in buttons ("登 録"); turn it off
const Wrapper = ({ children }: { children: ReactNode }) => (
<ConfigProvider button={{ autoInsertSpace: false }}>{children}</ConfigProvider>
)
// The demo stage around the form: header controls drawn with Ant Design
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button type="primary" htmlType="submit" form={form} size="small">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<Segmented size="small" value={value} options={options} onChange={next => onChange(String(next))} />
),
},
{ Wrapper },
)

