u-row.vue 2.8 KB

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