#React-Bootstrap
The registration form demo written with React-Bootstrap, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and grid) and the config.propsuse React-Bootstrap's own API, typed by its components. -
fields.tsx: the adapters, typed with React-Bootstrap's own props minus whatuseField()controls. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
Form.Group controlIdlinks each label to its control;Form.Control.Feedbackshows the error. -
Form.Control,Form.SelectandForm.Checkpass DOM events: reade.target.value/checked. -
Import
bootstrap/dist/css/bootstrap.min.css; no provider is needed.
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 { Button, Card, Stack } from 'react-bootstrap'
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 Bootstrap's responsive grid
function Root({ children, onSubmit }: { children: ReactNode; onSubmit: () => void }) {
const t = useT()
return (
<form id={FORM_ID} onSubmit={onSubmit} noValidate>
<h3 className="mb-3">{t('title')}</h3>
<div className="row row-cols-1 row-cols-sm-2 g-3">
{/* each field is a grid cell; sections and the guest list span the row */}
{children}
</div>
<Button type="submit" className="mt-3">
{t('action.save')}
</Button>
</form>
)
}
function Guests() {
const t = useT()
const { label, fields, renderItem, append, remove, errorMessage } = useArrayField()
return (
<Card className="w-100">
<Card.Body>
<Card.Title as="h6">{label}</Card.Title>
<Stack gap={2}>
{fields.map((item, index) => (
<div key={item.id} className="d-flex align-items-end gap-2">
<div className="flex-grow-1">{renderItem(item, index)}</div>
<Button variant="outline-danger" onClick={() => remove(index)}>
{t('action.remove')}
</Button>
</div>
))}
{errorMessage && <div className="text-danger small">{errorMessage}</div>}
<Button variant="outline-primary" onClick={() => append({ name: '' }, { shouldFocus: false })}>
{t('action.addGuest')}
</Button>
</Stack>
</Card.Body>
</Card>
)
}
export const [Form, defineConfig] = setupForm({
renderRoot: ({ children, onSubmit }) => <Root onSubmit={onSubmit}>{children}</Root>,
fieldMapping: defineMapping(fields),
})
// Labels are i18n keys; `props` is React-Bootstrap'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: {
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', size: 'sm' },
},
guests: {
type: 'array',
label: 'field.guests',
rules: { maxLength: 3 },
render: () => (
<div className="w-100">
<Guests />
</div>
),
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 }) => (
<div className="w-100">
<Card>
<Card.Body>
<Card.Title as="h6">{label}</Card.Title>
<div className="row row-cols-1 row-cols-sm-2 g-3">{children}</div>
</Card.Body>
</Card>
</div>
),
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: { plaintext: true, className: 'fw-bold' },
},
})
import { useId } from 'react'
import { Form, type FormCheckProps, type FormControlProps, type FormSelectProps } from 'react-bootstrap'
import { type AdapterProps, useField } from 'react-headless-form'
// AdapterProps: React-Bootstrap'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 React-Bootstrap's props.
type Extra = 'id' | 'isInvalid'
export type TextFieldProps = AdapterProps<FormControlProps, Extra>
function TextField(props: TextFieldProps) {
const id = useId()
const { value, onChange, onBlur, ref, label, errorMessage, required, disabled, readOnly } = useField()
return (
// Form.Group links the label and the control through controlId
<Form.Group controlId={id}>
<Form.Label>
{label}
{required && ' *'}
</Form.Label>
<Form.Control
{...props}
ref={ref}
value={value ?? ''}
onChange={e => onChange(e.target.value)}
onBlur={onBlur}
isInvalid={Boolean(errorMessage)}
disabled={disabled}
readOnly={readOnly}
/>
<Form.Control.Feedback type="invalid">{errorMessage}</Form.Control.Feedback>
</Form.Group>
)
}
/** A native select: the `<option>` children come from the config. */
export type SelectFieldProps = AdapterProps<FormSelectProps, Extra>
function SelectField(props: SelectFieldProps) {
const id = useId()
const { value, onChange, onBlur, ref, label, errorMessage, disabled } = useField()
return (
<Form.Group controlId={id}>
<Form.Label>{label}</Form.Label>
<Form.Select
{...props}
ref={ref}
value={value ?? ''}
onChange={e => onChange(e.target.value)}
onBlur={onBlur}
isInvalid={Boolean(errorMessage)}
disabled={disabled}
/>
<Form.Control.Feedback type="invalid">{errorMessage}</Form.Control.Feedback>
</Form.Group>
)
}
export type CheckboxFieldProps = AdapterProps<FormCheckProps, Extra | 'checked' | 'type'>
function CheckboxField(props: CheckboxFieldProps) {
const id = useId()
const { value, onChange, onBlur, ref, label, errorMessage, disabled } = useField()
return (
<Form.Check
{...props}
id={id}
ref={ref}
type="checkbox"
label={label}
checked={Boolean(value)}
onChange={e => onChange(e.target.checked)}
onBlur={onBlur}
isInvalid={Boolean(errorMessage)}
feedback={errorMessage}
feedbackType="invalid"
disabled={disabled}
/>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import 'bootstrap/dist/css/bootstrap.min.css'
import { Button, ToggleButton, ToggleButtonGroup } from 'react-bootstrap'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// The demo stage around the form: header controls drawn with React-Bootstrap
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button type="submit" form={form} size="sm">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<ToggleButtonGroup type="radio" name="lang" size="sm" value={value} onChange={onChange}>
{options.map(option => (
<ToggleButton key={option.value} id={`lang-${option.value}`} value={option.value} variant="outline-primary">
{option.label}
</ToggleButton>
))}
</ToggleButtonGroup>
),
},
)

