InputDate
날짜를 선택할 수 있는 입력 컴포넌트예요. 범위 선택, 색상·변형·크기 변경, 아이콘·아바타 표시, 사용 불가 날짜나 최소·최대 날짜 제한까지 다양하게 지원해요.
출처: Nuxt UI
본문
날짜 선택을 위한 입력 컴포넌트예요.
사용법
v-model 디렉티브로 선택된 날짜를 제어할 수 있어요.
<script setup lang="ts">
import { CalendarDate } from '@internationalized/date'
const value = shallowRef(new CalendarDate(2022, 2, 3))
</script>
<template>
<UInputDate v-model="value" />
</template>
상태를 제어할 필요가 없을 때는 default-value prop으로 초기 값을 설정할 수 있어요.
<script setup lang="ts">
import { CalendarDate } from '@internationalized/date'
const defaultValue = shallowRef(new CalendarDate(2022, 2, 6))
</script>
<template>
<UInputDate :default-value="defaultValue" />
</template>
Nuxt:
[!NOTE] See: /docs/getting-started/integrations/i18n/nuxt#locale
This component uses the
@internationalized/datepackage for locale-aware formatting. The date format is determined by thelocaleprop of the App component.
이 컴포넌트는 로케일 인식 포맷팅을 위해 @internationalized/date 패키지를 사용해요. 날짜 형식은 App 컴포넌트의 locale prop에 따라 결정돼요.
Vue:
[!NOTE] See: /docs/getting-started/integrations/i18n/vue#locale
This component uses the
@internationalized/datepackage for locale-aware formatting. The date format is determined by thelocaleprop of the App component.
이 컴포넌트는 로케일 인식 포맷팅을 위해 @internationalized/date 패키지를 사용해요. 날짜 형식은 App 컴포넌트의 locale prop에 따라 결정돼요.
범위 (Range)
range prop으로 날짜 범위를 선택할 수 있어요.
<script setup lang="ts">
import { CalendarDate } from '@internationalized/date'
const value = shallowRef({ start: new CalendarDate(2022, 2, 3), end: new CalendarDate(2022, 2, 20) })
</script>
<template>
<UInputDate range v-model="value" />
</template>
색상 (Color)
color prop으로 InputDate의 색상을 바꿀 수 있어요.
<template>
<UInputDate color="neutral" highlight />
</template>
변형 (Variant)
variant prop으로 InputDate의 변형을 바꿀 수 있어요.
<template>
<UInputDate variant="subtle" />
</template>
크기 (Size)
size prop으로 InputDate의 크기를 바꿀 수 있어요.
<template>
<UInputDate size="xl" />
</template>
아이콘 (Icon)
icon prop으로 InputDate 안에 Icon을 표시할 수 있어요.
<template>
<UInputDate icon="i-lucide-calendar" />
</template>
[!NOTE]
Use the
leadingandtrailingprops to set the icon position or theleading-iconandtrailing-iconprops to set a different icon for each position.
leading과 trailing prop으로 아이콘 위치를 정하거나 leading-icon, trailing-icon prop으로 각 위치에 다른 아이콘을 지정할 수 있어요.
구분자 아이콘 (Separator Icon)
separator-icon prop으로 범위 구분자의 Icon을 바꿀 수 있어요. 기본값은 i-lucide-minus예요.
<template>
<UInputDate range separator-icon="i-lucide-arrow-right" />
</template>
Nuxt:
[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme
You can customize this icon globally in your
app.config.tsunderui.icons.minuskey.
app.config.ts의 ui.icons.minus 키에서 이 아이콘을 전역으로 커스터마이징할 수 있어요.
Vue:
[!TIP] See: /docs/getting-started/integrations/icons/vue#theme
You can customize this icon globally in your
vite.config.tsunderui.icons.minuskey.
vite.config.ts의 ui.icons.minus 키에서 이 아이콘을 전역으로 커스터마이징할 수 있어요.
아바타 (Avatar)
avatar prop으로 InputDate 안에 Avatar를 표시할 수 있어요.
<template>
<UInputDate :avatar="{
src: 'https://github.com/vuejs.png',
loading: 'lazy'
}" size="md" variant="outline" />
</template>
비활성화 (Disabled)
disabled prop으로 InputDate를 비활성화할 수 있어요.
<template>
<UInputDate disabled />
</template>
예시 (Examples)
사용 불가 날짜로 (With unavailable dates)
is-date-unavailable prop에 함수를 전달해서 특정 날짜를 사용 불가로 표시할 수 있어요.
<script setup lang="ts">
import type { DateValue } from '@internationalized/date'
import { CalendarDate } from '@internationalized/date'
const modelValue = shallowRef({
start: new CalendarDate(2022, 1, 1),
end: new CalendarDate(2022, 1, 9)
})
const isDateUnavailable = (date: DateValue) => {
return date.day >= 10 && date.day <= 16
}
</script>
<template>
<UInputDate v-model="modelValue" :is-date-unavailable="isDateUnavailable" range />
</template>
최소·최대 날짜로 (With min/max dates)
min-value와 max-value prop으로 날짜 범위를 제한할 수 있어요.
<script setup lang="ts">
import { CalendarDate } from '@internationalized/date'
const modelValue = shallowRef(new CalendarDate(2023, 9, 10))
const minDate = new CalendarDate(2023, 9, 1)
const maxDate = new CalendarDate(2023, 9, 30)
</script>
<template>
<UInputDate v-model="modelValue" :min-value="minDate" :max-value="maxDate" />
</template>
날짜 피커로 (As a date picker)
Calendar와 Popover 컴포넌트를 사용해서 날짜 피커를 만들 수 있어요.
<script setup lang="ts">
import { CalendarDate } from '@internationalized/date'
const inputDate = useTemplateRef('inputDate')
const modelValue = shallowRef(new CalendarDate(2022, 1, 10))
</script>
<template>
<UInputDate ref="inputDate" v-model="modelValue">
<template #trailing>
<UPopover :reference="inputDate?.inputsRef[3]?.$el">
<UButton
color="neutral"
variant="link"
size="sm"
icon="i-lucide-calendar"
aria-label="Select a date"
class="px-0"
/>
<template #content>
<UCalendar v-model="modelValue" class="p-2" />
</template>
</UPopover>
</template>
</UInputDate>
</template>
날짜 범위 피커로 (As a date range picker)
Calendar와 Popover 컴포넌트를 사용해서 날짜 범위 피커를 만들 수 있어요.
<script setup lang="ts">
import { CalendarDate } from '@internationalized/date'
const inputDate = useTemplateRef('inputDate')
const modelValue = shallowRef({
start: new CalendarDate(2022, 1, 10),
end: new CalendarDate(2022, 1, 20)
})
</script>
<template>
<UInputDate ref="inputDate" v-model="modelValue" range>
<template #trailing>
<UPopover :reference="inputDate?.inputsRef[0]?.$el">
<UButton
color="neutral"
variant="link"
size="sm"
icon="i-lucide-calendar"
aria-label="Select a date range"
class="px-0"
/>
<template #content>
<UCalendar v-model="modelValue" class="p-2" :number-of-months="2" range />
</template>
</UPopover>
</template>
</UInputDate>
</template>
API
Props
/**
* Props for the InputDate component
*/
interface InputDateProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
/**
* @default 'primary'
*/
color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
/**
* @default 'outline'
*/
variant?: "outline" | "soft" | "subtle" | "ghost" | "none" | undefined;
/**
* @default 'md'
*/
size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
/**
* Highlight the ring color like a focus state.
*/
highlight?: boolean | undefined;
/**
* Keep the mobile text size on all breakpoints.
*/
fixed?: boolean | undefined;
autofocus?: boolean | undefined;
/**
* @default 0
*/
autofocusDelay?: number | undefined;
/**
* The icon to use as a range separator.
* @default appConfig.ui.icons.minus
*/
separatorIcon?: any;
/**
* Whether or not a range of dates can be selected
*/
range?: R | undefined;
defaultValue?: CalendarDate | CalendarDateTime | ZonedDateTime | DateRange;
modelValue?: null | CalendarDate | CalendarDateTime | ZonedDateTime | DateRange;
ui?: { base?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; leadingAvatar?: SlotClass; leadingAvatarSize?: SlotClass; trailing?: SlotClass; trailingIcon?: SlotClass; segment?: SlotClass; separatorIcon?: SlotClass; } | undefined;
/**
* Display an icon based on the `leading` and `trailing` props.
*/
icon?: any;
/**
* Display an avatar on the left side.
*/
avatar?: AvatarProps | undefined;
/**
* When `true`, the icon will be displayed on the left side.
*/
leading?: boolean | undefined;
/**
* Display an icon on the left side.
*/
leadingIcon?: any;
/**
* When `true`, the icon will be displayed on the right side.
*/
trailing?: boolean | undefined;
/**
* Display an icon on the right side.
*/
trailingIcon?: any;
/**
* When `true`, the loading icon will be displayed.
*/
loading?: boolean | undefined;
/**
* The icon when the `loading` prop is `true`.
* @default appConfig.ui.icons.loading
*/
loadingIcon?: any;
defaultPlaceholder?: CalendarDate | CalendarDateTime | ZonedDateTime;
placeholder?: CalendarDate | CalendarDateTime | ZonedDateTime;
/**
* The hour cycle used for formatting times. Defaults to the local preference
*/
hourCycle?: 12 | 24 | undefined;
/**
* The stepping interval for the time fields. Defaults to `1`.
*/
step?: DateStep | undefined;
/**
* Whether to enforce snapping the time value to the nearest step increment after input. Defaults to `false`.
*/
stepSnapping?: boolean | undefined;
/**
* The granularity to use for formatting times. Defaults to day if a CalendarDate is provided, otherwise defaults to minute. The field will render segments for each part of the date up to and including the specified granularity
*/
granularity?: "day" | "hour" | "minute" | "second" | undefined;
/**
* Whether or not to hide the time zone segment of the field
*/
hideTimeZone?: boolean | undefined;
maxValue?: CalendarDate | CalendarDateTime | ZonedDateTime;
minValue?: CalendarDate | CalendarDateTime | ZonedDateTime;
/**
* The locale to use for formatting dates
*/
locale?: string | undefined;
/**
* Whether or not the date field is disabled
*/
disabled?: boolean | undefined;
/**
* Whether or not the date field is readonly
*/
readonly?: boolean | undefined;
/**
* A function that returns whether or not a date is unavailable
*/
isDateUnavailable?: (date: DateValue): boolean | undefined;
/**
* Id of the element
*/
id?: string | undefined;
/**
* The name of the field. Submitted with its owning form as part of a name/value pair.
*/
name?: string | undefined;
/**
* When `true`, indicates that the user must set the value before the owning form can be submitted.
*/
required?: boolean | undefined;
}
Slots
/**
* Slots for the InputDate component
*/
interface InputDateSlots {
leading(): any;
default(): any;
trailing(): any;
separator(): any;
}
Emits
/**
* Emitted events for the InputDate component
*/
interface InputDateEmits {
update:modelValue: (payload: [value: InputDateModelValue<R>]) => void;
update:placeholder: (payload: [date: DateValue] & [date: DateValue]) => void;
change: (payload: [event: Event]) => void;
blur: (payload: [event: FocusEvent]) => void;
focus: (payload: [event: FocusEvent]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
inputDate: {
slots: {
base: [
'group relative inline-flex items-center rounded-md select-none',
'transition-colors'
],
leading: 'absolute inset-y-0 start-0 flex items-center',
leadingIcon: 'shrink-0 text-dimmed',
leadingAvatar: 'shrink-0',
leadingAvatarSize: '',
trailing: 'absolute inset-y-0 end-0 flex items-center',
trailingIcon: 'shrink-0 text-dimmed',
segment: [
'rounded-sm text-center outline-hidden data-placeholder:text-dimmed data-[segment=literal]:text-muted data-invalid:text-error data-disabled:cursor-not-allowed data-disabled:opacity-75',
'transition-colors'
],
separatorIcon: 'shrink-0 size-4 text-muted'
},
variants: {
fieldGroup: {
horizontal: 'not-only:first:rounded-e-none not-only:last:rounded-s-none not-last:not-first:rounded-none focus-visible:z-[1]',
vertical: 'not-only:first:rounded-b-none not-only:last:rounded-t-none not-last:not-first:rounded-none focus-visible:z-[1]'
},
size: {
xs: {
base: [
'px-2 py-1 text-sm/4 gap-1',
'gap-0.25'
],
leading: 'ps-2',
trailing: 'pe-2',
leadingIcon: 'size-4',
leadingAvatarSize: '3xs',
trailingIcon: 'size-4',
segment: 'data-[segment=day]:w-8 data-[segment=month]:w-8 data-[segment=year]:w-10'
},
sm: {
base: [
'px-2.5 py-1.5 text-sm/4 gap-1.5',
'gap-0.5'
],
leading: 'ps-2.5',
trailing: 'pe-2.5',
leadingIcon: 'size-4',
leadingAvatarSize: '3xs',
trailingIcon: 'size-4',
segment: 'data-[segment=day]:w-8 data-[segment=month]:w-8 data-[segment=year]:w-10'
},
md: {
base: [
'px-2.5 py-1.5 text-base/5 gap-1.5',
'gap-0.5'
],
leading: 'ps-2.5',
trailing: 'pe-2.5',
leadingIcon: 'size-5',
leadingAvatarSize: '2xs',
trailingIcon: 'size-5',
segment: 'data-[segment=day]:w-9 data-[segment=month]:w-9 data-[segment=year]:w-11'
},
lg: {
base: [
'px-3 py-2 text-base/5 gap-2',
'gap-0.75'
],
leading: 'ps-3',
trailing: 'pe-3',
leadingIcon: 'size-5',
leadingAvatarSize: '2xs',
trailingIcon: 'size-5',
segment: 'data-[segment=day]:w-9 data-[segment=month]:w-9 data-[segment=year]:w-11'
},
xl: {
base: [
'px-3 py-2 text-base gap-2',
'gap-0.75'
],
leading: 'ps-3',
trailing: 'pe-3',
leadingIcon: 'size-6',
leadingAvatarSize: 'xs',
trailingIcon: 'size-6',
segment: 'data-[segment=day]:w-10 data-[segment=month]:w-10 data-[segment=year]:w-12'
}
},
variant: {
outline: 'text-highlighted bg-default ring ring-inset ring-accented',
soft: 'text-highlighted bg-elevated/50 hover:bg-elevated has-focus:bg-elevated disabled:bg-elevated/50',
subtle: 'text-highlighted bg-elevated ring ring-inset ring-accented',
ghost: 'text-highlighted bg-transparent hover:bg-elevated has-focus:bg-elevated disabled:bg-transparent dark:disabled:bg-transparent',
none: 'text-highlighted bg-transparent has-focus:outline-none'
},
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
leading: {
true: ''
},
trailing: {
true: ''
},
loading: {
true: ''
},
highlight: {
true: ''
},
fixed: {
false: ''
},
type: {
file: 'file:me-1.5 file:font-medium file:text-muted file:outline-none'
}
},
compoundVariants: [
{
color: 'primary',
variant: [
'outline',
'subtle'
],
class: 'outline-primary/25 has-focus-visible:outline-3 has-focus-visible:ring-primary'
},
{
color: 'secondary',
variant: [
'outline',
'subtle'
],
class: 'outline-secondary/25 has-focus-visible:outline-3 has-focus-visible:ring-secondary'
},
{
color: 'success',
variant: [
'outline',
'subtle'
],
class: 'outline-success/25 has-focus-visible:outline-3 has-focus-visible:ring-success'
},
{
color: 'info',
variant: [
'outline',
'subtle'
],
class: 'outline-info/25 has-focus-visible:outline-3 has-focus-visible:ring-info'
},
{
color: 'warning',
variant: [
'outline',
'subtle'
],
class: 'outline-warning/25 has-focus-visible:outline-3 has-focus-visible:ring-warning'
},
{
color: 'error',
variant: [
'outline',
'subtle'
],
class: 'outline-error/25 has-focus-visible:outline-3 has-focus-visible:ring-error'
},
{
color: 'primary',
variant: [
'soft',
'ghost'
],
class: 'outline-primary/25 has-focus-visible:outline-3'
},
{
color: 'secondary',
variant: [
'soft',
'ghost'
],
class: 'outline-secondary/25 has-focus-visible:outline-3'
},
{
color: 'success',
variant: [
'soft',
'ghost'
],
class: 'outline-success/25 has-focus-visible:outline-3'
},
{
color: 'info',
variant: [
'soft',
'ghost'
],
class: 'outline-info/25 has-focus-visible:outline-3'
},
{
color: 'warning',
variant: [
'soft',
'ghost'
],
class: 'outline-warning/25 has-focus-visible:outline-3'
},
{
color: 'error',
variant: [
'soft',
'ghost'
],
class: 'outline-error/25 has-focus-visible:outline-3'
},
{
color: 'primary',
highlight: true,
class: 'ring ring-inset ring-primary'
},
{
color: 'secondary',
highlight: true,
class: 'ring ring-inset ring-secondary'
},
{
color: 'success',
highlight: true,
class: 'ring ring-inset ring-success'
},
{
color: 'info',
highlight: true,
class: 'ring ring-inset ring-info'
},
{
color: 'warning',
highlight: true,
class: 'ring ring-inset ring-warning'
},
{
color: 'error',
highlight: true,
class: 'ring ring-inset ring-error'
},
{
color: 'neutral',
variant: [
'outline',
'subtle'
],
class: 'outline-inverted/25 has-focus-visible:outline-3 has-focus-visible:ring-inverted'
},
{
color: 'neutral',
variant: [
'soft',
'ghost'
],
class: 'outline-inverted/25 has-focus-visible:outline-3'
},
{
color: 'neutral',
highlight: true,
class: 'ring ring-inset ring-inverted'
},
{
leading: true,
size: 'xs',
class: 'ps-7'
},
{
leading: true,
size: 'sm',
class: 'ps-8'
},
{
leading: true,
size: 'md',
class: 'ps-9'
},
{
leading: true,
size: 'lg',
class: 'ps-10'
},
{
leading: true,
size: 'xl',
class: 'ps-11'
},
{
trailing: true,
size: 'xs',
class: 'pe-7'
},
{
trailing: true,
size: 'sm',
class: 'pe-8'
},
{
trailing: true,
size: 'md',
class: 'pe-9'
},
{
trailing: true,
size: 'lg',
class: 'pe-10'
},
{
trailing: true,
size: 'xl',
class: 'pe-11'
},
{
loading: true,
leading: true,
class: {
leadingIcon: 'animate-spin'
}
},
{
loading: true,
leading: false,
trailing: true,
class: {
trailingIcon: 'animate-spin'
}
},
{
fixed: false,
size: 'xs',
class: 'md:text-xs'
},
{
fixed: false,
size: 'sm',
class: 'md:text-xs'
},
{
fixed: false,
size: 'md',
class: 'md:text-sm'
},
{
fixed: false,
size: 'lg',
class: 'md:text-sm'
},
{
variant: 'outline',
class: {
segment: 'focus:bg-elevated'
}
},
{
variant: 'soft',
class: {
segment: 'focus:bg-accented/50 group-hover:focus:bg-accented'
}
},
{
variant: 'subtle',
class: {
segment: 'focus:bg-accented'
}
},
{
variant: 'ghost',
class: {
segment: 'focus:bg-elevated group-hover:focus:bg-accented'
}
},
{
variant: 'none',
class: {
segment: 'focus:bg-elevated'
}
}
],
defaultVariants: {
size: 'md',
color: 'primary',
variant: 'outline'
}
}
}
})