Vue 自定义仿word表单录入之日期输入组件

因项目需要,要实现仿word方式录入数据,要实现鼠标经过时才显示编辑组件,预览及离开后则显示具体的文字。

鼠标经过时显示

正常显示及离开时显示

组件代码

javascript 复制代码
<template >
    <div class="paper-input flex flex-col border-box "   >
        <div  class="font-l border-box text margin-left-m text" style="font-family: FangSong;">{{text}}</div>
        <div class="flex flex-col  input hidden">
            <input type="text" v-model="value1" maxlength="4" ref="date1"   @input="changeToNext1(value1)"/>
            年
            <input type="text" v-model="value2" maxlength="2" ref="date2"  @input="changeToNext2(value2)"/>
            月
            <input type="text" v-model="value3" maxlength="2" ref="date3"  @input="changeToNext3(value3)"/>
            日
        </div>
    </div>
</template>

<script>
    export default{
        name:'PagerInput',
        data() {
            return {
                val:"",
                text:"年  月  日",
                value1:"",
                value2:"",
                value3:"",
            }
        },
        props: {
            value:{}
        },
        model: {
            prop: "value",
            event: "change"
        },
        watch:{
            value:{
                handler(newValue) {
                    if (newValue != null && newValue.length >0) {
                        let arr = newValue.split("-")
                        if ( arr.length === 3 ) {
                            this.value1 = arr[0]
                            this.value2 = arr[1]
                            this.value3 = arr[2]
                            this.setText();
                        }
                    }

                    this.val =  newValue;
                    
                },
                immediate: true,
                deep: true   //深度监听                
            },
        },
        methods:{
            changeToNext1(v) {
                if (v.toString().length === 4) {
                    this.$nextTick(() => {
                    this.$refs.date2.focus();
                    });
                }
                this.setText();
                this.setValue();
            },
            changeToNext2(v) {
                if (v.toString().length === 2) {
                    this.$nextTick(() => {
                    this.$refs.date3.focus();
                    });
                }
                this.setText();
                this.setValue();
            },
            changeToNext3(v) {
                this.setText();
                this.setValue();
            },
            setValue() {
                this.$emit("change", this.value1 + "-" + this.value2 + "-" + this.value3 );
            },
            setText() {
                this.text = this.value1 + "年" + this.value2 + "月" + this.value3 + "日"
            }

        }
    }
</script>

<style scoped>

    .paper-input:hover .text   {
        display: none;
    }
 
    .paper-input:hover .textplus   {
        display: none;
    }
    .paper-input:hover .input   {
        display: block;
    }
    .paper-input input {
        width: 50px;
    }

    


</style>

引用组件,支持数据双向绑定

javascript 复制代码
 <PaperDateInput v-model="paperData.protocolTime" ></PaperDateInput>
相关推荐
宋辰月3 分钟前
学习react第三天
前端·学习·react.js
bug总结6 分钟前
更新原生小程序封装(新增缓存订阅)完美解决
前端·缓存·小程序
5335ld31 分钟前
后端给的post 方法但是要求传表单数据格式(没有{})
开发语言·前端·javascript·vue.js·ecmascript
二川bro36 分钟前
第33节:程序化生成与无限地形算法
前端·算法·3d·threejs
QDKuz42 分钟前
掌握Vue2转Vue3, Options API 转 Composition API
前端·javascript·vue.js
老前端的功夫1 小时前
前端Echarts性能优化:从卡顿到流畅的百万级数据可视化
前端·javascript
进击的野人1 小时前
深入解析localStorage:前端数据持久化的核心技术
前端·javascript
懵圈1 小时前
第2章:项目启动 - 使用Vite脚手架初始化项目与工程化配置
前端
Mh1 小时前
如何优雅的消除“if...else...”
前端·javascript
火鸟21 小时前
给予虚拟成像台尝鲜版十之二,完善支持 HTML 原型模式
前端·html·原型模式·通用代码生成器·给予虚拟成像台·快速原型·rust语言