uni-forms-item.vue 12.5 KB
Newer Older
芊里 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3 4
	<view class="uni-forms-item" :class="{ 'uni-forms-item--border': border, 'is-first-border': border && isFirstBorder, 'uni-forms-item-error': msg }">
		<view class="uni-forms-item__box">
			<view class="uni-forms-item__inner" :class="['is-direction-' + labelPos]">
study夏羽's avatar
update  
study夏羽 已提交
5 6
				<view class="uni-forms-item__label" :style="{ width: labelWid , justifyContent: justifyContent }">
					<slot name="label">
7
						<text v-if="required" class="is-required">*</text>
DCloud_JSON's avatar
DCloud_JSON 已提交
8 9
						<uni-icons v-if="leftIcon" class="label-icon" size="16" :type="leftIcon" :color="iconColor" />
						<text class="label-text">{{ label }}</text>
10

DCloud_JSON's avatar
DCloud_JSON 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
						<view v-if="label" class="label-seat"></view>
					</slot>
				</view>
				<view class="uni-forms-item__content" :class="{ 'is-input-error-border': msg }"><slot></slot></view>
			</view>
			<view
				v-if="msg"
				class="uni-error-message"
				:class="{ 'uni-error-msg--boeder': border }"
				:style="{
					paddingLeft: labelLeft
				}"
			>
				<text class="uni-error-message-text">{{ showMsg === 'undertext' ? msg : '' }}</text>
			</view>
		</view>
芊里 已提交
27 28 29 30
	</view>
</template>

<script>
DCloud_JSON's avatar
DCloud_JSON 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 * Field 输入框
 * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
 * @tutorial https://ext.dcloud.net.cn/plugin?id=21001
 * @property {Boolean} 	required 			是否必填,左边显示红色"*"号(默认false)
 * @property {String} validateTrigger = [bind|submit]	校验触发器方式 默认 submit 可选
 * 	@value bind 	发生变化时触发
 * 	@value submit 	提交时触发
 * @property {String } 	leftIcon 			label左边的图标,限 uni-ui 的图标名称
 * @property {String } 	iconColor 			左边通过icon配置的图标的颜色(默认#606266)
 * @property {String } 	label 				输入框左边的文字提示
 * @property {Number } 	labelWidth 			label的宽度,单位px(默认65)
 * @property {String } 	labelAlign = [left|center|right] label的文字对齐方式(默认left)
 * 	@value left		label 左侧显示
 * 	@value center	label 居中
 * 	@value right	label 右侧对齐
 * @property {String } 	labelPosition = [top|left] label的文字的位置(默认left)
 * 	@value top	顶部显示 label
 * 	@value left	左侧显示 label
 * @property {String } 	errorMessage 		显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
 * @property {String } 	name 				表单域的属性名,在使用校验规则时必填
 */
芊里 已提交
53

DCloud_JSON's avatar
DCloud_JSON 已提交
54 55 56 57 58 59 60
export default {
	name: 'uniFormsItem',
	props: {
		// 自定义内容
		custom: {
			type: Boolean,
			default: false
芊里 已提交
61
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
62 63 64 65
		// 是否显示报错信息
		showMessage: {
			type: Boolean,
			default: true
芊里 已提交
66
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
67 68 69 70 71
		name: String,
		required: Boolean,
		validateTrigger: {
			type: String,
			default: ''
芊里 已提交
72
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
73 74 75 76
		leftIcon: String,
		iconColor: {
			type: String,
			default: '#606266'
芊里 已提交
77
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		label: String,
		// 左边标题的宽度单位px
		labelWidth: {
			type: [Number, String],
			default: ''
		},
		// 对齐方式,left|center|right
		labelAlign: {
			type: String,
			default: ''
		},
		// lable的位置,可选为 left-左边,top-上边
		labelPosition: {
			type: String,
			default: ''
		},
		errorMessage: {
			type: [String, Boolean],
			default: ''
		},
		// 表单校验规则
		rules: {
			type: Array,
			default() {
				return [];
芊里 已提交
103
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		}
	},
	data() {
		return {
			errorTop: false,
			errorBottom: false,
			labelMarginBottom: '',
			errorWidth: '',
			errMsg: '',
			val: '',
			labelPos: '',
			labelWid: '',
			labelAli: '',
			showMsg: 'undertext',
			border: false,
			isFirstBorder: false,
			isArray: false,
			arrayField: ''
		};
	},
	computed: {
		msg() {
			return this.errorMessage || this.errMsg;
芊里 已提交
127
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
		fieldStyle() {
			let style = {};
			if (this.labelPos == 'top') {
				style.padding = '0 0';
				this.labelMarginBottom = '6px';
			}
			if (this.labelPos == 'left' && this.msg !== false && this.msg != '') {
				style.paddingBottom = '0px';
				this.errorBottom = true;
				this.errorTop = false;
			} else if (this.labelPos == 'top' && this.msg !== false && this.msg != '') {
				this.errorBottom = false;
				this.errorTop = true;
			} else {
				// style.paddingBottom = ''
				this.errorTop = false;
				this.errorBottom = false;
芊里 已提交
145
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
146
			return style;
芊里 已提交
147
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

		// uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
		justifyContent() {
			if (this.labelAli === 'left') return 'flex-start';
			if (this.labelAli === 'center') return 'center';
			if (this.labelAli === 'right') return 'flex-end';
		},
		labelLeft(){
			return (this.labelPos === 'left' ? parseInt(this.labelWid) : 0) + 'px'
		}
	},
	watch: {
		validateTrigger(trigger) {
			this.formTrigger = trigger;
		}
	},
	created() {
		this.form = this.getForm();
		this.group = this.getForm('uniGroup');
		this.formRules = [];
		this.formTrigger = this.validateTrigger;
		// 处理 name,是否数组
		if (this.name && this.name.indexOf('[') !== -1 && this.name.indexOf(']') !== -1) {
			this.isArray = true;
			this.arrayField = this.name
			// fix by mehaotian 修改不修改的情况,动态值不检验的问题
			this.form.formData[this.name] = this.form._getValue(this.name, '')
		}
	},
	mounted() {
		if (this.form) {
			this.form.childrens.push(this);
		}
		this.init();
	},
study夏羽's avatar
update  
study夏羽 已提交
183 184 185 186 187 188 189 190 191 192 193
	// #ifndef VUE3
	destroyed() {
		if(this.__isUnmounted) return
		this.unInit()
	},
	// #endif
	// #ifdef VUE3
	unmounted(){
		this.__isUnmounted = true
		this.unInit()
	},
194
	// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
195 196 197 198
	methods: {
		init() {
			if (this.form) {
				let { formRules, validator, formData, value, labelPosition, labelWidth, labelAlign, errShowType } = this.form;
study夏羽's avatar
update  
study夏羽 已提交
199 200 201 202 203 204 205 206 207 208
				this.labelPos = this.labelPosition ? this.labelPosition : labelPosition;

				if(this.label){
					this.labelWid = (this.labelWidth ? this.labelWidth : (labelWidth||70))
				}else{
					this.labelWid =( this.labelWidth ? this.labelWidth : (labelWidth||'auto'))
				}
				if(this.labelWid && this.labelWid !=='auto') {
					this.labelWid +='px'
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
209
				this.labelAli = this.labelAlign ? this.labelAlign : labelAlign;
芊里 已提交
210

DCloud_JSON's avatar
DCloud_JSON 已提交
211 212 213 214 215
				// 判断第一个 item
				if (!this.form.isFirstBorder) {
					this.form.isFirstBorder = true;
					this.isFirstBorder = true;
				}
芊里 已提交
216

DCloud_JSON's avatar
DCloud_JSON 已提交
217 218 219 220 221
				// 判断 group 里的第一个 item
				if (this.group) {
					if (!this.group.isFirstBorder) {
						this.group.isFirstBorder = true;
						this.isFirstBorder = true;
芊里 已提交
222
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
223
				}
芊里 已提交
224

DCloud_JSON's avatar
DCloud_JSON 已提交
225 226 227
				this.border = this.form.border;
				this.showMsg = errShowType;
				let name = this.isArray ? this.arrayField : this.name;
228 229
				if(!name) return
				if (formRules && this.rules.length > 0) {
DCloud_JSON's avatar
DCloud_JSON 已提交
230 231 232 233
					if (!formRules[name]) {
						formRules[name] = {
							rules: this.rules
						}
芊里 已提交
234
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
235
					validator.updateSchema(formRules);
芊里 已提交
236
				}
237
				this.formRules = formRules[name] || {};
DCloud_JSON's avatar
DCloud_JSON 已提交
238 239 240 241 242 243
				this.validator = validator;
			} else {
				this.labelPos = this.labelPosition || 'left';
				this.labelWid = this.labelWidth || 65;
				this.labelAli = this.labelAlign || 'left';
			}
study夏羽's avatar
update  
study夏羽 已提交
244 245 246 247 248 249 250 251 252 253
		},
		unInit(){
			if (this.form) {
				this.form.childrens.forEach((item, index) => {
					if (item === this) {
						this.form.childrens.splice(index, 1)
						delete this.form.formData[item.name]
					}
				})
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267
		},
		/**
		 * 获取父元素实例
		 */
		getForm(name = 'uniForms') {
			let parent = this.$parent;
			let parentName = parent.$options.name;
			while (parentName !== name) {
				parent = parent.$parent;
				if (!parent) return false;
				parentName = parent.$options.name;
			}
			return parent;
		},
芊里 已提交
268

DCloud_JSON's avatar
DCloud_JSON 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		/**
		 * 移除该表单项的校验结果
		 */
		clearValidate() {
			this.errMsg = '';
		},
		/**
		 * 子组件调用,如 easyinput
		 * @param {Object} value
		 */
		setValue(value) {
			let name = this.isArray ? this.arrayField : this.name;
			if (name) {
				if (this.errMsg) this.errMsg = '';
				// 给组件赋值
				this.form.formData[name] = this.form._getValue(name, value);
				if (!this.formRules || (typeof this.formRules && JSON.stringify(this.formRules) === '{}')) return;
				this.triggerCheck(this.form._getValue(this.name, value));
			}
		},
芊里 已提交
289

DCloud_JSON's avatar
DCloud_JSON 已提交
290 291 292 293 294 295
		/**
		 * 校验规则
		 * @param {Object} value
		 */
		async triggerCheck(value,formTrigger) {
			let promise = null;
study夏羽's avatar
update  
study夏羽 已提交
296
			this.errMsg = '';
297 298
			// fix by mehaotian 解决没有检验规则的情况下,抛出错误的问题
			if (!this.validator || Object.keys(this.formRules).length === 0) return;
DCloud_JSON's avatar
DCloud_JSON 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
			const isNoField = this.isRequired(this.formRules.rules || []);
			let isTrigger = this.isTrigger(this.formRules.validateTrigger, this.validateTrigger, this.form.validateTrigger);
			let result = null;
			if (!!isTrigger || formTrigger) {
				let name = this.isArray ? this.arrayField : this.name;
				result = await this.validator.validateUpdate(
					{
						[name]: value
					},
					this.form.formData
				);
			}
			// 判断是否必填,非必填,不填不校验,填写才校验
			if (!isNoField && (value === undefined || value === '')) {
				result = null;
			}
			const inputComp = this.form.inputChildrens.find(child => child.rename === this.name);
			if ((isTrigger || formTrigger) && result && result.errorMessage) {
				if (inputComp) {
					inputComp.errMsg = result.errorMessage;
芊里 已提交
319
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
320 321 322 323 324
				if (this.form.errShowType === 'toast') {
					uni.showToast({
						title: result.errorMessage || '校验错误',
						icon: 'none'
					});
芊里 已提交
325
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
326 327 328 329 330
				if (this.form.errShowType === 'modal') {
					uni.showModal({
						title: '提示',
						content: result.errorMessage || '校验错误'
					});
芊里 已提交
331
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
332 333 334 335 336
			} else {
				if (inputComp) {
					inputComp.errMsg = '';
				}
			}
芊里 已提交
337

DCloud_JSON's avatar
DCloud_JSON 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
			this.errMsg = !result ? '' : result.errorMessage;
			// 触发validate事件
			this.form.validateCheck(result ? result : null);
			// typeof callback === 'function' && callback(result ? result : null);
			// if (promise) return promise
			return result ? result : null;
		},
		/**
		 * 触发时机
		 * @param {Object} event
		 */
		isTrigger(rule, itemRlue, parentRule) {
			let rl = true;
			//  bind  submit
			if (rule === 'submit' || !rule) {
				if (rule === undefined) {
					if (itemRlue !== 'bind') {
						if (!itemRlue) {
							return parentRule === 'bind' ? true : false;
芊里 已提交
357
						}
DCloud_JSON's avatar
DCloud_JSON 已提交
358
						return false;
芊里 已提交
359
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
360
					return true;
芊里 已提交
361
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
362 363 364 365 366 367 368 369 370 371 372 373
				return false;
			}
			return true;
		},
		// 是否有必填字段
		isRequired(rules) {
			let isNoField = false;
			for (let i = 0; i < rules.length; i++) {
				const ruleData = rules[i];
				if (ruleData.required) {
					isNoField = true;
					break;
芊里 已提交
374 375
				}
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
376
			return isNoField;
芊里 已提交
377
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
378 379
	}
};
芊里 已提交
380 381
</script>

study夏羽's avatar
update  
study夏羽 已提交
382
<style lang="scss" >
DCloud_JSON's avatar
DCloud_JSON 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
.uni-forms-item {
	position: relative;
	padding: 0px;
	text-align: left;
	color: #333;
	font-size: 14px;
	// margin-bottom: 22px;
}

.uni-forms-item__box {
	position: relative;
}

.uni-forms-item__inner {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	// flex-direction: row;
	// align-items: center;
	padding-bottom: 22px;
	// margin-bottom: 22px;
}

.is-direction-left {
	flex-direction: row;
}

.is-direction-top {
	flex-direction: column;
}

.uni-forms-item__label {
	/* #ifndef APP-NVUE */
	display: flex;
	flex-shrink: 0;
	box-sizing: border-box;
	/* #endif */
	flex-direction: row;
	align-items: center;
	width: 65px;
	// line-height: 2;
	// margin-top: 3px;
	padding: 5px 0;
	height: 36px;
	// margin-right: 5px;

	.label-text {
study夏羽's avatar
update  
study夏羽 已提交
430 431
		font-size: 13px;
		color: #666666;
芊里 已提交
432
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	.label-seat {
		margin-right: 5px;
	}
}

.uni-forms-item__content {
	/* #ifndef APP-NVUE */
	width: 100%;
	box-sizing: border-box;
	min-height: 36px;
	/* #endif */
	flex: 1;
}

.label-icon {
	margin-right: 5px;
	margin-top: -1px;
}

// 必填
.is-required {
454
	// color: $uni-color-error;
study夏羽's avatar
update  
study夏羽 已提交
455
	color: #dd524d;
456
	font-weight: bold;
DCloud_JSON's avatar
DCloud_JSON 已提交
457 458 459 460 461 462 463 464 465 466 467
}

.uni-error-message {
	position: absolute;
	bottom: 0px;
	left: 0;
	text-align: left;
}

.uni-error-message-text {
	line-height: 22px;
study夏羽's avatar
update  
study夏羽 已提交
468
	color: #dd524d;
DCloud_JSON's avatar
DCloud_JSON 已提交
469 470 471 472 473 474 475 476 477 478
	font-size: 12px;
}

.uni-error-msg--boeder {
	position: relative;
	bottom: 0;
	line-height: 22px;
}

.is-input-error-border {
study夏羽's avatar
update  
study夏羽 已提交
479
	border-color: #dd524d;
DCloud_JSON's avatar
DCloud_JSON 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
}

.uni-forms-item--border {
	margin-bottom: 0;
	padding: 10px 0;
	// padding-bottom: 0;
	border-top: 1px #eee solid;

	.uni-forms-item__inner {
		padding: 0;
	}
}

.uni-forms-item-error {
	// padding-bottom: 0;
}

.is-first-border {
	/* #ifndef APP-NVUE */
	border: none;
	/* #endif */
	/* #ifdef APP-NVUE */
	border-width: 0;
	/* #endif */
}

.uni-forms--no-padding {
	padding: 0;
}
study夏羽's avatar
update  
study夏羽 已提交
509
</style>