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

Time Field (시간 필드)

원문 보기 위키 갱신

시간 필드는 사용자가 키보드로 시간 값을 입력하고 편집할 수 있게 해주는 컴포넌트예요. 시간 값의 각 부분은 개별적으로 편집 가능한 세그먼트(segment)로 표시됩니다.

출처: 문서

본문

Import (불러오기)

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

Features (기능)

  • 시간에는 선택적으로 시간대(time zone)를 포함할 수 있어요. 모든 수정은 일광 절약 시간 같은 시간대 규칙을 따릅니다.
  • 로케일별 형식, 숫자 체계, 시간 주기(hour cycle), 오른쪽에서 왼쪽(RTL) 레이아웃을 지원해요.
  • 각 시간 단위는 개별적으로 포커스·편집 가능한 세그먼트로 표시되어, 어떤 형식과 로케일에서도 키보드로 쉽게 시간을 편집할 수 있어요.
  • 시간 세그먼트는 사용하기 쉬운 숫자 키패드로 편집할 수 있고, 모든 상호작용은 터치 기반 스크린 리더로 접근 가능해요.
  • 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.
  • HTML 폼과 통합할 수 있어요.

Anatomy (구조)

시간 필드는 다음의 부분들로 이루어져요:

  • TimeField: 시간 필드의 루트 컨테이너예요.
  • TimeField.Label: 시간 필드에 대한 정보를 사용자에게 알려주는 라벨이에요.
  • TimeField.Input: 세그먼트들을 담는 컨테이너예요.
  • TimeField.Segment: 시간의 한 단위를 나타내는 컴포넌트예요.
  • TimeField.Description: 시간 필드에 대한 더 많은 정보를 사용자에게 알려주는 설명이에요.
  • TimeField.ErrorMessage: 시간 필드의 검증 오류를 어떻게 고치는지에 대한 정보를 사용자에게 알려주는 오류 메시지예요.
  • TimeField.HiddenInput: 시간 필드에서 시각적으로 숨겨진 네이티브 html input이에요.
<TimeField>
	<TimeField.Label />
	<TimeField.Input>
		<TimeField.Segment />
	</TimeField.Input>
	<TimeField.Description />
	<TimeField.ErrorMessage />
	<TimeField.HiddenInput />
</TimeField>

Example (예제)

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

Usage (사용법)

Default Value (기본값)

TimeField는 기본적으로 자리 표시자(placeholder)를 표시해요. defaultValue prop을 사용하면 초기의 비제어 값을 지정할 수 있어요.

시간 값은 객체 형태입니다: { hour?: number; minute?: number; second?: number; }.

<TimeField defaultValue={{ hour: 11, minute: 45 }}>
	<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
</TimeField>

Controlled Value (제어 방식 값)

value prop을 사용하면 값을 제어할 수 있어요. 시간 값이 변경되면 onChange 이벤트가 발생해요.

import { createSignal, Show } from "solid-js";

function ControlledValueExample() {
	const [value, setValue] = createSignal({ hour: 9, minute: 45 });

	return (
		<>
			<TimeField value={value()} onChange={setValue}>
				<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
			</TimeField>
			<p>
				Selected time:{" "}
				<Show when={value()} fallback={"--"}>
					{value()}
				</Show>
			</p>
		</>
	);
}

Granularity (단위)

granularity prop을 사용하면 TimeField가 표시하는 가장 작은 단위를 제어할 수 있어요. 기본적으로 시간은 "minute" 단위로 표시돼요. 더 세밀한 시간 값을 표시하려면 granularity prop을 "second"로 설정하거나, 시간 슬롯을 키로 갖고 boolean 값을 갖는 객체로 설정하면 돼요.

<TimeField granularity="second">
	<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
</TimeField>

Minimum and Maximum Values (최소·최대 값)

min과 max prop을 사용하면 내장 검증을 수행할 수 있어요. 사용자가 유효하지 않은 시간을 입력하면 ARIA를 사용해 시간 필드를 유효하지 않은(invalid) 상태로 표시해요.

<TimeField
	defaultValue={{ hour: 9, minute: 45 }}
	min={{ hour: 9 }}
	max={{ hour: 17 }}
	hourCycle={12}
>
	<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
	<TimeField.ErrorMessage>Select time between 9 AM and 5 PM.</TimeField.ErrorMessage>
</TimeField>

Placeholder Value (자리 표시자 값)

대시(dash) 대신 자리 표시자 시간을 표시할 수 있으며, 이는 시작 시간에도 영향을 줍니다.

<TimeField placeholder={{ hour: 9 }}>
	<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
</TimeField>

Hour Cycle (시간 주기)

기본적으로 TimeField는 사용자의 로케일에 따라 12시간 또는 24시간 형식으로 시간을 표시해요. 이 값은 hourCycle prop으로 재정의할 수 있어요.

<TimeField hourCycle={12}>
	<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
</TimeField>

Description (설명)

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

<TimeField>
	<TimeField.Label>Time</TimeField.Label>
	<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
	<TimeField.Description>Select a meeting time.</TimeField.Description>
</TimeField>

Error message (오류 메시지)

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

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

import { createSignal } from "solid-js";

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

	return (
		<TimeField
			value={value()}
			onChange={setValue}
			validationState={value() === undefined ? "invalid" : "valid"}
		>
			<TimeField.Label>Time</TimeField.Label>
			<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
			<TimeField.ErrorMessage>Please select a time.</TimeField.ErrorMessage>
		</TimeField>
	);
}

HTML forms

name prop을 사용하면 HTML 폼과 통합할 수 있어요.

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

	return (
		<form onSubmit={onSubmit}>
			<TimeField name="time">
				<TimeField.Input>{segment => <TimeField.Segment segment={segment()} />}</TimeField.Input>
				<TimeField.HiddenInput />
			</TimeField>
			<div>
				<button type="reset">Reset</button>
				<button type="submit">Submit</button>
			</div>
		</form>
	);
}

API Reference (API 참조)

TimeField

TimeField은 @kobalte/core/time-field의 Root 임포트와 동일해요.

Prop Description
value Time ({ hour?: number; minute?: number; second?: number; })
The current value (controlled).
defaultValue Time
The default value (uncontrolled).
onChange (value: Time) => void
Handler that is called when the value changes.
hourCycle 12 | 24
Whether to display the time in 12 or 24-hour format. By default, this is determined by the user's locale.
granularity 'hour' | 'minute' | 'second' | { hour: boolean; minute: boolean; second: boolean; }
Determines the smallest unit that is displayed in the time field. Defaults to "minute".
forceLeadingZeros boolean
Whether to always show leading zeros in the hour field.
placeholder Time
A placeholder time shown when no value is selected, and acts as the starting point for interactions.
min Time
The minimum allowed time that a user may select.
max Time
The maximum allowed time that a user may select.
name string
The name of the time field. Submitted with its owning form as part of a name/value pair.
validationState 'valid' | 'invalid'
Whether the time field should display its "valid" or "invalid" visual styling.
required boolean
Whether the time field is required.
disabled boolean
Whether the time field is disabled.
readOnly boolean
Whether the time field is read only.
translations TimeFieldIntlTranslations
The localized strings of the component.
Data attribute Description
data-valid Present when the time field is valid according to the validation rules.
data-invalid Present when the time field is invalid according to the validation rules.
data-required Present when the time field is required.
data-disabled Present when the time field is disabled.
data-readonly Present when the time field is read only.

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

TimeField.Segment

Prop Description
segment TimeSegment
A segment of the time field.
Data attribute Description
data-separator Present when the segment is a separator.
data-type Always present.
data-placeholder Present when the segment's value is a placeholder.

TimeField.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
TimeField div
TimeField.Label span
TimeField.Input div
TimeField.Segment div
TimeField.Description div
TimeField.ErrorMessage div
TimeField.HiddenInput input

Accessibility (접근성)

Keyboard Interactions (키보드 상호작용)

Key Description
Backspace Deletes the value in the current segment and moves focus to the previous segment when empty.
Delete Deletes the value in the current segment and moves focus to the previous segment when empty.
ArrowRight Moves focus to the next segment.
ArrowLeft Moves focus to the previous segment.
ArrowUp Increments the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
ArrowDown Decrements the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
PageUp Increments the given segment by a larger amount, rounding it to the nearest increment. The amount to increment by depends on the segment, for example 2 hours, 15 minutes, and 15 seconds. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
PageDown Decrements the given segment by a larger amount, rounding it to the nearest decrement. The amount to decrement by depends on the segment, for example 2 hours, 15 minutes, and 15 seconds. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
Home Decrements the given segment by the segment's minimum value.
End Increments the given segment by the segment's maximum value.

더 알아보기 (Learn more)