其他分享
首页 > 其他分享> > 模仿element-ui封装vue组件库(checkbox,checkbox-group,form)

模仿element-ui封装vue组件库(checkbox,checkbox-group,form)

作者:互联网

因为接下来封装的几个组件比较简单,通过之前类比即可,这里不多做介绍了。

九、封装一个element-ui风格的checkbox组件

<template>
<label class="ra-checkbox" :class="{' is-checked':isChecked}">
<span class="ra-checkbox_input">
<span class="ra-checkbox_inner"></span>
<input type="checkbox"
class="ra-checkbox_original"
:name="name"
v-model="model"
:value="label"
>
</span>
<span class="ra-checkbox_label">
<slot></slot>
<template v-if="!$slots.default">
{{label}}
</template>
</span>
</label>
</template>
<script>
export default {
name: 'ra-checkbox',
inject: {
CheckboxGroup: {
default: ''
}
},
props: {
value: {
type: Boolean,
default: false
},
label: {
type: String,
default: ''
},
name: {
type: String,
default: ''
}
},
computed: {
model: {
get () {
return this.isGroup ? this.CheckboxGroup.value : this.value
},
set (value) {
this.isGroup ? this.CheckboxGroup.$emit('input', value) : this.$emit('input', value)
console.log(value)
}
},
isGroup () {
return !!this.CheckboxGroup
},
isChecked () {
// 如果十group包裹,判断label是否在model中
// 如果没有group包裹,直接使用model
return this.isGroup ? this.model.includes(this.label) : this.model
}
}
}
</script>

<style lang="scss" scoped>
.ra-checkbox{
color: #606266;
font-weight: 500;
font-size: 14px;
position: relative;
cursor: pointer;
display: inline-block;
white-space: nowrap;
user-select: none;
margin-right: 30px;
.ra-checkbox_input{
white-space: nowrap;
cursor: pointer;
outline: none;
display: inline-block;
line-height: 1;
position: relative;
vertical-align: middle;
.ra-checkbox_inner{
display: inline-block;
position: relative;
border: 1px solid #dcdfe6;
border-radius: 2px;
box-sizing: border-box;
width: 14px;
height: 14px;
background-color: #fff;
z-index: 1;
transition: border-color .25s cubic-bezier(.71,-.46,.29,1.46),background-color .25s,cubic-bezier(.71,-.46,.29,1.46);
&:after{
box-sizing: content-box;
content: '';
border: 1px solid #ffffff;
border-left: 0;
border-top: 0;
height: 7px;
left: 4px;
position: absolute;
top: 1px;
transform: rotate(45deg) scaleY(0);
width: 3px;
transition: transform .15s ease-in .05s;
transform-origin: center;
}
}
.ra-checkbox_original{
opacity: 0;
outline: none;
position: absolute;
left: 10px;
margin: 0;
width: 0;
height: 0;
z-index: -1;
}
}
.ra-checkbox_label{
display: inline-block;
padding-left: 10px;
line-height: 19px;
font-size: 14px;
}
}
// 选中的样式
.ra-checkbox.is-checked{
.ra-checkbox_input{
.ra-checkbox_inner{
background-color: #409eff;
border-color: #409eff;
}
&:after{
transform: rotate(45deg) scaleY(1);
}
}
.ra-checkbox_label{
color: #409eff;
}
}
</style>

十、封装一个element-ui风格的checkbox-group组件

checkbox-group组件和radio-group组件相似,这里不过多介绍了。

<template>
<div class="ra-checkbox-group">
<slot></slot>
</div>
</template>
<script>
export default {
name: 'ra-checkbox-group',
provide () {
return {
CheckboxGroup: this
}
},
props: {
value: {
type: Array
}
}
}
</script>

十一、封装一个element-ui风格的form组件

<template>
<div class="ra-form">
<slot></slot>
</div>
</template>
<script>
export default {
name: 'ra-form',
provide () {
return {
Form: this
}
},
props: {
model: {
type: Object,
required: true
},
labelWidth: {
type: String,
default: '80px'
}
}
}
</script>

十二、封装一个element-ui风格的form-item组件

<template>
<div class="ra-form-item">
<label :style="labelStyle" class="one-form-item_label">{{label}}</label>
<div class="one-form-item_content">
<slot></slot>
</div>
</div>
</template>
<script>
export default {
name: 'ra-form-item',
props: {
label: {
type: String,
default: ''
}
},
inject: ['Form'],
computed: {
labelStyle () {
return {
width: this.Form.labelWidth
}
}
}
}
</script>

<style lang="scss" scoped>
.ra-form-item{
margin-bottom: 25px;
.ra-form-item_label{
text-align: right;
vertical-align: middle;
float: left;
font-size: 14px;
color: #606266;
line-height: 40px;
padding: 0 12px 0 0;
box-sizing: border-box;
}
.ra-form-item_content{
line-height: 40px;
position: relative;
font-size: 14px;
overflow: hidde;
}
}
</style>

标签:checkbox,form,default,value,label,vue,ra,border
来源: https://www.cnblogs.com/Rainbow5421/p/16312950.html