DatePicker 日期选择器

DatePicker 日期选择器

输入或选择日期的控件。

何时使用

当用户需要输入一个日期,可以点击标准输入框,弹出日期面板进行选择。

导入

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

代码演示

基础用法

最简单的用法,在浮层中可以选择或者输入日期。

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

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

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

<template>
  <DatePicker v-model="value" @change="onChange" class="w-[200px]" />
</template>

选择器类型

通过 picker 属性指定不同类型。

<template>
  <div class="flex flex-col gap-2 w-[200px]">
    <DatePicker picker="date" placeholder="选择日期" />
    <DatePicker picker="month" placeholder="选择月份" />
    <DatePicker picker="year" placeholder="选择年份" />
    <DatePicker picker="week" placeholder="选择周" />
    <DatePicker picker="quarter" placeholder="选择季度" />
  </div>
</template>

日期格式

使用 format 属性自定义日期显示格式。

<template>
  <DatePicker
    :default-value="dayjs('2024-01-01')"
    format="YYYY/MM/DD"
    class="w-[200px]"
  />
</template>

日期时间选择

增加选择时间功能,配合 showTime 使用。

<script setup>
import { ref } from "vue";
import { dayjs } from "@reglow/reui";

const value = ref(dayjs());
</script>

<template>
  <DatePicker
    v-model="value"
    show-time
    format="YYYY-MM-DD HH:mm:ss"
    class="w-[260px]"
  />
</template>

日期限定范围

通过 minDate 和 maxDate 限定日期范围。

<script setup>
import { dayjs } from "@reglow/reui";

const minDate = dayjs("2024-01-01");
const maxDate = dayjs("2024-12-31");
</script>

<template>
  <DatePicker
    :min-date="minDate"
    :max-date="maxDate"
    class="w-[200px]"
  />
</template>

不可选择日期

通过 disabledDate 禁用部分日期。

<script setup>
import { dayjs } from "@reglow/reui";

// 禁用今天之前的日期
const disabledDate = (current) => {
  return current && current < dayjs().endOf("day");
};
</script>

<template>
  <DatePicker
    :disabled-date="disabledDate"
    class="w-[200px]"
  />
</template>

预设范围

预设常用的日期选项。

<script setup>
import { dayjs } from "@reglow/reui";

const presets = [
  { label: "今天", value: dayjs() },
  { label: "昨天", value: dayjs().add(-1, "day") },
  { label: "上周", value: dayjs().add(-7, "day") },
  { label: "上月", value: dayjs().add(-1, "month") },
];
</script>

<template>
  <DatePicker :presets="presets" class="w-[200px]" />
</template>

大小

不同大小的日期选择器。

<template>
  <div class="flex flex-col gap-2 w-[200px]">
    <DatePicker size="large" placeholder="大型" />
    <DatePicker placeholder="默认" />
    <DatePicker size="small" placeholder="小型" />
  </div>
</template>

形态变体与校验状态

variant、status、bordered 与 Input 完全同一套主题映射,放在同一表单里视觉天然一致。

<template>
  <div class="flex flex-col gap-2 w-[240px]">
    <DatePicker variant="filled" placeholder="填充样式" />
    <DatePicker variant="borderless" placeholder="无边框" />
    <DatePicker variant="underlined" placeholder="下划线" />
    <DatePicker status="error" placeholder="错误状态" />
    <DatePicker status="warning" placeholder="警告状态" />
    <DatePicker :bordered="false" placeholder="无边框" />
  </div>
</template>

与 Input 混排示例:

<template>
  <div class="flex w-[480px] flex-col gap-3">
    <Input v-model="name" variant="filled" placeholder="姓名" />
    <DatePicker v-model="date" variant="filled" placeholder="入职日期" />
    <DateRangePicker v-model="range" variant="filled" placeholder="在职期间" />
  </div>
</template>

禁用

<template>
  <DatePicker disabled :default-value="dayjs()" class="w-[200px]" />
</template>

可清除

<template>
  <DatePicker allow-clear :default-value="dayjs()" class="w-[200px]" />
</template>

范围选择 RangePicker

通过 DateRangePicker 实现范围日期选择,双面板日历,支持悬停预览、快捷预设、时间选择。

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

const value = ref<[string | null, string | null] | null>([
  "2024-01-10",
  "2024-01-20",
]);

const presets: DatePickerRangePreset[] = [
  { label: "今天", value: () => [dayjs(), dayjs()] },
  {
    label: "最近 7 天",
    value: () => [dayjs().add(-7, "day"), dayjs()],
  },
  {
    label: "本月",
    value: () => [dayjs().startOf("month"), dayjs().endOf("month")],
  },
];
</script>

<template>
  <DateRangePicker v-model="value" :presets="presets" class="w-[320px]" />
</template>

范围选择 + 时间

<script setup lang="ts">
import { ref } from "vue";
import { DateRangePicker } from "@reglow/reui";

const value = ref<[string | null, string | null] | null>([
  "2024-01-10 08:00:00",
  "2024-01-20 18:30:00",
]);
</script>

<template>
  <DateRangePicker
    v-model="value"
    show-time
    format="YYYY-MM-DD HH:mm:ss"
    class="w-[360px]"
  />
</template>

范围选择器类型

DateRangePicker 同样支持 picker:week / month / quarter / year。month / quarter / year 为单面板,date / week 为双面板。

<script setup lang="ts">
import { ref } from "vue";
import { DateRangePicker } from "@reglow/reui";

const monthRange = ref<[string | null, string | null] | null>(null);
const quarterRange = ref<[string | null, string | null] | null>(null);
const yearRange = ref<[string | null, string | null] | null>(null);
const weekRange = ref<[string | null, string | null] | null>(null);
</script>

<template>
  <div class="flex w-[360px] flex-col gap-3">
    <DateRangePicker v-model="monthRange" picker="month" class="w-full" />
    <DateRangePicker v-model="quarterRange" picker="quarter" class="w-full" />
    <DateRangePicker v-model="yearRange" picker="year" class="w-full" />
    <DateRangePicker v-model="weekRange" picker="week" class="w-full" />
  </div>
</template>

范围禁用与清除

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

const disabledDate = (current: any) => {
  // 禁用周末
  return current.day() === 0 || current.day() === 6;
};
</script>

<template>
  <DateRangePicker
    :disabled-date="disabledDate"
    placeholder="选择日期范围"
    class="w-[320px]"
  />
</template>

API

DatePicker Props

参数说明类型默认值
modelValue (v-model)当前值Dayjs | nullnull
defaultValue默认值Dayjs | nullnull
placeholder占位文字string请选择日期
picker选择器类型date | week | month | quarter | yeardate
disabled是否禁用booleanfalse
allowClear是否支持清除booleantrue
size输入框大小small | medium | largemedium
variant形态变体(与 Input 一致)outlined | borderless | filled | underlinedoutlined
status校验状态error | warning-
bordered是否显示边框booleantrue
format日期格式string根据 picker 自动
showTime是否显示时间选择booleanfalse
minDate最小日期Dayjs | nullnull
maxDate最大日期Dayjs | nullnull
disabledDate禁用日期函数(current: Dayjs) => boolean-
presets预设选项DatePickerPreset[]-
side弹出位置top | bottombottom
sideOffset偏移量number4

DatePicker Events

事件说明回调参数
change日期变化回调(value: Dayjs | null, dateString: string)
openChange弹出层状态变化(open: boolean)

默认格式

pickerformat
dateYYYY-MM-DD
monthYYYY-MM
yearYYYY
weekYYYY 第 N 周
quarterYYYY 第 N 季度
date + showTimeYYYY-MM-DD HH:mm:ss

DatePickerPreset

参数说明类型
label预设标签string
value预设值Dayjs

DateRangePicker Props

参数说明类型默认值
modelValue (v-model)范围值[string | null, string | null] | nullnull
placeholder占位文字,可传 [开始占位, 结束占位]string | [string, string]开始日期 / 结束日期
picker选择器类型date | week | month | quarter | yeardate
disabled是否禁用booleanfalse
allowClear是否支持清除booleantrue
size输入框大小small | medium | largemedium
variant形态变体(与 Input 一致)outlined | borderless | filled | underlinedoutlined
status校验状态error | warning-
bordered是否显示边框booleantrue
format日期格式stringYYYY-MM-DD
showTime是否显示时间选择booleanfalse
minDate最小日期Dayjs | string | nullnull
maxDate最大日期Dayjs | string | nullnull
disabledDate禁用日期函数(current: Dayjs) => boolean-
presets范围快捷预设DatePickerRangePreset[]-
separator开始与结束分隔符string~
class自定义类名string-

DateRangePicker Events

事件说明回调参数
change范围变化回调(value: [string, string] | null, dateStrings: [string, string])
openChange弹出层状态变化(open: boolean)

DateRangePickerPreset

参数说明类型
label预设标签string
value预设值[Dayjs, Dayjs] | (() => [Dayjs, Dayjs])

CalendarPanel

日历面板组件,可独立使用,内置范围高亮能力。

import { CalendarPanel } from "@reglow/reui";
参数说明类型默认值
viewDate当前显示日期Dayjsdayjs()
selected选中的日期Dayjs | nullnull
picker面板类型date | month | yeardate
minDate最小日期Dayjs | nullnull
maxDate最大日期Dayjs | nullnull
disabledDate禁用日期函数(current: Dayjs) => boolean-
rangeStart范围开始日期Dayjs | nullnull
rangeEnd范围结束日期Dayjs | nullnull
hoverDate悬停预览日期Dayjs | nullnull
事件说明回调参数
select选择日期(date: Dayjs)
hover悬停日期(date: Dayjs | null)