Blame view

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