@opensig/opendesign
Version:
134 lines (133 loc) • 4.96 kB
JavaScript
;
const vue = require("vue");
const provide$1 = require("./provide.js");
const provide = require("../checkbox-group/provide.js");
const types = require("./types.js");
const icons = require("../_utils/icons.js");
const is = require("../_utils/is.js");
const helper = require("../_utils/helper.js");
const _hoisted_1 = ["for"];
const _hoisted_2 = { class: "o-checkbox-wrap" };
const _hoisted_3 = ["id", "value", "disabled", "checked"];
const _hoisted_4 = { class: "o-checkbox-input-wrap" };
const _hoisted_5 = { class: "o-checkbox-input" };
const _hoisted_6 = {
key: 0,
class: "o-checkbox-input-icon-indeterminate"
};
const _hoisted_7 = { class: "o-checkbox-label" };
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
__name: "OCheckbox",
props: types.checkboxProps,
emits: ["update:modelValue", "change"],
setup(__props, { expose: __expose, emit: __emit }) {
const props = __props;
const emits = __emit;
const inputId = vue.ref(props.inputId);
vue.onMounted(() => {
if (!inputId.value) {
inputId.value = helper.uniqueId();
}
});
const checkboxGroupInjection = vue.inject(provide.checkboxGroupInjectKey, null);
const _checked = vue.ref(props.defaultChecked);
const isChecked = vue.computed(() => {
if (is.isUndefined(props.value)) {
return false;
}
if (checkboxGroupInjection) {
return checkboxGroupInjection.realValue.value.includes(props.value);
}
if (is.isArray(props.modelValue)) {
return props.modelValue.includes(props.value);
}
return _checked.value;
});
vue.watch(
isChecked,
(val) => {
_checked.value = val;
},
{ immediate: true }
);
const isDisabled = vue.computed(() => {
return (checkboxGroupInjection == null ? void 0 : checkboxGroupInjection.disabled.value) || props.disabled || isChecked.value && ((checkboxGroupInjection == null ? void 0 : checkboxGroupInjection.isMinimum.value) ?? false) || !isChecked.value && ((checkboxGroupInjection == null ? void 0 : checkboxGroupInjection.isMaximum.value) ?? false);
});
const onClick = (ev) => {
ev.stopPropagation();
};
const onChange = (ev) => {
if (is.isUndefined(props.value)) {
return;
}
const { checked } = ev.target;
const set = checkboxGroupInjection ? /* @__PURE__ */ new Set([...checkboxGroupInjection.realValue.value]) : is.isArray(props.modelValue) ? /* @__PURE__ */ new Set([...props.modelValue]) : /* @__PURE__ */ new Set([]);
if (checked) {
set.add(props.value);
} else {
set.delete(props.value);
}
_checked.value = checked;
const val = Array.from(set);
emits("update:modelValue", val);
checkboxGroupInjection == null ? void 0 : checkboxGroupInjection.updateModelValue(val);
vue.nextTick(() => {
emits("change", val, ev);
checkboxGroupInjection == null ? void 0 : checkboxGroupInjection.onChange(val, ev);
});
};
__expose({
/**
* @zh-CN 是否已选中
* @en-US Whether the checkbox is checked
*/
checked: isChecked
});
vue.provide(provide$1.checkboxInjectKey, {
checked: isChecked,
disabled: isDisabled
});
return (_ctx, _cache) => {
return vue.openBlock(), vue.createElementBlock("label", {
class: vue.normalizeClass(["o-checkbox", {
"o-checkbox-checked": isChecked.value,
"o-checkbox-disabled": isDisabled.value,
"o-checkbox-indeterminate": props.indeterminate
}]),
for: inputId.value
}, [
vue.createElementVNode("div", _hoisted_2, [
vue.createElementVNode("input", {
id: inputId.value,
type: "checkbox",
value: props.value,
disabled: isDisabled.value,
checked: isChecked.value,
onClick,
onChange
}, null, 40, _hoisted_3),
vue.renderSlot(_ctx.$slots, "checkbox", {
checked: isChecked.value,
disabled: isDisabled.value
}, () => [
vue.createElementVNode("div", _hoisted_4, [
vue.createElementVNode("span", _hoisted_5, [
vue.createVNode(vue.Transition, { name: "o-fade-in" }, {
default: vue.withCtx(() => [
props.indeterminate ? (vue.openBlock(), vue.createElementBlock("span", _hoisted_6)) : isChecked.value ? (vue.openBlock(), vue.createBlock(vue.unref(icons.IconChecked), { key: 1 })) : vue.createCommentVNode("v-if", true)
]),
_: 1
/* STABLE */
})
])
]),
vue.createElementVNode("span", _hoisted_7, [
vue.renderSlot(_ctx.$slots, "default")
])
])
])
], 10, _hoisted_1);
};
}
});
module.exports = _sfc_main;