本文へ移動
日本語

仕組み

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.

向いている場面

  • 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.

向かない場面

  • 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.

使い方

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" />

プロパティ

プロパティ型既定値説明
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>.

アクセシビリティ

  • 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.

モーション

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.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

TextInput.jsxtext-input.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。