#Semi Design
The registration form demo written with Semi Design, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and layout) and the config.propsuse Semi Design's own API, typed by its components. -
fields.tsx: the adapters, typed with Semi Design's own props throughAdapterProps. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
Inputpasses the value first (and the event second);SelecttakesoptionList. -
With React 19 use
@douyinfe/semi-ui-19; import itsdist/css/semi.min.css. -
LocaleProvidersets the built-in texts (here it follows the language switch).
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, Typography } from '@douyinfe/semi-ui-19'
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>
<Typography.Title heading={4}>{t('title')}</Typography.Title>
<div style={grid}>{children}</div>
<Button theme="solid" type="primary" htmlType="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} style={fullRow}>
{fields.map((item, index) => (
<div key={item.id} style={{ ...row, marginBottom: 8 }}>
<div style={{ flex: 1 }}>{renderItem(item, index)}</div>
<Button type="danger" theme="borderless" onClick={() => remove(index)}>
{t('action.remove')}
</Button>
</div>
))}
{errorMessage && <Typography.Text type="danger">{errorMessage}</Typography.Text>}
<Button 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 Semi Design'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', prefix: '@', showClear: true },
},
ticket: {
type: 'select',
label: 'field.ticket',
props: {
// Semi's options prop is `optionList`
optionList: [
{ 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', showClear: 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} 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: { prefix: '$' },
},
})
import { useId } from 'react'
import { Checkbox, Input, Select, Typography } from '@douyinfe/semi-ui-19'
import type { ComponentProps, ReactNode } from 'react'
import { type AdapterProps, useField } from 'react-headless-form'
// Semi's Form.* fields need Semi's Form: plain markup for label and message
function FieldShell({ id, children }: { id: string; children: ReactNode }) {
const { label, errorMessage, required } = useField()
return (
<div style={{ display: 'grid', gap: 4 }}>
<label htmlFor={id}>
{label}
{required && ' *'}
</label>
{children}
{errorMessage && <Typography.Text type="danger">{errorMessage}</Typography.Text>}
</div>
)
}
// AdapterProps: Semi's own props minus what useField() controls, and what is set below
export type TextFieldProps = AdapterProps<ComponentProps<typeof Input>, 'id' | 'validateStatus' | 'readonly'>
function TextField(props: TextFieldProps) {
const id = useId()
const { value, onChange, onBlur, errorMessage, disabled, readOnly } = useField()
return (
<FieldShell id={id}>
<Input
{...props}
id={id}
value={value ?? ''}
onChange={next => onChange(next)} // value first, event second
onBlur={onBlur}
validateStatus={errorMessage ? 'error' : 'default'}
disabled={disabled}
readonly={readOnly}
/>
</FieldShell>
)
}
/** `optionList` comes from the config, in Semi's own shape. */
export type SelectFieldProps = AdapterProps<ComponentProps<typeof Select>, 'id' | 'validateStatus'>
function SelectField(props: SelectFieldProps) {
const id = useId()
const { value, onChange, onBlur, errorMessage, disabled } = useField()
return (
<FieldShell id={id}>
<Select
style={{ width: '100%' }}
{...props}
id={id}
value={value}
onChange={next => onChange(next)}
onBlur={onBlur}
validateStatus={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={e => onChange(Boolean(e.target.checked))} disabled={disabled}>
{label}
</Checkbox>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import '@douyinfe/semi-ui-19/dist/css/semi.min.css'
import { Button, LocaleProvider, Radio, RadioGroup } from '@douyinfe/semi-ui-19'
import enUS from '@douyinfe/semi-ui-19/lib/es/locale/source/en_US'
import jaJP from '@douyinfe/semi-ui-19/lib/es/locale/source/ja_JP'
import type { ReactNode } from 'react'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// Semi's built-in texts follow the demo language
const Wrapper = ({ children, lang }: { children: ReactNode; lang: string }) => (
<LocaleProvider locale={lang === 'ja' ? jaJP : enUS}>{children}</LocaleProvider>
)
// The demo stage around the form: header controls drawn with Semi Design
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button theme="solid" type="primary" htmlType="submit" form={form} size="small">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<RadioGroup type="button" value={value} onChange={e => onChange(e.target.value)}>
{options.map(option => (
<Radio key={option.value} value={option.value}>
{option.label}
</Radio>
))}
</RadioGroup>
),
},
{ Wrapper },
)

