You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.4 KiB

5 months ago
  1. <template>
  2. <view
  3. class="u-col"
  4. ref="u-col"
  5. :class="[
  6. 'u-col-' + span
  7. ]"
  8. :style="[colStyle]"
  9. @tap="clickHandler"
  10. >
  11. <slot></slot>
  12. </view>
  13. </template>
  14. <script>
  15. import { props } from './props';
  16. import { mpMixin } from '../../libs/mixin/mpMixin';
  17. import { mixin } from '../../libs/mixin/mixin';
  18. import { addStyle, addUnit, deepMerge, getPx } from '../../libs/function/index';
  19. /**
  20. * CodeInput 栅格系统的列
  21. * @description 该组件一般用于Layout 布局 通过基础的 12 分栏迅速简便地创建布局
  22. * @tutorial https://ijry.github.io/uview-plus/components/Layout.html
  23. * @property {String | Number} span 栅格占据的列数总12等份 (默认 12 )
  24. * @property {String | Number} offset 分栏左边偏移计算方式与span相同 (默认 0 )
  25. * @property {String} justify 水平排列方式可选值为`start`(`flex-start`)`end`(`flex-end`)`center``around`(`space-around`)`between`(`space-between`) (默认 'start' )
  26. * @property {String} align 垂直对齐方式可选值为topcenterbottomstretch (默认 'stretch' )
  27. * @property {String} textAlign 文字水平对齐方式 (默认 'left' )
  28. * @property {Object} customStyle 定义需要用到的外部样式
  29. * @event {Function} click col被点击会阻止事件冒泡到row
  30. * @example <u-col span="3" offset="3" > <view class="demo-layout bg-purple"></view> </u-col>
  31. */
  32. export default {
  33. name: 'u-col',
  34. mixins: [mpMixin, mixin, props],
  35. data() {
  36. return {
  37. width: 0,
  38. parentData: {
  39. gutter: 0
  40. },
  41. gridNum: 12
  42. }
  43. },
  44. // 微信小程序中 options 选项
  45. options: {
  46. virtualHost: true // 将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等
  47. },
  48. computed: {
  49. uJustify() {
  50. if (this.justify == 'end' || this.justify == 'start') return 'flex-' + this.justify
  51. else if (this.justify == 'around' || this.justify == 'between') return 'space-' + this.justify
  52. else return this.justify
  53. },
  54. uAlignItem() {
  55. if (this.align == 'top') return 'flex-start'
  56. if (this.align == 'bottom') return 'flex-end'
  57. else return this.align
  58. },
  59. colStyle() {
  60. const style = {
  61. // 这里写成"padding: 0 10px"的形式是因为nvue的需要
  62. paddingLeft: addUnit(getPx(this.parentData.gutter)/2),
  63. paddingRight: addUnit(getPx(this.parentData.gutter)/2),
  64. alignItems: this.uAlignItem,
  65. justifyContent: this.uJustify,
  66. textAlign: this.textAlign,
  67. // #ifndef APP-NVUE
  68. // 在非nvue上,使用百分比形式
  69. flex: `0 0 ${100 / this.gridNum * this.span}%`,
  70. marginLeft: 100 / 12 * this.offset + '%',
  71. // #endif
  72. // #ifdef APP-NVUE
  73. // 在nvue上,由于无法使用百分比单位,这里需要获取父组件的宽度,再计算得出该有对应的百分比尺寸
  74. width: addUnit(Math.floor(this.width / this.gridNum * Number(this.span))),
  75. marginLeft: addUnit(Math.floor(this.width / this.gridNum * Number(this.offset))),
  76. // #endif
  77. }
  78. return deepMerge(style, addStyle(this.customStyle))
  79. }
  80. },
  81. mounted() {
  82. this.init()
  83. },
  84. emits: ["click"],
  85. methods: {
  86. async init() {
  87. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  88. this.updateParentData()
  89. this.width = await this.parent.getComponentWidth()
  90. },
  91. updateParentData() {
  92. this.getParentData('u-row')
  93. },
  94. clickHandler(e) {
  95. this.$emit('click');
  96. }
  97. },
  98. }
  99. </script>
  100. <style lang="scss" scoped>
  101. @import "../../libs/css/components.scss";
  102. .u-col {
  103. padding: 0;
  104. /* #ifndef APP-NVUE */
  105. box-sizing:border-box;
  106. /* #endif */
  107. /* #ifdef MP */
  108. display: block;
  109. /* #endif */
  110. }
  111. // nvue下百分比无效
  112. /* #ifndef APP-NVUE */
  113. .u-col-0 {
  114. width: 0;
  115. }
  116. .u-col-1 {
  117. width: calc(100%/12);
  118. }
  119. .u-col-2 {
  120. width: calc(100%/12 * 2);
  121. }
  122. .u-col-3 {
  123. width: calc(100%/12 * 3);
  124. }
  125. .u-col-4 {
  126. width: calc(100%/12 * 4);
  127. }
  128. .u-col-5 {
  129. width: calc(100%/12 * 5);
  130. }
  131. .u-col-6 {
  132. width: calc(100%/12 * 6);
  133. }
  134. .u-col-7 {
  135. width: calc(100%/12 * 7);
  136. }
  137. .u-col-8 {
  138. width: calc(100%/12 * 8);
  139. }
  140. .u-col-9 {
  141. width: calc(100%/12 * 9);
  142. }
  143. .u-col-10 {
  144. width: calc(100%/12 * 10);
  145. }
  146. .u-col-11 {
  147. width: calc(100%/12 * 11);
  148. }
  149. .u-col-12 {
  150. width: calc(100%/12 * 12);
  151. }
  152. /* #endif */
  153. </style>