u-grid-item.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "u-grid-item",
  5. mixins: [common_vendor.mpMixin, common_vendor.mixin, common_vendor.props$2],
  6. data() {
  7. return {
  8. parentData: {
  9. col: 0,
  10. // 父组件划分的宫格数
  11. border: true
  12. // 是否显示边框,根据父组件决定
  13. },
  14. classes: []
  15. // 类名集合,用于判断是否显示右边和下边框
  16. };
  17. },
  18. mounted() {
  19. this.init();
  20. },
  21. emits: ["click"],
  22. // 微信小程序中 options 选项
  23. options: {
  24. virtualHost: true
  25. //将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等
  26. },
  27. computed: {
  28. itemStyle() {
  29. const style = {
  30. background: this.bgColor
  31. };
  32. style["width"] = "100%";
  33. return common_vendor.deepMerge(style, common_vendor.addStyle(this.customStyle));
  34. }
  35. },
  36. methods: {
  37. init() {
  38. common_vendor.index.$on("$uGridItem", () => {
  39. this.gridItemClasses();
  40. });
  41. this.updateParentData();
  42. common_vendor.index.$emit("$uGridItem");
  43. this.gridItemClasses();
  44. },
  45. // 获取父组件的参数
  46. updateParentData() {
  47. this.getParentData("u-grid");
  48. },
  49. clickHandler() {
  50. var _a;
  51. let name = this.name;
  52. const children = (_a = this.parent) == null ? void 0 : _a.children;
  53. if (children && this.name === null) {
  54. name = children.findIndex((child) => child === this);
  55. }
  56. this.parent && this.parent.childClick(name);
  57. this.$emit("click", name);
  58. },
  59. async getItemWidth() {
  60. let width = 0;
  61. if (this.parent) {
  62. const parentWidth = await this.getParentWidth();
  63. width = parentWidth / Number(this.parentData.col) + "px";
  64. }
  65. this.width = width;
  66. },
  67. // 获取父元素的尺寸
  68. getParentWidth() {
  69. },
  70. gridItemClasses() {
  71. if (this.parentData.border) {
  72. let classes = [];
  73. this.parent.children.map((child, index) => {
  74. if (this === child) {
  75. const len = this.parent.children.length;
  76. if ((index + 1) % this.parentData.col !== 0 && index + 1 !== len) {
  77. classes.push("u-border-right");
  78. }
  79. const lessNum = len % this.parentData.col === 0 ? this.parentData.col : len % this.parentData.col;
  80. if (index < len - lessNum) {
  81. classes.push("u-border-bottom");
  82. }
  83. }
  84. });
  85. this.classes = classes;
  86. }
  87. }
  88. },
  89. beforeUnmount() {
  90. common_vendor.index.$off("$uGridItem");
  91. }
  92. };
  93. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  94. return common_vendor.e({
  95. a: $data.parentData.col > 0
  96. }, $data.parentData.col > 0 ? {
  97. b: common_vendor.o((...args) => $options.clickHandler && $options.clickHandler(...args)),
  98. c: common_vendor.n($data.classes),
  99. d: common_vendor.s($options.itemStyle)
  100. } : {});
  101. }
  102. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d5274fb5"], ["__file", "C:/Users/Administrator/Desktop/srcaaa/node_modules/uview-plus/components/u-grid-item/u-grid-item.vue"]]);
  103. wx.createComponent(Component);