日期范围选择字段
特性
- 完整的键盘导航支持
- 可控制或非受控模式
- 焦点管理完善
- 本地化支持
- 高度可组合
- 默认具备可访问性
- 同时支持日期和日期时间格式
前言
该组件依赖于 @internationalized/date 包,该包解决了在 JavaScript 中处理日期和时间时的诸多问题。
我们强烈建议阅读该包的文档,以对其工作原理有扎实的了解,并且您需要在项目中安装它才能使用与日期相关的组件。
安装
安装日期包。
$ npm add @internationalized/date从命令行安装该组件。
$ npm add reka-ui结构
导入所有部分并将它们组合在一起。
<script setup>
import {
DateRangeFieldInput,
DateRangeFieldRoot,
} from 'reka-ui'
</script>
<template>
<DateRangeFieldRoot>
<DateRangeFieldInput />
</DateRangeFieldRoot>
</template>API 参考
根组件 (Root)
包含日期字段的所有部分
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
defaultPlaceholder | DateValueThe default placeholder date | |
defaultValue | DateRangeThe default value for the calendar | |
dir | 'ltr' | 'rtl'The reading direction of the date field when applicable. | |
disabled | false | booleanWhether 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 | booleanWhether or not to hide the time zone segment of the field | |
hourCycle | 12 | 24The hour cycle used for formatting times. Defaults to the local preference | |
id | stringId of the element | |
isDateUnavailable | MatcherA function that returns whether or not a date is unavailable | |
locale | stringThe locale to use for formatting dates | |
maxValue | DateValueThe maximum date that can be selected | |
minValue | DateValueThe minimum date that can be selected | |
modelValue | DateRange | nullThe controlled checked state of the calendar. Can be bound as | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
placeholder | DateValueThe 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 | booleanWhether or not the date field is readonly |
required | booleanWhen | |
step | DateStepThe stepping interval for the time fields. Defaults to |
| Emit | Payload |
|---|---|
update:modelValue | [DateRange]Event handler called whenever the model value changes |
update:placeholder | [date: DateValue]Event handler called whenever the placeholder value changes |
| Slots (default) | Payload |
|---|---|
modelValue | DateRange | null |
segments | { start: { part: SegmentPart; value: string; }[]; end: { part: SegmentPart; value: string; }[]; } |
| Methods | Type |
|---|---|
isDateUnavailable | MatcherA function that returns whether or not a date is unavailable |
setFocusedElement | (el: HTMLElement) => void |
| Data Attribute | Value |
|---|---|
[data-readonly] | 只读时出现 |
[data-disabled] | 禁用时出现 |
[data-invalid] | 无效时出现 |
输入组件 (Input)
包含日期字段的各个分段
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange 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 | |
type* | 'start' | 'end'The type of field to render (start or end) |
| Data Attribute | Value |
|---|---|
[data-disabled] | 禁用时出现 |
[data-invalid] | 无效时出现 |
[data-placeholder] | 未设置值时出现 |
可访问性
键盘交互
| Key | Description |
|---|---|
Tab | 当焦点移动到日期字段上时,聚焦第一个分段。 |
ArrowLeftArrowRight |
在日期字段的各分段之间导航。
|
ArrowUpArrowDown | 递增/更改分段的值。 |
0-9 |
当焦点位于数字型 DateFieldInput 上时,输入数字,并且如果下一个输入将导致无效值,则聚焦下一个分段。
|
Backspace | 从聚焦的数字分段中删除一位数字。 |
AP | 当焦点位于日周期分段上时,将其设置为 AM 或 PM。 |
