@ccos/webos-vue
Version:
A Web-Ui Framework for Skyworth/Coocaa TV
121 lines (120 loc) • 4.36 kB
JavaScript
import { defineComponent, computed, ref, watchEffect, openBlock, createElementBlock, normalizeStyle, Fragment, renderList, normalizeClass, createElementVNode, renderSlot, createTextVNode, toDisplayString } from "vue";
const _hoisted_1 = ["onClick"];
const _hoisted_2 = { class: "u-label" };
const _hoisted_3 = {
key: 1,
class: "m-checkbox-wrap"
};
const _hoisted_4 = { class: "u-label" };
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "checkbox",
props: {
options: { default: () => [] },
disabled: { type: Boolean, default: false },
vertical: { type: Boolean, default: false },
value: { default: () => [] },
gap: { default: 8 },
width: { default: "auto" },
height: { default: "auto" },
indeterminate: { type: Boolean, default: false },
checked: { type: Boolean, default: false }
},
emits: ["update:value", "update:checked", "change"],
setup(__props, { emit: __emit }) {
const props = __props;
const sum = computed(() => {
return props.options.length;
});
const maxWidth = computed(() => {
if (typeof props.width === "number") {
return props.width + "px";
} else {
return props.width;
}
});
const maxHeight = computed(() => {
if (typeof props.height === "number") {
return props.height + "px";
} else {
return props.height;
}
});
const styleObject = computed(() => {
if (props.vertical) {
return {
marginBottom: props.gap + "px"
};
} else {
return {
marginRight: props.gap + "px"
};
}
});
const checkedValue = ref([]);
watchEffect(() => {
checkedValue.value = props.value;
});
const emits = __emit;
function onClick(value) {
if (props.value.includes(value)) {
const newVal = checkedValue.value.filter((target) => target !== value);
emits("update:value", newVal);
emits("change", newVal);
} else {
const newVal = [...checkedValue.value, value];
emits("update:value", newVal);
emits("change", newVal);
}
}
function onCheckAll() {
emits("update:checked", !props.checked);
}
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div", {
class: "m-checkbox",
style: normalizeStyle(`max-width: ${maxWidth.value}; max-height: ${maxHeight.value};`)
}, [
sum.value ? (openBlock(true), createElementBlock(Fragment, { key: 0 }, renderList(_ctx.options, (option, index) => {
return openBlock(), createElementBlock("div", {
class: normalizeClass(["m-checkbox-wrap", { "vertical": _ctx.vertical }]),
style: normalizeStyle(sum.value !== index + 1 ? styleObject.value : ""),
key: index
}, [
createElementVNode("div", {
class: normalizeClass(["m-box", { "disabled": _ctx.disabled || option.disabled }]),
onClick: ($event) => _ctx.disabled || option.disabled ? () => false : onClick(option.value)
}, [
createElementVNode("span", _hoisted_2, [
renderSlot(_ctx.$slots, "default", {
label: option.label
}, () => [
createTextVNode(toDisplayString(option.label), 1)
], true)
]),
createElementVNode("span", {
class: normalizeClass(["u-checkbox", { "u-checkbox-checked": checkedValue.value.includes(option.value) }])
}, null, 2)
], 10, _hoisted_1)
], 6);
}), 128)) : (openBlock(), createElementBlock("div", _hoisted_3, [
createElementVNode("div", {
class: normalizeClass(["m-box", { "disabled": _ctx.disabled }]),
onClick: onCheckAll
}, [
createElementVNode("span", {
class: normalizeClass(["u-checkbox", { "u-checkbox-checked": _ctx.checked && !_ctx.indeterminate, "indeterminate": _ctx.indeterminate }])
}, null, 2),
createElementVNode("span", _hoisted_4, [
renderSlot(_ctx.$slots, "default", {}, () => [
createTextVNode("Check all")
], true)
])
], 2)
]))
], 4);
};
}
});
export {
_sfc_main as default
};