TimePicker 时间选择器

点击输入框弹出时间面板进行选择,基于 dayjs。

何时使用

  • 需要选择时间(时/分/秒)时。

基础用法

<script setup lang="ts">
import { ref } from "vue";
import { TimePicker, dayjs } from "@reglow/reui";
import type { Dayjs } from "dayjs";

const value = ref<Dayjs | null>(null);

const onChange = (time: Dayjs | null, timeString: string) => {
  console.log(time, timeString);
};
</script>

<template>
  <TimePicker
    v-model="value"
    :default-open-value="dayjs('00:00:00', 'HH:mm:ss')"
    @change="onChange"
    class="w-[200px]"
  />
</template>
选中:12:08:23

导入

import { TimePicker, dayjs } from "@reglow/reui";
import type { Dayjs } from "dayjs";

更多示例

受控组件

valueonChange 需要配合使用。

<script setup lang="ts">
import { ref } from "vue";
import { TimePicker } from "@reglow/reui";
import type { Dayjs } from "dayjs";

const value = ref<Dayjs | null>(null);
</script>

<template>
  <TimePicker v-model="value" class="w-[200px]" />
</template>

三种大小

三种大小的输入框,大的用在表单中,中的为默认。

<script setup lang="ts">
import { TimePicker, dayjs } from "@reglow/reui";

const val = dayjs("12:08:23", "HH:mm:ss");
</script>

<template>
  <div class="flex gap-2">
    <TimePicker :default-value="val" size="large" class="w-[200px]" />
    <TimePicker :default-value="val" class="w-[200px]" />
    <TimePicker :default-value="val" size="small" class="w-[200px]" />
  </div>
</template>

选择确认

通过 needConfirm 手动设置是否需要确认按钮。确认模式下需要点击确认按钮才会提交。

<script setup lang="ts">
import { TimePicker } from "@reglow/reui";
import type { Dayjs } from "dayjs";

const onChange = (time: Dayjs | null, timeString: string) => {
  console.log(time, timeString);
};
</script>

<template>
  <TimePicker need-confirm @change="onChange" class="w-[200px]" />
</template>

禁用

<script setup lang="ts">
import { TimePicker, dayjs } from "@reglow/reui";

const val = dayjs("12:08:23", "HH:mm:ss");
</script>

<template>
  <TimePicker :default-value="val" disabled class="w-[200px]" />
</template>

选择时分

通过 format 控制显示哪些列。

<script setup lang="ts">
import { TimePicker, dayjs } from "@reglow/reui";

const format = "HH:mm";
const val = dayjs("12:08", format);
</script>

<template>
  <TimePicker :default-value="val" :format="format" class="w-[200px]" />
</template>

步长选项

使用 hourStepminuteStepsecondStep 按步长展示可选的时分秒。

<template>
  <TimePicker :hour-step="1" :minute-step="15" :second-step="10" class="w-[200px]" />
</template>

12 小时制

12 小时制的时间选择器。

<template>
  <div class="flex gap-2">
    <TimePicker use-12-hours class="w-[200px]" />
    <TimePicker use-12-hours format="h:mm:ss A" class="w-[200px]" />
    <TimePicker use-12-hours format="h:mm a" class="w-[200px]" />
  </div>
</template>

此刻按钮

<template>
  <TimePicker show-now class="w-[200px]" />
</template>

禁用时间

通过 disabledTime 禁用部分时间。

<script setup lang="ts">
import { TimePicker } from "@reglow/reui";
import type { Dayjs } from "dayjs";

const disabledTime = (now: Dayjs) => ({
  disabledHours: () => [0, 1, 2, 3, 4],
  disabledMinutes: () => [30, 31, 32, 33, 34, 35],
  disabledSeconds: () => [55, 56, 57, 58, 59],
});
</script>

<template>
  <TimePicker :disabled-time="disabledTime" class="w-[200px]" />
</template>

隐藏禁用选项

<template>
  <TimePicker :disabled-time="disabledTime" hide-disabled-options class="w-[200px]" />
</template>

附加内容

在选择框底部显示自定义的内容。

<template>
  <TimePicker class="w-[200px]">
    <template #renderExtraFooter>
      <Button size="small">OK</Button>
    </template>
  </TimePicker>
</template>

可清除

<script setup lang="ts">
import { TimePicker, dayjs } from "@reglow/reui";

const val = dayjs();
</script>

<template>
  <TimePicker allow-clear :default-value="val" class="w-[200px]" />
</template>

API

Props

属性类型默认值说明
modelValueDayjs | nullnull当前值(v-model)
defaultValueDayjs | nullnull默认时间
placeholderstring请选择时间占位文字
disabledbooleanfalse是否禁用
allowClearbooleantrue是否支持清除
sizesmall | medium | largemedium输入框大小
formatstringHH:mm:ss时间格式
use12Hoursbooleanfalse12 小时制
hourStepnumber1小时间隔
minuteStepnumber1分钟间隔
secondStepnumber1秒间隔
needConfirmbooleanfalse需要确认按钮
hideDisabledOptionsbooleanfalse隐藏禁用选项
showNowbooleanfalse显示「此刻」按钮
disabledTimeDisabledTimeFn-禁用时间
defaultOpenValueDayjs-默认打开时间
sidetop | bottombottom弹出位置
sideOffsetnumber4偏移量

Events

事件说明回调参数
change时间变化回调(value: Dayjs | null, timeString: string)
openChange面板打开/关闭(open: boolean)

Slots

名称说明
renderExtraFooter面板底部附加内容

类型

type DisabledTimeFn = (now: Dayjs) => {
  disabledHours?: () => number[];
  disabledMinutes?: (selectedHour: number) => number[];
  disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
};

默认格式

模式format
24 小时制HH:mm:ss
12 小时制h:mm:ss a
仅时分HH:mm

列显示规则

列根据 format 自动显示:

  • 包含 Hh -> 显示小时列
  • 包含 m -> 显示分钟列
  • 包含 s -> 显示秒列
  • use12Hourstrue -> 显示 AM/PM 列

国际化

placeholder、「此刻」按钮文案默认跟随全局语言(zh-CN / zh-TW / en-US),传 prop 可覆盖。