Forms
Text field
Text, email, password or number input.
Examples
The label is required and is part of the component. The hint text (hint) and the error message (error) are shown between the label and the field, and read by screen readers.
import { CrInput } from '@cristal-ui/react';
export default function InputBasic() {
return (
<>
<CrInput label="Email" type="email" name="email" hint="Format: name@domain.com" autocomplete="email" required />
<CrInput label="Name" error="Name is required. Enter at least 2 characters." />
<CrInput label="Reference" value="REF-001" disabled />
</>
);
}Automatic validation
Input errors appear on their own (in English by default, in French with lang="fr") when the user leaves the field after changing it, or when they try to submit the form. They disappear as soon as the input is fixed.
| Case | Message |
|---|---|
| Empty required field | This field is required. |
Malformed type="email" | Enter a valid email address, for example name@domain.com. |
Malformed type="url" | Enter a valid web address, for example https://example.com. |
Non-numeric type="number" | Enter a number. |
On submit, focus moves to the first invalid field, and its message is read by screen readers. For an error coming from the server ("this address is already in use"), pass your own message with error: it replaces the automatic message.
Animated error
error-animation (errorAnimation in React) animates the error when it appears, then again on every rejected submit, even when the message is the same. Pick an animation, then click Continue with the field empty or an incomplete address.
'use client';
import { useState } from 'react';
import { CrBox, CrButton, CrInput, CrSegmented } from '@cristal-ui/react';
const animations = [
['zigzag', 'Zigzag'],
['pop', 'Badge'],
['drop', 'Sign'],
['echo', 'Echo'],
['jitter', 'Letters'],
['wobble', 'Jelly'],
] as const;
export default function InputErrorAnimation() {
const [animation, setAnimation] = useState(0);
const [sent, setSent] = useState('');
return (
<CrBox display="flex" direction="column" gap="xl" align="center">
<CrSegmented label="Error animation" fit value={String(animation)} onCrChange={(event) => setAnimation(Number(event.detail))}>
{animations.map(([value, label], index) => (
<option key={value} value={index}>
{label}
</option>
))}
</CrSegmented>
<form
style={{ display: 'grid', gap: 24, width: 'min(100%, 360px)', boxSizing: 'border-box', padding: 32, borderRadius: 'var(--cr-radius-l)', background: 'var(--cr-color-surface)', boxShadow: 'var(--cr-shadow-m)' }}
onSubmit={(event) => {
event.preventDefault();
setSent(String(new FormData(event.currentTarget).get('email')));
}}
>
<div>
<h3 style={{ margin: 0 }}>Create your account</h3>
<p style={{ margin: '4px 0 0', color: 'var(--cr-color-text-muted)' }}>We will send you a sign-in link.</p>
</div>
<CrInput label="Email" name="email" type="email" autocomplete="email" required errorAnimation={animations[animation][0]} onCrInput={() => setSent('')} />
<CrButton type="submit" fullWidth>
Continue
</CrButton>
<p role="status" style={{ margin: 0 }}>
{sent && `Link sent to ${sent}.`}
</p>
</form>
</CrBox>
);
}error-animation | Effect |
|---|---|
none | no animation (default) |
zigzag | the bottom edge of the field snaps into a buzzing red zigzag, then settles flat |
pop | a red "!" badge springs out at the end of the field with a few sparks, and stays |
drop | the message drops in and swings from its top-left corner, like a hanging sign |
echo | the border flashes red twice and sends out two widening echoes |
jitter | the letters of the message hop and shiver one after another |
wobble | the field rocks like jelly, then settles |
The same prop exists on the text area, one-time code, select, tags, payment card, checkbox, radio group, date picker, quantity picker and file upload. On the checkbox and the radio group, the zigzag runs under the box and its label, the badge sits after them and the whole block wobbles; on the one-time code, the badge follows the last box and the echoes wrap all the boxes; on the file upload, the animation plays on every rejected file.
Screen readers still get the message as one sentence (with jitter, the hopping letters are a hidden copy), and it stays readable at every frame: it moves, but never fades in from transparent. When the user asks for less motion (prefers-reduced-motion), nothing moves: the red border and the message appear at once, with no sparks or echoes. In forced colors mode, the field in error gets a system-coloured outline.
Password strength and completion
- With
strength(fortype="password"), a four-segment meter fills up and changes color while typing; the level is written out and announced: "Strength: Good". The score takes into account length, upper and lower case, digits and symbols. - With
completions, the rest of the first suggestion that extends the input is shown in gray behind the text. Right Arrow or a click on it accepts it: it is filled in, and "Filled in for you" is shown. The field is markedaria-autocomplete="inline"and the suggestion is read with the field description.
import { CrInput } from '@cristal-ui/react';
const emails = ['mia@mail.com', 'mia.smith@company.com', 'lucas@example.com'];
export default function InputExtras() {
return (
<>
<CrInput label="New password" type="password" autocomplete="new-password" strength />
<CrInput label="Email" type="email" completions={emails} hint="Type “mia”, then press the right arrow to accept the suggestion" />
</>
);
}In a form
Cristal fields take part in the parent <form> like native fields: their value is submitted under their name, required blocks submission, type="reset" restores their initial value, and Enter in a text field submits the form.
Try submitting the empty form, typing an incomplete email address, then filling everything in.
'use client';
import { useState } from 'react';
import { CrButton, CrCheckbox, CrInput, CrSelect } from '@cristal-ui/react';
export default function FormComplete() {
const [sent, setSent] = useState('');
return (
<form
className="flex flex-col gap-4"
onSubmit={(event) => {
event.preventDefault();
setSent(JSON.stringify(Object.fromEntries(new FormData(event.currentTarget))));
}}
>
<CrInput label="Email" name="email" type="email" required />
<CrSelect label="Country" name="country" placeholder="Choose a country" required>
<option value="us">United States</option>
<option value="gb">United Kingdom</option>
<option value="ca">Canada</option>
</CrSelect>
<CrCheckbox label="I accept the terms" name="terms" required />
<div className="flex gap-2">
<CrButton type="submit">Submit</CrButton>
<CrButton type="reset" variant="secondary">
Reset
</CrButton>
</div>
{sent && <output>Submitted data: {sent}</output>}
</form>
);
}Accessibility
- Label linked to the field: clicking the label puts the cursor in the field.
hintanderrorlinked witharia-describedby;erroraddsaria-invalid="true"and a red border (the error does not rely on color alone: the message is displayed).- The asterisk on required fields is hidden from screen readers, which already announce "required".
- Use
autocomplete(email,name,tel…) to help autofill.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
autocomplete | string | — | Indication de remplissage automatique du navigateur. |
completions | string[] | — | Suggestions : la suite de la première qui prolonge la saisie s'affiche en gris ; flèche droite ou clic pour l'accepter. |
disabled | boolean | false | Désactive le champ. |
error | string | — | Message d'erreur ; remplace le message automatique de validation. |
errorAnimation | "drop" | "echo" | "jitter" | "none" | "pop" | "wobble" | "zigzag" | 'none' | Animation de l'erreur quand elle apparaît et à chaque envoi refusé : none (par défaut), zigzag, pop, drop, echo, jitter ou wobble. |
hint | string | — | Texte d'aide, lu par les lecteurs d'écran. |
label (obligatoire) | string | — | Libellé affiché au-dessus du champ. |
name | string | — | Nom envoyé avec le formulaire. |
placeholder | string | — | Texte indicatif affiché quand le champ est vide. |
required | boolean | false | Rend la saisie obligatoire. |
strength | boolean | false | Affiche une jauge de sécurité sous le champ (pour type="password"). |
type | "email" | "number" | "password" | "search" | "tel" | "text" | "url" | 'text' | Type de saisie. |
value | string | '' | Valeur du champ. |
Events
| React | event.detail | Description |
|---|---|---|
onCrChange | string | Émis quand le champ perd le focus après modification ; detail : la valeur. |
onCrInput | string | Émis à chaque frappe ; detail : la valeur. |
CSS parts
To style it from outside: cr-input::part(name) { … }
| Part | Description |
|---|---|
control | Le champ natif. |
error | Le message d'erreur. |
hint | Le texte d'aide. |
label | Le libellé. |