backdrop
组件

日期字段

Alpha
允许用户在指定字段内输入特定日期。
mm
dd
yyyy

功能特点

  • 支持完整键盘导航
  • 可受控或非受控使用
  • 焦点管理完备
  • 支持本地化
  • 高度可组合
  • 默认支持无障碍访问
  • 同时支持日期和日期时间格式

前言

该组件依赖于 @internationalized/date 包,它解决了 JavaScript 中处理日期和时间时的许多问题。

我们强烈建议阅读该包的文档以深入了解其工作原理,并且您需要在项目中安装它才能使用与日期相关的组件。

安装

安装日期包。

sh
$ npm add @internationalized/date

在命令行中安装该组件。

sh
$ npm add reka-ui

结构

导入所有部件并将它们组合在一起。

vue
<script setup>
import {
  DateFieldInput,
  DateFieldRoot,
} from 'reka-ui'
</script>

<template>
  <DateFieldRoot>
    <DateFieldInput />
  </DateFieldRoot>
</template>

API 参考

Root

包含日期字段的所有部分

PropDefaultType
as
'div'
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

defaultPlaceholder
DateValue

The default placeholder date

defaultValue
DateValue

The default value for the calendar

dir
'ltr' | 'rtl'

The reading direction of the date field when applicable.
If omitted, inherits globally from ConfigProvider or assumes LTR (left-to-right) reading mode.

disabled
false
boolean

Whether or not the date field is disabled

granularity
'day' | 'hour' | 'minute' | 'second'

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

hideTimeZone
boolean

Whether or not to hide the time zone segment of the field

hourCycle
12 | 24

The hour cycle used for formatting times. Defaults to the local preference

id
string

Id of the element

isDateUnavailable
Matcher

A function that returns whether or not a date is unavailable

locale
string

The locale to use for formatting dates

maxValue
DateValue

The maximum date that can be selected

minValue
DateValue

The minimum date that can be selected

modelValue
DateValue | null

The controlled checked state of the calendar. Can be bound as v-model.

name
string

The name of the field. Submitted with its owning form as part of a name/value pair.

placeholder
DateValue

The placeholder date, which is used to determine what month to display when no date is selected. This updates as the user navigates the calendar and can be used to programmatically control the calendar view

readonly
false
boolean

Whether or not the date field is readonly

required
boolean

When true, indicates that the user must set the value before the owning form can be submitted.

step
DateStep

The stepping interval for the time fields. Defaults to 1.

EmitPayload
update:modelValue
[date: DateValue]

Event handler called whenever the model value changes

update:placeholder
[date: DateValue]

Event handler called whenever the placeholder value changes

Slots (default)Payload
modelValue
DateValue | undefined

The current date of the field

segments
{ part: SegmentPart; value: string; }[]

The date field segment contents

isInvalid
boolean

Value if the input is invalid

MethodsType
isDateUnavailable
Matcher

A function that returns whether or not a date is unavailable

setFocusedElement
(el: HTMLElement) => void

Helper to set the focused element inside the DateField

Data AttributeValue
[data-readonly]只读时存在
[data-disabled]禁用时存在
[data-invalid]无效时存在

Input

包含日期字段的各部分

PropDefaultType
as
'div'
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

part*
'day' | 'month' | 'year' | 'hour' | 'minute' | 'second' | 'dayPeriod' | 'literal' | 'timeZoneName'

The part of the date to render

Data AttributeValue
[data-disabled]禁用时存在
[data-invalid]无效时存在
[data-placeholder]未设置值时存在

无障碍访问

键盘交互

KeyDescription
Tab
当焦点移入日期字段时,聚焦于第一个部分。
ArrowLeftArrowRight
在日期字段的各个部分之间导航。
ArrowUpArrowDown
增加/更改当前部分的值。
0-9
当焦点位于数值型 DateFieldInput 上时,输入数字,并且如果继续输入会导致无效值,则自动聚焦到下一部分。
Backspace
从聚焦的数值型部分删除一个数字。
AP
当焦点位于时段部分(如 AM/PM)时,将其设置为 AM 或 PM。