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

Color Field (색상 필드)

원문 보기 위키 갱신

사용자가 16진수(hex) 색상 값을 입력하고 조정할 수 있게 해주는 컴포넌트예요. 입력 시 유효한 hex 문자만 받아들이도록 키보드 입력을 검증해요.

출처: 문서

본문

임포트 (Import)

import { ColorField } from "@kobalte/core/color-field";
// or
import { Root, Label, ... } from "@kobalte/core/color-field";

기능 (Features)

  • hex 색상 값의 파싱과 포맷을 지원해요.
  • 사용자가 입력하는 동안 키보드 입력을 검증해서 유효한 hex 문자만 받아들여요.
  • 시각적 및 ARIA 라벨링을 지원해요.
  • 필수(required)·유효하지 않은(invalid) 상태를 ARIA를 통해 보조 기술에 노출해요.
  • 입력과 ARIA로 연결된 설명(description)·오류 메시지(error message) 도움말 텍스트를 지원해요.
  • 폼 리셋 이벤트와 동기화해요.
  • 제어(controlled) 또는 비제어(uncontrolled) 방식으로 사용할 수 있어요.

구조 (Anatomy)

색상 필드는 다음 요소로 구성돼요.

  • ColorField: 색상 필드의 루트 컨테이너예요.
  • ColorField.Label: 색상 필드에 대한 정보를 사용자에게 알려주는 라벨이에요.
  • ColorField.Input: 색상 필드의 네이티브 HTML 입력 요소예요.
  • ColorField.Description: 색상 필드에 대한 자세한 정보를 사용자에게 알려주는 설명이에요.
  • ColorField.ErrorMessage: 색상 필드의 유효성 검사 오류를 해결하는 방법을 사용자에게 알려주는 오류 메시지예요.
<ColorField>
	<ColorField.Label />
	<ColorField.Input />
	<ColorField.Description />
	<ColorField.ErrorMessage />
</ColorField>

예제 (Example)

기본 예제는 공식 문서 페이지에서 ColorField, Label, Input 조합과 스타일시트로 확인할 수 있어요.

사용법 (Usage)

기본값 (Default value)

비제어 상태의 초기값은 defaultValue prop으로 제공할 수 있어요.

<ColorField class="color-field" defaultValue="#7f007f">
	<ColorField.Label class="color-field__label">Favorite hex color</ColorField.Label>
	<ColorField.Input class="color-field__input" />
</ColorField>

제어값 (Controlled value)

value prop을 사용해 값을 제어할 수 있어요. 사용자가 입력에 타이핑하면 onChange 이벤트가 발생하며 새 값을 받아요.

import { createSignal } from "solid-js";

function ControlledExample() {
	const [value, setValue] = createSignal("#7f007f");

	return (
		<>
			<ColorField class="color-field" value={value()} onChange={setValue}>
				<ColorField.Label class="color-field__label">Favorite hex color</ColorField.Label>
				<ColorField.Input class="color-field__input" />
			</ColorField>
			<p>Your favorite color is: {value()}</p>
		</>
	);
}

설명 (Description)

ColorField.Description 컴포넌트를 사용해 색상 필드에 추가 도움말 텍스트를 연결할 수 있어요.

<ColorField class="color-field">
	<ColorField.Label class="color-field__label">Favorite hex color</ColorField.Label>
	<ColorField.Input class="color-field__input" />
	<ColorField.Description class="color-field__description">
		Choose the color you like the most.
	</ColorField.Description>
</ColorField>

오류 메시지 (Error message)

ColorField.ErrorMessage 컴포넌트를 사용해 사용자가 유효성 검사 오류를 해결하도록 도울 수 있어요. 보조 기술에 색상 필드를 의미상 유효하지 않은 상태로 표시하려면 validationState prop과 함께 사용해야 해요.

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

import { createSignal } from "solid-js";

function ErrorMessageExample() {
	const [value, setValue] = createSignal("#7f007f");

	return (
		<ColorField
			value={value()}
			onChange={setValue}
			validationState={value() !== "#000000" ? "invalid" : "valid"}
		>
			<ColorField.Label>Favorite hex color</ColorField.Label>
			<ColorField.Input />
			<ColorField.ErrorMessage>Hmm, I prefer black.</ColorField.ErrorMessage>
		</ColorField>
	);
}

HTML 폼 (HTML forms)

색상 필드의 name prop을 사용해 HTML 폼과 통합할 수 있어요.

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

	return (
		<form onSubmit={onSubmit}>
			<ColorField name="favorite-color">
				<ColorField.Label>Favorite hex color</ColorField.Label>
				<ColorField.Input />
			</ColorField>
		</form>
	);
}

API 참조 (API Reference)

ColorField

ColorField는 @kobalte/core/color-field의 Root 임포트와 동일해요.

Prop Description
value string
The controlled value of the color 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 color field changes.
name string
The name of the color field, used when submitting an HTML form. See MDN.
validationState 'valid' | 'invalid'
Whether the color field should display its "valid" or "invalid" visual styling.
required boolean
Whether the user must fill the color field before the owning form can be submitted.
disabled boolean
Whether the color field is disabled.
readOnly boolean
Whether the color field items can be selected but not changed by the user.
Data attribute Description
data-valid Present when the color field is valid according to the validation rules.
data-invalid Present when the color field is invalid according to the validation rules.
data-required Present when the user must fill the color field before the owning form can be submitted.
data-disabled Present when the color field is disabled.
data-readonly Present when the color field is read only.

ColorField.Label, ColorField.Input, ColorField.Description 및 ColorField.ErrorMessage는 동일한 data-attributes를 공유해요.

ColorField.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
ColorField div
ColorField.Label label
ColorField.Input input
ColorField.Description div
ColorField.ErrorMessage div

더 알아보기 (Learn more)