3
0

u-row.vue 2.9 KB

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