Skip to content

TimePicker 时间选择

介绍

时间选择器,通常与弹出层组件配合使用。

引入

通过以下方式来引入组件。

js
import { createApp } from 'vue';
import { TimePicker } from '@szhn/dh-design-mobile';

const app = createApp();
app.use(TimePicker);

代码演示

基础用法

通过 v-model 绑定当前选中的时间。

html
<van-time-picker v-model="currentTime" title="选择时间" />
js
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref(['12', '00']);
    return { currentTime };
  },
};

选项类型

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

比如:

  • 传入 ['hour'] 来单独选择小时。
  • 传入 ['minute'] 来单独选择分钟。
  • 传入 ['minute', 'second'] 来选择分钟和秒。
  • 传入 ['hour', 'minute', 'second'] 来选择小时、分钟和秒。
html
<van-time-picker
  v-model="currentTime"
  title="选择时间"
  :columns-type="columnsType"
/>
js
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref(['12', '00', '00']);
    const columnsType = ['hour', 'minute', 'second'];
    return {
      currentTime,
      columnsType,
    };
  },
};

时间范围

你可以使用 min-hourmax-hour 等属性来限制小时(hour)范围、分钟(minute)范围和秒(second)范围。

比如以下示例,用户可以选择的小时是 10 ~ 20 ,分钟是 30 ~ 40

html
<van-time-picker
  v-model="currentTime"
  title="选择时间"
  :min-hour="10"
  :max-hour="20"
  :min-minute="30"
  :max-minute="40"
/>
js
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref(['12', '35']);
    return { currentTime };
  },
};

整体时间范围

你可以使用 min-timemax-time 属性来限制整体时间范围,约定格式 10:00:00

  • 设置 min-time 后,min-hourmin-minutemin-second 属性将不会生效。
  • 设置 max-time 后,max-hourmax-minutemax-second 属性将不会生效。

比如以下示例,用户可以选择从 09:40:1020:20:50 的任意时间。

html
<van-time-picker
  v-model="currentTime"
  title="选择时间"
  :columns-type="['hour', 'minute', 'second']"
  min-time="09:40:10"
  max-time="20:20:50"
/>
js
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref(['12', '00', '00']);
    return { currentTime };
  },
};

格式化选项

通过传入 formatter 函数,可以对选项的文字进行格式化。

html
<van-time-picker
  v-model="currentTime"
  title="选择时间"
  :formatter="formatter"
/>
js
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref(['12', '00']);
    const formatter = (type, option) => {
      if (type === 'hour') {
        option.text += '时';
      }
      if (type === 'minute') {
        option.text += '分';
      }
      return option;
    };

    return {
      formatter,
      currentTime,
    };
  },
};

过滤选项

通过传入 filter 函数,可以对选项数组进行过滤,剔除不需要的时间,实现自定义时间间隔。

html
<van-time-picker v-model="currentTime" title="选择时间" :filter="filter" />
js
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref(['12', '00']);
    const filter = (type, options) => {
      if (type === 'minute') {
        return options.filter((option) => Number(option.value) % 10 === 0);
      }
      return options;
    };

    return {
      filter,
      currentTime,
    };
  },
};

高级用法

filter 函数的第三个参数能获取到当前选择的时间,这在使用非受控模式时,可以更灵活地过滤掉不需要的时间。

html
<van-time-picker title="选择时间" :filter="filter" />
js
export default {
  setup() {
    const filter = (type, options, values) => {
      const hour = +values[0];

      if (type === 'hour') {
        return options.filter(
          (option) => Number(option.value) >= 8 && Number(option.value) <= 18,
        );
      }

      if (type === 'minute') {
        options = options.filter((option) => Number(option.value) % 10 === 0);

        if (hour === 8) {
          return options.filter((option) => Number(option.value) >= 40);
        }

        if (hour === 18) {
          return options.filter((option) => Number(option.value) <= 20);
        }
      }

      return options;
    };

    return {
      filter,
    };
  },
};

API

Props

参数说明类型默认值
v-model当前选中的时间string[]-
columns-type选项类型,由 hourminutesecond 组成的数组string[]['hour', 'minute']
min-hour可选的最小小时number | string0
max-hour可选的最大小时number | string23
min-minute可选的最小分钟number | string0
max-minute可选的最大分钟number | string59
min-second可选的最小秒数number | string0
max-second可选的最大秒数number | string59
min-time v4.5.0可选的最小时间,格式参考 07:40:00,使用时 min-hour min-minute min-second 不会生效string-
max-time v4.5.0可选的最大时间,格式参考 10:20:00,使用时 max-hour max-minute max-second 不会生效string-
title顶部栏标题string''
confirm-button-text确认按钮文字string确认
cancel-button-text取消按钮文字string取消
show-toolbar是否显示顶部栏booleantrue
loading是否显示加载状态booleanfalse
readonly是否为只读状态,只读状态下无法切换选项booleanfalse
filter选项过滤函数(type: string, options: PickerOption[], values: string[]) => PickerOption[]-
formatter选项格式化函数(type: string, option: PickerOption) => PickerOption-
option-height选项高度,支持 px vw vh rem 单位,默认 pxnumber | string44
visible-option-num可见的选项个数number | string6
swipe-duration快速滑动时惯性滚动的时长,单位 msnumber | string1000

Events

事件名说明回调参数
confirm点击完成按钮时触发{ selectedValues, selectedOptions, selectedIndexes }
cancel点击取消按钮时触发{ selectedValues, selectedOptions, selectedIndexes }
change选项改变时触发{ selectedValues, selectedOptions, selectedIndexes, columnIndex }

Slots

名称说明参数
toolbar自定义整个顶部栏的内容-
title自定义标题内容-
confirm自定义确认按钮内容-
cancel自定义取消按钮内容-
option自定义选项内容option: PickerOption, index: number
columns-top自定义选项上方内容-
columns-bottom自定义选项下方内容-

方法

通过 ref 可以获取到 Picker 实例并调用实例方法,详见组件实例方法

方法名说明参数返回值
confirm停止惯性滚动并触发 confirm 事件--
getSelectedTime获取当前选中的时间-string[]

类型定义

组件导出以下类型定义:

ts
import type { TimePickerProps, TimePickerColumnType } from '@szhn/dh-design-mobile';

TimePickerInstance 是组件实例的类型,用法如下:

ts
import { ref } from 'vue';
import type { TimePickerInstance } from '@szhn/dh-design-mobile';

const timePickerRef = ref<TimePickerInstance>();

timePickerRef.value?.confirm();

常见问题

在桌面端无法操作组件?

参见桌面端适配