Collapse.mjs 2.8 KB

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