#animal-island-ui
The registration form demo written with animal-island-ui, self-contained:
-
form.tsx:setupForm(with arenderRootfor the title and grid) and the config.propsuse animal-island-ui's own API, typed by its components. -
fields.tsx: the adapters, typed with animal-island-ui's own props minus whatuseField()controls. -
main.tsx: mounts it in the demo stage (header, payload drawer). -
Selectoptions usekey(notvalue) andonChangereceives the key. -
There is no
TextArea; long text usesInput. ItsCheckboxis a group (array of values), so a boolean field usesSwitch. -
Import
animal-island-ui/styleonce.
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, Title } from 'animal-island-ui'
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: 12 }
const fullRow = { gridColumn: '1 / -1', display: 'grid', gap: 12 }
// 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 style={{ display: 'grid', gap: 16 }}>
<Title>{t('title')}</Title>
<div style={grid}>{children}</div>
<Button type="primary" htmlType="submit">
{t('action.save')}
</Button>
</form>
)
}
function Guests() {
const t = useT()
const { label, fields, renderItem, append, remove, errorMessage } = useArrayField()
return (
<Card color="app-yellow" style={fullRow}>
<Title size="small" color="app-yellow">
{label}
</Title>
{fields.map((item, index) => (
<div key={item.id} style={{ display: 'flex', gap: 8, alignItems: 'end' }}>
<div style={{ flex: 1 }}>{renderItem(item, index)}</div>
<Button size="small" onClick={() => remove(index)}>
{t('action.remove')}
</Button>
</div>
))}
{errorMessage && <span style={{ color: '#d9534f' }}>{errorMessage}</span>}
<Button onClick={() => append({ name: '' }, { shouldFocus: false })}>{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 animal-island-ui's own API (InputProps, SelectProps, ...)
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: '@' },
},
ticket: {
type: 'select',
label: 'field.ticket',
props: {
// its options use `key`
options: [
{ key: 'standard', label: t('ticket.standard') },
{ key: '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', allowClear: 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 color="app-blue" style={fullRow}>
<Title size="small" color="app-blue">
{label}
</Title>
<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: '$', shadow: true },
},
})
import { useId } from 'react'
import { Input, Select, Switch } from 'animal-island-ui'
import type { CSSProperties, ReactNode } from 'react'
import { type AdapterProps, useField } from 'react-headless-form'
import type { InputProps, SelectProps, SwitchProps } from 'animal-island-ui'
const stack: CSSProperties = { display: 'grid', gap: 12 }
// animal-island-ui has no form-field wrapper: label and error are plain markup.
function FieldRow({ 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 && <span style={{ color: '#d9534f', fontSize: 13 }}>{errorMessage}</span>}
</div>
)
}
// AdapterProps: the library'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 animal-island-ui's.
type Extra = 'id' | 'status'
export type TextFieldProps = AdapterProps<InputProps, Extra>
function TextField(props: TextFieldProps) {
const id = useId()
const { value, onChange, onBlur, errorMessage, disabled, readOnly } = useField()
return (
<FieldRow id={id}>
<Input
{...props}
id={id}
value={value ?? ''}
onChange={e => onChange(e.target.value)}
onBlur={onBlur}
status={errorMessage ? 'error' : undefined}
disabled={disabled}
readOnly={readOnly}
/>
</FieldRow>
)
}
/** `options` come from the config, in animal-island-ui's shape (`key`). */
export type SelectFieldProps = AdapterProps<SelectProps, 'aria-label'>
function SelectField(props: SelectFieldProps) {
const id = useId() // its Select takes no id: the label names it through aria-label
const { value, onChange, label, disabled } = useField()
return (
<FieldRow id={id}>
{/* onChange passes the key, not an event */}
<Select
{...props}
aria-label={typeof label === 'string' ? label : undefined}
value={value}
onChange={key => onChange(key)}
disabled={disabled}
/>
</FieldRow>
)
}
// Its Checkbox is a group (array of values); a Switch fits a boolean.
export type CheckboxFieldProps = AdapterProps<SwitchProps, 'checked' | 'defaultChecked'>
function CheckboxField(props: CheckboxFieldProps) {
const { value, onChange, label, disabled } = useField()
return (
<label style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
<Switch {...props} checked={Boolean(value)} onChange={checked => onChange(checked)} disabled={disabled} />
{label}
</label>
)
}
export const fields = {
text: TextField,
select: SelectField,
checkbox: CheckboxField,
}
import 'animal-island-ui/style'
import { Button } from 'animal-island-ui'
import { mountDemo } from '../shared/mount'
import { Form, buildConfig, defaultValues } from './form'
// The demo stage around the form: header controls drawn with animal-island-ui
mountDemo(
{ Form, buildConfig, defaultValues },
{
SubmitButton: ({ form, children }) => (
<Button type="primary" htmlType="submit" form={form} size="small">
{children}
</Button>
),
LanguageSwitch: ({ value, options, onChange }) => (
<div style={{ display: 'flex', gap: 6 }}>
{options.map(option => (
<Button
key={option.value}
size="small"
type={option.value === value ? 'primary' : 'default'}
aria-pressed={option.value === value}
onClick={() => onChange(option.value)}
>
{option.label}
</Button>
))}
</div>
),
},
)

