You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.6 KiB

2 months ago
  1. <template>
  2. <view
  3. class="u-grid"
  4. ref='u-grid'
  5. :style="[gridStyle]"
  6. >
  7. <slot />
  8. </view>
  9. </template>
  10. <script>
  11. import { props } from './props';
  12. import { mpMixin } from '../../libs/mixin/mpMixin';
  13. import { mixin } from '../../libs/mixin/mixin';
  14. import { addStyle, deepMerge } from '../../libs/function/index';
  15. /**
  16. * grid 宫格布局
  17. * @description 宫格组件一般用于同时展示多个同类项目的场景可以给宫格的项目设置徽标组件(badge)或者图标等也可以扩展为左右滑动的轮播形式
  18. * @tutorial https://ijry.github.io/uview-plus/components/grid.html
  19. * @property {String | Number} col 宫格的列数默认 3
  20. * @property {Boolean} border 是否显示宫格的边框默认 false
  21. * @property {String} align 宫格对齐方式表现为数量少的时候靠左居中还是靠右 默认 'left'
  22. * @property {Object} customStyle 定义需要用到的外部样式
  23. * @event {Function} click 点击宫格触发
  24. * @example <u-grid :col="3" @click="click"></u-grid>
  25. */
  26. export default {
  27. name: 'u-grid',
  28. mixins: [mpMixin, mixin, props],
  29. data() {
  30. return {
  31. index: 0,
  32. width: 0
  33. }
  34. },
  35. watch: {
  36. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  37. parentData() {
  38. if (this.children.length) {
  39. this.children.map(child => {
  40. // 判断子组件(u-radio)如果有updateParentData方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  41. typeof(child.updateParentData) == 'function' && child.updateParentData();
  42. })
  43. }
  44. },
  45. },
  46. created() {
  47. // 如果将children定义在data中,在微信小程序会造成循环引用而报错
  48. this.children = []
  49. },
  50. computed: {
  51. // 计算父组件的值是否发生变化
  52. parentData() {
  53. return [this.hoverClass, this.col, this.size, this.border];
  54. },
  55. // 宫格对齐方式
  56. gridStyle() {
  57. let style = {};
  58. switch (this.align) {
  59. case 'left':
  60. style.justifyContent = 'flex-start';
  61. break;
  62. case 'center':
  63. style.justifyContent = 'center';
  64. break;
  65. case 'right':
  66. style.justifyContent = 'flex-end';
  67. break;
  68. default:
  69. style.justifyContent = 'flex-start';
  70. };
  71. return deepMerge(style, addStyle(this.customStyle));
  72. }
  73. },
  74. emits: ['click'], // 防止事件执行两次
  75. // 20240409发现抖音小程序如果开启virtualHost会出现严重问题,几乎所有事件包括created等生命周期事件全部失效。
  76. // #ifdef MP-WEIXIN
  77. options: {
  78. // virtualHost: true ,//将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等
  79. },
  80. // #endif
  81. methods: {
  82. // 此方法由u-grid-item触发,用于在u-grid发出事件
  83. childClick(name) {
  84. this.$emit('click', name)
  85. }
  86. }
  87. };
  88. </script>
  89. <style lang="scss" scoped>
  90. @import "../../libs/css/components.scss";
  91. $u-grid-width:100% !default;
  92. .u-grid {
  93. /* #ifdef MP */
  94. width: $u-grid-width;
  95. position: relative;
  96. box-sizing: border-box;
  97. overflow: hidden;
  98. display: block;
  99. /* #endif */
  100. justify-content: center;
  101. @include flex;
  102. flex-wrap: wrap;
  103. align-items: center;
  104. // 在uni-app中应尽量避免使用flex布局以外的方式,因为nvue/uvue等方案都支持flex布局
  105. // 这里使用grid布局使用为目前20240409uni-app在抖音小程序开启virtualHost时有bug,存在事件失效问题。
  106. /* #ifdef MP-TOUTIAO */
  107. display: grid;
  108. grid-template-columns: repeat(v-bind(col), 1fr);
  109. /* #endif */
  110. }
  111. </style>