@cqmcui/cqmcui
Version:
轻量级移动端 Vue2、Vue3 组件库(支持小程序开发)
88 lines (87 loc) • 2.8 kB
JavaScript
import { ref, computed, watch, onMounted, getCurrentInstance, provide, openBlock, createElementBlock, normalizeClass, renderSlot } from "vue";
import { c as createComponent } from "./component-81a4c1d0.js";
import { _ as _export_sfc } from "./_plugin-vue_export-helper-cc2b3d55.js";
import "../locale/lang";
const { create, componentName } = createComponent("collapse");
const _sfc_main = create({
props: {
modelValue: {
type: [String, Number, Array],
default: () => []
},
accordion: {
type: Boolean,
default: false
}
},
emits: ["update:modelValue", "change"],
setup(props, { emit }) {
const collapseDom = ref(null);
const collapseChldren = ref([]);
const classes = computed(() => {
const prefixCls = componentName;
return {
[prefixCls]: true
};
});
watch(
() => props.modelValue,
(newval) => {
let doms = collapseChldren.value;
Array.from(doms).forEach((item) => {
if (typeof newval == "number" || typeof newval == "string") {
item.changeOpen(newval == item.name ? true : false);
} else if (Object.values(newval) instanceof Array) {
const isOpen = newval.indexOf(Number(item.name)) > -1 || newval.indexOf(String(item.name)) > -1;
item.changeOpen(isOpen);
}
item.animation();
});
}
);
onMounted(() => {
collapseChldren.value = getCurrentInstance().provides.collapseParent.children || [];
});
const changeVal = (val) => {
emit("update:modelValue", val);
emit("change", val);
};
const changeValAry = (name) => {
const activeItem = props.modelValue instanceof Object ? Object.values(props.modelValue) : props.modelValue;
let index2 = -1;
activeItem.forEach((item, idx) => {
if (String(item) == String(name)) {
index2 = idx;
}
});
index2 > -1 ? activeItem.splice(index2, 1) : activeItem.push(name);
changeVal(activeItem);
};
const isExpanded = (name) => {
const { accordion, modelValue } = props;
if (accordion) {
return typeof modelValue === "number" || typeof modelValue === "string" ? modelValue == name : false;
}
};
provide("collapseParent", {
children: [],
props,
changeValAry,
changeVal,
isExpanded
});
return { collapseDom, classes };
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return openBlock(), createElementBlock("view", {
class: normalizeClass(_ctx.classes),
ref: "collapseDom"
}, [
renderSlot(_ctx.$slots, "default")
], 2);
}
const index = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export {
index as default
};