<template>
<view class="u-checkbox">
<view class="u-checkbox__icon-wrap" @tap="toggle">
<image
v-if="!value"
src="/static/base-module/home/noclick1.png"
class="icon"
mode="aspectFit"
></image>
<image
v-else
src="/static/base-module/home/click1.png"
class="icon"
mode="aspectFit"
></image>
<slot />
</view>
</view>
</template>
<script>
export default {
name: "u-checkbox",
props: {
name: {
type: [String, Number],
default: "",
},
shape: {
type: String,
default: "",
},
value: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
labelDisabled: {
type: Boolean,
default: false,
},
activeColor: {
type: String,
default: "",
},
iconSize: {
type: [String, Number],
default: "",
},
labelSize: {
type: [String, Number],
default: "16",
},
size: {
type: [String, Number],
default: "",
},
},
data() {
return {
parentDisabled: false,
newParams: {},
};
},
created() {
this.parent = this.$u.$parent.call(this, "u-checkbox-group");
this.parent && this.parent.children.push(this);
},
computed: {
isDisabled() {
return this.parent
? this.parent.disabled || this.disabled
: this.disabled;
},
isLabelDisabled() {
return this.parent
? this.parent.labelDisabled || this.labelDisabled
: this.labelDisabled;
},
checkboxSize() {
return this.size ? this.size : this.parent ? this.parent.size : 34;
},
checkboxIconSize() {
return this.iconSize
? this.iconSize
: this.parent
? this.parent.iconSize
: 20;
},
elActiveColor() {
return this.activeColor
? this.activeColor
: this.parent
? this.parent.activeColor
: "primary";
},
elShape() {
return this.shape
? this.shape
: this.parent
? this.parent.shape
: "square";
},
iconStyle() {
let style = {};
if (this.elActiveColor && this.value && !this.isDisabled) {
style.borderColor = this.elActiveColor;
style.backgroundColor = this.elActiveColor;
}
style.width = this.$u.addUnit(this.checkboxSize);
style.height = this.$u.addUnit(this.checkboxSize);
return style;
},
iconColor() {
return this.value ? "#ffffff" : "transparent";
},
iconClass() {
let classs = [];
classs.push("u-checkbox__icon--" + this.elShape);
if (this.value == true) classs.push("u-checkbox__icon--checked");
if (this.isDisabled) classs.push("u-checkbox__icon--disabled");
if (this.value && this.isDisabled)
classs.push("u-checkbox__icon--disabled--checked");
console.log(classs);
return classs;
},
checkboxStyle() {
let style = {};
if (this.parent && this.parent.width) {
style.width = this.parent.width;
style.float = "left";
style.flex = `0 0 ${this.parent.width}`;
}
if (this.parent && this.parent.wrap) {
style.width = "100%";
style.flex = "0 0 100%";
}
return style;
},
},
methods: {
onClickLabel() {
if (!this.isLabelDisabled) {
this.setValue();
}
},
toggle() {
if (!this.isDisabled) {
this.setValue();
}
},
emitEvent() {
this.$emit("change", {
value: this.value,
name: this.name,
});
if (this.parent && this.parent.emitEvent) this.parent.emitEvent();
},
setValue() {
let checkedNum = 0;
if (this.parent && this.parent.children) {
this.parent.children.map((val) => {
if (val.value) checkedNum++;
});
}
if (this.value == true) {
this.$emit("input", !this.value);
this.$nextTick(function () {
this.emitEvent();
});
} else if (
(this.parent && checkedNum < this.parent.max) ||
!this.parent
) {
this.$emit("input", !this.value);
this.$nextTick(function () {
this.emitEvent();
});
}
},
},
};
</script>
<style lang="scss" scoped>
@import "../../libs/css/style.components.scss";
.u-checkbox {
display: inline-flex;
align-items: center;
overflow: hidden;
user-select: none;
line-height: 1.8;
}
.u-checkbox__icon-wrap,
.u-checkbox__label {
font-size: rpx(45);
font-family: Roboto-Regular, Roboto;
font-weight: 400;
color: rgba(0, 0, 0, 1);
padding-left: rpx(20);
}
.u-checkbox__icon-wrap {
display: flex;
align-items: center;
}
.u-checkbox__icon {
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
width: 42rpx;
height: 42rpx;
color: transparent;
text-align: center;
transition-property: color, border-color, background-color;
font-size: 20px;
border: 1px solid #c8c9cc;
transition-duration: 0.2s;
}
.u-checkbox__icon--circle {
border-radius: 100%;
}
.u-checkbox__icon--square {
border-radius: 3px;
}
.u-checkbox__icon--checked {
color: #fff;
background-color: #2979ff;
border-color: #2979ff;
}
.u-checkbox__icon--disabled {
background-color: #ebedf0;
border-color: #c8c9cc;
}
.u-checkbox__icon--disabled--checked {
color: #c8c9cc !important;
}
.u-checkbox__label {
word-wrap: break-word;
margin-left: 10rpx;
margin-right: 24rpx;
color: #606266;
font-size: 30rpx;
}
.u-checkbox__label--disabled {
color: #c8c9cc;
}
.u-checkbox__label:empty {
margin: 0;
}
.icon {
width: rpx(66);
height: rpx(66);
}
</style>