#TDesign
The registration form demo written with TDesign, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and layout) and the config.propsuse TDesign's own API, typed by its components. -
fields.tsx: the adapters, typed with TDesign's own props throughAdapterProps. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
Input,SelectandCheckboxpass the value toonChange;Inputhas notype="email"(a type error in the config). -
A wrapping
<label>names the input, since TDesignInputtakes noid. -
Import
tdesign-react/es/style/index.css;ConfigProvider globalConfigsets the built-in texts, which default to Chinese.
main.tsx only mounts the docs' demo stage (language switch, payload drawer); your app renders Form itself. See Connect your UI kit.
import { Button, Card } from 'tdesign-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 }
/** 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 theme="primary" type="submit" style={{ marginTop: 16 }}>
{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 title={label} bordered style={fullRow}>
{fields.map((item, index) => (
<div key={item.id} style={{ ...row, marginBottom: 8 }}>
<div style={{ flex: 1 }}>{renderItem(item, index)}</div>
<Button theme="danger" variant="text" onClick={() => remove(index)}>
{t('action.remove')}
</Button>
</div>
))}
{errorMessage && <div style={{ color: 'var(--td-error-color)' }}>{errorMessage}</div>}
<Button variant="dashed" block onClick={add}>
{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 TDesign'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: { placeholder: 'hint.email', clearable: true },
},
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', clearable: 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} bordered 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: { align: 'right' },
},
})
import { Checkbox, Input, Select } from 'tdesign-react'
import type { ComponentProps, ReactNode } from 'react'
import { type AdapterProps, useField } from 'react-headless-form'
// TDesign's Form.FormItem needs TDesign's Form: a wrapping <label> names the input
function FieldShell({ children }: { children: ReactNode }) {
const { label, errorMessage, required } = useField()
return (
<div style={{ display: 'grid', gap: 4 }}>
<label style={{ display: 'grid', gap: 4 }}>
<span>
{label}
{required && ' *'}
</span>
{children}
</label>
{errorMessage && <div style={{ color: 'var(--td-error-color)', fontSize: 12 }}>{errorMessage}</div>}
</div>
)
}
// AdapterProps: TDesign's own props minus what useField() controls, and what is set below
export type TextFieldProps = AdapterProps<ComponentProps<typeof Input>, 'status' | 'readonly'>
function TextField(props: TextFieldProps) {
const { value, onChange, onBlur, errorMessage, disabled, readOnly } = useField()
return (
<FieldShell>
<Input
{...props}
value={value ?? ''}
onChange={next => onChange(next)} // value, not an event
onBlur={() => onBlur()}
status={errorMessage ? 'error' : 'default'}
disabled={disabled}
readonly={readOnly}
/>
</FieldShell>
)
}
/** `options` come from the config, in TDesign's own shape. */
export type SelectFieldProps = AdapterProps<ComponentProps<typeof Select>, 'status'>
function SelectField(props: SelectFieldProps) {
const { value, onChange, onBlur, errorMessage, disabled } = useField()
return (
<FieldShell>
<Select
{...props}
value={value}
onChange={next => onChange(next)}
onBlur={() => onBlur()}
status={errorMessage ? 'error' : 'default'}
disabled={disabled}
/>
</FieldShell>
)
}
export type CheckboxFieldProps = AdapterProps<ComponentProps<typeof Checkbox>, 'checked'>
function CheckboxField(props: CheckboxFieldProps) {
const { value, onChange, label, disabled } = useField()
return (
<Checkbox {...props} checked={Boolean(value)} onChange={checked => onChange(checked)} disabled={disabled}>
{label}
</Checkbox>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import 'tdesign-react/es/style/index.css'
import type { ReactNode } from 'react'
import { Button, ConfigProvider, Radio } from 'tdesign-react'
import enUS from 'tdesign-react/es/locale/en_US'
import jaJP from 'tdesign-react/es/locale/ja_JP'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// TDesign's built-in texts (placeholders, empty states) follow the demo language
const Wrapper = ({ children, lang }: { children: ReactNode; lang: string }) => (
<ConfigProvider globalConfig={lang === 'ja' ? jaJP : enUS}>{children}</ConfigProvider>
)
// The demo stage around the form: header controls drawn with TDesign
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button theme="primary" type="submit" form={form} size="small">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<Radio.Group variant="default-filled" size="small" value={value} onChange={next => onChange(String(next))}>
{options.map(option => (
<Radio.Button key={option.value} value={option.value}>
{option.label}
</Radio.Button>
))}
</Radio.Group>
),
},
{ Wrapper },
)

