uni-collapse.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "uniCollapse",
  5. emits: ["change", "activeItem", "input", "update:modelValue"],
  6. props: {
  7. value: {
  8. type: [String, Array],
  9. default: ""
  10. },
  11. modelValue: {
  12. type: [String, Array],
  13. default: ""
  14. },
  15. accordion: {
  16. // 是否开启手风琴效果
  17. type: [Boolean, String],
  18. default: false
  19. }
  20. },
  21. data() {
  22. return {};
  23. },
  24. computed: {
  25. // TODO 兼容 vue2 和 vue3
  26. dataValue() {
  27. let value = typeof this.value === "string" && this.value === "" || Array.isArray(this.value) && this.value.length === 0;
  28. let modelValue = typeof this.modelValue === "string" && this.modelValue === "" || Array.isArray(this.modelValue) && this.modelValue.length === 0;
  29. if (value) {
  30. return this.modelValue;
  31. }
  32. if (modelValue) {
  33. return this.value;
  34. }
  35. return this.value;
  36. }
  37. },
  38. watch: {
  39. dataValue(val) {
  40. this.setOpen(val);
  41. }
  42. },
  43. created() {
  44. this.childrens = [];
  45. this.names = [];
  46. },
  47. mounted() {
  48. this.$nextTick(() => {
  49. this.setOpen(this.dataValue);
  50. });
  51. },
  52. methods: {
  53. setOpen(val) {
  54. let str = typeof val === "string";
  55. let arr = Array.isArray(val);
  56. this.childrens.forEach((vm, index) => {
  57. if (str) {
  58. if (val === vm.nameSync) {
  59. if (!this.accordion) {
  60. console.warn("accordion 属性为 false ,v-model 类型应该为 array");
  61. return;
  62. }
  63. vm.isOpen = true;
  64. }
  65. }
  66. if (arr) {
  67. val.forEach((v) => {
  68. if (v === vm.nameSync) {
  69. if (this.accordion) {
  70. console.warn("accordion 属性为 true ,v-model 类型应该为 string");
  71. return;
  72. }
  73. vm.isOpen = true;
  74. }
  75. });
  76. }
  77. });
  78. this.emit(val);
  79. },
  80. setAccordion(self) {
  81. if (!this.accordion)
  82. return;
  83. this.childrens.forEach((vm, index) => {
  84. if (self !== vm) {
  85. vm.isOpen = false;
  86. }
  87. });
  88. },
  89. resize() {
  90. this.childrens.forEach((vm, index) => {
  91. vm.getCollapseHeight();
  92. });
  93. },
  94. onChange(isOpen, self) {
  95. let activeItem = [];
  96. if (this.accordion) {
  97. activeItem = isOpen ? self.nameSync : "";
  98. } else {
  99. this.childrens.forEach((vm, index) => {
  100. if (vm.isOpen) {
  101. activeItem.push(vm.nameSync);
  102. }
  103. });
  104. }
  105. this.$emit("change", activeItem);
  106. this.emit(activeItem);
  107. },
  108. emit(val) {
  109. this.$emit("input", val);
  110. this.$emit("update:modelValue", val);
  111. }
  112. }
  113. };
  114. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  115. return {};
  116. }
  117. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "D:/zx/mall-front-app/uni_modules/uni-collapse/components/uni-collapse/uni-collapse.vue"]]);
  118. wx.createComponent(Component);