feat: migrate DateField and shared date input group

This commit is contained in:
maoyingjie
2026-10-23 23:56:18 +08:00
parent 3c878d2582
commit 01f0776805
63 changed files with 3974 additions and 90 deletions
+477
View File
@@ -0,0 +1,477 @@
---
title: DateField 日期字段
description: 日期输入字段,包含标签、说明与校验
---
## 用法
```ts
import {
DateField,
DateFieldGroup,
DateFieldInput,
DateFieldSegment,
} from "@heroui-vue/components";
```
<ComponentPreview name="date-field-basic" />
## 组件结构
```vue
<script setup lang="ts">
import {
DateField,
DateFieldGroup,
DateFieldInput,
DateFieldSegment,
Label,
Description,
FieldError,
} from "@heroui-vue/components";
</script>
<template>
<DateField>
<Label>日期</Label>
<DateFieldGroup>
<DateFieldInput v-slot="{ segment }">
<DateFieldSegment :segment="segment" />
</DateFieldInput>
</DateFieldGroup>
<Description>输入日期</Description>
<FieldError />
</DateField>
</template>
```
> **DateField** 将标签、日期输入、说明与错误信息组合为单个无障碍组件。
## 示例
### 带图标
通过前缀或后缀图标增强日期输入。
<ComponentPreview name="date-field-with-prefix-icon" />
<ComponentPreview name="date-field-with-suffix-icon" />
<ComponentPreview name="date-field-with-prefix-and-suffix" />
### 变体
DateFieldGroup 组件支持两种视觉变体:
- **`primary`**(默认)- 标准样式带阴影,适用于大多数场景
- **`secondary`** - 低强调变体无阴影,适用于 Surface 组件内
<ComponentPreview name="date-field-variants" />
### 表面样式
在 [Surface](/components/surface) 内使用时,请在 DateFieldGroup 上使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。
<ComponentPreview name="date-field-on-surface" />
### 带描述
<ComponentPreview name="date-field-with-description" />
### 必填字段
<ComponentPreview name="date-field-required" />
### 禁用
<ComponentPreview name="date-field-disabled" />
### 宽度充满
<ComponentPreview name="date-field-full-width" />
### 表单校验
配合 `FieldError` 使用 `isInvalid` 展示校验消息。
<ComponentPreview name="date-field-invalid" />
### 时间粒度
<ComponentPreview name="date-field-granularity" />
### 受控组件
使用 `v-model` 控制日期值,以与其他组件或状态管理同步。
<ComponentPreview name="date-field-controlled" />
### 表单示例
包含校验与提交的完整表单示例。
<ComponentPreview name="date-field-form-example" />
### 带校验
DateField 支持 `minValue`、`maxValue` 与自定义校验逻辑。
<ComponentPreview name="date-field-with-validation" />
### 渲染函数
<ComponentPreview name="date-field-render-function" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="date-field-custom-styles" />
### 全局 CSS
DateField 默认样式较轻量。覆盖 `.hv-date-field` 类可自定义容器样式。
```css
@layer components {
.hv-date-field {
@apply flex flex-col gap-1;
&[data-invalid="true"],
&[aria-invalid="true"] {
[data-slot="description"] {
@apply hidden;
}
}
[data-slot="label"] {
@apply w-fit;
}
[data-slot="description"] {
@apply px-1;
}
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
- `.hv-date-field` – 轻量样式的根容器(`flex flex-col gap-1`)
> **Note:** 子组件([Label](/components/label)、[Description](/components/description)、[FieldError](/components/field-error))拥有各自的 CSS 类与样式。自定义方式请参阅对应文档。DateFieldGroup 样式见下文 API 参考。
### 交互状态
DateField 会根据状态自动设置以下 data 属性:
- **Invalid**:`[data-invalid="true"]` 或 `[aria-invalid="true"]` - 无效时自动隐藏 description slot
- **Required**:`[data-required="true"]` - 当 `isRequired` 为 true 时应用
- **Disabled**:`[data-disabled="true"]` - 当 `isDisabled` 为 true 时应用
- **Focus Within**:`[data-focus-within="true"]` - 任一子输入聚焦时应用
## API 参考
### DateField
DateField 使用 Vue 日期值、作用域插槽和共享字段协议。与固定 HeroUI 基线的差异见下方兼容说明。
#### Base Props
| Prop | 类型 | 默认值 | 描述 |
| ----------- | ---------------- | ------- | ---------------------------------------------------- |
| 默认插槽 | `DateFieldState` | - | 子组件(Label、DateFieldGroup 等),支持作用域状态。 |
| `class` | `string` | - | 与组件默认样式合并。 |
| `style` | `StyleValue` | - | Vue 内联样式。 |
| `fullWidth` | `boolean` | `false` | 日期字段是否占满容器宽度。 |
| `id` | `string` | - | 元素的唯一 id。 |
| `as` | `string` | `"div"` | 自定义容器元素,属性与事件绑定到该容器。 |
#### Value Props
| Prop | 类型 | 默认值 | 描述 |
| ------------------------------ | ------------------------------------ | ------ | ------------------------------------------------------------------------------------------------------------ |
| `modelValue` / `value` | `DateValue \| null` | - | 当前值(受控)。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
| `defaultValue` | `DateValue \| null` | - | 默认值(非受控)。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
| `update:modelValue` / `change` | `(value: DateValue \| null) => void` | - | 值变化时触发的事件处理函数。 |
| `placeholderValue` | `DateValue \| null` | - | 影响占位符格式的占位日期。 |
#### Validation Props
| Prop | 类型 | 默认值 | 描述 |
| -------------------- | ------------------------------------------------------------------------------- | ---------- | -------------------------------------------------------------------------------------------------------------- |
| `isRequired` | `boolean` | `false` | 是否在提交表单前要求用户输入。 |
| `isInvalid` | `boolean` | - | 值是否无效。 |
| `minValue` | `DateValue \| null` | - | 用户可选择最早日期。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
| `maxValue` | `DateValue \| null` | - | 用户可选择最晚日期。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
| `isDateUnavailable` | `(date: DateValue) => boolean` | - | 针对每个日期调用;返回 true 表示该日期不可用。 |
| `validate` | `(value: DateValue \| null) => string \| string[] \| true \| null \| undefined` | - | 自定义校验函数。 |
| `validationBehavior` | `'native' \| 'aria'` | `'native'` | 使用原生 HTML 表单校验还是 ARIA 属性。 |
#### Format Props
| Prop | 类型 | 默认值 | 描述 |
| ------------------------- | ------------- | ------- | ----------------------------------------------------------- |
| `granularity` | `Granularity` | - | 显示的最小单位。日期默认为 `"day"`,时间默认为 `"minute"`。 |
| `hourCycle` | `12 \| 24` | - | 以 12 或 24 小时制显示时间;默认由语言环境决定。 |
| `hideTimeZone` | `boolean` | `false` | 是否隐藏时区缩写。 |
| `shouldForceLeadingZeros` | `boolean` | - | 是否始终为月、日、小时等显示前导零。 |
#### State Props
| Prop | 类型 | 默认值 | 描述 |
| ------------ | --------- | ------ | ---------------------- |
| `isDisabled` | `boolean` | - | 是否禁用输入。 |
| `isReadOnly` | `boolean` | - | 是否可选中但不可修改。 |
#### Form Props
| Prop | 类型 | 默认值 | 描述 |
| -------------- | --------- | ------ | ------------------------------------------------------------- |
| `name` | `string` | - | 输入元素的 name,用于 HTML 表单提交;以 ISO 8601 字符串提交。 |
| `autoFocus` | `boolean` | - | 是否在渲染后自动聚焦该元素。 |
| `autoComplete` | `string` | - | 输入应提供的自动完成类型。 |
#### Accessibility Props
| Prop | 类型 | 默认值 | 描述 |
| ------------------ | -------- | ------ | -------------------------- |
| `aria-label` | `string` | - | 无可见标签时的无障碍标签。 |
| `aria-labelledby` | `string` | - | 标注该字段的元素 id。 |
| `aria-describedby` | `string` | - | 描述该字段的元素 id。 |
| `aria-details` | `string` | - | 包含额外详情的元素 id。 |
### Composition Components
DateField 与以下独立组件配合使用,请分别导入并直接使用:
- **Label** – 来自 `@heroui-vue/components` 的字段标签
- **DateFieldGroup** – 日期输入分组(详见下文)
- **DateFieldInput** – 来自 `@heroui-vue/components` 的分段位编辑输入
- **DateFieldInputContainer** – 可横向滚动的容器,用于组合多个输入(例如开始/结束范围)
- **DateFieldSegment** – 单个日期段位(年、月、日等)
- **DateFieldPrefix** / **DateFieldSuffix** – 输入组的前缀与后缀插槽
- **Description** – 来自 `@heroui-vue/components` 的辅助说明
- **FieldError** – 来自 `@heroui-vue/components` 的校验错误信息
这些组件各自有独立的 props API。在 DateField 中直接组合使用:
```vue
<script setup lang="ts">
import { shallowRef } from "vue";
import {
today,
getLocalTimeZone,
type DateValue,
} from "@internationalized/date";
import {
DateField,
DateFieldGroup,
DateFieldInput,
DateFieldSegment,
Label,
Description,
FieldError,
} from "@heroui-vue/components";
const date = shallowRef<DateValue | null>(null);
const earliest = today(getLocalTimeZone());
</script>
<template>
<DateField v-model="date" is-required :min-value="earliest">
<Label>Appointment Date</Label>
<DateFieldGroup>
<DateFieldInput v-slot="{ segment }">
<DateFieldSegment :segment="segment" />
</DateFieldInput>
</DateFieldGroup>
<Description>Select a date from today onwards.</Description>
<FieldError />
</DateField>
</template>
```
### DateValue Types
DateField 使用 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/) 中的类型:
- `CalendarDate` – 不含时间与时区的日期
- `CalendarDateTime` – 含时间、不含时区
- `ZonedDateTime` – 含时间与时区
`Time` 仅表示时间,不属于 DateField 的 `DateValue`;后续 TimeField 单独提供。
示例:
```ts
import { parseDate, today, getLocalTimeZone } from "@internationalized/date";
const date = parseDate("2024-01-15");
const todayDate = today(getLocalTimeZone());
```
> **说明:** DateField 依赖 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/) 进行解析、运算与类型定义。更多类型与函数见 [Internationalized Date 文档](https://react-aria.adobe.com/internationalized/date/)。
### Render Props
默认作用域插槽可读取以下状态;`class`、`style` 使用 Vue 绑定表达式。
| Prop | 类型 | 描述 |
| ------------------ | ------------------- | ---------------------- |
| `isDisabled` | `boolean` | 字段是否禁用。 |
| `isInvalid` | `boolean` | 字段当前是否无效。 |
| `isReadOnly` | `boolean` | 字段是否只读。 |
| `isRequired` | `boolean` | 字段是否必填。 |
| `isFocusVisible` | `boolean` | 键盘焦点是否可见。 |
| `isFocused` | `boolean` | 字段是否聚焦。 |
| `isFocusWithin` | `boolean` | 是否有子元素聚焦。 |
| `value` | `DateValue \| null` | 当前接受的日期。 |
| `validationErrors` | `string[]` | 当前可显示的校验错误。 |
### DateFieldGroup
DateFieldGroup 与独立 DateInputGroup 共用部件,透传原生容器属性,并支持:
| Prop | 类型 | 默认值 | 描述 |
| ----------- | -------------------------- | ----------- | -------------------------------------------------------------------------------------- |
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
| `fullWidth` | `boolean` | `false` | 日期输入组是否占满容器宽度。 |
| `variant` | `"primary" \| "secondary"` | `"primary"` | 视觉变体。`primary` 为默认带阴影样式;`secondary` 为低强调、无阴影,适合用于 Surface。 |
### DateFieldInput
DateFieldInput 使用默认作用域插槽逐段渲染;省略插槽时自动渲染全部段位。
| Prop | 类型 | 默认值 | 描述 |
| ------- | -------- | ------- | ---------------------------------- |
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
| `as` | `string` | `"div"` | 输入容器元素。 |
`DateFieldInput` 通过 `v-slot="{ segment, index }"` 提供日期段位;每个段位对应日期的一部分(年、月、日等)。
### DateFieldSegment
DateFieldSegment 渲染来自输入作用域插槽的段位:
| Prop | 类型 | 默认值 | 描述 |
| --------- | ------------- | ------ | ----------------------------------------------------- |
| `segment` | `DateSegment` | - | 来自 DateFieldInput 作用域插槽的 `DateSegment` 对象。 |
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
### DateFieldInputContainer
DateFieldInputContainer 接受标准 HTML `div` 属性:
| Prop | 类型 | 默认值 | 描述 |
| -------- | -------- | ------ | ------------------------------------------------- |
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
| 默认插槽 | 默认插槽 | - | 滚动容器中的内容(通常为多个 `DateFieldInput`)。 |
### DateFieldPrefix
DateFieldPrefix 接受标准 HTML `div` 属性:
| Prop | 类型 | 默认值 | 描述 |
| -------- | -------- | ------ | ---------------------------------- |
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
| 默认插槽 | 默认插槽 | - | 前缀插槽中要显示的内容。 |
### DateFieldSuffix
DateFieldSuffix 接受标准 HTML `div` 属性:
| Prop | 类型 | 默认值 | 描述 |
| -------- | -------- | ------ | ---------------------------------- |
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
| 默认插槽 | 默认插槽 | - | 后缀插槽中要显示的内容。 |
## DateFieldGroup Styling
### Customizing the component classes
基础类作用于所有实例,可通过 `@layer components` 一次性覆盖。
```css
@layer components {
.hv-date-input-group {
@apply inline-flex h-9 items-center overflow-hidden rounded-field border bg-field text-sm text-field-foreground shadow-field outline-none;
&:hover,
&[data-hovered="true"] {
@apply bg-field-hover;
}
&[data-focus-within="true"],
&:focus-within {
@apply status-focused-field;
}
&[data-invalid="true"] {
@apply status-invalid-field;
}
&[data-disabled="true"],
&[aria-disabled="true"] {
@apply status-disabled;
}
}
.hv-date-input-group__input {
@apply flex flex-1 items-center gap-px rounded-none border-0 bg-transparent px-3 py-2 shadow-none outline-none;
}
.hv-date-input-group__segment {
@apply inline-block rounded-md px-0.5 text-end tabular-nums outline-none;
&:focus,
&[data-focused="true"] {
@apply bg-accent-soft text-accent-soft-foreground;
}
}
.hv-date-input-group__input-container {
@apply flex flex-1 items-center;
overflow-x: auto;
overflow-y: clip;
scrollbar-width: none;
}
.hv-date-input-group__prefix,
.hv-date-input-group__suffix {
@apply pointer-events-none shrink-0 text-field-placeholder flex items-center;
}
}
```
### DateFieldGroup CSS Classes
- `.hv-date-input-group` – 根容器样式
- `.hv-date-input-group__input` – 输入包裹层样式
- `.hv-date-input-group__input-container` – 用于组合多个输入的滚动容器
- `.hv-date-input-group__segment` – 单个日期段位样式
- `.hv-date-input-group__prefix` – 前缀元素样式
- `.hv-date-input-group__suffix` – 后缀元素样式
### DateFieldGroup Interactive States
- **Hover**:`:hover` 或 `[data-hovered="true"]`
- **Focus Within**:`[data-focus-within="true"]` 或 `:focus-within`
- **Invalid**:`[data-invalid="true"]`(与 `aria-invalid` 同步)
- **Disabled**:`[data-disabled="true"]` 或 `[aria-disabled="true"]`
- **Segment Focus**:段位上 `:focus` 或 `[data-focused="true"]`
- **Segment Placeholder**:段位上 `[data-placeholder="true"]`
## 相关组件
[TextField 文本输入框](./text-field) · [NumberField 数字输入框](./number-field) · [Select 选择器](./select)
## Vue 兼容说明
- DateField 七部件与 DateInputGroup 六部件共用实现。React 对象组合名改为具名导出,`render` 改为 `as`,渲染函数改为作用域插槽。
- 值使用 `v-model` / `update:modelValue`,`change` 是同一次日期提案通知。父组件拒绝更新时显示恢复接受值;未完成的空字段分段输入保留为草稿。显式 `null` / `undefined` 均表示受控空值,初始值只用于非受控状态。
- 分段按 locale 日历显示,事件与原生 ISO 提交保留原始模型日历、时间精度和命名时区;`granularity` 只改变显示,不截断提交值。可用 `locale` / `dir` 覆盖 ConfigProvider。
- `autoComplete` 传给视觉隐藏的 ISO 文本提交输入;自动填充的 ISO 输入与普通编辑共用接受值协议,不承诺各浏览器会为分段字段提供日期自动填充建议。
- 安装版 Reka 2.11.0 的 Root 忽略 `asChild`,内部保留一个无视觉包装;`as`、样式及 DOM 引用归属本库内层容器。原底座左右段位导航不检查 `defaultPrevented`,拦截左右导航时在 Segment 使用 `@keydown.stop.prevent`;仅 `.prevent` 可取消数字/增减编辑,但不能取消祖先底座导航。
- 本轮支持键盘与输入法数字编辑。底座没有完整日期粘贴解析;不声明支持粘贴任意本地化日期字符串。全格式粘贴、不同日历的 13 月/era 编辑和完整读屏验收仍需后续专项补齐。
- 默认占位日期采用当前本地日期;SSR 跨时区使用者应传入明确的 `placeholderValue` 或初始日期以固定首屏。