From b5bf7e4a1141c9694126f964f88b4043e03124ce Mon Sep 17 00:00:00 2001 From: qiang Date: Mon, 5 Jul 2021 14:59:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BA=9F=E5=BC=83=20input=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=20verifyNumber=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/view/components/input/index.vue | 47 +++++++++--------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src/core/view/components/input/index.vue b/src/core/view/components/input/index.vue index 26b15ec29..892c027fe 100644 --- a/src/core/view/components/input/index.vue +++ b/src/core/view/components/input/index.vue @@ -5,7 +5,7 @@ class="uni-input-wrapper" >
0 && $event.target.value.length > maxlength) { // 输入前字符长度超出范围,则不触发input,且将值还原 @@ -198,37 +193,31 @@ export default { this.valueSync = $event.target.value } } - } - if (~NUMBER_TYPES.indexOf(this.type)) { - // 在输入 - 负号 的情况下,event.target.value没有值,但是会触发校验 false - this.valid = this.$refs.input.validity && this.$refs.input.validity.valid - if (!this.verifyNumber) { - this.cachedValue = this.valueSync - } else { - // 处理部分输入法可以输入其它字符的情况,此处理无法先输入 -(负号),只能输入数字再移动光标输入负号 - if (this.$refs.input.validity && !this.valid) { - $event.target.value = this.cachedValue - this.valueSync = $event.target.value - // 输入非法字符不触发 input 事件 + // 数字类型输入错误时无法获取具体的值,自定义校验和纠正。 + this.__clearCachedValue && $event.target.removeEventListener('blur', this.__clearCachedValue) + if ($event.target.validity && !$event.target.validity.valid) { + if ((!this.cachedValue && $event.data === '-') || (this.cachedValue[0] === '-' && $event.inputType === 'deleteContentBackward')) { + this.cachedValue = '-' + const clearCachedValue = this.__clearCachedValue = () => { + this.cachedValue = '' + } + $event.target.addEventListener('blur', clearCachedValue) return - } else { - this.cachedValue = this.valueSync } + this.cachedValue = this.valueSync = $event.target.value = this.cachedValue === '-' ? '' : this.cachedValue + // 输入非法字符不触发 input 事件 + return + } else { + this.cachedValue = this.valueSync } } if (outOfMaxlength) return - this.$triggerInput($event, Object.assign({ + this.$triggerInput($event, { value: this.valueSync - }, (() => { - if (!this.verifyNumber) { - return { - valid: this.valid - } - } - })()), force) + }, force) }, _onComposition ($event) { if ($event.type === 'compositionstart') { -- GitLab