欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在Vant的基础上封装下拉日期控件的代码示例

程序员文章站 2023-10-31 16:04:10
需求分析 在实际项目中,表单里面的日期选择是常用的组件。vant有提供日期组件,但是居然没有提供下拉形式的日期组件,不过该有的元件都有,就自己封装一个。 封装组件过程中...

需求分析

在实际项目中,表单里面的日期选择是常用的组件。vant有提供日期组件,但是居然没有提供下拉形式的日期组件,不过该有的元件都有,就自己封装一个。

封装组件过程中我们要解决:

  • 和表单的样式能兼容
  • 错误提示
  • 参数问题
  • 事件机制
  • 格式化

解决问题

就给新的组件取名为 vantfielddate

期望使用的时候是这样的

<vant-field-date
 label="发布时间"
 v-model="formdata.publishdate"
 type="datetime"
 :max-date="new date()"
/>

具体实现,我贴上代码详细讲解。

<template>
 <div class="vant-field-date">
  <van-cell
   :title="label"
   :class="{'readonly': readonly, 'placeholder' : text}"
   :is-link="!readonly"
   :required="required"
   @click="show">
   <!-- 显示当前值,没有值显示提示文字 -->
   {{ 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 可以把根节点的attr放到目标组件上,如此可以像使用 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
   },
   // 只读状态
   readonly: {
    type: boolean,
    default: false
   },
   // 占位提示文字
   placeholder: {
    type: string,
    default: '请选择'
   },
   // 展示的格式化
   format: {
    type: string,
    default: null
   }
  },
  data() {
   return {
    selecteditem: null,
    isshowpicker: false
   }
  },
  computed: {
   // 展示的格式化,时间提交的值是date类型数据
   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 值,第二个参数是毫秒数,第三个参数是原始值,根据自己的项目的数据结构来修改
    // input 事件同时也会触发 vee-validate 的验证事件
    this.$emit('input', value.gettime(), value)
    // onchange事件,虽然重写 @input可以实现,但这样会破坏 v-model 写法。
    this.$emit('change', value.gettime(), value)
    this.cancel()
   },
   // 隐藏弹框
   cancel() {
    this.isshowpicker = false
   }
  }
 }
</script>

效果

在Vant的基础上封装下拉日期控件的代码示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。