Skip to content

H5预览

APP下载

SelectDate 日期选择器

一个功能强大的日期时间选择器组件,支持多种粒度的日期时间选择,提供灵活的配置选项和自定义样式。

基础参数

参数说明类型可选值默认值
pt样式穿透配置PassThrough--
modelValue双向绑定的日期值string--
title选择器弹窗顶部标题string-"请选择"
placeholder输入框占位符文本string-"请选择"
headers选择器列表头部标题数组string[]-['年', '月', '日', '时', '分', '秒']
showTrigger是否显示默认的选择器触发元素booleantrue / falsetrue
disabled是否禁用选择器,禁用后无法操作booleantrue / falsefalse
type选择器类型,控制日期选择的精度string"year" | "month" | "date" | "hour" | "minute" | "second""second"
confirmText确认按钮显示文本string"确定"
showConfirm是否显示确认按钮booleantrue
cancelText取消按钮显示文本string"取消"
showCancel是否显示取消按钮booleantrue
labelFormat选中值在触发器中的显示格式string""
valueFormat输出值的格式化规则string""
start可选择的最早日期时间string"1950-01-01 00:00:00"
end可选择的最晚日期时间string"2050-12-31 23:59:59"
rangeable是否范围选择booleanfalse
startPlaceholder开始日期占位符string"开始日期"
endPlaceholder结束日期占位符string"结束日期"
rangeSeparator范围分隔符string"至"
showShortcuts是否显示快捷选项booleantrue
shortcuts快捷选项ClSelectDateShortcut[][]

事件

事件名称说明回调参数
update:modelValue范围选择完成后更新绑定值 modelValuevalue: string
change日期选择完成后触发的事件value: string
update:values范围选择完成后更新绑定值 valuesvalue: string[]
range-change范围选择完成后触发的事件value: string[]

方法

方法名说明参数
open打开弹窗callback: (value: any) => void
close关闭弹窗
clear清空
setValue设置值value: string
setValues设置范围值value: string[]
setRange设置范围索引index: number
confirm确认

PassThrough

样式穿透配置对象,用于深度自定义组件内部元素的样式。

参数说明类型
trigger选择器触发元素样式配置ClSelectTriggerPassThrough
popup弹窗容器样式配置ClPopupPassThrough

示例

基础用法

最简单的日期选择器,默认精确到秒级别。

html
<template>
  <cl-select-date v-model="selectedDate"></cl-select-date>
</template>

<script setup>
  import { ref } from "vue";

  const selectedDate = ref("");
</script>

不同精度选择

根据业务需求选择不同的时间精度。

选择年份

html
<cl-select-date v-model="year" type="year"></cl-select-date>

选择年月

html
<cl-select-date v-model="month" type="month"></cl-select-date>

选择年月日

html
<cl-select-date v-model="date" type="date"></cl-select-date>

选择精确时间

html
<cl-select-date
  v-model="dateTime"
  type="second"
  title="选择时间"
  placeholder="请选择日期时间"
></cl-select-date>

格式化显示

自定义日期的显示和输出格式。

html
<cl-select-date
  v-model="formattedDate"
  type="date"
  label-format="YYYY年MM月DD日"
  value-format="YYYY-MM-DD"
  title="选择日期"
></cl-select-date>

国际化支持

自定义表头文本,支持多语言场景。

html
<cl-select-date
  v-model="internationalDate"
  :headers="['Year', 'Month', 'Date', 'Hour', 'Minute', 'Second']"
  confirm-text="Confirm"
  cancel-text="Cancel"
  title="Select Date"
></cl-select-date>

限制时间范围

设置可选择的时间范围,适用于预约、活动等场景。

html
<cl-select-date
  v-model="appointmentDate"
  type="date"
  start="2025-01-01 00:00:00"
  end="2025-12-31 23:59:59"
  title="选择预约日期"
  placeholder="请选择预约日期"
></cl-select-date>

自定义触发器

隐藏默认触发器,使用自定义的触发元素。

html
<template>
  <cl-select-date
    ref="selectDateRef"
    v-model="customDate"
    :show-trigger="false"
    type="date"
  ></cl-select-date>

  <button @click="openDatePicker">点击选择日期</button>
</template>

<script setup lang="ts">
  import { ref } from "vue";

  const selectDateRef = ref<ClSelectDateComponentPublicInstance | null>(null);
  const customDate = ref("");

  function openDatePicker() {
    selectDateRef.value!.open((value: string) => {
      console.log("选择的日期:", value);
    });
  }
</script>

禁用状态

html
<cl-select-date
  v-model="disabledDate"
  disabled
  placeholder="此选择器已禁用"
></cl-select-date>

范围选择

  • 绑定值是 v-model:values,并且是 string[] 类型
  • 添加 rangeable
html
<cl-select-date v-model:values="values" rangeable></cl-select-date>

自定义快捷选项

  • 添加参数 shortcuts,并且是 ClSelectDateShortcut[] 类型
vue
<cl-select-date
  v-model:values="values"
  rangeable
  :shortcuts="shortcuts"
></cl-select-date>

<script setup lang="ts">
import { ref } from "vue";
import { type ClSelectDateShortcut } from "@/uni_modules/cool-ui";
import { dayUts } from "@/cool";

const shortcuts = ref<ClSelectDateShortcut[]>([
  {
    label: "昨日",
    value: [
      dayUts().subtract(1, "day").format("YYYY-MM-DD"),
      dayUts().format("YYYY-MM-DD"),
    ],
  },
  {
    label: "本周",
    value: [
      dayUts().startOf("week").format("YYYY-MM-DD"),
      dayUts().endOf("week").format("YYYY-MM-DD"),
    ],
  },
  {
    label: "本月",
    value: [
      dayUts().startOf("month").format("YYYY-MM-DD"),
      dayUts().endOf("month").format("YYYY-MM-DD"),
    ],
  },
]);
</script>

注意事项

  • 时间范围startend 参数必须是有效的日期时间字符串
  • 类型限制:选择的 type 会影响 headers 数组的使用长度
  • 双向绑定v-model 绑定的值会根据 valueFormat 进行格式化输出