@opensig/opendesign
Version:
98 lines (97 loc) • 3.3 kB
JavaScript
import { defineComponent, toRefs, inject, computed, toValue, ref, watch, createElementBlock, openBlock, createElementVNode, normalizeClass, createBlock, renderSlot, unref, withCtx, createTextVNode, toDisplayString } from "vue";
import { selectOptionInjectKey } from "../select/provide.mjs";
import { optionProps } from "./types.mjs";
import { OCheckbox } from "../checkbox/index.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "OOption",
props: optionProps,
setup(__props) {
const props = __props;
const { label, value } = toRefs(props);
const selectInject = inject(selectOptionInjectKey, null);
const isMultiple = computed(() => toValue(selectInject == null ? void 0 : selectInject.multiple));
const currentVal = computed(() => {
return selectInject == null ? void 0 : selectInject.selectValue.value;
});
const isActive = ref(false);
watch(
[currentVal, value],
() => {
var _a;
isActive.value = Boolean((_a = currentVal.value) == null ? void 0 : _a.includes(value.value));
},
// currentVal 会被 OSelect 通过数组下标及push方法修改,所以需要deep
{ immediate: true, deep: true }
);
watch(
[value, label],
([newValue, newLabel]) => {
selectInject == null ? void 0 : selectInject.registerOption({
label: newLabel || `${newValue}`,
value: newValue
});
},
{ immediate: true }
);
const clickOption = () => {
if (!props.disabled) {
selectInject == null ? void 0 : selectInject.select({
label: label.value || `${value.value}`,
value: value.value
});
}
};
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div", {
class: "o-option",
onClick: clickOption
}, [
createElementVNode(
"div",
{
class: normalizeClass(["o-option-item", [
{
active: isActive.value,
"o-option-disabled": props.disabled,
"o-option-multiple": isMultiple.value
}
]])
},
[
isMultiple.value ? (openBlock(), createBlock(unref(OCheckbox), {
key: 0,
"model-value": currentVal.value,
value: props.value,
class: "o-option-checkbox",
disabled: props.disabled,
indeterminate: props.indeterminate
}, {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default", {}, () => [
createTextVNode(
toDisplayString(props.label || `${props.value}`),
1
/* TEXT */
)
])
]),
_: 3
/* FORWARDED */
}, 8, ["model-value", "value", "disabled", "indeterminate"])) : renderSlot(_ctx.$slots, "default", { key: 1 }, () => [
createTextVNode(
toDisplayString(props.label || `${props.value}`),
1
/* TEXT */
)
])
],
2
/* CLASS */
)
]);
};
}
});
export {
_sfc_main as default
};