u-row.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. emits: ["click"],
  62. methods: {
  63. clickHandler(e) {
  64. this.$emit('click')
  65. },
  66. async getComponentWidth() {
  67. // 延时一定时间,以确保节点渲染完成
  68. await uni.$u.sleep()
  69. return new Promise(resolve => {
  70. // uView封装的获取节点的方法,详见文档
  71. // #ifndef APP-NVUE
  72. this.$uGetRect('.u-row').then(res => {
  73. resolve(res.width)
  74. })
  75. // #endif
  76. // #ifdef APP-NVUE
  77. // nvue的dom模块用于获取节点
  78. dom.getComponentRect(this.$refs['u-row'], (res) => {
  79. resolve(res.size.width)
  80. })
  81. // #endif
  82. })
  83. },
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. @import "../../libs/css/components.scss";
  89. .u-row {
  90. @include flex;
  91. }
  92. </style>