3.87 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3var __getOwnPropNames = Object.getOwnPropertyNames;
4var __hasOwnProp = Object.prototype.hasOwnProperty;
5var __export = (target, all) => {
6 for (var name2 in all)
7 __defProp(target, name2, { get: all[name2], enumerable: true });
8};
9var __copyProps = (to, from, except, desc) => {
10 if (from && typeof from === "object" || typeof from === "function") {
11 for (let key of __getOwnPropNames(from))
12 if (!__hasOwnProp.call(to, key) && key !== except)
13 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14 }
15 return to;
16};
17var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18var stdin_exports = {};
19__export(stdin_exports, {
20 COLLAPSE_KEY: () => COLLAPSE_KEY,
21 collapseProps: () => collapseProps,
22 default: () => stdin_default
23});
24module.exports = __toCommonJS(stdin_exports);
25var import_vue = require("vue");
26var import_utils = require("../utils");
27var import_use = require("@vant/use");
28var import_use_expose = require("../composables/use-expose");
29const [name, bem] = (0, import_utils.createNamespace)("collapse");
30const COLLAPSE_KEY = Symbol(name);
31const collapseProps = {
32 border: import_utils.truthProp,
33 accordion: Boolean,
34 modelValue: {
35 type: [String, Number, Array],
36 default: ""
37 }
38};
39function validateModelValue(modelValue, accordion) {
40 if (accordion && Array.isArray(modelValue)) {
41 console.error('[Vant] Collapse: "v-model" should not be Array in accordion mode');
42 return false;
43 }
44 if (!accordion && !Array.isArray(modelValue)) {
45 console.error('[Vant] Collapse: "v-model" should be Array in non-accordion mode');
46 return false;
47 }
48 return true;
49}
50var stdin_default = (0, import_vue.defineComponent)({
51 name,
52 props: collapseProps,
53 emits: ["change", "update:modelValue"],
54 setup(props, {
55 emit,
56 slots
57 }) {
58 const {
59 linkChildren,
60 children
61 } = (0, import_use.useChildren)(COLLAPSE_KEY);
62 const updateName = (name2) => {
63 emit("change", name2);
64 emit("update:modelValue", name2);
65 };
66 const toggle = (name2, expanded) => {
67 const {
68 accordion,
69 modelValue
70 } = props;
71 if (accordion) {
72 updateName(name2 === modelValue ? "" : name2);
73 } else if (expanded) {
74 updateName(modelValue.concat(name2));
75 } else {
76 updateName(modelValue.filter((activeName) => activeName !== name2));
77 }
78 };
79 const toggleAll = (options = {}) => {
80 if (props.accordion) {
81 return;
82 }
83 if (typeof options === "boolean") {
84 options = {
85 expanded: options
86 };
87 }
88 const {
89 expanded,
90 skipDisabled
91 } = options;
92 const expandedChildren = children.filter((item) => {
93 if (item.disabled && skipDisabled) {
94 return item.expanded.value;
95 }
96 return expanded != null ? expanded : !item.expanded.value;
97 });
98 const names = expandedChildren.map((item) => item.itemName.value);
99 updateName(names);
100 };
101 const isExpanded = (name2) => {
102 const {
103 accordion,
104 modelValue
105 } = props;
106 if (process.env.NODE_ENV !== "production" && !validateModelValue(modelValue, accordion)) {
107 return false;
108 }
109 return accordion ? modelValue === name2 : modelValue.includes(name2);
110 };
111 (0, import_use_expose.useExpose)({
112 toggleAll
113 });
114 linkChildren({
115 toggle,
116 isExpanded
117 });
118 return () => {
119 var _a;
120 return (0, import_vue.createVNode)("div", {
121 "class": [bem(), {
122 [import_utils.BORDER_TOP_BOTTOM]: props.border
123 }]
124 }, [(_a = slots.default) == null ? void 0 : _a.call(slots)]);
125 };
126 }
127});