Skip to content

PickerDate 日期选择器

介绍

日期选择器,用于选择年、月、日。

基础用法

通过 v-model 绑定当前选中日期,设置空数组时默认选中 今天

html
<vd-picker-date v-model="date" title="基础用法" />
ts
const date = ref([]);

自定义列类型

通过 columns-type 属性来控制选项的类型,支持以任意顺序对 yearmonthday 进行排列组合。

html
<vd-picker-date
  v-model="date"
  title="自定义列"
  :columns-type="columnsType"
/>
ts
import type { PickerDateColumnsType } from 'vital-design';

const date = ref([]);

const columnsType: PickerDateColumnsType = [
  'year',
  'month',
];

限制日期范围

通过 min-datemax-date 属性来设定可选的时间范围,支持 dayjs 日期。

html
<vd-picker-date
  v-model="date"
  title="限制日期范围"
  :min-date="limit.min"
  :max-date="limit.max"
/>
ts
import dayjs from 'dayjs';

const date = ref([]);

// 字符日期
const limit = reactive({
  min: '2025-01-01',
  max: '2026-01-01',
});

// dayjs日期
const limit2 = reactive({
  min: dayjs().subtract(1, 'month'),
  max: dayjs().add(1 ,'month'),
});

格式化标签

通过 formatter 方法来格式化标签。

html
<vd-picker-date
  v-model="date"
  title="格式化标签"
  :formatter="dateOptionFormatter"
/>
ts
import type { PickerDateFormatter } from 'vital-design';

const date = ref([]);

const dateOptionFormatter: PickerDateFormatter = (type, value) => {
  if (type === 'year') {
    return 'Y' + value;
  }

  if (type === 'month') {
    return 'M' + value;
  }

  if (type === 'day') {
    return 'D' + value;
  }

  return '';
}

过滤日期选项

通过 filter 方法来过滤日期选项。

html
<vd-picker-date
  v-model="date"
  title="过滤日期选项"
  :filter="dateOptionFilter"
/>
ts
import type { PickerDateFilter } from 'vital-design';

const date = ref([]);

const dateOptionFilter: PickerDateFilter = (type, options, values) => {
  if (type === 'year') {
    return options.filter(({ value }) => ['2024', '2025'].includes(value));
  }

  if (type === 'month') {
    return options.filter(({ value }) => ['01', '03'].includes(value));
  }

  if (type === 'day') {
    return options.filter(({ value }) => ['01', '15'].includes(value));
  }

  return options;
}

API

Props

参数说明类型默认值
v-model选中值(string | number)[]-
min-date最小日期string | number | Date | dayjs.Dayjs十年前
max-date最大日期string | number | Date | dayjs.Dayjs十年后
columns-type选项类型'year' | 'month' | 'day'['year', 'month', 'day']
filter过滤选项方法PickerDateFilter-
formatter格式化标签方法PickerDateFormatter-
title标题string-
confirm-button-text确认按钮文字string-
cancel-button-text取消按钮文字string-
immediate-change是否在手指松开时立即触发 change 事件booleantrue

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Events

事件名说明回调参数
click点击时触发MouseEvent
change改变时触发PickerDateSelectedChangeData
updated更新时触发PickerDateSelectedData
confirm确认时触发PickerDateSelectedData
cancel取消时触发PickerDateSelectedData

Slots

名称说明
toolbar工具栏

赣ICP备2025061025号-1