feat: migrate public TimeField with shared date input parts

This commit is contained in:
maoyingjie
2026-10-26 17:56:41 +08:00
parent d74ad1b711
commit 97263d82e1
45 changed files with 2415 additions and 61 deletions
+19
View File
@@ -0,0 +1,19 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
} from "@heroui-vue/components";
</script>
<template>
<TimeField class="w-[256px]" name="time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
</TimeField>
</template>
+38
View File
@@ -0,0 +1,38 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
Description,
Button,
} from "@heroui-vue/components";
import { shallowRef } from "vue";
import { Time, getLocalTimeZone, now } from "@internationalized/date";
import type { TimeValue } from "@heroui-vue/components";
const value = shallowRef<TimeValue | null>(null);
function setCurrent() {
const current = now(getLocalTimeZone());
value.value = new Time(current.hour, current.minute, current.second);
}
</script>
<template>
<div class="flex flex-col gap-4">
<TimeField class="w-[256px]" name="time" v-model="value">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>
当前值:{{ value ? value.toString() : "(空)" }}
</Description>
</TimeField>
<div class="flex gap-2">
<Button variant="tertiary" @click="setCurrent">设为当前时间</Button>
<Button variant="tertiary" @click="value = null">清空</Button>
</div>
</div>
</template>
@@ -0,0 +1,23 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
Description,
} from "@heroui-vue/components";
</script>
<template>
<TimeField class="w-full max-w-48 gap-1.5" name="reminder">
<Label class="font-medium text-foreground">提醒时间</Label>
<Description>每日签到通知。</Description>
<TimeFieldGroup
class="rounded-xl border border-border/80 bg-surface px-2 py-1 shadow-sm ring-1 ring-accent/5 focus-within:border-accent/25 focus-within:ring-2 focus-within:ring-accent/15"
>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment class="text-foreground" :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
</TimeField>
</template>
+39
View File
@@ -0,0 +1,39 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
Description,
} from "@heroui-vue/components";
import { Time, getLocalTimeZone, now } from "@internationalized/date";
const currentTime = now(getLocalTimeZone());
const timeValue = new Time(
currentTime.hour,
currentTime.minute,
currentTime.second,
);
</script>
<template>
<div class="flex flex-col gap-4">
<TimeField is-disabled class="w-[256px]" name="time" :value="timeValue">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>此时间字段已禁用</Description>
</TimeField>
<TimeField is-disabled class="w-[256px]" name="time-empty">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>此时间字段已禁用</Description>
</TimeField>
</div>
</template>
@@ -0,0 +1,85 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
TimeFieldPrefix,
Label,
Description,
FieldError,
Button,
Form,
} from "@heroui-vue/components";
import { IconClock } from "@tabler/icons-vue";
import { computed, shallowRef } from "vue";
import { parseTime, Time } from "@internationalized/date";
import type { TimeValue } from "@heroui-vue/components";
const value = shallowRef<TimeValue | null>(null);
const minTime = parseTime("09:00");
const maxTime = parseTime("17:00");
const isInvalid = computed(() => {
if (!value.value) return false;
const time = new Time(
value.value.hour,
value.value.minute,
value.value.second,
value.value.millisecond,
);
return time.compare(minTime) < 0 || time.compare(maxTime) > 0;
});
import { onBeforeUnmount } from "vue";
const isSubmitting = shallowRef(false);
const submitted = shallowRef("");
let timer: ReturnType<typeof setTimeout> | undefined;
function submit() {
if (!value.value || isInvalid.value || isSubmitting.value) return;
const payload = value.value.toString();
isSubmitting.value = true;
timer = setTimeout(() => {
submitted.value = payload;
value.value = null;
isSubmitting.value = false;
}, 1500);
}
onBeforeUnmount(() => clearTimeout(timer));
</script>
<template>
<Form class="flex w-[280px] flex-col gap-4" @submit.prevent="submit">
<TimeField
is-required
class="w-full"
name="time"
v-model="value"
:is-invalid="isInvalid"
:min-value="minTime"
:max-value="maxTime"
>
<Label>预约时间</Label>
<TimeFieldGroup>
<TimeFieldPrefix>
<IconClock class="size-4 text-muted" />
</TimeFieldPrefix>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<FieldError v-if="isInvalid">
时间须在上午 9:00 至下午 5:00 之间
</FieldError>
<Description v-else>输入上午 9:00 至下午 5:00 之间的时间</Description>
</TimeField>
<Button
class="w-full"
:is-disabled="!value || isInvalid"
:is-pending="isSubmitting"
type="submit"
variant="primary"
>
{{ isSubmitting ? "提交中…" : "提交" }}
</Button>
<output v-if="submitted" class="text-sm text-muted">
已提交:{{ submitted }}
</output>
</Form>
</template>
+38
View File
@@ -0,0 +1,38 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
TimeFieldPrefix,
TimeFieldSuffix,
Label,
} from "@heroui-vue/components";
import { IconClock, IconChevronDown } from "@tabler/icons-vue";
</script>
<template>
<div class="w-[400px] space-y-4">
<TimeField full-width name="time">
<Label>时间</Label>
<TimeFieldGroup full-width>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
</TimeField>
<TimeField full-width name="time-icons">
<Label>时间</Label>
<TimeFieldGroup full-width>
<TimeFieldPrefix>
<IconClock class="size-4 text-muted" />
</TimeFieldPrefix>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
<TimeFieldSuffix>
<IconChevronDown class="size-4 text-muted" />
</TimeFieldSuffix>
</TimeFieldGroup>
</TimeField>
</div>
</template>
+32
View File
@@ -0,0 +1,32 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
FieldError,
} from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-4">
<TimeField is-invalid is-required class="w-[256px]" name="time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<FieldError>请输入有效时间</FieldError>
</TimeField>
<TimeField is-invalid class="w-[256px]" name="invalid-time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<FieldError>时间须在工作时间内</FieldError>
</TimeField>
</div>
</template>
+38
View File
@@ -0,0 +1,38 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
TimeFieldPrefix,
Label,
Description,
Surface,
} from "@heroui-vue/components";
import { IconClock } from "@tabler/icons-vue";
</script>
<template>
<Surface class="flex w-full max-w-sm flex-col gap-4 rounded-3xl p-6">
<TimeField class="w-full" name="time">
<Label>时间</Label>
<TimeFieldGroup variant="secondary">
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>输入时间</Description>
</TimeField>
<TimeField class="w-full" name="time-2">
<Label>预约时间</Label>
<TimeFieldGroup variant="secondary">
<TimeFieldPrefix>
<IconClock class="size-4 text-muted" />
</TimeFieldPrefix>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>输入预约时间</Description>
</TimeField>
</Surface>
</template>
@@ -0,0 +1,19 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
} from "@heroui-vue/components";
</script>
<template>
<TimeField as="div" data-custom="foo" class="w-[256px]" name="time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
</TimeField>
</template>
+31
View File
@@ -0,0 +1,31 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
Description,
} from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-4">
<TimeField is-required class="w-[256px]" name="time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
</TimeField>
<TimeField is-required class="w-[256px]" name="appointment-time">
<Label>预约时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>必填项</Description>
</TimeField>
</div>
</template>
@@ -0,0 +1,32 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
Description,
} from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-4">
<TimeField class="w-[256px]" name="time">
<Label>开始时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>输入开始时间</Description>
</TimeField>
<TimeField class="w-[256px]" name="end-time">
<Label>结束时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<Description>输入结束时间</Description>
</TimeField>
</div>
</template>
@@ -0,0 +1,28 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
TimeFieldPrefix,
TimeFieldSuffix,
Label,
Description,
} from "@heroui-vue/components";
import { IconClock, IconChevronDown } from "@tabler/icons-vue";
</script>
<template>
<TimeField class="w-[256px]" name="time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldPrefix><IconClock class="size-4 text-muted" /></TimeFieldPrefix>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
<TimeFieldSuffix>
<IconChevronDown class="size-4 text-muted" />
</TimeFieldSuffix>
</TimeFieldGroup>
<Description>输入时间</Description>
</TimeField>
</template>
@@ -0,0 +1,22 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
TimeFieldPrefix,
Label,
} from "@heroui-vue/components";
import { IconClock } from "@tabler/icons-vue";
</script>
<template>
<TimeField class="w-[256px]" name="time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldPrefix><IconClock class="size-4 text-muted" /></TimeFieldPrefix>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
</TimeField>
</template>
@@ -0,0 +1,22 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
TimeFieldSuffix,
Label,
} from "@heroui-vue/components";
import { IconClock } from "@tabler/icons-vue";
</script>
<template>
<TimeField class="w-[256px]" name="time">
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
<TimeFieldSuffix><IconClock class="size-4 text-muted" /></TimeFieldSuffix>
</TimeFieldGroup>
</TimeField>
</template>
@@ -0,0 +1,51 @@
<script setup lang="ts">
import {
TimeField,
TimeFieldGroup,
TimeFieldInput,
TimeFieldSegment,
Label,
Description,
FieldError,
} from "@heroui-vue/components";
import { computed, shallowRef } from "vue";
import { parseTime, Time } from "@internationalized/date";
import type { TimeValue } from "@heroui-vue/components";
const value = shallowRef<TimeValue | null>(null);
const minTime = parseTime("09:00");
const maxTime = parseTime("17:00");
const isInvalid = computed(() => {
if (!value.value) return false;
const time = new Time(
value.value.hour,
value.value.minute,
value.value.second,
value.value.millisecond,
);
return time.compare(minTime) < 0 || time.compare(maxTime) > 0;
});
</script>
<template>
<div class="flex flex-col gap-4">
<TimeField
is-required
class="w-[256px]"
name="time"
v-model="value"
:is-invalid="isInvalid"
:min-value="minTime"
:max-value="maxTime"
>
<Label>时间</Label>
<TimeFieldGroup>
<TimeFieldInput v-slot="{ segment }">
<TimeFieldSegment :segment="segment" />
</TimeFieldInput>
</TimeFieldGroup>
<FieldError v-if="isInvalid">
时间须在上午 9:00 至下午 5:00 之间
</FieldError>
<Description v-else>输入上午 9:00 至下午 5:00 之间的时间</Description>
</TimeField>
</div>
</template>