Live example

At least two characters. Tab out of the field to validate.
Between 0 and 50,000
Usage
<TextField
  label="Destination"
  required
  validate={validators.compose(validators.required('Enter a destination'), validators.minLength(2))}
/>

Props

TextField props
PropTypeDefaultDescription
labelstringnoneVisible label. Placeholders are not labels.
description?ReactNodenoneHelp text, linked with aria-describedby.
validate?(value: string) => string | undefinednoneRuns on blur, then on each change once an error is showing.
error?stringnoneError from the parent, such as after submit. Wins over validate.
value?stringnoneControlled value.
defaultValue?string''Uncontrolled initial value.
onChange?(value: string) => voidnoneCalled with the new string value.
prefix?ReactNodenoneDecorative text before the input, such as a currency symbol.
hideLabel?booleanfalseVisually hide the label. Use only when context makes it obvious.

Do and don't

Do

  • Validate on blur, not on every keystroke of a fresh value.
  • Write errors that say how to fix it: "Enter 0 or more".
  • Mark required fields in the label text.

Don't

  • Use placeholder text as the label.
  • Clear the user's input when it is invalid.
  • Show an error before the user has left the field.

Accessibility

  • Label is a real label element. Help text and error are referenced by aria-describedby.
  • aria-invalid is set only while an error shows. The error has an icon and text, never color alone.
  • The error container exists before an error appears, so its id is stable for assistive tech.

Keyboard

TextField keyboard support
KeysAction
TabMoves into and out of the field; leaving it runs validation