UNPKG

@gyenno/nutui-taro

Version:

京东风格的轻量级移动端 Vue2、Vue3 组件库(支持小程序开发)

122 lines (121 loc) 4.03 kB
import { ref, computed, onMounted, getCurrentInstance, watch, provide, openBlock, createElementBlock, normalizeClass, renderSlot } from "vue"; import { c as createComponent } from "./component-25dcca32.js"; import { nextTick } from "@tarojs/taro"; 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] }, accordion: { type: Boolean, default: false } }, emits: ["update:modelValue", "change"], setup(props, { emit, slots }) { const collapseDom = ref(null); const classes = computed(() => { const prefixCls = componentName; return { [prefixCls]: true }; }); const changeVal = (val) => { emit("update:modelValue", val); emit("change", val); }; const changeValAry = (name) => { const activeItem = props.modelValue; let index = -1; activeItem.forEach((item, idx) => { if (String(item) == String(name)) { index = idx; } }); index > -1 ? activeItem.splice(index, 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; } }; const activeIndex = () => { var _a; const activeCollapse = props.modelValue; const childrenList = (_a = slots.default) == null ? void 0 : _a.call(slots); let act = []; childrenList.forEach((item, index) => { if (typeof activeCollapse == "number" || typeof activeCollapse == "string") { if (item.props.name == activeCollapse) { act.push(item.flag); return act; } } else { let ary = Array.from(activeCollapse); if (ary.includes(String(item.props.name)) || ary.includes(Number(item.props.name))) { act.push(item.flag); } } }); return act; }; const childrenDom = ref(null); onMounted(() => { childrenDom.value = getCurrentInstance().provides.collapseParent.children; }); watch( () => props.modelValue, (newval) => { nextTick(() => { var _a; let domsProps = (_a = slots == null ? void 0 : slots.default) == null ? void 0 : _a.call(slots); let doms = childrenDom.value; Array.from(doms).forEach((item, index) => { if (typeof newval == "number" || typeof newval == "string") { if (domsProps[index] && domsProps[index].props) { item.changeOpen(newval == domsProps[index].props.name ? true : false); } else { item.changeOpen(newval == item.name ? true : false); } } else if (Object.values(newval) instanceof Array) { const isOpen = newval.indexOf(Number(domsProps[index].props.name)) > -1 || newval.indexOf(String(domsProps[index].props.name)) > -1; item.changeOpen(isOpen); } item.animation(); }); }); } ); const getParentChildren = () => { var _a; return (_a = slots.default) == null ? void 0 : _a.call(slots); }; provide("collapseParent", { children: [], props, changeValAry, changeVal, isExpanded, activeIndex, getParentChildren }); 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_taro = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]); export { index_taro as default };