Time Range Field (시간 범위 필드)
TimeRangeField는 두 개의 Time Field를 결합해 시간 범위를 입력받는 컴포넌트예요. 시작 시각과 종료 시각을 함께 관리해야 하는 예약·일정 같은 화면에서 유용합니다. 커스터마이즈 방법은 Time Field 문서를 함께 참고하면 됩니다.
출처: 문서
본문
<script lang="ts">
import { TimeRangeField } from "bits-ui";
</script>
<TimeRangeField.Root class="group flex w-full max-w-[320px] flex-col gap-1.5">
<TimeRangeField.Label class="block select-none text-sm font-medium">
Hotel dates
</TimeRangeField.Label>
<div
class="h-input rounded-input border-border-input bg-background text-foreground focus-within:border-border-input-hover focus-within:shadow-date-field-focus hover:border-border-input-hover group-data-invalid:border-destructive flex w-full select-none items-center border px-2 py-3 text-sm tracking-[0.01em]"
>
{#each ["start", "end"] as const as type (type)}
<TimeRangeField.Input {type}>
{#snippet children({ segments })}
{#each segments as { part, value }, i (part + i)}
<div class="inline-block select-none">
{#if part === "literal"}
<TimeRangeField.Segment
{part}
class="text-muted-foreground p-1"
>
{value}
</TimeRangeField.Segment>
{:else}
<TimeRangeField.Segment
{part}
class="rounded-5px hover:bg-muted focus:bg-muted focus:text-foreground aria-[valuetext=Empty]:text-muted-foreground focus-visible:ring-0! focus-visible:ring-offset-0! px-1 py-1"
>
{value}
</TimeRangeField.Segment>
{/if}
</div>
{/each}
{/snippet}
</TimeRangeField.Input>
{#if type === "start"}
<div aria-hidden="true" class="text-muted-foreground pl-1 pr-2">to</div>
{/if}
{/each}
</div>
</TimeRangeField.Root>
참고!
이 컴포넌트를 자세히 살펴보기 전에, Bits UI에서 날짜/시간이 어떻게 동작하는지 이해하는 게 중요해요. 더 알고 싶다면 Dates/Times 문서를 먼저 읽어 주세요!
개요
TimeRangeField 컴포넌트는 두 개의 Time Field 컴포넌트를 결합해 시간 범위 필드를 만들어요. 이 컴포넌트를 커스터마이즈하는 방법은 Time Field 컴포넌트 문서를 참고하세요.
구조
<script lang="ts">
import { TimeRangeField } from "bits-ui";
</script>
<TimeRangeField.Root>
<TimeRangeField.Label>Working Hours</TimeRangeField.Label>
{#each ["start", "end"] as const as type}
<TimeRangeField.Input {type}>
{#snippet children({ segments })}
{#each segments as { part, value }}
<TimeRangeField.Segment {part}>
{value}
</TimeRangeField.Segment>
{/each}
{/snippet}
</TimeRangeField.Input>
{/each}
</TimeRangeField.Root>
placeholder 상태 다루기
이번 절에서는 컴포넌트의 placeholder 상태를 어떻게 관리하는지 다뤄요.
양방향 바인딩 (Two-Way Binding)
간단하고 자동적인 상태 동기화를 위해 bind:placeholder를 사용해요:
<script lang="ts">
import { TimeRangeField } from "bits-ui";
import { Time } from "@internationalized/date";
let myPlaceholder = $state(new Time(12, 30));
</script>
<TimeRangeField.Root bind:placeholder={myPlaceholder}>
<!-- ... -->
</TimeRangeField.Root>
완전 제어 (Fully Controlled)
상태의 읽기와 쓰기를 완전히 통제하고 싶다면 Function Binding을 사용해요.
<script lang="ts">
import { TimeRangeField, type TimeValue } from "bits-ui";
import { Time } from "@internationalized/date";
let myPlaceholder = $state(new Time(12, 30));
function getPlaceholder() {
return myPlaceholder;
}
function setPlaceholder(newPlaceholder: TimeValue) {
myPlaceholder = newPlaceholder;
}
</script>
<TimeRangeField.Root bind:placeholder={getPlaceholder, setPlaceholder}>
<!-- ... -->
</TimeRangeField.Root>
value 상태 다루기
이번 절에서는 컴포넌트의 value 상태를 어떻게 관리하는지 다뤄요.
양방향 바인딩 (Two-Way Binding)
간단하고 자동적인 상태 동기화를 위해 bind:value를 사용해요:
<script lang="ts">
import { TimeRangeField, type TimeRange } from "bits-ui";
import { Time } from "@internationalized/date";
let myValue = $state<TimeRange>({
start: new Time(12, 30),
end: new Time(12, 30),
});
</script>
<button
onclick={() => {
myValue = {
start: myValue.start.add({ hours: 1 }),
end: myValue.end.add({ hours: 1 }),
};
}}
>
Add 1 hour
</button>
<TimeRangeField.Root bind:value={myValue}>
<!-- ... -->
</TimeRangeField.Root>
완전 제어 (Fully Controlled)
상태의 읽기와 쓰기를 완전히 통제하고 싶다면 Function Binding을 사용해요.
<script lang="ts">
import { TimeRangeField, type TimeRange } from "bits-ui";
let myValue = $state<TimeRange | undefined>({
start: undefined,
end: undefined,
});
function getValue() {
return myValue;
}
function setValue(newValue: TimeRange | undefined) {
myValue = newValue;
}
</script>
<DateRangeField.Root bind:value={getValue, setValue}>
<!-- ... -->
</DateRangeField.Root>
API 참조
TimeRangeField.Root
루트 시간 필드 컴포넌트예요.
| Property | Type | Description | Details |
|---|---|---|---|
value $bindable |
TimeRange - { start: TimeValue | undefined; end: TimeValue | undefined } |
The selected time range.Default: —— undefined |
|
onValueChange |
function - (value: TimeRange) => void |
A function that is called when the selected time range changes.Default: —— undefined |
|
placeholder $bindable |
TimeValue - import type { Time, CalendarDateTime, ZonedDateTime } from "@internationalized/date"; type TimeValue = Time | CalendarDateTime | ZonedDateTime |
The placeholder time, which is used to determine what time to start the segments from when no value exists.Default: —— undefined |
|
onPlaceholderChange |
function - (value: TimeValue) => void |
A function that is called when the placeholder time changes.Default: —— undefined |
|
errorMessageId |
string |
The id of the element which contains the error messages for the field when the time is invalid.Default: —— undefined |
|
validate |
function - (time: TimeValue) => string[] | string | void |
A function that returns whether or not a time is unavailable.Default: —— undefined |
|
onInvalid |
function - (reason: 'min' | 'max' | 'custom', msg?: string | string[]) => void |
A callback fired when the field's value is invalid.Default: —— undefined |
|
minValue |
TimeValue - import type { Time, CalendarDateTime, ZonedDateTime } from "@internationalized/date"; type TimeValue = Time | CalendarDateTime | ZonedDateTime |
The minimum valid time that can be entered.Default: —— undefined |
|
maxValue |
TimeValue - import type { Time, CalendarDateTime, ZonedDateTime } from "@internationalized/date"; type TimeValue = Time | CalendarDateTime | ZonedDateTime |
The maximum valid time that can be entered.Default: —— undefined |
|
granularity |
enum - 'hour' | 'minute' | 'second' |
The granularity to use for formatting the field. The field will render segments for each part of the time up to and including the specified granularity.Default: 'minute' |
|
hideTimeZone |
boolean |
Whether or not to hide the time zone segment of the field. This only applies when using a ZonedDateTime as the value prop.Default: false |
|
hourCycle |
enum - '12' | '24' |
The hour cycle to use for formatting times. Defaults to the locale preferenceDefault: —— undefined |
|
locale |
string |
The locale to use for formatting times.Default: en-US |
|
disabled |
boolean |
Whether or not the field is disabled.Default: false |
|
readonly |
boolean |
Whether or not the field is readonly.Default: false |
|
readonlySegments |
EditableTimeSegmentPart[] - "hour" | "minute" | "second" | "dayPeriod" |
An array of segments that should be readonly, which prevent user input on them.Default: —— undefined |
|
required |
boolean |
Whether or not the field is required.Default: false |
|
onStartValueChange |
function - (value: TimeValue | undefined) => void |
A function that is called when the start time changes.Default: —— undefined |
|
onEndValueChange |
function - (value: TimeValue | undefined) => void |
A function that is called when the end time changes.Default: —— undefined |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-time-range-field-root |
'' |
Present on the root element. |
TimeRangeField.Input
시간 필드의 세그먼트들을 감싸는 컨테이너예요.
| Property | Type | Description | Details |
|---|---|---|---|
type required |
enum - 'start' | 'end' |
The type of field to render (start or end).Default: —— undefined |
|
name |
string |
The name of the time field used for form submission. If provided, a hidden input element will be rendered alongside the time field.Default: —— undefined |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet - import type { TimeSegmentPart } from "bits-ui" type TimeFieldInputChildrenSnippetProps = { segments: Array<{ part: TimeSegmentPart; value: string }> } |
The children content to render.Default: —— undefined |
|
child |
Snippet - import type { TimeSegmentPart } from "bits-ui" type ChildSnippetProps = { props: Record<string, unknown> segments: Array<{ part: TimeSegmentPart; value: string }> } |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-invalid |
'' |
Present on the element when the field is invalid. | |
data-disabled |
'' |
Present on the element when the field is disabled. | |
data-time-field-input |
'' |
Present on the element. |
TimeRangeField.Segment
시간 필드의 세그먼트예요.
| Property | Type | Description | Details |
|---|---|---|---|
part required |
TimeSegmentPart - "hour" | "minute" | "second" | "dayPeriod" | "timeZoneName" | "literal" |
The part of the time to render.Default: —— undefined |
|
ref $bindable |
HTMLSpanElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-invalid |
'' |
Present on the element when the field is invalid | |
data-disabled |
'' |
Present on the element when the field is disabled | |
data-segment |
enum - 'hour' | 'minute' | 'second' | 'dayPeriod' | 'timeZoneName' | 'literal' |
The type of segment the element represents. | |
data-time-field-segment |
'' |
Present on the element. |
TimeRangeField.Label
시간 필드의 라벨이에요.
| Property | Type | Description | Details |
|---|---|---|---|
ref $bindable |
HTMLSpanElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-invalid |
'' |
Present on the element when the field is invalid | |
data-time-field-label |
'' |
Present on the element. |
더 알아보기 (Learn more)
TimeRangeField는 시작·종료 시간을 함께 다루므로, 시간 범위가 필요한 예약·일정 화면에 바로 적용하기 좋아요. 커스터마이즈는 결국 TimeField의 옵션을 따르니, Time Field 문서를 기본으로 참고하면 됩니다.