vue3+naiveUI二次封装的v-model 联动输入框

根据官网说明使用

请添加图片描述

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<template>
<div class="clw-input pt-3">
<n-input
ref="input"
:value="modelValue"
:type="type"
:title="title"
clearable
:disabled="disabled"
:size="size"
placeholder=""
@focus="$emit('focus')"
@blur="blur"
@input="input"
@change="change"
/>
<label class="z-1 text-warmgray" :style="style">{{ placeholder }}</label>
</div>
</template>

<script>
/**
* @author 全易
* @time 2022-10-11 13:08:46 星期二
* @description 自定义输入框组件
**/

export default {
name: 'ClwInput',
props: {
placeholder: {
type: String,
default: '',
},
// 父组件v-model绑定的值
modelValue: {
type: String,
default: '',
},
size: {
type: String,
default: 'medium',
},
disabled: {
type: Boolean,
},
labelBG: {
type: String,
default: '#ffffff',
},
labelColor: {
type: String,
default: '#19aa8d',
},
type: {
type: String,
default: '',
},
},
emits: ['focus', 'blur', 'change', 'update:modelValue'],
data() {
return {
inputHeight: null,
style: {},
}
},
computed: {
title() {
return `${this.placeholder}${this.modelValue}`
},
},
watch: {
modelValue: {
deep: true,
handler() {
this.resetStyle()
},
},
},
mounted() {
this.inputHeight = this.$refs.input.$el.offsetHeight
this.resetStyle()
},
methods: {
blur() {
this.resetStyle()
this.$emit('blur')
},
input(val) {
this.resetStyle()
this.$emit('update:modelValue', val)
},
change(val) {
this.$emit('change', val)
},
resetStyle() {
if (this.modelValue) {
this.style = {
transform: `translateY(${-(this.inputHeight / 2)}px)`,
color: `${this.labelColor} !important`,
'background-color': this.labelBG,
padding: '0 10px',
}
} else {
this.style = {
bottom: `${this.inputHeight * 0.18}px`,
}
}
},
},
}
</script>

<style lang="scss" scoped>
.clw-input {
position: relative;

label {
position: absolute;
left: 15px;
pointer-events: none;
transition: all 0.3s ease;
}
}
</style>

使用

1
2
3
4
5
<clw-input
v-model="queryForm.propertyNo"
placeholder="物业编号"
@keydown.enter="getData"
></clw-input>

vue3+naiveUI二次封装的v-model 联动输入框
https://github.com/chergn/chergn.github.io/5537d7051ddf/
作者
全易
发布于
2024年3月28日
许可协议