Bedform UIControls

Textfield

Enter and edit text.

Textfield

*
string
*
boolean
*
string
*
string
*
string
*
boolean
*
string

Usage

import { Textfield } from "@bedform/ui/textfield";
import { useState } from "react";

export function WorldNameField() {
  const [value, setValue] = useState("");

  return (
    <Textfield
      name="world"
      label="World name"
      description="Shown to other players"
      placeholder="New World"
      value={value}
      onChange={(event) => setValue(event.currentTarget.value)}
    />
  );
}

Textfield renders the label, the framed native input, and the description and error text. It links them with id and aria-describedby, generating an id when you do not pass one.

Validation

Use native input constraints for validation. Set error to show a custom message: Textfield passes it to setCustomValidity(), sets aria-invalid, and connects the visible message with aria-describedby.

<Textfield label="Seed" name="seed" type="number" required />
<Textfield label="World name" name="world" error="This name is already in use" />

Events

Textfield uses the native input, change, focus, blur, and form events of its <input>, so onChange, onInput, onFocus and onBlur work as on any input. Its value is the input's native value, and its ref points to the input.

API

Textfield passes every input attribute to the native <input>, such as autoComplete, inputMode, list, name, value, type, placeholder, min, max, step, minLength, maxLength, pattern, required, readOnly, disabled and id. className and style apply to the root element instead.

PropTypeDefault
descriptionstring—
errorstring—
labelstring—

Public classes

ClassPurpose
.ore-textfield-labelVisible label
.ore-textfield-controlInput frame
.ore-textfield-inputNative input
.ore-textfield-iconLeading icon
.ore-textfield-descriptionSupporting text
.ore-textfield-errorValidation message

CSS variables

VariablePurpose
--ore-textfield-unitBase size unit
--ore-textfield-backgroundInput background color
--ore-textfield-shadowBottom shadow color
--ore-textfield-borderBorder color
--ore-textfield-foregroundLabel text color
--ore-textfield-input-foregroundInput text color
--ore-textfield-descriptionDescription text color
--ore-textfield-errorError text and border color
--ore-textfield-caretText caret color

Accessibility

Use a visible <label for="..."> for every input whenever possible. The for value must match the input id, and the input remains the accessible control and form participant. Use aria-label only when a visible label is not appropriate.

Give supporting text an id and reference it from aria-describedby. For a visible validation message, reference the error element from the same attribute and set aria-invalid="true" while the value is invalid. Keep the browser's validity state as the source of truth with native constraints or setCustomValidity().

disabled removes the input from focus and form submission. readonly keeps it focusable and submittable while preventing edits. Use a surrounding <fieldset disabled> when a group of fields must be disabled together.

On this page