Ir al contenido
Español

Cómo funciona

The fill, hairline and states are drawn on the outer element, around a borderless native input, so ref, name and autoComplete land on the input and work with form libraries. The clear button keeps its place and only fades, so text never shifts; the count reserves width for the limit's digits. Clearing uses the native setter and an input event, so controlled and uncontrolled share one onChange. Toggling a password restores the caret.

Úsalo para

  • Single-line text in forms: names, titles, usernames, phone numbers, search terms.
  • Inputs that need clearing, a password toggle, a length limit, or a unit or area-code prefix or suffix.
  • Mobile forms: use size="lg" (52px high; 16px text does not zoom the page on iOS).
  • Use inputMode="numeric" for phone numbers and codes and "decimal" for amounts to get the right keypad.

Busca otra opción cuando

  • Multi-line content such as notes or messages: use a textarea.
  • Numbers that need plus and minus buttons: use a number input.
  • One-time codes: use a code input.
  • Choosing from fixed options: use a select instead of making people type.

Uso

import { useState } from 'react'
import { TextInput } from './TextInput.jsx'

export function ProjectTitleField() {
  const [title, setTitle] = useState('')
  const tooShort = title !== '' && title.length < 4
  return (
    <>
      <label htmlFor="project-title">Project title</label>
      <TextInput
        id="project-title"
        value={title}
        onChange={setTitle}
        clearable
        showCount
        maxLength={20}
        invalid={tooShort}
        aria-describedby="project-title-msg"
        placeholder="e.g. Spring launch"
      />
      <p id="project-title-msg">{tooShort ? 'At least 4 characters' : 'Shown in the project list'}</p>
    </>
  )
}

// Password field with a built-in show / hide toggle
// <TextInput type="password" autoComplete="current-password" />

// Prefix and suffix: <TextInput prefix="+1" inputMode="numeric" /> <TextInput suffix="@example.com" />

Propiedades

PropiedadTipoPor defectoQué hace
valuestring—Controlled value. Omit for uncontrolled.
defaultValuestring—Initial value when uncontrolled.
onChange(value: string, event: ChangeEvent) => void—Receives the string value and the native event. The clear button also triggers it, with an empty string.
size'sm' | 'md' | 'lg''md'Height 36 / 44 / 52px, radius 10 / 12 / 14px, text 14 / 15 / 16px.
prefixReactNode—Icon or text inside the field on the left, such as a search icon or “+1”.
suffixReactNode—Icon or text inside the field on the right, such as “kg” or “@example.com”.
clearablebooleanfalseShows a clear button when there is content and the field is focused or hovered. Its place is always reserved; hidden when read-only or disabled.
onClear() => void—Called after the clear button is pressed; onChange still receives the empty string.
typestring'text'Native input type. With password, a show / hide toggle is added.
revealablebooleantrueWhether a password field has the show toggle. Pass false to never reveal the text.
maxLengthnumber—Maximum number of characters (native maxLength; extra input is rejected).
showCountbooleanfalseShows the count: “typed / limit” with maxLength, otherwise just the typed count.
invalidbooleanfalseError state: danger hairline and a faint danger tint, plus aria-invalid. Put the message below the field and link it with aria-describedby.
disabledbooleanfalseDisabled: the fill stays, the hairline goes, text is muted and it cannot be focused.
labelsobject—Replaces built-in text (clear, showPassword, count, countMax). Shallow-merged over the English defaults.
refRef<HTMLInputElement>—Forwarded to the native <input>; works with react-hook-form's register.
...restInputHTMLAttributes—className and style go on the outer element; other native attributes (id, name, placeholder, readOnly, autoComplete, inputMode, onBlur) go to the <input>.

Accesibilidad

  • It is a native <input>: link a <label htmlFor>; a prefix or suffix does not replace the label.
  • invalid sets aria-invalid; link the message with aria-describedby so it is read on focus.
  • The count is linked through aria-describedby and read as “12 of 20 characters entered”, not character by character.
  • The show-password button is tabbable, named “Show password” and uses aria-pressed; the clear button is outside the Tab order (select all and delete works) and named “Clear”.
  • Clicking buttons in the frame keeps focus in the input, so there is no blur validation and the mobile keyboard stays open.

Movimiento

On focus the fill turns white in 160ms and the accent line and 4px halo grow in over 220ms (ease-out). The clear button fades in over 160ms and out over 100ms. The slash on the eye draws and retracts in 240ms; the eye presses to 0.9.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

TextInput.jsxtext-input.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Formularios
Cuadrícula de disponibilidad viene con Pro. La vista previa aparece al desbloquearlo.
Operaciones booleanas viene con Pro. La vista previa aparece al desbloquearlo.
Selector en cascada viene con Pro. La vista previa aparece al desbloquearlo.