Reference: form patterns
A complete multi-field form (controlled + manual validation)
No form library — state is plain React, validation is a function, errors surface through
color + message + messageColor.
import { useId, useState } from 'react';
import {
Input,
Select,
TextArea,
Checkbox,
Button,
Field,
} from '@allxsmith/bestax-bulma';
interface Values {
name: string;
email: string;
country: string;
bio: string;
agree: boolean;
}
function validate(v: Values) {
const errors: Partial<Record<keyof Values, string>> = {};
if (!v.name.trim()) errors.name = 'Name is required.';
if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v.email))
errors.email = 'Enter a valid email.';
if (!v.country) errors.country = 'Pick a country.';
if (!v.agree) errors.agree = 'You must accept the terms.';
return errors;
}
export function ProfileForm() {
const [values, setValues] = useState<Values>({
name: '',
email: '',
country: '',
bio: '',
agree: false,
});
const [submitted, setSubmitted] = useState(false);
const agreeErrorId = useId();
const errors = validate(values);
const show = (k: keyof Values) => (submitted ? errors[k] : undefined);
const set = (k: keyof Values, val: Values[keyof Values]) =>
setValues(prev => ({ ...prev, [k]: val }));
return (
<form
onSubmit={e => {
e.preventDefault();
setSubmitted(true);
if (Object.keys(errors).length === 0) {
// submit values…
}
}}
>
<Input
label="Name"
autoComplete="name"
value={values.name}
onChange={e => set('name', e.target.value)}
color={show('name') ? 'danger' : undefined}
aria-invalid={!!show('name')}
message={show('name')}
messageColor="danger"
/>
<Input
label="Email"
type="email"
autoComplete="email"
value={values.email}
onChange={e => set('email', e.target.value)}
color={show('email') ? 'danger' : undefined}
aria-invalid={!!show('email')}
message={show('email')}
messageColor="danger"
iconLeftName="envelope"
/>
<Select
label="Country"
autoComplete="country"
value={values.country}
onChange={e => set('country', e.target.value)}
color={show('country') ? 'danger' : undefined}
aria-invalid={!!show('country')}
message={show('country')}
messageColor="danger"
>
<option value="">Select…</option>
<option value="us">United States</option>
<option value="ca">Canada</option>
</Select>
<TextArea
label="Bio"
value={values.bio}
onChange={e => set('bio', e.target.value)}
rows={3}
/>
<Field>
<Checkbox
checked={values.agree}
onChange={e => set('agree', e.target.checked)}
aria-invalid={!!show('agree')}
aria-describedby={show('agree') ? agreeErrorId : undefined}
>
{' '}
I accept the terms
</Checkbox>
{show('agree') && (
<p id={agreeErrorId} className="help is-danger">
{show('agree')}
</p>
)}
</Field>
<Button color="primary" type="submit" mt="4">
Save
</Button>
</form>
);
}Notes:
- Validation runs every render as a pure function;
submittedgates when errors are shown so the form isn't red before the user interacts. You could gate per-field on blur instead. - For the checkbox the convenience
messageprop isn't used; the help text is rendered manually inside theField— a good illustration of dropping to composition when needed. It gets anidthe checkbox'saria-describedbypoints at, whichmessagedoes for you on the inputs.
Grouped controls and addons
An addon row with its label on an outer Field is the example in SKILL.md, under "Layout via
Field".
// Two controls on one row, each with its own label.
<Field grouped>
<Control>
<label className="label" htmlFor="first-name">
First name
</label>
<InputBase id="first-name" autoComplete="given-name" />
</Control>
<Control>
<label className="label" htmlFor="last-name">
Last name
</label>
<InputBase id="last-name" autoComplete="family-name" />
</Control>
</Field>
// A search box whose button says what it does.
<Field hasAddons>
<Control isExpanded>
<InputBase type="search" aria-label="Search" />
</Control>
<Control>
<Button color="primary">Search</Button>
</Control>
</Field>A grouped or hasAddons Field names none of the controls it holds, so give each input in
the row a label on screen (a placeholder isn't one: it goes once the user types).
- Addon row: a
hasAddonsField lays out everything it holds in its row, alabelincluded, so the label goes on an outerField. Wire it withlabelProps={{ htmlFor }}and a matchingidon the input: the innerFieldstarts its own scope, so without them the label shows and names nothing. The label needs anidtoo,labelProps={{ htmlFor, id }}, when the row holds anAutocomplete,ComboboxorTaginput(whose list pointsaria-labelledbyat it; aCombobox'shtmlFornames its field'sid) or a rangeSlider(whose thumbs do). A second control beside the labeled input, such as a country-codeSelect, takes anaria-label. - Grouped row: each
Controlholds a<label className="label">whosehtmlForpoints at its input. A convenience input's ownlabelrenders nothing inside aField(a grouped row, an addon row or an outer labeledField), so it can't stand in for one. An input and the button that sends it read better as an addon row with an outer label: in a labeled grouped row the button lines up with the labels, not the inputs. - Search box: an
aria-labelalone is enough when its button says what it does.
In a component that renders more than once, make each id with useId rather than a fixed
string.
A show-password button is not an addon recipe: use PasswordInput, whose button sits inside
the field. Never put it in iconRight, since Bulma gives control icons pointer-events: none
and a click on one lands on the input.
Recipes built on addons, each written out in full in the docs:
- Copy field (docs): a read-only input
and a
CopyButton value={text}in the secondControl. It writes, falls back where the Clipboard API is missing, reads "Copied" and announces the result itself, from a region it portals out of theControl. For a failure, keeplabels.failedStatusshort and put the instruction ("the link is selected, so copy it from the field") in a help line under the field that is the input'saria-describedby, not another live region. Count failures inonCopyError, and focus and select the input in an effect on that count, so focus arrives once the description is in the page and a screen reader reads it with the field.onCopiedChangeclears the count. - Split button (docs): a
Buttonand aDropdowninField hasAddons, oneControleach.Buttons hasAddonsdoes not join the Dropdown's trigger. Keep the default color, since the trigger takes nocolor.
Horizontal field with explicit label/body
Field.Label is the label column, not a <label>, so text placed straight in it names nothing:
put a <label className="label"> inside it with htmlFor pointing at the input's id.
<Field horizontal>
<Field.Label size="normal">
<label className="label" htmlFor="horizontal-email">
Email
</label>
</Field.Label>
<Field.Body>
<Control iconLeftName="envelope" hasIconsLeft>
<InputBase
id="horizontal-email"
type="email"
autoComplete="email"
placeholder="you@example.com"
/>
</Control>
</Field.Body>
</Field>Advanced inputs
These are controlled the same way — own the value, pass value/onChange.
// Autocomplete — onInput fires on typing, onSelect when a suggestion is chosen.
const [city, setCity] = useState('');
<Autocomplete
value={city}
onInput={setCity}
onSelect={item =>
setCity(typeof item === 'string' ? item : (item?.value ?? ''))
}
data={['Austin', 'Boston', 'Chicago']}
openOnFocus
/>;
// Combobox: a value from a list. Given data, it filters by label, keeps the
// value and shows its label in the field.
const [country, setCountry] = useState<string | null>(null);
<Combobox
label="Country"
data={['Canada', 'France', 'Japan']}
value={country}
onChange={setCountry}
empty="No countries match"
/>;
// Several values, shown as tags under the field; groups come from `group`.
const [labels, setLabels] = useState<string[]>([]);
<Combobox
label="Labels"
multiple
data={[
{ value: 'bug', label: 'Bug', group: 'Type' },
{ value: 'docs', label: 'Docs', group: 'Area' },
]}
value={labels}
onChange={setLabels}
/>;
// From parts, when data doesn't fit (a server search, a "Create" item): you
// filter, keep the value and set the text, and a pick calls onSelect.
const countries = ['Canada', 'France', 'Japan'];
const [choice, setChoice] = useState<string | null>(null);
const [query, setQuery] = useState('');
const term = query === choice ? '' : query.trim().toLowerCase();
const matches = countries.filter(c => c.toLowerCase().includes(term));
<Combobox
label="Country"
onSelect={picked => {
setChoice(picked);
setQuery(picked);
}}
>
<Combobox.Input value={query} onChange={e => setQuery(e.target.value)} />
<Combobox.Menu>
{matches.length > 0 ? (
<Combobox.List>
{matches.map(c => (
<Combobox.Item key={c} value={c} checked={c === choice}>
{c}
</Combobox.Item>
))}
</Combobox.List>
) : (
<Combobox.Empty>No countries match</Combobox.Empty>
)}
</Combobox.Menu>
</Combobox>;
// Slider (single and dual thumb)
const [volume, setVolume] = useState(50);
<Slider
label="Volume"
value={volume}
onChange={setVolume}
min={0}
max={100}
tooltip="auto"
/>;
const [range, setRange] = useState<[number, number]>([20, 80]);
// The label names both thumbs: "Price range Minimum value" / "... Maximum value".
<Slider
range
label="Price range"
value={range}
onChange={setRange}
min={0}
max={100}
/>;
// Numberinput
const [qty, setQty] = useState(1);
<Numberinput
label="Quantity"
value={qty}
onChange={setQty}
min={1}
max={10}
step={1}
/>;
// PinInput: a one-time code or PIN, a box per character, all in one input.
// onComplete fires once each time a change fills the last box.
const [code, setCode] = useState('');
const verify = (otp: string) => {
// Send it to your server, then show the result with color and message.
};
<PinInput
label="Enter the 6-digit code"
name="otp"
value={code}
onChange={setCode}
onComplete={verify}
/>;
// Rate
const [stars, setStars] = useState(3);
<Rate value={stars} onChange={setStars} max={5} />;
// Taginput — tags can be strings or { value, label } objects; onChange yields TaginputTag[].
const [tags, setTags] = useState<string[]>(['react']);
<Taginput
value={tags}
onChange={next =>
setTags(next.map(t => (typeof t === 'string' ? t : t.value)))
}
data={['react', 'bulma', 'ts']}
/>;Each renders inside Bulma's field/control structure already, so wrap them in a Field only when
you need a label or grouped layout. Reflect validation on them with the same color /
message / messageColor approach used for text inputs.