145a208687
HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
459 lines
20 KiB
Markdown
459 lines
20 KiB
Markdown
---
|
||
title: TimeField 时间字段
|
||
description: 时间输入字段,包含标签、说明与校验。
|
||
---
|
||
|
||
## 用法
|
||
|
||
```ts
|
||
import {
|
||
TimeField,
|
||
TimeFieldGroup,
|
||
TimeFieldInput,
|
||
TimeFieldSegment,
|
||
} from "@heroui-vue/components";
|
||
```
|
||
|
||
<ComponentPreview name="time-field-basic" />
|
||
|
||
## 组件结构
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
TimeField,
|
||
TimeFieldGroup,
|
||
TimeFieldInput,
|
||
TimeFieldSegment,
|
||
Label,
|
||
Description,
|
||
FieldError,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
<template>
|
||
<TimeField>
|
||
<Label>时间</Label>
|
||
<TimeFieldGroup>
|
||
<TimeFieldInput v-slot="{ segment }">
|
||
<TimeFieldSegment :segment="segment" />
|
||
</TimeFieldInput>
|
||
</TimeFieldGroup>
|
||
<Description>输入时间</Description>
|
||
<FieldError />
|
||
</TimeField>
|
||
</template>
|
||
```
|
||
|
||
> **TimeField** 将标签、时间输入、说明与错误信息组合为单个无障碍组件。
|
||
|
||
## 示例
|
||
|
||
### 带图标
|
||
|
||
添加前缀或后缀图标以增强时间字段。
|
||
|
||
<ComponentPreview name="time-field-with-prefix-icon" />
|
||
|
||
<ComponentPreview name="time-field-with-suffix-icon" />
|
||
|
||
<ComponentPreview name="time-field-with-prefix-and-suffix" />
|
||
|
||
### 表面样式
|
||
|
||
在 [Surface](/components/surface) 内使用时,请在 `TimeFieldGroup` 上使用 `variant="secondary"`,以应用适合表面背景的低强调变体。
|
||
|
||
<ComponentPreview name="time-field-on-surface" />
|
||
|
||
### 带描述
|
||
|
||
<ComponentPreview name="time-field-with-description" />
|
||
|
||
### 必填字段
|
||
|
||
<ComponentPreview name="time-field-required" />
|
||
|
||
### 禁用
|
||
|
||
<ComponentPreview name="time-field-disabled" />
|
||
|
||
### 宽度充满
|
||
|
||
<ComponentPreview name="time-field-full-width" />
|
||
|
||
### 表单校验
|
||
|
||
将 `isInvalid` 与 `FieldError` 配合使用,以展示校验消息。
|
||
|
||
<ComponentPreview name="time-field-invalid" />
|
||
|
||
### 受控组件
|
||
|
||
使用 `v-model` 控制值,以与其他组件或状态管理同步。
|
||
|
||
<ComponentPreview name="time-field-controlled" />
|
||
|
||
### 表单示例
|
||
|
||
包含校验与提交处理的完整表单示例。
|
||
|
||
<ComponentPreview name="time-field-form-example" />
|
||
|
||
### 带校验
|
||
|
||
TimeField 支持通过 `minValue`、`maxValue` 及自定义校验逻辑进行验证。
|
||
|
||
<ComponentPreview name="time-field-with-validation" />
|
||
|
||
### 渲染函数
|
||
|
||
<ComponentPreview
|
||
name="time-field-render-function"
|
||
/>
|
||
|
||
## 自定义样式
|
||
|
||
### Tailwind CSS
|
||
|
||
<ComponentPreview name="time-field-custom-styles" />
|
||
|
||
### 全局 CSS
|
||
|
||
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||
|
||
```css
|
||
@layer components {
|
||
.hv-time-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-time-field` – 轻量样式的根容器(`flex flex-col gap-1`)
|
||
|
||
> **说明:** 子组件([Label](/components/label)、[Description](/components/description)、[FieldError](/components/field-error))拥有各自的 CSS 类与样式。自定义方式请参阅对应文档。`TimeFieldGroup` 的样式见下文 API 参考。
|
||
|
||
### 交互状态
|
||
|
||
TimeField 会根据状态自动设置以下 data 属性:
|
||
|
||
- **无效**:`[data-invalid="true"]` 或 `[aria-invalid="true"]` – 无效时自动隐藏 description 插槽
|
||
- **必填**:`[data-required="true"]` – 当 `isRequired` 为 true 时添加
|
||
- **禁用**:`[data-disabled="true"]` – 当 `isDisabled` 为 true 时添加
|
||
- **焦点在内**:`[data-focus-within="true"]` – 任一子输入聚焦时添加
|
||
|
||
## API 参考
|
||
|
||
### TimeField
|
||
|
||
TimeField 使用 Vue 时间值、作用域插槽和共享字段协议,完整差异见下方兼容说明。
|
||
|
||
#### Base Props
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ----------- | ---------------------------------------------- | ------- | ---------------------------------------------- |
|
||
| 默认插槽 | `(values: TimeFieldRenderProps) => VNodeChild` | - | 子组件(Label、TimeFieldGroup 等)或渲染函数。 |
|
||
| `class` | `string` | - | 与默认样式合并的 CSS 类。 |
|
||
| `style` | `StyleValue` | - | Vue 内联样式。 |
|
||
| `fullWidth` | `boolean` | `false` | 时间字段是否占满容器宽度。 |
|
||
| `id` | `string` | - | 元素的唯一 id。 |
|
||
| `as` | `string` | `"div"` | 自定义根元素,attrs 和 DOM 事件透传到此节点。 |
|
||
|
||
#### Value Props
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------------------------------ | ------------------------------------ | ------ | ------------------------------------------------------------------------------------------------------------ |
|
||
| `modelValue` / `value` | `TimeValue \| null` | - | 当前值(受控)。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
|
||
| `defaultValue` | `TimeValue \| null` | - | 默认值(非受控)。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
|
||
| `update:modelValue` / `change` | `(value: TimeValue \| null) => void` | - | 值变化时触发的事件处理函数。 |
|
||
| `placeholderValue` | `TimeValue \| null` | - | 影响占位符格式的占位时间;默认随小时制为 12:00 AM 或 00:00。 |
|
||
|
||
#### Validation Props
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| -------------------- | ---------------------------------------------- | ---------- | -------------------------------------------------------------------------------------------------------------- |
|
||
| `isRequired` | `boolean` | `false` | 是否在提交表单前要求用户输入。 |
|
||
| `isInvalid` | `boolean` | - | 值是否无效。 |
|
||
| `minValue` | `TimeValue \| null` | - | 用户可选择最早时间。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
|
||
| `maxValue` | `TimeValue \| null` | - | 用户可选择最晚时间。类型见 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/)。 |
|
||
| `validate` | `(value: TimeValue \| null) => ValidateResult` | - | 自定义校验函数。 |
|
||
| `validationBehavior` | `'native' \| 'aria'` | `'native'` | 使用原生 HTML 表单校验还是 ARIA 属性。 |
|
||
|
||
#### Format Props
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------------------------- | -------------------------------- | ---------- | -------------------------------------------------- |
|
||
| `granularity` | `'hour' \| 'minute' \| 'second'` | `'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` | - | 是否在渲染后自动聚焦该元素。 |
|
||
|
||
#### Accessibility Props
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------------------ | -------- | ------ | -------------------------- |
|
||
| `aria-label` | `string` | - | 无可见标签时的无障碍标签。 |
|
||
| `aria-labelledby` | `string` | - | 标注该字段的元素 id。 |
|
||
| `aria-describedby` | `string` | - | 描述该字段的元素 id。 |
|
||
| `aria-details` | `string` | - | 包含额外详情的元素 id。 |
|
||
|
||
### 组合组件
|
||
|
||
TimeField 与以下独立组件配合使用,请分别导入并直接使用:
|
||
|
||
- **Label** – 来自 `@heroui-vue/components` 的字段标签
|
||
- **TimeFieldGroup** – 时间输入分组(详见下文)
|
||
- **TimeFieldInput** – 来自 `@heroui-vue/components` 的分段位编辑输入
|
||
- **TimeFieldSegment** – 单个时间段位(时、分、秒等)
|
||
- **TimeFieldPrefix** / **TimeFieldSuffix** – 输入组的前缀与后缀插槽
|
||
- **Description** – 来自 `@heroui-vue/components` 的辅助说明
|
||
- **FieldError** – 来自 `@heroui-vue/components` 的校验错误信息
|
||
|
||
这些组件各自有独立的 props API。在 TimeField 中直接组合使用:
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import { shallowRef } from "vue";
|
||
import { parseTime } from "@internationalized/date";
|
||
import {
|
||
TimeField,
|
||
TimeFieldGroup,
|
||
TimeFieldInput,
|
||
TimeFieldSegment,
|
||
Label,
|
||
Description,
|
||
FieldError,
|
||
type TimeValue,
|
||
} from "@heroui-vue/components";
|
||
const time = shallowRef<TimeValue | null>(null);
|
||
</script>
|
||
<template>
|
||
<TimeField
|
||
v-model="time"
|
||
is-required
|
||
:min-value="parseTime('09:00')"
|
||
:max-value="parseTime('17:00')"
|
||
>
|
||
<Label>Appointment Time</Label>
|
||
<TimeFieldGroup>
|
||
<TimeFieldInput v-slot="{ segment }">
|
||
<TimeFieldSegment :segment="segment" />
|
||
</TimeFieldInput>
|
||
</TimeFieldGroup>
|
||
<Description>Select a time between 9:00 AM and 5:00 PM.</Description>
|
||
<FieldError />
|
||
</TimeField>
|
||
</template>
|
||
```
|
||
|
||
### TimeValue 类型
|
||
|
||
TimeField 使用 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/) 中的类型:
|
||
|
||
- `Time` – 仅时间(时、分、秒)
|
||
- `CalendarDateTime` – 含日期与时间、不含时区(TimeField 仅展示时间部分)
|
||
- `ZonedDateTime` – 含日期、时间与时区(TimeField 仅展示时间部分)
|
||
|
||
示例:
|
||
|
||
```ts
|
||
import {
|
||
parseTime,
|
||
Time,
|
||
getLocalTimeZone,
|
||
now,
|
||
} from "@internationalized/date";
|
||
const time = parseTime("14:30");
|
||
const currentTime = now(getLocalTimeZone());
|
||
const timeValue = new Time(
|
||
currentTime.hour,
|
||
currentTime.minute,
|
||
currentTime.second,
|
||
);
|
||
```
|
||
|
||
> **说明:** TimeField 使用 [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/) 进行时间处理、解析与类型定义。更多类型与函数见 [Internationalized Date 文档](https://react-aria.adobe.com/internationalized/date/)。
|
||
|
||
### TimeFieldRenderProps
|
||
|
||
默认作用域插槽可读取以下状态;`class` 和 `style` 使用 Vue 原生绑定:
|
||
|
||
| Prop | 类型 | 描述 |
|
||
| ---------------- | --------- | -------------------------- |
|
||
| `isDisabled` | `boolean` | 字段是否禁用。 |
|
||
| `isInvalid` | `boolean` | 字段当前是否无效。 |
|
||
| `isReadOnly` | `boolean` | 字段是否只读。 |
|
||
| `isRequired` | `boolean` | 字段是否必填。 |
|
||
| `isFocused` | `boolean` | 字段是否聚焦。 |
|
||
| `isFocusWithin` | `boolean` | 是否有子元素聚焦。 |
|
||
| `isFocusVisible` | `boolean` | 焦点是否可见(键盘导航)。 |
|
||
|
||
### TimeFieldGroup
|
||
|
||
TimeFieldGroup 复用 DateInputGroup,支持:
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| --------- | -------------------------- | ----------- | -------------------------------------------------------------------------------------- |
|
||
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
|
||
| `variant` | `"primary" \| "secondary"` | `"primary"` | 视觉变体。`primary` 为默认带阴影样式;`secondary` 为低强调、无阴影,适合用于 Surface。 |
|
||
|
||
### TimeFieldInput
|
||
|
||
TimeFieldInput 复用 DateInputGroupInput,支持:
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------- | -------- | ------ | ---------------------------------- |
|
||
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
|
||
|
||
TimeFieldInput 使用 `v-slot="{ segment, index }"` 逐段渲染;省略插槽时自动渲染全部段位。视觉 `variant` 放在 TimeFieldGroup 上。
|
||
|
||
### TimeFieldSegment
|
||
|
||
TimeFieldSegment 复用 DateInputGroupSegment,支持:
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| --------- | ------------- | ------ | --------------------------------------------------- |
|
||
| `segment` | `DateSegment` | - | 来自 TimeFieldInput 渲染函数的 `DateSegment` 对象。 |
|
||
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
|
||
|
||
### TimeFieldInputContainer
|
||
|
||
输入组合容器,复用 DateInputGroupInputContainer。支持 `as`(默认 div)、原生 attrs 与默认插槽。
|
||
|
||
### TimeFieldPrefix
|
||
|
||
TimeFieldPrefix 接受标准 HTML `div` 属性:
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| -------- | ------------ | ------ | ---------------------------------- |
|
||
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
|
||
| 默认插槽 | `VNodeChild` | - | 前缀插槽中要显示的内容。 |
|
||
|
||
### TimeFieldSuffix
|
||
|
||
TimeFieldSuffix 接受标准 HTML `div` 属性:
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| -------- | ------------ | ------ | ---------------------------------- |
|
||
| `class` | `string` | - | 与组件样式合并的 Tailwind CSS 类。 |
|
||
| 默认插槽 | `VNodeChild` | - | 后缀插槽中要显示的内容。 |
|
||
|
||
## TimeFieldGroup 样式
|
||
|
||
### 全局 CSS
|
||
|
||
基础类作用于所有实例,可通过 `@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__prefix,
|
||
.hv-date-input-group__suffix {
|
||
@apply pointer-events-none shrink-0 text-field-placeholder flex items-center;
|
||
}
|
||
}
|
||
```
|
||
|
||
### TimeFieldGroup CSS 类
|
||
|
||
- `.hv-date-input-group` – 根容器样式
|
||
- `.hv-date-input-group__input` – 输入包裹层样式
|
||
- `.hv-date-input-group__segment` – 单个时间段位样式
|
||
- `.hv-date-input-group__prefix` – 前缀元素样式
|
||
- `.hv-date-input-group__suffix` – 后缀元素样式
|
||
|
||
### TimeFieldGroup 交互状态
|
||
|
||
- **悬停**:`:hover` 或 `[data-hovered="true"]`
|
||
- **焦点在内**:`[data-focus-within="true"]` 或 `:focus-within`
|
||
- **无效**:`[data-invalid="true"]`(同时与 `aria-invalid` 同步)
|
||
- **禁用**:`[data-disabled="true"]` 或 `[aria-disabled="true"]`
|
||
- **段位聚焦**:段位上的 `:focus` 或 `[data-focused="true"]`
|
||
- **段位占位符**:段位上的 `[data-placeholder="true"]`
|
||
|
||
## 相关组件
|
||
|
||
[Label 标签](./label) · [FieldError 字段错误](./field-error) · [Description 描述](./description)
|
||
|
||
## Vue 兼容说明
|
||
|
||
- Root 使用 `TimeField` / `TimeFieldRoot`;输入六部件以具名导出提供,并与 DateInputGroup 部件共用实现。`TimeFieldInputContainer` 用于组合输入容器。
|
||
- `modelValue`(`v-model`)优先于 `value`,包括显式 `null` / `undefined`;未传受控属性时读取 `defaultValue`。`update:modelValue` / `change` 发送值副本或 `null`。父级拒绝后恢复已接受值,保持分段 DOM 和焦点。
|
||
- `TimeValue = Time | CalendarDateTime | ZonedDateTime` 从包根导出;类型构造和解析使用 `@internationalized/date`。编辑时间保留日期、日历、时区及未显示的精度,原生提交完整 `toString()`。min/max 只比较本地时分秒,不比较日期或 UTC 时刻。
|
||
- `locale` / `dir` 默认读取 ConfigProvider(否则 en-US / ltr);可显式覆盖。`form` 支持外部表单,`autoComplete` 默认 off;`disabled` 是 isDisabled 的原生别名。Root 暴露 `element` / `focus()`。
|
||
- React `render` 改为 `as` 和原生 attrs;children render prop 改默认作用域插槽。class/style 函数不直接移植,使用插槽状态和 Vue 绑定。没有声明继承全部 RAC props;Reka 的 step/stepSnapping 不作为额外公共 API。
|
||
- Group 支持 `variant`、`fullWidth`、`as`;Input、InputContainer、Segment、Prefix、Suffix 支持 `as` 和原生 attrs,Segment 还要求 `segment`。Input 的视觉变体由 Group 设置。取消按键需在实际 Segment 上使用 `@keydown.prevent`(可按需 stop);Root 的冒泡监听晚于分段编辑,不能撤销已经完成的子处理。
|
||
- 表单示例与基线一致:模拟提交(1500ms 加载)后将实际提交值输出到控制台并清空输入,卸载时清理计时器。
|
||
- 当前仅支持浏览器客户端。未开展完整系统 IME、读屏、真机或逐像素自动对比专项。
|
||
|
||
Enter 在实际 Segment 上触发所属原生表单的 `requestSubmit()`(支持外部 form),遵守原生约束校验和 `@keydown.prevent`。blur 不重复发送已经接受的值。
|