#Chakra UI
The registration form demo written with Chakra UI, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and layout) and the config.propsuse Chakra UI's own API, typed by its components. -
fields.tsx: the adapters, typed with Chakra UI's own props throughAdapterProps. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
Field.Rootcarries the label, error and required / disabled / read-only state; the select isNativeSelect(its options are<option>children). -
Checkbox.Rootreports{ checked }inonCheckedChange. -
Wrap the app in
ChakraProvider value={defaultSystem}.
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 { Box, Button, Card, Heading, HStack, SimpleGrid, Text } from '@chakra-ui/react'
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 }
// 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>
<Heading size="xl" mb="4">
{t('title')}
</Heading>
<SimpleGrid columns={{ base: 1, sm: 2 }} gap="4">
{children}
</SimpleGrid>
<Button type="submit" mt="4">
{t('action.save')}
</Button>
</form>
)
}
function Guests() {
const t = useT()
const { label, fields, renderItem, append, remove, errorMessage } = useArrayField()
const add = () => append({ name: '' }, { shouldFocus: false })
return (
<Card.Root gridColumn="1 / -1">
<Card.Body gap="3">
<Card.Title>{label}</Card.Title>
{fields.map((item, index) => (
<HStack key={item.id} align="end">
<Box flex="1">{renderItem(item, index)}</Box>
<Button variant="ghost" colorPalette="red" onClick={() => remove(index)}>
{t('action.remove')}
</Button>
</HStack>
))}
{errorMessage && (
<Text color="fg.error" textStyle="sm">
{errorMessage}
</Text>
)}
<Button variant="outline" onClick={add}>
{t('action.addGuest')}
</Button>
</Card.Body>
</Card.Root>
)
}
export const [Form, defineConfig] = setupForm({
renderRoot: ({ children, onSubmit }) => <Root onSubmit={onSubmit}>{children}</Root>,
fieldMapping: defineMapping(fields),
})
// Labels are i18n keys; `props` is Chakra UI'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', autoComplete: 'email' },
},
ticket: {
type: 'select',
label: 'field.ticket',
props: {
// a native select: the options are its children
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', variant: 'subtle' },
},
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.Root gridColumn="1 / -1">
<Card.Body gap="3">
<Card.Title>{label}</Card.Title>
<SimpleGrid columns={{ base: 1, sm: 2 }} gap="4">
{children}
</SimpleGrid>
</Card.Body>
</Card.Root>
),
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: 'subtle', fontWeight: 'bold' },
},
})
import {
Checkbox,
type CheckboxRootProps,
Field,
Input,
type InputProps,
NativeSelect,
type NativeSelectFieldProps,
} from '@chakra-ui/react'
import type { ReactNode } from 'react'
import { type AdapterProps, useField } from 'react-headless-form'
// Field.Root carries the label, the error and the required / disabled / read-only state
function FieldShell({ children }: { children: ReactNode }) {
const { label, errorMessage, required, disabled, readOnly } = useField()
return (
<Field.Root invalid={Boolean(errorMessage)} required={required} disabled={disabled} readOnly={readOnly}>
<Field.Label>
{label} <Field.RequiredIndicator />
</Field.Label>
{children}
<Field.ErrorText>{errorMessage}</Field.ErrorText>
</Field.Root>
)
}
// AdapterProps: Chakra's own props minus what useField() controls (value, onChange, label, ...)
export type TextFieldProps = AdapterProps<InputProps>
function TextField(props: TextFieldProps) {
const { value, onChange, onBlur, ref } = useField()
return (
<FieldShell>
<Input {...props} ref={ref} value={value ?? ''} onChange={e => onChange(e.target.value)} onBlur={onBlur} />
</FieldShell>
)
}
/** A native select: the `<option>` children come from the config. */
export type SelectFieldProps = AdapterProps<NativeSelectFieldProps>
function SelectField(props: SelectFieldProps) {
const { value, onChange, onBlur } = useField()
return (
<FieldShell>
<NativeSelect.Root>
<NativeSelect.Field {...props} value={value ?? ''} onChange={e => onChange(e.target.value)} onBlur={onBlur} />
<NativeSelect.Indicator />
</NativeSelect.Root>
</FieldShell>
)
}
export type CheckboxFieldProps = AdapterProps<CheckboxRootProps, 'checked' | 'onCheckedChange'>
function CheckboxField(props: CheckboxFieldProps) {
const { value, onChange, label, disabled } = useField()
return (
<Checkbox.Root {...props} checked={Boolean(value)} onCheckedChange={e => onChange(Boolean(e.checked))} disabled={disabled}>
<Checkbox.HiddenInput />
<Checkbox.Control />
<Checkbox.Label>{label}</Checkbox.Label>
</Checkbox.Root>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import { Button, ChakraProvider, defaultSystem, SegmentGroup } from '@chakra-ui/react'
import type { ReactNode } from 'react'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
const Wrapper = ({ children }: { children: ReactNode }) => <ChakraProvider value={defaultSystem}>{children}</ChakraProvider>
// The demo stage around the form: header controls drawn with Chakra UI
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button type="submit" form={form} size="xs">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<SegmentGroup.Root size="xs" value={value} onValueChange={e => e.value && onChange(e.value)}>
<SegmentGroup.Indicator />
<SegmentGroup.Items items={options} />
</SegmentGroup.Root>
),
},
{ Wrapper },
)

