uni-easyinput.vue 12.5 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
	<view class="uni-easyinput" :class="{'uni-easyinput-error':msg}" :style="{color:inputBorder && msg?'#e43d33':styles.color}">
		<view class="uni-easyinput__content" :class="{'is-input-border':inputBorder ,'is-input-error-border':inputBorder && msg,'is-textarea':type==='textarea','is-disabled':disabled}"
		 :style="{'border-color':inputBorder && msg?'#dd524d':styles.borderColor,'background-color':disabled?styles.disableColor:''}">
			<uni-icons v-if="prefixIcon" class="content-clear-icon" :type="prefixIcon" color="#c0c4cc" @click="onClickIcon('prefix')"></uni-icons>
			<textarea v-if="type === 'textarea'" class="uni-easyinput__content-textarea" :class="{'input-padding':inputBorder}"
			 :name="name" :value="val" :placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled" placeholder-class="uni-easyinput__placeholder-class"
			 :maxlength="inputMaxlength" :focus="focused" :autoHeight="autoHeight" @input="onInput" @blur="onBlur"  @focus="onFocus"
			 @confirm="onConfirm"></textarea>
			<input v-else :type="type === 'password'?'text':type" class="uni-easyinput__content-input" :style="{
				 'padding-right':type === 'password' ||clearable || prefixIcon?'':'10px',
				 'padding-left':prefixIcon?'':'10px'
			 }"
			 :name="name" :value="val" :password="!showPassword && type === 'password'" :placeholder="placeholder"
			 :placeholderStyle="placeholderStyle" placeholder-class="uni-easyinput__placeholder-class" :disabled="disabled" :maxlength="inputMaxlength" :focus="focused" :confirmType="confirmType" @focus="onFocus"
			 @blur="onBlur" @input="onInput" @confirm="onConfirm" />
			<template v-if="type === 'password' && passwordIcon" >
				<uni-icons v-if="val" class="content-clear-icon" :class="{'is-textarea-icon':type==='textarea'}" :type="showPassword?'eye-slash-filled':'eye-filled'"
				 :size="18" color="#c0c4cc" @click="onEyes"></uni-icons>
DCloud_JSON's avatar
DCloud_JSON 已提交
20 21
			</template>
			<template v-else-if="suffixIcon">
DCloud_JSON's avatar
DCloud_JSON 已提交
22
				<uni-icons v-if="suffixIcon" class="content-clear-icon" :type="suffixIcon" color="#c0c4cc" @click="onClickIcon('suffix')"></uni-icons>
DCloud_JSON's avatar
DCloud_JSON 已提交
23 24
			</template>
			<template v-else>
DCloud_JSON's avatar
DCloud_JSON 已提交
25 26
				<uni-icons class="content-clear-icon" :class="{'is-textarea-icon':type==='textarea'}" type="clear" :size="clearSize"
				 v-if="clearable && val && !disabled" color="#c0c4cc" @click="onClear"></uni-icons>
27
			</template>
DCloud_JSON's avatar
DCloud_JSON 已提交
28 29 30 31 32
			<slot name="right"></slot>
		</view>
	</view>
</template>

33
<script>
DCloud_JSON's avatar
DCloud_JSON 已提交
34 35 36 37
	// import {
	// 	debounce,
	// 	throttle
	// } from './common.js'
DCloud_JSON's avatar
DCloud_JSON 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
	/**
	 * Easyinput 输入框
	 * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=3455
	 * @property {String}	value	输入内容
	 * @property {String }	type	输入框的类型(默认text) password/text/textarea/..
	 * 	@value text			文本输入键盘
	 * 	@value textarea	多行文本输入键盘
	 * 	@value password	密码输入键盘
	 * 	@value number		数字输入键盘,注意iOS上app-vue弹出的数字键盘并非9宫格方式
	 * 	@value idcard		身份证输入键盘,信、支付宝、百度、QQ小程序
	 * 	@value digit		带小数点的数字键盘	,App的nvue页面、微信、支付宝、百度、头条、QQ小程序支持
	 * @property {Boolean}	clearable	是否显示右侧清空内容的图标控件,点击可清空输入框内容(默认true)
DCloud_JSON's avatar
DCloud_JSON 已提交
51
	 * @property {Boolean}	autoHeight	是否自动增高输入区域,type为textarea时有效(默认false)
DCloud_JSON's avatar
DCloud_JSON 已提交
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
	 * @property {String }	placeholder	输入框的提示文字
	 * @property {String }	placeholderStyle	placeholder的样式(内联样式,字符串),如"color: #ddd"
	 * @property {Boolean}	focus	是否自动获得焦点(默认false)
	 * @property {Boolean}	disabled	是否禁用(默认false)
	 * @property {Number }	maxlength	最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
	 * @property {String }	confirmType	设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
	 * @property {Number }	clearSize	清除图标的大小,单位px(默认15)
	 * @property {String}	prefixIcon	输入框头部图标
	 * @property {String}	suffixIcon	输入框尾部图标
	 * @property {Boolean}	trim	是否自动去除两端的空格
	 * @value both	去除两端空格
	 * @value left	去除左侧空格
	 * @value right	去除右侧空格
	 * @value start	去除左侧空格
	 * @value end		去除右侧空格
	 * @value all		去除全部空格
	 * @value none	不去除空格
	 * @property {Boolean}	inputBorder	是否显示input输入框的边框(默认true)
	 * @property {Boolean}	passwordIcon	type=password时是否显示小眼睛图标
	 * @property {Object}	styles	自定义颜色
	 * @event {Function}	input	输入框内容发生变化时触发
	 * @event {Function}	focus	输入框获得焦点时触发
	 * @event {Function}	blur	输入框失去焦点时触发
	 * @event {Function}	confirm	点击完成按钮时触发
	 * @event {Function}	iconClick	点击图标时触发
	 * @example <uni-easyinput v-model="mobile"></uni-easyinput>
	 */

DCloud_JSON's avatar
DCloud_JSON 已提交
80
	 export default {
DCloud_JSON's avatar
DCloud_JSON 已提交
81
		name: 'uni-easyinput',
DCloud_JSON's avatar
DCloud_JSON 已提交
82 83 84 85
		emits:['click','iconClick','update:modelValue','input','focus','blur','confirm'],
		model:{
			prop:'modelValue',
			event:'update:modelValue'
DCloud_JSON's avatar
DCloud_JSON 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		},
		props: {
			name: String,
			value: [Number, String],
			modelValue: [Number, String],
			type: {
				type: String,
				default: 'text'
			},
			clearable: {
				type: Boolean,
				default: true
			},
			autoHeight: {
				type: Boolean,
				default: false
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
103
			placeholder: String,
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
			placeholderStyle: String,
			focus: {
				type: Boolean,
				default: false
			},
			disabled: {
				type: Boolean,
				default: false
			},
			maxlength: {
				type: [Number, String],
				default: 140
			},
			confirmType: {
				type: String,
				default: 'done'
			},
			clearSize: {
				type: [Number, String],
DCloud_JSON's avatar
DCloud_JSON 已提交
123
				default: 15
DCloud_JSON's avatar
DCloud_JSON 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
			},
			inputBorder: {
				type: Boolean,
				default: true
			},
			prefixIcon: {
				type: String,
				default: ''
			},
			suffixIcon: {
				type: String,
				default: ''
			},
			trim: {
				type: [Boolean, String],
				default: true
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
141
			passwordIcon:{
DCloud_JSON's avatar
DCloud_JSON 已提交
142 143 144 145 146 147 148 149
				type: Boolean,
				default: true
			},
			styles: {
				type: Object,
				default () {
					return {
						color: '#333',
150
						disableColor: '#F7F6F6',
DCloud_JSON's avatar
DCloud_JSON 已提交
151 152 153 154
						borderColor: '#e5e5e5'
					}
				}
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
155 156 157
			errorMessage:{
				type:[String,Boolean],
				default:''
DCloud_JSON's avatar
DCloud_JSON 已提交
158 159 160 161 162
			}
		},
		data() {
			return {
				focused: false,
DCloud_JSON's avatar
DCloud_JSON 已提交
163
				errMsg: '',
DCloud_JSON's avatar
DCloud_JSON 已提交
164 165 166 167 168
				val: '',
				showMsg: '',
				border: false,
				isFirstBorder: false,
				showClearIcon: false,
DCloud_JSON's avatar
DCloud_JSON 已提交
169
				showPassword: false
DCloud_JSON's avatar
DCloud_JSON 已提交
170 171 172 173
			};
		},
		computed: {
			msg() {
DCloud_JSON's avatar
DCloud_JSON 已提交
174
				return this.errorMessage || this.errMsg;
DCloud_JSON's avatar
DCloud_JSON 已提交
175
			},
176
			// 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,用户可以传入字符串数值
DCloud_JSON's avatar
DCloud_JSON 已提交
177 178 179 180 181 182
			inputMaxlength() {
				return Number(this.maxlength);
			},
		},
		watch: {
			value(newVal) {
DCloud_JSON's avatar
DCloud_JSON 已提交
183
				if (this.errMsg) this.errMsg = ''
184
				this.val = newVal
DCloud_JSON's avatar
DCloud_JSON 已提交
185 186 187 188 189
				// fix by mehaotian is_reset 在 uni-forms 中定义
				if (this.form && this.formItem &&!this.is_reset) {
					this.is_reset = false
					this.formItem.setValue(newVal)
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
190 191
			},
			modelValue(newVal) {
DCloud_JSON's avatar
DCloud_JSON 已提交
192
				if (this.errMsg) this.errMsg = ''
DCloud_JSON's avatar
DCloud_JSON 已提交
193
				this.val = newVal
DCloud_JSON's avatar
DCloud_JSON 已提交
194 195 196 197
				if (this.form && this.formItem &&!this.is_reset) {
					this.is_reset = false
					this.formItem.setValue(newVal)
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
198 199 200 201 202 203 204 205
			},
			focus(newVal) {
				this.$nextTick(() => {
					this.focused = this.focus
				})
			}
		},
		created() {
DCloud_JSON's avatar
DCloud_JSON 已提交
206 207 208 209 210 211 212 213
			if(!this.value && this.value !== 0){
				this.val = this.modelValue
			}
			if(!this.modelValue && this.modelValue !== 0){
				this.val = this.value
			}
			this.form = this.getForm('uniForms')
			this.formItem = this.getForm('uniFormsItem')
DCloud_JSON's avatar
DCloud_JSON 已提交
214
			if (this.form && this.formItem) {
DCloud_JSON's avatar
DCloud_JSON 已提交
215 216 217 218 219 220 221 222
				if (this.formItem.name) {
					if(!this.is_reset){
						this.is_reset = false
						this.formItem.setValue(this.val)
					}
					this.rename = this.formItem.name
					this.form.inputChildrens.push(this)
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
223 224 225 226 227 228 229 230 231 232 233 234
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.focused = this.focus
			})
		},
		methods: {
			/**
			 * 初始化变量值
			 */
			init() {
235

DCloud_JSON's avatar
DCloud_JSON 已提交
236
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
237 238 239 240
			onClickIcon(type) {
				this.$emit('iconClick', type)
			},
			/**
DCloud_JSON's avatar
DCloud_JSON 已提交
241
			 * 获取父元素实例
DCloud_JSON's avatar
DCloud_JSON 已提交
242
			 */
DCloud_JSON's avatar
DCloud_JSON 已提交
243 244 245 246 247 248 249 250 251 252 253
			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;
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
			onEyes() {
				this.showPassword = !this.showPassword
			},
			onInput(event) {
				let value = event.detail.value;
				// 判断是否去除空格
				if (this.trim) {
					if (typeof(this.trim) === 'boolean' && this.trim) {
						value = this.trimStr(value)
					}
					if (typeof(this.trim) === 'string') {
						value = this.trimStr(value, this.trim)
					}
				};
				if (this.errMsg) this.errMsg = ''
				this.val = value
				// TODO 兼容 vue2
				this.$emit('input', value);
				// TODO 兼容 vue3
DCloud_JSON's avatar
DCloud_JSON 已提交
273
				this.$emit('update:modelValue',value)
DCloud_JSON's avatar
DCloud_JSON 已提交
274 275
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
276
			onFocus(event) {
DCloud_JSON's avatar
DCloud_JSON 已提交
277 278
				this.$emit('focus', event);
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
279
			onBlur(event) {
DCloud_JSON's avatar
DCloud_JSON 已提交
280 281 282 283
				let value = event.detail.value;
				this.$emit('blur', event);
			},
			onConfirm(e) {
DCloud_JSON's avatar
DCloud_JSON 已提交
284
				this.$emit('confirm', e.detail.value);
DCloud_JSON's avatar
DCloud_JSON 已提交
285 286 287 288 289 290 291
			},
			onClear(event) {
				this.val = '';
				// TODO 兼容 vue2
				this.$emit('input', '');
				// TODO 兼容 vue2
				// TODO 兼容 vue3
DCloud_JSON's avatar
DCloud_JSON 已提交
292 293 294 295
				this.$emit('update:modelValue','')
			},
			fieldClick() {
				this.$emit('click');
DCloud_JSON's avatar
DCloud_JSON 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
			},
			trimStr(str, pos = 'both') {
				if (pos === 'both') {
					return str.trim();
				} else if (pos === 'left') {
					return str.trimLeft();
				} else if (pos === 'right') {
					return str.trimRight();
				} else if (pos === 'start') {
					return str.trimStart()
				} else if (pos === 'end') {
					return str.trimEnd()
				} else if (pos === 'all') {
					return str.replace(/\s+/g, '');
				} else if (pos === 'none') {
					return str;
				}
				return str;
			}
		}
	};
</script>

DCloud_JSON's avatar
DCloud_JSON 已提交
319
<style lang="scss" >
320
	$uni-error: #e43d33;
321
	$uni-border-1: #DCDFE6 !default;
DCloud_JSON's avatar
DCloud_JSON 已提交
322 323 324 325 326 327 328 329
	.uni-easyinput {
		/* #ifndef APP-NVUE */
		width: 100%;
		/* #endif */
		flex: 1;
		position: relative;
		text-align: left;
		color: #333;
330
		font-size: 14px;
DCloud_JSON's avatar
DCloud_JSON 已提交
331 332 333 334 335 336 337
	}

	.uni-easyinput__content {
		flex: 1;
		/* #ifndef APP-NVUE */
		width: 100%;
		display: flex;
338
		box-sizing: border-box;
DCloud_JSON's avatar
DCloud_JSON 已提交
339
		min-height: 36px;
340
		/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
341
		flex-direction: row;
342
		align-items: center;
DCloud_JSON's avatar
DCloud_JSON 已提交
343 344 345 346 347 348 349 350 351 352
	}

	.uni-easyinput__content-input {
		/* #ifndef APP-NVUE */
		width: auto;
		/* #endif */
		position: relative;
		overflow: hidden;
		flex: 1;
		line-height: 1;
353
		font-size: 14px;
354
	}
355 356 357
	.uni-easyinput__placeholder-class {
		color: #999;
		font-size: 12px;
DCloud_JSON's avatar
DCloud_JSON 已提交
358
		font-weight: 200;
DCloud_JSON's avatar
DCloud_JSON 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	}
	.is-textarea {
		align-items: flex-start;
	}

	.is-textarea-icon {
		margin-top: 5px;
	}

	.uni-easyinput__content-textarea {
		position: relative;
		overflow: hidden;
		flex: 1;
		line-height: 1.5;
		font-size: 14px;
DCloud_JSON's avatar
DCloud_JSON 已提交
374 375
		padding-top: 6px;
		padding-bottom: 10px;
DCloud_JSON's avatar
DCloud_JSON 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
		height: 80px;
		/* #ifndef APP-NVUE */
		min-height: 80px;
		width: auto;
		/* #endif */
	}

	.input-padding {
		padding-left: 10px;
	}

	.content-clear-icon {
		padding: 0 5px;
	}

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

	// 显示边框
	.is-input-border {
		/* #ifndef APP-NVUE */
		display: flex;
		box-sizing: border-box;
		/* #endif */
		flex-direction: row;
		align-items: center;
404
		border: 1px solid $uni-border-1;
DCloud_JSON's avatar
DCloud_JSON 已提交
405 406 407 408 409 410 411 412
		border-radius: 4px;
	}

	.uni-error-message {
		position: absolute;
		bottom: -17px;
		left: 0;
		line-height: 12px;
413
		color: $uni-error;
DCloud_JSON's avatar
DCloud_JSON 已提交
414 415 416 417 418 419 420 421 422 423 424
		font-size: 12px;
		text-align: left;
	}

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

	.is-input-error-border {
425 426
		border-color: $uni-error;
		.uni-easyinput__placeholder-class {
DCloud_JSON's avatar
DCloud_JSON 已提交
427
			color: mix(#fff, $uni-error, 50%);;
428
		}
429
	}
430

DCloud_JSON's avatar
DCloud_JSON 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

	.uni-easyinput--border {
		margin-bottom: 0;
		padding: 10px 15px;
		// padding-bottom: 0;
		border-top: 1px #eee solid;
	}

	.uni-easyinput-error {
		padding-bottom: 0;
	}

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

452
	.is-disabled {
DCloud_JSON's avatar
DCloud_JSON 已提交
453
		border-color: red;
454 455 456 457 458
		background-color: #F7F6F6;
		color: #D5D5D5;
		.uni-easyinput__placeholder-class {
			color: #D5D5D5;
			font-size: 12px;
459
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
460
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
461
</style>