u-grid-item.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <!-- #ifndef APP-NVUE -->
  3. <view
  4. v-if="parentData.col > 0"
  5. class="u-grid-item"
  6. hover-class="u-grid-item--hover-class"
  7. :hover-stay-time="200"
  8. @tap="clickHandler"
  9. :class="classes"
  10. :style="[itemStyle]"
  11. >
  12. <slot />
  13. </view>
  14. <!-- #endif -->
  15. <!-- #ifdef APP-NVUE -->
  16. <view
  17. class="u-grid-item"
  18. :hover-stay-time="200"
  19. @tap="clickHandler"
  20. :class="classes"
  21. :style="[itemStyle]"
  22. >
  23. <slot />
  24. </view>
  25. <!-- #endif -->
  26. </template>
  27. <script>
  28. import props from './props.js';
  29. import mpMixin from '../../libs/mixin/mpMixin.js';
  30. import mixin from '../../libs/mixin/mixin.js';
  31. /**
  32. * gridItem 提示
  33. * @description 宫格组件一般用于同时展示多个同类项目的场景,可以给宫格的项目设置徽标组件(badge),或者图标等,也可以扩展为左右滑动的轮播形式。搭配u-grid使用
  34. * @tutorial https://ijry.github.io/uview-plus/components/grid.html
  35. * @property {String | Number} name 宫格的name ( 默认 null )
  36. * @property {String} bgColor 宫格的背景颜色 (默认 'transparent' )
  37. * @property {Object} customStyle 自定义样式,对象形式
  38. * @event {Function} click 点击宫格触发
  39. * @example <u-grid-item></u-grid-item>
  40. */
  41. export default {
  42. name: "u-grid-item",
  43. mixins: [mpMixin, mixin, props],
  44. data() {
  45. return {
  46. parentData: {
  47. col: 0, // 父组件划分的宫格数
  48. border: true, // 是否显示边框,根据父组件决定
  49. },
  50. // #ifdef APP-NVUE
  51. width: 0, // nvue下才这么计算,vue下放到computed中,否则会因为延时造成闪烁
  52. // #endif
  53. classes: [], // 类名集合,用于判断是否显示右边和下边框
  54. };
  55. },
  56. mounted() {
  57. this.init()
  58. },
  59. emits: ['click'],
  60. // 微信小程序中 options 选项
  61. options: {
  62. virtualHost: true //将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等
  63. },
  64. computed: {
  65. // #ifndef APP-NVUE
  66. // vue下放到computed中,否则会因为延时造成闪烁
  67. width() {
  68. if (this.parentData.col > 0) {
  69. return 100 / Number(this.parentData.col) + '%'
  70. } else {
  71. return 0;
  72. }
  73. },
  74. // #endif
  75. itemStyle() {
  76. const style = {
  77. background: this.bgColor,
  78. width: this.width
  79. }
  80. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  81. }
  82. },
  83. methods: {
  84. init() {
  85. // 用于在父组件u-grid的children中被添加入子组件时,
  86. // 重新计算item的边框
  87. uni.$on('$uGridItem', () => {
  88. this.gridItemClasses()
  89. })
  90. // 父组件的实例
  91. this.updateParentData()
  92. // #ifdef APP-NVUE
  93. // 获取元素该有的长度,nvue下要延时才准确
  94. this.$nextTick(function(){
  95. this.getItemWidth()
  96. })
  97. // #endif
  98. // 发出事件,通知所有的grid-item都重新计算自己的边框
  99. uni.$emit('$uGridItem')
  100. this.gridItemClasses()
  101. },
  102. // 获取父组件的参数
  103. updateParentData() {
  104. // 此方法写在mixin中
  105. this.getParentData('u-grid');
  106. },
  107. clickHandler() {
  108. let name = this.name
  109. // 如果没有设置name属性,历遍父组件的children数组,判断当前的元素是否和本实例this相等,找出当前组件的索引
  110. const children = this.parent?.children
  111. if(children && this.name === null) {
  112. name = children.findIndex(child => child === this)
  113. }
  114. // 调用父组件方法,发出事件
  115. this.parent && this.parent.childClick(name)
  116. this.$emit('click', name)
  117. },
  118. async getItemWidth() {
  119. // 如果是nvue,不能使用百分比,只能使用固定宽度
  120. let width = 0
  121. if(this.parent) {
  122. // 获取父组件宽度后,除以栅格数,得出每个item的宽度
  123. const parentWidth = await this.getParentWidth()
  124. width = parentWidth / Number(this.parentData.col) + 'px'
  125. }
  126. this.width = width
  127. },
  128. // 获取父元素的尺寸
  129. getParentWidth() {
  130. // #ifdef APP-NVUE
  131. // 返回一个promise,让调用者可以用await同步获取
  132. const dom = uni.requireNativePlugin('dom')
  133. return new Promise(resolve => {
  134. // 调用父组件的ref
  135. dom.getComponentRect(this.parent.$refs['u-grid'], res => {
  136. resolve(res.size.width)
  137. })
  138. })
  139. // #endif
  140. },
  141. gridItemClasses() {
  142. if(this.parentData.border) {
  143. let classes = []
  144. this.parent.children.map((child, index) =>{
  145. if(this === child) {
  146. const len = this.parent.children.length
  147. // 贴近右边屏幕边沿的child,并且最后一个(比如只有横向2个的时候),无需右边框
  148. if((index + 1) % this.parentData.col !== 0 && index + 1 !== len) {
  149. classes.push('u-border-right')
  150. }
  151. // 总的宫格数量对列数取余的值
  152. // 如果取余后,值为0,则意味着要将最后一排的宫格,都不需要下边框
  153. const lessNum = len % this.parentData.col === 0 ? this.parentData.col : len % this.parentData.col
  154. // 最下面的一排child,无需下边框
  155. if(index < len - lessNum) {
  156. classes.push('u-border-bottom')
  157. }
  158. }
  159. })
  160. // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  161. // #ifdef MP-ALIPAY || MP-TOUTIAO
  162. classes = classes.join(' ')
  163. // #endif
  164. this.classes = classes
  165. }
  166. }
  167. },
  168. beforeDestroy() {
  169. // 移除事件监听,释放性能
  170. uni.$off('$uGridItem')
  171. }
  172. };
  173. </script>
  174. <style lang="scss" scoped>
  175. @import "../../libs/css/components.scss";
  176. $u-grid-item-hover-class-opcatiy:.5 !default;
  177. $u-grid-item-margin-top:1rpx !default;
  178. $u-grid-item-border-right-width:0.5px !default;
  179. $u-grid-item-border-bottom-width:0.5px !default;
  180. $u-grid-item-border-right-color:$u-border-color !default;
  181. $u-grid-item-border-bottom-color:$u-border-color !default;
  182. .u-grid-item {
  183. align-items: center;
  184. justify-content: center;
  185. position: relative;
  186. flex-direction: column;
  187. /* #ifndef APP-NVUE */
  188. box-sizing: border-box;
  189. display: flex;
  190. /* #endif */
  191. /* #ifdef MP */
  192. position: relative;
  193. float: left;
  194. /* #endif */
  195. /* #ifdef MP-WEIXIN */
  196. margin-top:$u-grid-item-margin-top;
  197. /* #endif */
  198. &--hover-class {
  199. opacity:$u-grid-item-hover-class-opcatiy;
  200. }
  201. }
  202. /* #ifdef APP-NVUE */
  203. // 由于nvue不支持组件内引入app.vue中再引入的样式,所以需要写在这里
  204. .u-border-right {
  205. border-right-width:$u-grid-item-border-right-width;
  206. border-color: $u-grid-item-border-right-color;
  207. }
  208. .u-border-bottom {
  209. border-bottom-width:$u-grid-item-border-bottom-width;
  210. border-color:$u-grid-item-border-bottom-color;
  211. }
  212. /* #endif */
  213. </style>