본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Text Field (텍스트 필드)

원문 보기 위키 갱신

텍스트 필드는 사용자가 키보드로 자유로운 텍스트를 입력할 수 있게 해주는 텍스트 입력 요소예요. 한 줄 입력(Input)과 여러 줄 입력(TextArea)을 모두 지원하며, 라벨·설명·오류 메시지를 ARIA로 연결해 접근성을 높일 수 있답니다.

출처: 문서

본문

Import (불러오기)

import { TextField } from "@kobalte/core/text-field";
// or
import { Root, Label, ... } from "@kobalte/core/text-field";
// or (deprecated)
import { TextField } from "@kobalte/core";

Features (기능)

  • 네이티브 <input> 또는 <textarea> 요소로 만들어져요.
  • 시각적·ARIA 라벨링을 지원해요.
  • 필수(required) 및 유효하지 않은(invalid) 상태를 ARIA를 통해 보조 기술에 노출해요.
  • ARIA를 통해 input에 연결된 설명(description)과 오류 메시지 도움말 텍스트를 지원해요.
  • 폼 reset 이벤트와 동기화돼요.
  • 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.

Anatomy (구조)

텍스트 필드는 다음의 부분들로 이루어져요:

  • TextField: 텍스트 필드의 루트 컨테이너예요.
  • TextField.Label: 텍스트 필드에 대한 정보를 사용자에게 알려주는 라벨이에요.
  • TextField.Input: 한 줄 텍스트에 사용하는 텍스트 필드의 네이티브 HTML input이에요.
  • TextField.TextArea: 여러 줄 텍스트에 사용하는 텍스트 필드의 네이티브 HTML textarea예요.
  • TextField.Description: 텍스트 필드에 대한 더 많은 정보를 사용자에게 알려주는 설명이에요.
  • TextField.ErrorMessage: 텍스트 필드의 검증 오류를 어떻게 고치는지에 대한 정보를 사용자에게 알려주는 오류 메시지예요.
<TextField>
	<TextField.Label />
	<TextField.Input /> {/* or <TextField.TextArea /> */}
	<TextField.Description />
	<TextField.ErrorMessage />
</TextField>

Example (예제)

기본 예제를 통해 텍스트 필드의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.

Usage (사용법)

Default value (기본값)

defaultValue prop을 사용하면 초기의 비제어 값을 지정할 수 있어요.

<TextField defaultValue="Apple">
	<TextField.Label>Favorite fruit</TextField.Label>
	<TextField.Input />
</TextField>

Controlled value (제어 방식 값)

value prop을 사용하면 값을 제어할 수 있어요. 사용자가 input에 입력하면 onChange 이벤트가 발생하고 새 값이 전달되답니다.

import { createSignal } from "solid-js";

function ControlledExample() {
	const [value, setValue] = createSignal("Apple");

	return (
		<>
			<TextField value={value()} onChange={setValue}>
				<TextField.Label>Favorite fruit</TextField.Label>
				<TextField.Input />
			</TextField>
			<p>Your favorite fruit is: {value()}.</p>
		</>
	);
}

Multiline (여러 줄 입력)

TextField.Input 대신 TextField.TextArea 컴포넌트를 사용하면 여러 줄 텍스트 필드를 만들 수 있어요.

<TextField>
	<TextField.Label>Favorite fruit</TextField.Label>
	<TextField.TextArea />
</TextField>

추가로, autoResize prop을 사용하면 텍스트 영역 높이를 내용에 맞게 조절할 수 있어요. 아래 텍스트 필드에 직접 입력해 보면 동작을 확인할 수 있답니다.

<TextField>
	<TextField.Label>Favorite fruit</TextField.Label>
	<TextField.TextArea autoResize />
</TextField>

Description (설명)

TextField.Description 컴포넌트를 사용하면 텍스트 필드에 추가적인 도움말 텍스트를 연결할 수 있어요.

<TextField>
	<TextField.Label>Favorite fruit</TextField.Label>
	<TextField.Input />
	<TextField.Description>Choose the fruit you like the most.</TextField.Description>
</TextField>

Error message (오류 메시지)

TextField.ErrorMessage 컴포넌트를 사용하면 검증 오류를 고칠 수 있도록 도와줄 수 있어요. 보조 기술을 위해 텍스트 필드를 의미적으로(invalid) 표시하려면 validationState prop과 함께 사용해야 해요.

기본적으로 validationState prop이 invalid로 설정된 경우에만 렌더링돼요. 오류 메시지를 항상 렌더링하려면 forceMount prop을 사용하세요 (예: 애니메이션 라이브러리와 함께 사용할 때).

import { createSignal } from "solid-js";

function ErrorMessageExample() {
	const [value, setValue] = createSignal("Orange");

	return (
		<TextField
			value={value()}
			onChange={setValue}
			validationState={value() !== "Apple" ? "invalid" : "valid"}
		>
			<TextField.Label>Favorite fruit</TextField.Label>
			<TextField.Input />
			<TextField.ErrorMessage>Hmm, I prefer apples.</TextField.ErrorMessage>
		</TextField>
	);
}

HTML forms

텍스트 필드의 name prop을 사용하면 HTML 폼과 통합할 수 있어요.

function HTMLFormExample() {
	const onSubmit = (e: SubmitEvent) => {
		// handle form submission.
	};

	return (
		<form onSubmit={onSubmit}>
			<TextField name="favorite-fruit">
				<TextField.Label>Favorite fruit</TextField.Label>
				<TextField.Input />
			</TextField>
			<div>
				<button type="reset">Reset</button>
				<button type="submit">Submit</button>
			</div>
		</form>
	);
}

API Reference (API 참조)

TextField

TextField은 @kobalte/core/text-field의 Root 임포트(그리고 더 이상 사용되지 않는 TextField.Root)와 동일해요.

Prop Description
value string
The controlled value of the text field to check.
defaultValue string
The default value when initially rendered. Useful when you do not need to control the value.
onChange (value: string) => void
Event handler called when the value of the textfield changes.
name string
The name of the text field, used when submitting an HTML form. See MDN.
validationState 'valid' | 'invalid'
Whether the text field should display its "valid" or "invalid" visual styling.
required boolean
Whether the user must fill the text field before the owning form can be submitted.
disabled boolean
Whether the text field is disabled.
readOnly boolean
Whether the text field items can be selected but not changed by the user.
Data attribute Description
data-valid Present when the text field is valid according to the validation rules.
data-invalid Present when the text field is invalid according to the validation rules.
data-required Present when the user must fill the text field before the owning form can be submitted.
data-disabled Present when the text field is disabled.
data-readonly Present when the text field is read only.

TextField.Label, TextField.Input, TextField.TextArea, TextField.Description 및 TextField.ErrorMessage는 동일한 data-attribute를 공유해요.

TextField.TextArea

Prop Description
autoResize boolean
Whether the textarea should adjust its height when the value changes.
submitOnEnter boolean
Whether the form should be submitted when the user presses the enter key.

TextField.ErrorMessage

Prop Description
forceMount boolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Rendered elements (렌더링되는 요소)

Component Default rendered element
TextField div
TextField.Label label
TextField.Input input
TextField.TextArea textarea
TextField.Description div
TextField.ErrorMessage div

더 알아보기 (Learn more)