From 18a0ea62a8090493f13fd2d59c9cf7d5d91510f2 Mon Sep 17 00:00:00 2001 From: xiaoyucoding Date: Tue, 27 Nov 2018 14:28:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=20number=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=20input=20=E6=94=AF=E6=8C=81=20maxlength=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/view/components/input/index.vue | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/core/view/components/input/index.vue b/src/core/view/components/input/index.vue index f8671b563..7c0fe8b39 100644 --- a/src/core/view/components/input/index.vue +++ b/src/core/view/components/input/index.vue @@ -31,7 +31,7 @@ import { emitter } from 'uni-mixins' -const TYPES = ['text', 'number', 'idcard', 'digit', 'password'] +const INPUT_TYPES = ['text', 'number', 'idcard', 'digit', 'password'] const NUMBER_TYPES = ['number', 'digit'] export default { name: 'Input', @@ -109,14 +109,14 @@ export default { type = 'number' break default: - type = ~TYPES.indexOf(this.type) ? this.type : 'text' + type = ~INPUT_TYPES.indexOf(this.type) ? this.type : 'text' break } return this.password ? 'password' : type }, step () { // 处理部分设备中无法输入小数点的问题 - return ~['digit', 'number'].indexOf(this.type) ? '0.000000000000000001' : '' + return ~NUMBER_TYPES.indexOf(this.type) ? '0.000000000000000001' : '' } }, watch: { @@ -130,7 +130,7 @@ export default { this.$emit('update:value', value) }, maxlength (value) { - const realValue = this.realValue.slice(0, value) + const realValue = this.realValue.slice(0, parseInt(value, 10)) realValue !== this.inputValue && (this.inputValue = realValue) } }, @@ -195,13 +195,22 @@ export default { // 处理部分输入法可以输入其它字符的情况 if (~NUMBER_TYPES.indexOf(this.type)) { if (this.$refs.input.validity && !this.$refs.input.validity.valid) { - this.inputValue = this.cachedValue $event.target.value = this.cachedValue + this.inputValue = $event.target.value } else { this.cachedValue = this.inputValue } } + // type="number" 不支持 maxlength 属性,因此需要主动限制长度。 + if (this.inputType === 'number') { + const maxlength = parseInt(this.maxlength, 10) + if (maxlength > 0 && $event.target.value.length > maxlength) { + $event.target.value = $event.target.value.slice(0, maxlength) + this.inputValue = $event.target.value + } + } + this.$trigger('input', $event, { value: this.inputValue }) -- GitLab