Browse Source

input吃字bug

panhui 5 years ago
parent
commit
266acf6903
2 changed files with 21 additions and 23 deletions
  1. 16 19
      src/vant/field/index.js
  2. 5 4
      src/vant/field/props.js

+ 16 - 19
src/vant/field/index.js

@@ -45,15 +45,15 @@ VantComponent({
         showClear: false
     },
     created() {
-        this.value = this.data.value;
-        this.setData({ innerValue: this.value });
+        // this.value = this.data.value;
+        this.setData({ innerValue: this.data.value });
     },
     methods: {
         onInput(event) {
             const { value = '' } = event.detail || {};
-            this.value = value;
+            // this.value = value;
+            this.emitChange(value);
             this.setShowClear();
-            this.emitChange();
         },
         onFocus(event) {
             this.focused = true;
@@ -71,25 +71,24 @@ VantComponent({
         onClear() {
             this.setData({ innerValue: '' });
             this.value = '';
+            this.emitChange('');
+            this.$emit('clear', '');
             this.setShowClear();
-            wx.nextTick(() => {
-                this.emitChange();
-                this.$emit('clear', '');
-            });
         },
         onConfirm(event) {
             const { value = '' } = event.detail || {};
-            this.value = value;
+            // this.value = value;
+            this.setData({ innerValue: '' });
             this.setShowClear();
             this.$emit('confirm', value);
         },
         setValue(value) {
-            this.value = value;
+            // this.value = value;
             this.setShowClear();
             if (value === '') {
                 this.setData({ innerValue: '' });
             }
-            this.emitChange();
+            this.emitChange(value);
         },
         onLineChange(event) {
             this.$emit('linechange', event.detail);
@@ -97,18 +96,16 @@ VantComponent({
         onKeyboardHeightChange(event) {
             this.$emit('keyboardheightchange', event.detail);
         },
-        emitChange() {
-            this.setData({ value: this.value });
-            wx.nextTick(() => {
-                this.$emit('input', this.value);
-                this.$emit('change', this.value);
-            });
+        emitChange(value) {
+            this.setData({ innerValue: value });
+            this.$emit('input', value);
+            this.$emit('change', value);
         },
         setShowClear() {
             const { clearable, readonly } = this.data;
-            const { focused, value } = this;
+            const { focused } = this;
             this.setData({
-                showClear: !!clearable && !!focused && !!value && !readonly
+                showClear: !!clearable && !!focused && !!this.data.innerValue && !readonly
             });
         },
         noop() {}

+ 5 - 4
src/vant/field/props.js

@@ -2,10 +2,11 @@ export const commonProps = {
     value: {
         type: String,
         observer(value) {
-            if (value !== this.value) {
-                this.setData({ innerValue: value });
-                this.value = value;
-            }
+            wx.nextTick(() => {
+                if(value!==this.data.innerValue){
+                    this.setData({ innerValue: value });
+                }
+            })
         }
     },
     placeholder: String,