一、vue 实现 ui 组件库之 Input 组件
- 在项目的
文件夹中,建立一个components
文件,在input.vue
中,定义template
组件,通过input
控制按钮是否禁止的样式,通过disabled
控制输入框中的默认值。通过placeholder
控制密码框是否隐藏和显示,如果父组件传了type
就判断是否需要切换,如果不传就不判断。通过show-password
判断name
输入框的名字,通过input
判断value
输入框的值,通过input
绑定输入事件@input
。需要注意的是,handleInput
和value
是为了实现@input
双向数据绑定的功能。通过v-model
判断v-if
,是否有图标。如果是有清空图标,绑定showSuffix
清空事件。如果是有显示密码的图标,绑定clear
显示密码事件,代码如下所示:handlePassword
<template>
<div class="jc-input" :class="{'jc-input--suffix': showSuffix}">
<input class="jc-input__inner"
:class="{'is-disabled': disabled}"
:placeholder="placeholder"
:type="showPassword ? (passwordVisible ? 'text' : 'password') : type"
:name="name"
:disabled="disabled"
:value="value"
@input="handleInput"
>
<span class="jc-input__suffix" v-if="showSuffix">
<i class="jc-input__icon jc-icon-circle-close" v-if="clearable && value" @click="clear"></i>
<i class="jc-input__icon jc-icon-view" v-if="showPassword" @click="handlePassword" :class="{'jc-icon-view-active': passwordVisible }"></i>
</span>
</div>
</template>
- 在
中,定义script
的值为name
,定义JcInput
中的数据data
为passwordVisible
,false
是用于控制是否显示密码框。在passwordVisible
中,定义props
的类型为placeholder
,默认值为空;定义String
的类型为type
,默认值为String
;定义text
的类型为name
,默认值为空;定义String
的类型为disabled
,默认值为Boolean
;定义false
的类型为value
,默认值为空;定义String
的类型为clearable
,默认值为Boolean
;定义false
的类型为showPassword
,默认值为Boolean
。在false
中,定义methods
输入框输入事件,定义handleInput
输入框清空事件,定义clear
输入框显示密码事件。在handlePassword
计算属性中,计算computed
是否显示清空或者是显示密码按钮,代码如下所示:showSuffix
<script>
export default {
name: 'JcInput',
data () {
return {
passwordVisible: false
}
},
props: {
placeholder: {
type: String,
default: ''
},
type: {
type: String,
default: 'text'
},
name: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
},
value: {
type: String,
default: ''
},
clearable: {
type: Boolean,
default: false
},
showPassword: {
type: Boolean,
default: false
}
},
methods: {
handleInput (e) {
this.$emit('input', e.target.value)
},
clear () {
this.$emit('input', '')
},
handlePassword () {
this.passwordVisible = !this.passwordVisible
}
},
computed: {
showSuffix () {
return this.clearable || this.showPassword
}
}
}
</script>
- 在
中,定义style
输入框中的一些样式,代码如下所示:input
<style lang="scss" scoped>
.jc-input {
width: 100%;
position: relative;
font-size: 14px;
display: inline-block;
.jc-input__inner {
-webkit-appearance: none;
background-color: #fff;
background-image: none;
border-radius: 4px;
border: 1px solid #dcdfe6;
box-sizing: border-box;
color: #606266;
display: inline-block;
font-size: inherit;
height: 40px;
line-height: 40px;
outline: none;
padding: 0 15px;
transition: border-color .2s cubic-bezier(.645,.045,.355,1);
width: 100%;
&:focus {
outline: none;
border-color: #409eff;
}
&.is-disabled {
background-color: #f5f7fa;
border-color: #e4e7ed;
color: #c0c4cc;
cursor: not-allowed;
}
}
}
.jc-input--suffix {
.jc-input__inner {
padding-right: 30px;
}
.jc-input__suffix {
position: absolute;
height: 100%;
right: 10px;
top: 0;
line-height: 40px;
text-align: center;
color: #c0c4cc;
transition: all .3s;
z-index: 900;
i {
color: #c0c4cc;
font-size: 14px;
cursor: pointer;
transition: color .2s cubic-bezier(.645,.045,.355,1);
}
}
}
.jc-icon-view-active {
color: #409eff !important;
}
</style>
- 这个
按钮组件的参数支持,如下所示:Input
参数名称 | 参数描述 | 参数类型 | 默认值 |
---|---|---|---|
placeholder | 占位符 | string | 无 |
type | 文本框类型(text/password) | string | text |
disabled | 禁用 | boolean | false |
clearable | 是否显示清空按钮 | boolean | false |
show-password | 是否显示密码切换按钮 | boolean | false |
name | name属性 | string | 无 |
- 在
中,引入main.js
组件,并且全局注册组件,代码如下所示:JcInput
import JcInput from './components/input.vue'
Vue.component(JcInput.name, JcInput)
- 在
中,使用App.vue
组件,使用jc-input
定义输入框的默认值,使用placeholder
定义输入框的类型,使用type
定义输入框的名字,使用name
实现输入框的双向数据绑定,使用v-model
实现清空输入框,使用clearable
展示输入框的值,使用show-password
禁止输入框输入。需要特别注意的是, 给组件使用disabled
指令,实质上相当于给组件传递了v-model
属性以及监听了value
事件,也就是等价于input
,在<input type="text" :value="username" @input="username=$event.target.value">
中定义data
的值,代码如下所示:username
<template>
<div id="app">
<jc-input placeholder="请输入用户名" type="text" name="username" v-model="username" clearable></jc-input>
<jc-input placeholder="请输入密码" type="password" name="password" show-password></jc-input>
<jc-input placeholder="请输入邮箱" type="text" disabled></jc-input>
<jc-input></jc-input>
</div>
</template>
<script>
export default {
data () {
return {
username: ''
}
}
}
</script>
<style lang="scss">
.jc-input {
width: 180px !important;
}
</style>
- 在浏览器中输入
,显示如下所示:http://localhost:8080/
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPB1ENBRkT4llaNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zROBlL2ETNyQzNwATMyATNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)