PeakUI

TextField

A text input with a label, a description and validation, composed from separate parts.

Usage

import {Input, Label, TextField} from "@peakui/react";

Anatomy

import {Description, FieldError, Input, Label, TextField} from "@peakui/react";

export default () => (
  <TextField>
    <Label />
    <Input /> {/* or <InputGroup /> */}
    <Description />
    <FieldError />
  </TextField>
);

TextField is the root: it owns the value, the validation state and the ids that link the parts. Label, Input, Description and FieldError are its children, each a component of its own. There is no assembled form such as <Input label="Email" />.

Examples

With Description

Required

isRequired marks the label and makes the field required for form submission.

With Error Message

isInvalid marks the field as invalid and makes FieldError render.

Validation

validate receives the current value (an empty string for an empty field) and returns string | string[] | true | null | undefined: a message, or several, for an invalid value, and true, null or undefined when the value is valid.

Which message shows, and when:

  • A constraint and validate both fail. The message from validate is shown. The browser's message for isRequired, minLength, pattern or type appears only while validate returns a valid result, so make validate return null for an empty value if the browser's "required" message should be the one shown for it.
  • Native behavior (the default). Messages appear when the form is submitted, and again when the browser reports a change for the input, which is when it loses focus after its value was edited. They do not appear on every keystroke.
  • validationBehavior="aria". The message from validate, and from isInvalid, shows as the value changes. isRequired becomes aria-required instead of the native required attribute, and the constraints produce no message of their own: write them as validate or isInvalid instead.

Form

A complete form: two fields, each with a description and a FieldError, and a submit button. Submit it empty, then with a username that contains a space, to see each message appear under its own field.

Controlled

onChange receives the value as a string, not an event.

Variants

The field's variant is passed down to its Input or InputGroup.

Full Width

Disabled State

Customization

Tailwind CSS

<TextField className="gap-2" name="email">
  <Label className="text-xs uppercase">Email</Label>
  <Input className="rounded-full" />
</TextField>

Global CSS

@layer components {
  .textfield {
    @apply gap-2;
  }
}

Styling Reference

CSS Classes

The TextField component uses these CSS classes (view source styles):

  • .textfield - The field root. The block is one word, textfield
  • .textfield--full-width - Stretches to the container's width

Slots

  • [data-slot="textfield"]

Interactive States

The root carries the field's state, and its parts are styled from it:

  • Invalid: [data-invalid] or [aria-invalid="true"]
  • Disabled: [data-disabled]
  • Required: [data-required]
  • Read only: [data-readonly]

API Reference

TextField

PropTypeDefaultDescription
variant'primary' | 'secondary''primary'Surface style, passed down to Input and InputGroup
fullWidthbooleanfalseWhether the field takes the full width of its container
valuestring-The controlled value
defaultValuestring-The initial value when uncontrolled
onChange(value: string) => void-Handler called with the new value
namestring-Name of the field in form submission
type'text' | 'search' | 'url' | 'tel' | 'email' | 'password' | (string & {})'text'The input type
isRequiredbooleanfalseWhether a value is required for form submission
isInvalidbooleanfalseWhether the value is invalid
isDisabledbooleanfalseWhether the field is disabled
isReadOnlybooleanfalseWhether the value can be selected but not changed
validate(value: string) => string | string[] | true | null | undefined-Returns an error message, or messages, for an invalid value, and true, null or undefined for a valid one
validationBehavior'native' | 'aria''native'Whether validation blocks form submission (native) or only marks the field (aria)
childrenReact.ReactNode | (values: TextFieldRenderProps) => React.ReactNode-The parts of the field, or a function of the field's state
classNamestring | (values: TextFieldRenderProps) => string-Additional classes, or a function of the field's state

Every other React Aria TextField prop is accepted, including minLength, maxLength, pattern and autoComplete.

Render Props

When children or className is a function, it receives:

PropTypeDescription
isDisabledbooleanWhether the field is disabled
isInvalidbooleanWhether the field is invalid
isReadOnlybooleanWhether the field is read only
isRequiredbooleanWhether the field is required

On this page