u-row.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <view
  3. class="u-row"
  4. ref="u-row"
  5. :style="[rowStyle]"
  6. @tap="clickHandler"
  7. >
  8. <slot />
  9. </view>
  10. </template>
  11. <script>
  12. // #ifdef APP-NVUE
  13. const dom = uni.requireNativePlugin('dom')
  14. // #endif
  15. import props from './props.js';
  16. import mpMixin from '../../libs/mixin/mpMixin.js';
  17. import mixin from '../../libs/mixin/mixin.js';
  18. /**
  19. * Row 栅格系统中的行
  20. * @description 通过基础的 12 分栏,迅速简便地创建布局
  21. * @tutorial https://ijry.github.io/uview-plus/components/layout.html
  22. * @property {String | Number} gutter 栅格间隔,左右各为此值的一半,单位px (默认 0 )
  23. * @property {String} justify 水平排列方式(微信小程序暂不支持) 可选值为`start`(或`flex-start`)、`end`(或`flex-end`)、`center`、`around`(或`space-around`)、`between`(或`space-between`) (默认 'start' )
  24. * @property {String} align 垂直排列方式 (默认 'center' )
  25. * @property {Object} customStyle 定义需要用到的外部样式
  26. *
  27. * @event {Function} click row被点击
  28. * @example <u-row justify="space-between" customStyle="margin-bottom: 10px"></u-row>
  29. */
  30. export default {
  31. name: "u-row",
  32. mixins: [mpMixin, mixin, props],
  33. data() {
  34. return {
  35. }
  36. },
  37. computed: {
  38. uJustify() {
  39. if (this.justify == 'end' || this.justify == 'start') return 'flex-' + this.justify
  40. else if (this.justify == 'around' || this.justify == 'between') return 'space-' + this.justify
  41. else return this.justify
  42. },
  43. uAlignItem() {
  44. if (this.align == 'top') return 'flex-start'
  45. if (this.align == 'bottom') return 'flex-end'
  46. else return this.align
  47. },
  48. rowStyle() {
  49. const style = {
  50. alignItems: this.uAlignItem,
  51. justifyContent: this.uJustify
  52. }
  53. // 通过给u-row左右两边的负外边距,消除u-col在有gutter时,第一个和最后一个元素的左内边距和右内边距造成的影响
  54. if(this.gutter) {
  55. style.marginLeft = uni.$u.addUnit(-Number(this.gutter)/2)
  56. style.marginRight = uni.$u.addUnit(-Number(this.gutter)/2)
  57. }
  58. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  59. }
  60. },
  61. methods: {
  62. clickHandler(e) {
  63. this.$emit('click')
  64. },
  65. async getComponentWidth() {
  66. // 延时一定时间,以确保节点渲染完成
  67. await uni.$u.sleep()
  68. return new Promise(resolve => {
  69. // uView封装的获取节点的方法,详见文档
  70. // #ifndef APP-NVUE
  71. this.$uGetRect('.u-row').then(res => {
  72. resolve(res.width)
  73. })
  74. // #endif
  75. // #ifdef APP-NVUE
  76. // nvue的dom模块用于获取节点
  77. dom.getComponentRect(this.$refs['u-row'], (res) => {
  78. resolve(res.size.width)
  79. })
  80. // #endif
  81. })
  82. },
  83. }
  84. }
  85. </script>
  86. <style lang="scss" scoped>
  87. @import "../../libs/css/components.scss";
  88. .u-row {
  89. @include flex;
  90. }
  91. </style>