@opensig/opendesign
Version:
70 lines (69 loc) • 2.48 kB
JavaScript
import { defineComponent, toRefs, ref, computed, watch, onMounted, provide, toRef, createElementBlock, openBlock, normalizeClass, unref, renderSlot } from "vue";
import { menuInjectKey } from "./provide.mjs";
import { menuProps } from "./types.mjs";
import MenuTree from "./menu.mjs";
import { isArray } from "../_utils/is.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "OMenu",
props: menuProps,
emits: ["update:modelValue", "change", "update:expanded", "expanded-change"],
setup(__props, { emit: __emit }) {
const props = __props;
const emits = __emit;
const menuTree = new MenuTree(NaN, null);
const { size, accordion, modelValue, defaultValue, expanded, defaultExpanded } = toRefs(props);
const innerValue = ref((modelValue == null ? void 0 : modelValue.value) ?? defaultValue.value);
const realValue = computed(() => (modelValue == null ? void 0 : modelValue.value) ?? innerValue.value);
const updateModelValue = (val) => {
innerValue.value = val;
emits("update:modelValue", val);
emits("change", val);
};
const innerExpanded = ref(isArray(expanded == null ? void 0 : expanded.value) ? expanded == null ? void 0 : expanded.value : defaultExpanded.value);
const realExpanded = computed(() => {
if (isArray(expanded == null ? void 0 : expanded.value)) {
return expanded.value;
}
return innerExpanded.value;
});
const activeNodes = ref([]);
const notifyTreeChange = () => {
activeNodes.value = menuTree.selectNode(realValue.value || "");
};
watch(realValue, notifyTreeChange, { flush: "post" });
onMounted(() => notifyTreeChange());
const updateExpanded = (val) => {
innerExpanded.value = val;
emits("update:expanded", val);
emits("expanded-change", val);
};
provide(menuInjectKey, {
size,
accordion,
realValue,
activeNodes,
realExpanded,
menuTree,
notifyTreeChange,
updateModelValue,
updateExpanded,
arrowPosition: toRef(props, "arrowPosition")
});
return (_ctx, _cache) => {
return openBlock(), createElementBlock(
"ul",
{
class: normalizeClass(["o-menu", `o-menu-${unref(size)}`, _ctx.arrowPosition && `o-menu-arrow-${_ctx.arrowPosition}`])
},
[
renderSlot(_ctx.$slots, "default")
],
2
/* CLASS */
);
};
}
});
export {
_sfc_main as default
};