Files
EdgeAdmin/web/public/js/components/common/checkbox.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-11-18 15:41:29 +08:00
let checkboxId = 0
Vue.component("checkbox", {
2021-01-11 18:15:53 +08:00
props: ["name", "value", "v-value", "id", "checked"],
2020-11-18 15:41:29 +08:00
data: function () {
checkboxId++
let elementId = this.id
if (elementId == null) {
elementId = "checkbox" + checkboxId
}
let elementValue = this.vValue
if (elementValue == null) {
elementValue = "1"
}
2021-01-11 18:15:53 +08:00
let checkedValue = this.value
2022-03-11 20:27:45 +08:00
if (checkedValue == null && this.checked == "checked") {
checkedValue = elementValue
}
2021-01-11 18:15:53 +08:00
2020-11-18 15:41:29 +08:00
return {
elementId: elementId,
elementValue: elementValue,
2021-01-11 18:15:53 +08:00
newValue: checkedValue
2020-11-18 15:41:29 +08:00
}
},
methods: {
change: function () {
this.$emit("input", this.newValue)
2022-03-11 20:27:45 +08:00
},
check: function () {
this.newValue = this.elementValue
},
uncheck: function () {
this.newValue = ""
},
isChecked: function () {
return this.newValue == this.elementValue
}
},
watch: {
value: function (v) {
if (typeof v == "boolean") {
this.newValue = v
}
2020-11-18 15:41:29 +08:00
}
},
template: `<div class="ui checkbox">
<input type="checkbox" :name="name" :value="elementValue" :id="elementId" @change="change" v-model="newValue"/>
2020-12-23 16:49:53 +08:00
<label :for="elementId" style="font-size: 0.85em!important;"><slot></slot></label>
2020-11-18 15:41:29 +08:00
</div>`
})