在Vant的基礎(chǔ)上封裝下拉日期控件的代碼示例
需求分析
在實際項目中,表單里面的日期選擇是常用的組件。Vant有提供日期組件,但是居然沒有提供下拉形式的日期組件,不過該有的元件都有,就自己封裝一個。
封裝組件過程中我們要解決:
- 和表單的樣式能兼容
- 錯誤提示
- 參數(shù)問題
- 事件機制
- 格式化
解決問題
就給新的組件取名為 VantFieldDate。
期望使用的時候是這樣的
<vant-field-date label="發(fā)布時間" v-model="formData.publishDate" type="datetime" :max-date="new Date()" />
具體實現(xiàn),我貼上代碼詳細講解。
<template>
<div class="vant-field-date">
<van-cell
:title="label"
:class="{'readonly': readonly, 'placeholder' : text}"
:is-link="!readonly"
:required="required"
@click="show">
<!-- 顯示當(dāng)前值,沒有值顯示提示文字 -->
{{ text ? text : placeholder }}
<!-- 自定義錯誤顯示 -->
<div
v-if="$attrs.error"
v-text="$attrs['error-message']"
class="van-field__error-message"
/>
</van-cell>
<!-- 用 actionsheet 來包裹彈出層日期控件 -->
<van-actionsheet v-model="isShowPicker">
<!-- $attrs 可以把根節(jié)點的attr放到目標(biāo)組件上,如此可以像使用 DatePicker 組件一樣使用這個新組件 -->
<van-datetime-picker
v-bind="$attrs"
:type="type"
title="請選擇日期"
: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會報錯
},
// Cell 顯示的文字
label: {
type: String,
default: null
},
// 必填的星號
required: {
type: Boolean,
default: false
},
// 只讀狀態(tài)
readonly: {
type: Boolean,
default: false
},
// 占位提示文字
placeholder: {
type: String,
default: '請選擇'
},
// 展示的格式化
format: {
type: String,
default: null
}
},
data() {
return {
selectedItem: null,
isShowPicker: false
}
},
computed: {
// 展示的格式化,時間提交的值是Date類型數(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 值,第二個參數(shù)是毫秒數(shù),第三個參數(shù)是原始值,根據(jù)自己的項目的數(shù)據(jù)結(jié)構(gòu)來修改
// input 事件同時也會觸發(fā) vee-validate 的驗證事件
this.$emit('input', value.getTime(), value)
// onChange事件,雖然重寫 @input可以實現(xiàn),但這樣會破壞 v-model 寫法。
this.$emit('change', value.getTime(), value)
this.cancel()
},
// 隱藏彈框
cancel() {
this.isShowPicker = false
}
}
}
</script>
效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue.js中settimeout遇到的問題(時間參數(shù)短效果不穩(wěn)定)
這篇文章主要介紹了解決vue.js中settimeout遇到的問題(時間參數(shù)短效果不穩(wěn)定),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Unocss(原子化css)?使用及vue3?+?vite?+?ts講解
這篇文章主要介紹了Unocss(原子化css)使用vue3?+?vite?+?ts的方法,簡單介紹了Unocss使用及圖標(biāo)庫使用,通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-11-11
vue 詳情跳轉(zhuǎn)至列表頁實現(xiàn)列表頁緩存
這篇文章主要介紹了vue 詳情跳轉(zhuǎn)至列表頁實現(xiàn)列表頁緩存,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Element Notification通知的實現(xiàn)示例
這篇文章主要介紹了Element Notification通知的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue?parseHTML源碼解析hars?end?comment鉤子函數(shù)
這篇文章主要為大家介紹了vue?parseHTML源碼解析hars?end?comment鉤子函數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07

