#React Native Web
The registration form demo written with React Native Web, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and layout) and the config.propsuse React Native Web's own API, typed by its components. -
fields.tsx: the adapters, typed with React Native Web's own props throughAdapterProps. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
There is no
<form>:renderRootrenders aViewand itsButtoncallsonSubmit(); the header button calls it too. -
TextInputreports text withonChangeText; the select is a row ofPressableoptions; the checkbox is aSwitch. -
On the web, alias
react-nativetoreact-native-webin the bundler (see the demos'vite.config.ts).
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 { Button, Text, View } from 'react-native'
import { type Translator, useT } from '../shared/i18n'
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: there is no <form> in React Native, so submit is a Button calling onSubmit
function Root({ children, onSubmit }: { children: ReactNode; onSubmit: () => void }) {
const t = useT()
return (
<View style={styles.form}>
<Text style={styles.title}>{t('title')}</Text>
<View style={styles.grid}>{children}</View>
<Button title={t('action.save')} onPress={() => onSubmit()} />
</View>
)
}
function Guests() {
const t = useT()
const { label, fields, renderItem, append, remove, errorMessage } = useArrayField()
const add = () => append({ name: '' }, { shouldFocus: false })
return (
<View style={styles.card}>
<Text style={styles.cardTitle}>{label}</Text>
{fields.map((item, index) => (
<View key={item.id} style={styles.guest}>
<View style={{ flex: 1 }}>{renderItem(item, index)}</View>
<Button title={t('action.remove')} color="#dc2626" onPress={() => remove(index)} />
</View>
))}
{errorMessage && <Text style={styles.error}>{errorMessage}</Text>}
<Button title={t('action.addGuest')} onPress={add} />
</View>
)
}
export const [Form, defineConfig] = setupForm({
renderRoot: ({ children, onSubmit }) => <Root onSubmit={onSubmit}>{children}</Root>,
fieldMapping: defineMapping(fields),
})
// Labels are i18n keys; `props` is React Native's own API (TextInputProps, ...)
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: { placeholder: 'hint.email', keyboardType: 'email-address', autoCapitalize: 'none' },
},
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', autoCapitalize: 'characters' },
},
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 }) => (
<View style={styles.card}>
<Text style={styles.cardTitle}>{label}</Text>
<View style={styles.grid}>{children}</View>
</View>
),
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' } },
},
})
// Plain style objects: React Native Web accepts them as `style`
const styles = {
form: { gap: 16 },
title: { fontSize: 22, fontWeight: '700' },
grid: { flexDirection: 'row', flexWrap: 'wrap', gap: 16 },
card: { flexBasis: '100%', gap: 8, padding: 12, borderWidth: 1, borderColor: '#e2e8f0', borderRadius: 8 },
cardTitle: { fontWeight: '700' },
guest: { flexDirection: 'row', alignItems: 'flex-end', gap: 8 },
error: { color: '#dc2626', fontSize: 12 },
} as const
import type { ReactNode } from 'react'
import { type AdapterProps, useField } from 'react-headless-form'
import {
Pressable,
Switch,
type SwitchProps,
Text,
TextInput,
type TextInputProps,
View,
type ViewProps,
} from 'react-native'
// No DOM form controls in React Native: label and message are <Text>
function FieldShell({ children }: { children: ReactNode }) {
const { label, errorMessage, required } = useField()
return (
<View style={styles.field}>
<Text style={styles.label}>
{label}
{required && ' *'}
</Text>
{children}
{errorMessage && <Text style={styles.error}>{errorMessage}</Text>}
</View>
)
}
// AdapterProps: TextInput's own props minus what useField() controls, and what is set below
export type TextFieldProps = AdapterProps<TextInputProps, 'editable' | 'onChangeText'>
function TextField(props: TextFieldProps) {
const { value, onChange, onBlur, label, errorMessage, disabled, readOnly } = useField()
return (
<FieldShell>
<TextInput
placeholderTextColor="#94a3b8"
{...props}
accessibilityLabel={typeof label === 'string' ? label : undefined}
value={value == null ? '' : String(value)}
onChangeText={text => onChange(text)} // the text, not an event
onBlur={onBlur}
editable={!disabled && !readOnly}
style={[styles.input, errorMessage ? styles.inputError : null, (disabled || readOnly) && styles.inputMuted, props.style]}
/>
</FieldShell>
)
}
/** No native select on the web build: a row of options. */
export type SelectFieldProps = AdapterProps<ViewProps> & { options: { value: string; label: string }[] }
function SelectField({ options, ...props }: SelectFieldProps) {
const { value, onChange, disabled } = useField()
return (
<FieldShell>
<View {...props} style={[styles.segments, props.style]}>
{options.map(option => (
<Pressable
key={option.value}
aria-checked={option.value === value}
disabled={disabled}
onPress={() => onChange(option.value)}
style={[styles.segment, option.value === value && styles.segmentActive]}
>
<Text style={option.value === value ? styles.segmentTextActive : undefined}>{option.label}</Text>
</Pressable>
))}
</View>
</FieldShell>
)
}
export type CheckboxFieldProps = AdapterProps<SwitchProps, 'onValueChange'>
function CheckboxField(props: CheckboxFieldProps) {
const { value, onChange, label, disabled } = useField()
return (
<View style={[styles.field, styles.row]}>
<Switch {...props} value={Boolean(value)} onValueChange={next => onChange(next)} disabled={disabled} />
<Text onPress={() => !disabled && onChange(!value)}>{label}</Text>
</View>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
// Plain style objects: React Native Web accepts them as `style`
const styles = {
field: { flexGrow: 1, flexBasis: 260, gap: 4 },
row: { flexDirection: 'row', alignItems: 'center', gap: 8 },
label: { fontWeight: '600' },
error: { color: '#dc2626', fontSize: 12 },
input: { borderWidth: 1, borderColor: '#cbd5e1', borderRadius: 6, paddingHorizontal: 10, paddingVertical: 8 },
inputError: { borderColor: '#dc2626' },
inputMuted: { backgroundColor: '#f1f5f9' },
segments: { flexDirection: 'row', gap: 6 },
segment: { borderWidth: 1, borderColor: '#cbd5e1', borderRadius: 6, paddingHorizontal: 10, paddingVertical: 8 },
segmentActive: { backgroundColor: '#2563eb', borderColor: '#2563eb' },
segmentTextActive: { color: '#fff' },
} as const
import { Button, View } from 'react-native'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// The demo stage around the form: header controls drawn with React Native components.
// There is no <form> to bind with `form=`, so the header button calls onSubmit.
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ onSubmit, children }) => <Button title={String(children)} onPress={onSubmit} />,
LanguageSwitch: ({ value, options, onChange }) => (
<View style={{ flexDirection: 'row', gap: 6 }}>
{options.map(option => (
<Button
key={option.value}
title={option.label}
color={option.value === value ? '#2563eb' : '#94a3b8'}
onPress={() => onChange(option.value)}
/>
))}
</View>
),
},
)

