u-grid-item.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="u-grid-item" :hover-class="parentData.hoverClass"
  3. :hover-stay-time="200" @tap="click" :style="{
  4. background: bgColor,
  5. width: width,
  6. }">
  7. <view class="u-grid-item-box" :style="[customStyle]" :class="[parentData.border ? 'u-border-right u-border-bottom' : '']">
  8. <slot />
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. /**
  14. * gridItem 提示
  15. * @description 宫格组件一般用于同时展示多个同类项目的场景,可以给宫格的项目设置徽标组件(badge),或者图标等,也可以扩展为左右滑动的轮播形式。搭配u-grid使用
  16. * @tutorial https://www.uviewui.com/components/grid.html
  17. * @property {String} bg-color 宫格的背景颜色(默认#ffffff)
  18. * @property {String Number} index 点击宫格时,返回的值
  19. * @property {Object} custom-style 自定义样式,对象形式
  20. * @event {Function} click 点击宫格触发
  21. * @example <u-grid-item></u-grid-item>
  22. */
  23. export default {
  24. name: "u-grid-item",
  25. emits: ["click"],
  26. props: {
  27. // 背景颜色
  28. bgColor: {
  29. type: String,
  30. default: '#ffffff'
  31. },
  32. // 点击时返回的index
  33. index: {
  34. type: [Number, String],
  35. default: ''
  36. },
  37. // 自定义样式,对象形式
  38. customStyle: {
  39. type: Object,
  40. default() {
  41. return {
  42. padding: '30rpx 0'
  43. }
  44. }
  45. }
  46. },
  47. data() {
  48. return {
  49. parentData: {
  50. hoverClass: '', // 按下去的时候,是否显示背景灰色
  51. col: 3, // 父组件划分的宫格数
  52. border: true, // 是否显示边框,根据父组件决定
  53. }
  54. };
  55. },
  56. created() {
  57. // 父组件的实例
  58. this.updateParentData();
  59. // this.parent在updateParentData()中定义
  60. if (this.parent && this.parent.children) {
  61. this.parent.children.push(this);
  62. }
  63. },
  64. computed: {
  65. // 每个grid-item的宽度
  66. width() {
  67. return 100 / Number(this.parentData.col) + '%';
  68. },
  69. },
  70. methods: {
  71. // 获取父组件的参数
  72. updateParentData() {
  73. // 此方法写在mixin中
  74. this.getParentData('u-grid');
  75. },
  76. click() {
  77. this.$emit('click', this.index);
  78. this.parent && this.parent.click(this.index);
  79. }
  80. }
  81. };
  82. </script>
  83. <style scoped lang="scss">
  84. @import "../../libs/css/style.components.scss";
  85. .u-grid-item {
  86. box-sizing: border-box;
  87. background: #fff;
  88. @include vue-flex;
  89. align-items: center;
  90. justify-content: center;
  91. position: relative;
  92. flex-direction: column;
  93. /* #ifdef MP */
  94. position: relative;
  95. float: left;
  96. /* #endif */
  97. }
  98. .u-grid-item-hover {
  99. background: #f7f7f7 !important;
  100. }
  101. .u-grid-marker-box {
  102. position: absolute;
  103. /* #ifndef APP-NVUE */
  104. display: inline-flex;
  105. /* #endif */
  106. line-height: 0;
  107. }
  108. .u-grid-marker-wrap {
  109. position: absolute;
  110. }
  111. .u-grid-item-box {
  112. padding: 30rpx 0;
  113. @include vue-flex;
  114. align-items: center;
  115. justify-content: center;
  116. flex-direction: column;
  117. flex: 1;
  118. width: 100%;
  119. height: 100%;
  120. }
  121. </style>