在Vant的基礎(chǔ)上封裝下拉日期控件的代碼示例
需求分析
在實(shí)際項(xiàng)目中,表單里面的日期選擇是常用的組件。Vant有提供日期組件,但是居然沒(méi)有提供下拉形式的日期組件,不過(guò)該有的元件都有,就自己封裝一個(gè)。
封裝組件過(guò)程中我們要解決:
- 和表單的樣式能兼容
- 錯(cuò)誤提示
- 參數(shù)問(wèn)題
- 事件機(jī)制
- 格式化
解決問(wèn)題
就給新的組件取名為 VantFieldDate
。
期望使用的時(shí)候是這樣的
<vant-field-date label="發(fā)布時(shí)間" v-model="formData.publishDate" type="datetime" :max-date="new Date()" />
具體實(shí)現(xiàn),我貼上代碼詳細(xì)講解。
<template> <div class="vant-field-date"> <van-cell :title="label" :class="{'readonly': readonly, 'placeholder' : text}" :is-link="!readonly" :required="required" @click="show"> <!-- 顯示當(dāng)前值,沒(méi)有值顯示提示文字 --> {{ text ? text : placeholder }} <!-- 自定義錯(cuò)誤顯示 --> <div v-if="$attrs.error" v-text="$attrs['error-message']" class="van-field__error-message" /> </van-cell> <!-- 用 actionsheet 來(lái)包裹彈出層日期控件 --> <van-actionsheet v-model="isShowPicker"> <!-- $attrs 可以把根節(jié)點(diǎn)的attr放到目標(biāo)組件上,如此可以像使用 DatePicker 組件一樣使用這個(gè)新組件 --> <van-datetime-picker v-bind="$attrs" :type="type" title="請(qǐng)選擇日期" :min-date="minDate" :max-date="maxDate" @cancel="cancel" @confirm="confirm" /> </van-actionsheet> </div> </template> <script> export default { name: 'VantFieldDate', inheritAttrs: false, // https://cn.vuejs.org/v2/api/#inheritAttrs props: { value: { type: [Number, Date], default: undefined // 值不能是 null,DatePicker會(huì)報(bào)錯(cuò) }, // Cell 顯示的文字 label: { type: String, default: null }, // 必填的星號(hào) required: { type: Boolean, default: false }, // 只讀狀態(tài) readonly: { type: Boolean, default: false }, // 占位提示文字 placeholder: { type: String, default: '請(qǐng)選擇' }, // 展示的格式化 format: { type: String, default: null } }, data() { return { selectedItem: null, isShowPicker: false } }, computed: { // 展示的格式化,時(shí)間提交的值是Date類(lèi)型數(shù)據(jù) formatFormula() { if(this.format){ return this.format } else if (this.type === 'date') { return 'yyyy-MM-dd' } else if (this.type === 'datetime') { return 'yyyy-MM-dd hh:mm' } else if (this.type === 'time') { return 'hh:mm' } else if (this.type === 'year-month') { return 'yyyy-MM' } }, text() { return this.value ? this.dateFormat(this.value, this.formatFormula) : '' } }, methods: { dateFormat: (value, format) => { if (!value) return if (!(value instanceof Date)) { value = new Date(value) } let o = { 'M+': value.getMonth() + 1, // month 'd+': value.getDate(), // day 'h+': value.getHours(), // hour 'm+': value.getMinutes(), // minute 's+': value.getSeconds(), // second 'q+': Math.floor((value.getMonth() + 3) / 3), // quarter 'S': value.getMilliseconds() // millisecond } if (!format || format === '') { format = 'yyyy-MM-dd hh:mm:ss' } if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (value.getFullYear() + '').substr(4 - RegExp.$1.length)) } for (let k in o) { if (new RegExp('(' + k + ')').test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)) } } return format }, show() { if (!this.readonly) { this.isShowPicker = true } }, confirm(value) { // 更新 v-model 綁定的 value 值,第二個(gè)參數(shù)是毫秒數(shù),第三個(gè)參數(shù)是原始值,根據(jù)自己的項(xiàng)目的數(shù)據(jù)結(jié)構(gòu)來(lái)修改 // input 事件同時(shí)也會(huì)觸發(fā) vee-validate 的驗(yàn)證事件 this.$emit('input', value.getTime(), value) // onChange事件,雖然重寫(xiě) @input可以實(shí)現(xiàn),但這樣會(huì)破壞 v-model 寫(xiě)法。 this.$emit('change', value.getTime(), value) this.cancel() }, // 隱藏彈框 cancel() { this.isShowPicker = false } } } </script>
效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定)
這篇文章主要介紹了解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Unocss(原子化css)?使用及vue3?+?vite?+?ts講解
這篇文章主要介紹了Unocss(原子化css)使用vue3?+?vite?+?ts的方法,簡(jiǎn)單介紹了Unocss使用及圖標(biāo)庫(kù)使用,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11vue實(shí)現(xiàn)滾動(dòng)鼠標(biāo)滾輪切換頁(yè)面
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滾動(dòng)鼠標(biāo)滾輪切換頁(yè)面,類(lèi)似于縱向走馬燈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11教你使用vue3寫(xiě)Json-Preview的示例代碼
這篇文章主要介紹了用vue3寫(xiě)了一個(gè)Json-Preview的相關(guān)知識(shí),引入后直接<json-preview?v-model="jsonData"></json-preview>就可以使用了,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06vue 詳情跳轉(zhuǎn)至列表頁(yè)實(shí)現(xiàn)列表頁(yè)緩存
這篇文章主要介紹了vue 詳情跳轉(zhuǎn)至列表頁(yè)實(shí)現(xiàn)列表頁(yè)緩存,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Vue實(shí)現(xiàn)自動(dòng)檢測(cè)以及版本的更新
當(dāng)用戶(hù)在當(dāng)前站點(diǎn)停留時(shí)間比較長(zhǎng),中途站點(diǎn)進(jìn)行升級(jí)更新之后,用戶(hù)如果不刷新頁(yè)面就任然停留在舊的頁(yè)面里,如何讓用戶(hù)收到一個(gè)提示,引導(dǎo)用戶(hù)進(jìn)行更新操作呢?下面給大家介紹如何站點(diǎn)更新如何在生產(chǎn)環(huán)境提示用戶(hù)更新,進(jìn)行頁(yè)面刷新操作,核心原理其實(shí)很簡(jiǎn)單2023-03-03Element Notification通知的實(shí)現(xiàn)示例
這篇文章主要介紹了Element Notification通知的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vue?parseHTML源碼解析hars?end?comment鉤子函數(shù)
這篇文章主要為大家介紹了vue?parseHTML源碼解析hars?end?comment鉤子函數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07