Collapse.js 3.8 KB

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