refactor time-label

This commit is contained in:
Timi
2026-10-21 11:25:05 +08:00
parent 5b4f588cfb
commit 929ba4d0e1
10 changed files with 52 additions and 49 deletions
+4 -4
View File
@@ -86,11 +86,11 @@ export const COMPONENT_LIST: ComponentMeta[] = [
entry: "src/components/description-grid/index.vue"
},
{
name: "PassTimeLabel",
title: "PassTimeLabel 时间标签",
description: "把时间戳显示为「多久以前」,点击可切换为完整日期时间。",
name: "TimeLabel",
title: "TimeLabel 时间标签",
description: "把时间戳显示为过去或未来的相对时间,点击可切换为完整日期时间。",
category: "数据展示",
entry: "src/components/passtime-label/index.vue"
entry: "src/components/time-label/index.vue"
},
{
name: "ContactNumber",
+1 -1
View File
@@ -17,7 +17,7 @@
| --- | --- |
| [AnimatedNumber](/components/animated-number) | 数值变化时按缓动函数滚动过渡,支持小数位与千分位。 |
| [DescriptionGrid](/components/description-grid) | 键值对描述列表,可指定列数、对齐方式与逐项具名插槽。 |
| [PassTimeLabel](/components/passtime-label) | 把时间戳显示为「多久以前」,点击可切换为完整日期时间。 |
| [TimeLabel](/components/time-label) | 把时间戳显示为过去或未来的相对时间,点击可切换为完整日期时间。 |
| [ContactNumber](/components/contact-number) | 按号段分组的电话号码展示,可点击拨号并支持前置图标。 |
## 内容与浮层
-23
View File
@@ -1,23 +0,0 @@
# PassTimeLabel 时间标签
接收时间戳或 `yyyy-MM-dd HH:mm:ss`、`yyyy-MM-ddTHH:mm:ss` 格式的日期时间字符串,显示为「多久以前」,点击可切换为完整日期时间。
## 基础用法
:::preview 基础用法 || 点击标签在相对时间与完整时间之间切换
demo-preview=../demos/passtime-label/basic.vue
:::
## 何时使用
动态列表、评论、消息流里的时间展示。用户往往更关心「多久之前」,需要精确时间时点一下即可。
## 交互与可访问性
标签带 `role="button"` 与 `tabindex="0"`,支持键盘回车与空格触发切换,因此可以安全地放在列表项里。
## API
<api-table name="PassTimeLabel" />
+23
View File
@@ -0,0 +1,23 @@
# TimeLabel 时间标签
接收时间戳或 `yyyy-MM-dd HH:mm:ss`、`yyyy-MM-ddTHH:mm:ss` 格式的日期时间字符串,自动显示过去或未来的相对时间;超过 3 天时显示具体日期时间。点击可切换为完整日期时间。
## 基础用法
:::preview 基础用法 || 点击标签在相对时间与完整时间之间切换
demo-preview=../demos/time-label/basic.vue
:::
## 何时使用
动态列表、评论、消息流里的时间展示。用户通常关心时间间隔,需要精确时间时点一下即可。
## 交互与可访问性
标签带 `role="button"` 与 `tabindex="0"`,支持键盘回车与空格触发切换,因此可以安全地放在列表项里。
## API
<api-table name="TimeLabel" />
@@ -2,28 +2,33 @@
<div class="demo">
<div v-for="item in timeList" :key="item.offset" class="row">
<span class="label" v-text="item.label" />
<pass-time-label :timestamp="item.timestamp" underline />
<TimeLabel :timestamp="item.timestamp" underline />
</div>
<div class="row">
<span class="label" v-text="'仅时间 / 仅日期'" />
<pass-time-label :timestamp="now" only-time underline />
<pass-time-label :timestamp="now" only-date underline />
<TimeLabel :timestamp="now" only-time underline />
<TimeLabel :timestamp="now" only-date underline />
</div>
</div>
</template>
<script lang="ts" setup>
import { PassTimeLabel } from "timi-web";
import { TimeLabel } from "timi-web";
const now = Date.now();
/** 点击标签可在「多久以前」与完整时间之间切换。 */
/** 点击标签可在相对时间与完整时间之间切换。 */
const timeList = [
{ offset: 30_000, label: "30 秒前" },
{ offset: 30 * 60_000, label: "30 分钟前" },
{ offset: 5 * 3600_000, label: "5 小时前" },
{ offset: 3 * 86400_000, label: "3 天前" }
{ offset: 3 * 86400_000, label: "3 天前" },
{ offset: -30_000, label: "30 秒后" },
{ offset: -30 * 60_000, label: "30 分钟后" },
{ offset: -5 * 3600_000, label: "5 小时后" },
{ offset: -2 * 86400_000, label: "2 天后" },
{ offset: -4 * 86400_000, label: "4 天后" }
].map(item => ({
...item,
timestamp: now - item.offset
-1
View File
@@ -9,5 +9,4 @@ CenterTips.install = (app: App) => {
};
/** 兼容旧组件名。 */
export const EmptyTips = CenterTips;
export default CenterTips;
+4 -5
View File
@@ -5,11 +5,11 @@ import HoverPopup, { VPopupComponent } from "./v-popup";
import Captcha from "./captcha";
import Loading from "./loading";
import Copyright from "./copyright";
import CenterTips, { EmptyTips } from "./center-tips";
import CenterTips from "./center-tips";
import MarkdownView from "./markdown-view";
import BEFlowerFall from "./background-effect/flower-fall";
import MarkdownEditor from "./markdown-editor";
import PassTimeLabel from "./passtime-label";
import TimeLabel from "./time-label";
import ContactNumber from "./contact-number";
import AnimatedNumber from "./animated-number";
import AnimatedAmount from "./animated-amount";
@@ -26,7 +26,7 @@ export default [
MarkdownView,
BEFlowerFall,
MarkdownEditor,
PassTimeLabel,
TimeLabel,
ContactNumber,
AnimatedNumber,
AnimatedAmount,
@@ -42,11 +42,10 @@ export {
Loading,
Copyright,
CenterTips,
EmptyTips,
MarkdownView,
BEFlowerFall,
MarkdownEditor,
PassTimeLabel,
TimeLabel,
ContactNumber,
AnimatedNumber,
AnimatedAmount,
-5
View File
@@ -1,5 +0,0 @@
import view from "./index.vue";
import WebToolkit from "../../utils/WebToolkit";
export const PassTimeLabel = WebToolkit.withInstall(view);
export default PassTimeLabel;
+5
View File
@@ -0,0 +1,5 @@
import view from "./index.vue";
import WebToolkit from "../../utils/WebToolkit";
export const TimeLabel = WebToolkit.withInstall(view);
export default TimeLabel;
@@ -1,7 +1,7 @@
<template>
<span
v-if="currentTimestamp"
class="tui-passtime-label"
class="tui-time-label"
:class="{ underline }"
role="button"
tabindex="0"
@@ -15,7 +15,7 @@
import { Time } from "timi-ts";
defineOptions({
name: "PassTimeLabel"
name: "TimeLabel"
});
const props = withDefaults(defineProps<{
@@ -42,7 +42,7 @@ const currentTimestamp = computed(() => {
const content = computed(() => {
if (!showingDetail.value) {
return Time.toPassedDateTime(currentTimestamp.value);
return Time.formatDateTime(currentTimestamp.value);
}
if (onlyTime.value) {
return Time.toTime(currentTimestamp.value);
@@ -61,7 +61,7 @@ function toggle(event: MouseEvent | KeyboardEvent): void {
</script>
<style lang="less" scoped>
.tui-passtime-label {
.tui-time-label {
cursor: pointer;
&.underline {