1
0

u-grid-item.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. this.parent.children.push(this);
  61. },
  62. computed: {
  63. // 每个grid-item的宽度
  64. width() {
  65. return 100 / Number(this.parentData.col) + '%';
  66. },
  67. },
  68. methods: {
  69. // 获取父组件的参数
  70. updateParentData() {
  71. // 此方法写在mixin中
  72. this.getParentData('u-grid');
  73. },
  74. click() {
  75. this.$emit('click', this.index);
  76. this.parent && this.parent.click(this.index);
  77. }
  78. }
  79. };
  80. </script>
  81. <style scoped lang="scss">
  82. @import "../../libs/css/style.components.scss";
  83. .u-grid-item {
  84. box-sizing: border-box;
  85. background: #fff;
  86. @include vue-flex;
  87. align-items: center;
  88. justify-content: center;
  89. position: relative;
  90. flex-direction: column;
  91. /* #ifdef MP */
  92. position: relative;
  93. float: left;
  94. /* #endif */
  95. }
  96. .u-grid-item-hover {
  97. background: #f7f7f7 !important;
  98. }
  99. .u-grid-marker-box {
  100. position: absolute;
  101. /* #ifndef APP-NVUE */
  102. display: inline-flex;
  103. /* #endif */
  104. line-height: 0;
  105. }
  106. .u-grid-marker-wrap {
  107. position: absolute;
  108. }
  109. .u-grid-item-box {
  110. padding: 30rpx 0;
  111. @include vue-flex;
  112. align-items: center;
  113. justify-content: center;
  114. flex-direction: column;
  115. flex: 1;
  116. width: 100%;
  117. height: 100%;
  118. }
  119. </style>