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>
相关推荐
Chelsea05221 分钟前
PC浏览器在线调试 Android 浏览器教程-chrome://inspect/#devices
android·前端·chrome
加号34 分钟前
【C#】VS2022 传统 ASP.NET Web 服务(.asmx)接口实现指南
前端·c#·asp.net
Rain5098 分钟前
2.3. 安全配置:环境变量与 API 密钥管理
前端·人工智能·后端·安全·ai·node.js·ai编程
用户938515635078 分钟前
HTML5 Canvas 从入门到AI驱动游戏开发:手把手教你用原生JS打造飞机游戏与数据可视化
前端·javascript·人工智能
William_Xu10 分钟前
var [a, b] = { a: 1, b: 2 } 解构赋值
前端
用户0595401744611 分钟前
Playwright 网络拦截踩坑实录:我花了 3 小时才搞懂数据持久化验证的正确姿势
前端·css
weedsfly11 分钟前
React 开发中的闭包陷阱:四个真实场景,让你彻底理解闭包
前端·react.js
MariaH12 分钟前
Git Cherry Pick 常用操作
前端
初圣魔门首席弟子18 分钟前
AI Agent 核心原理:工具调用(Function Calling)完整工作流程详解
前端·数据库·人工智能
CodeSheep23 分钟前
又是梁文锋,有点猛啊。
前端·后端·程序员