Live email field
The full thing, wired up. The label lifts and recolours as you focus, the underline draws in from the left, and the @ pops as the cursor lands. Type a valid address and a check strokes in; leave a bad one and it shakes once, then turns red. Try it — focus the field and type.
function Field() {
const [value, setValue] = useState('');
const [touched, setTouched] = useState(false);
const valid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
const showError = touched && value.length > 0 && !valid;
return (
<Input
type="email"
label="Email address"
icon={<AtIcon />}
placeholder="you@company.com"
value={value}
onChange={(e) => setValue(e.target.value)}
onBlur={() => setTouched(true)}
error={showError ? 'Enter a valid email address' : false}
success={valid}
helperText="We'll only use this to send your receipt."
/>
);
}