Collapse.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { createVNode as _createVNode } from "vue";
  2. import { defineComponent } from "vue";
  3. import { truthProp, createNamespace, BORDER_TOP_BOTTOM } from "../utils/index.mjs";
  4. import { useChildren } from "@vant/use";
  5. import { useExpose } from "../composables/use-expose.mjs";
  6. const [name, bem] = createNamespace("collapse");
  7. const COLLAPSE_KEY = Symbol(name);
  8. const collapseProps = {
  9. border: truthProp,
  10. accordion: Boolean,
  11. modelValue: {
  12. type: [String, Number, Array],
  13. default: ""
  14. }
  15. };
  16. function validateModelValue(modelValue, accordion) {
  17. if (accordion && Array.isArray(modelValue)) {
  18. console.error('[Vant] Collapse: "v-model" should not be Array in accordion mode');
  19. return false;
  20. }
  21. if (!accordion && !Array.isArray(modelValue)) {
  22. console.error('[Vant] Collapse: "v-model" should be Array in non-accordion mode');
  23. return false;
  24. }
  25. return true;
  26. }
  27. var stdin_default = defineComponent({
  28. name,
  29. props: collapseProps,
  30. emits: ["change", "update:modelValue"],
  31. setup(props, {
  32. emit,
  33. slots
  34. }) {
  35. const {
  36. linkChildren,
  37. children
  38. } = useChildren(COLLAPSE_KEY);
  39. const updateName = (name2) => {
  40. emit("change", name2);
  41. emit("update:modelValue", name2);
  42. };
  43. const toggle = (name2, expanded) => {
  44. const {
  45. accordion,
  46. modelValue
  47. } = props;
  48. if (accordion) {
  49. updateName(name2 === modelValue ? "" : name2);
  50. } else if (expanded) {
  51. updateName(modelValue.concat(name2));
  52. } else {
  53. updateName(modelValue.filter((activeName) => activeName !== name2));
  54. }
  55. };
  56. const toggleAll = (options = {}) => {
  57. if (props.accordion) {
  58. return;
  59. }
  60. if (typeof options === "boolean") {
  61. options = {
  62. expanded: options
  63. };
  64. }
  65. const {
  66. expanded,
  67. skipDisabled
  68. } = options;
  69. const expandedChildren = children.filter((item) => {
  70. if (item.disabled && skipDisabled) {
  71. return item.expanded.value;
  72. }
  73. return expanded != null ? expanded : !item.expanded.value;
  74. });
  75. const names = expandedChildren.map((item) => item.itemName.value);
  76. updateName(names);
  77. };
  78. const isExpanded = (name2) => {
  79. const {
  80. accordion,
  81. modelValue
  82. } = props;
  83. if (process.env.NODE_ENV !== "production" && !validateModelValue(modelValue, accordion)) {
  84. return false;
  85. }
  86. return accordion ? modelValue === name2 : modelValue.includes(name2);
  87. };
  88. useExpose({
  89. toggleAll
  90. });
  91. linkChildren({
  92. toggle,
  93. isExpanded
  94. });
  95. return () => {
  96. var _a;
  97. return _createVNode("div", {
  98. "class": [bem(), {
  99. [BORDER_TOP_BOTTOM]: props.border
  100. }]
  101. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)]);
  102. };
  103. }
  104. });
  105. export {
  106. COLLAPSE_KEY,
  107. stdin_default as default
  108. };