vant4 封裝日期段選擇器的實現(xiàn)
更新時間:2023年03月01日 11:10:30 作者:請叫我彭彭
本文主要介紹了vant4 封裝日期段選擇器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
前言
偶然間在群里看到一個小伙伴的需求,需要使用vant 封裝時間段選擇器,看到這個需求后,自己也想實現(xiàn)一下,說干就干!倉庫地址
TimeRangePickerTypes.ts
import { ExtractPropTypes, PropType } from 'vue' import dayjs from 'dayjs' export const props = { /** 窗口是否顯示 */ visible: { type: Boolean, default: false }, /** 時間段,[HH:mm,HH:mm] */ times: { type: Array as PropType<string[]>, default: () => [dayjs().format('HH-mm'), dayjs().format('HH-mm')] }, /** 中間分隔符 */ apart: { type: String, default: '~' }, /** 最大時間 */ maxTime: { type: Number, default: 23 }, /** 最小時間 */ minTime: { type: Number, default: 1 }, /** 最大分鐘數(shù) */ maxMinute: { type: Number, default: 59 }, /** 最小分鐘數(shù) */ minMinute: { type: Number, default: 1 } } export type Props = ExtractPropTypes<typeof props> export interface timeResult { /** 開始時間 */ startTime: string /** 開始分鐘 */ startMinute: string /** 結束時間 */ endTime: string /** 結束分鐘 */ endMinute: string }
TimeRangePicker.vue
<script lang="ts" setup> import { ref, unref, watchEffect } from 'vue' import { Popup, Picker } from 'vant' import { props as TimeRangePickerProps } from './types' import { useColumns } from './composable/useColumns' const props = defineProps(TimeRangePickerProps) interface Emits { /* 顯示窗口 */ (e: 'update:visible', value: boolean): void /* 更新時間段 */ (e: 'update:times', value: string[]): void /** 確認事件 */ (e: 'confirm'): void } const emits = defineEmits<Emits>() /** 選擇的值 */ const selectedValues = ref<string[]>([]) /** 窗口是否顯示 */ const popupVisible = ref(false) watchEffect(() => { popupVisible.value = props.visible if (props.times.length !== 2) throw new Error('時間格式錯誤') /** 開始時間 分秒 */ const startTimes = props.times[0].split(':') /** 結束時間 分秒 */ const endTimes = props.times[1].split(':') if (startTimes.length !== 2) throw new Error('開始時間格式錯誤') else if (endTimes.length !== 2) throw new Error('結束時間錯誤') selectedValues.value = [startTimes[0], startTimes[1], props.apart, endTimes[0], endTimes[1]] }) const { columns } = useColumns(props) /** 選擇時間段取消事件 */ const onPopupClose = () => { emits('update:visible', false) } /** 確定按鈕單擊事件 */ const onConfirm = () => { const onValue = unref(selectedValues.value).filter((item) => item !== props.apart) emits('update:times', [`${onValue[0]}:${onValue[1]}`, `${onValue[2]}:${onValue[3]}`]) emits('confirm') onPopupClose() } </script> <template> <Popup v-model:show="popupVisible" position="bottom" @close="onPopupClose"> <Picker v-bind="$attrs" v-model="selectedValues" :columns="columns" @confirm="onConfirm" @cancel="onPopupClose" /> </Popup> </template>
useColumns.ts
import { computed, ref } from 'vue' import { PickerOption } from 'vant' import { Props } from '../types' export function useColumns(props: Props) { /** 時段 */ const times = computed(() => { const result: PickerOption[] = [] for (let i = props.minTime; i <= props.maxTime; i++) { const v = `${i}`.padStart(2, '0') result.push({ text: v, value: v }) } return result }) /** 分鐘 */ const minutes = computed(() => { const result: PickerOption[] = [] for (let i = props.minMinute; i <= props.maxMinute; i++) { const v = `${i}`.padStart(2, '0') result.push({ text: v, value: v }) } return result }) /** 顯示列 */ const columns = ref<PickerOption[][]>([ times.value, minutes.value, [{ text: props.apart, value: props.apart }], times.value, minutes.value ]) return { columns } }
使用
<script setup lang="ts"> import { ref } from 'vue' import { TimeRangePicker } from './components' const visible = ref(false) const times = ref(['10:10', '12:10']) const onConfirm = () => { console.log('選擇的時間是', times.value) } </script> <template> <div> <van-button type="primary" @click="visible = true">選擇日期</van-button> <time-range-picker v-model:visible="visible" v-model:times="times" :max-time="23" @confirm="onConfirm" ></time-range-picker> </div> </template>
效果
到此這篇關于vant4 封裝日期段選擇器的實現(xiàn)的文章就介紹到這了,更多相關vant4 日期段選擇器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue前端項目打包成Docker鏡像并運行的實現(xiàn)
這篇文章主要介紹了vue前端項目打包成Docker鏡像并運行的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue拖拽組件列表實現(xiàn)動態(tài)頁面配置功能
這篇文章主要介紹了Vue拖拽組件列表實現(xiàn)動態(tài)頁面配置功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06Vue使用Three.js創(chuàng)建交互式3D場景的全過程
在現(xiàn)代Web開發(fā)中,通過在頁面中嵌入3D場景,可以為用戶提供更加豐富和交互性的體驗,Three.js是一款強大的3D JavaScript庫,它簡化了在瀏覽器中創(chuàng)建復雜3D場景的過程,本文將介紹如何在Vue中使用Three.js,創(chuàng)建一個簡單的交互式3D場景,需要的朋友可以參考下2023-11-11Vue實現(xiàn)電商網(wǎng)站商品放大鏡效果示例
這篇文章主要為大家介紹了Vue實現(xiàn)電商網(wǎng)站商品放大鏡效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Vue2中如何使用全局事件總線實現(xiàn)任意組件間通信
全局事件總線就是一種組件間通信的方式,適用于任意組件間通信,下面這篇文章主要給大家介紹了關于Vue2中如何使用全局事件總線實現(xiàn)任意組件間通信的相關資料,需要的朋友可以參考下2022-12-12