Skip to content

Commit 9849343

Browse files
committed
Update form-text.js
1 parent 4dcb5fb commit 9849343

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/mixins/form-text.js

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ export default {
5454
}
5555
},
5656
data() {
57-
const value = this.stringifyValue(this.value)
58-
5957
return {
60-
localValue: value,
61-
formattedValue: value
58+
localValue: this.stringifyValue(this.value),
59+
vModelValue: this.value
6260
}
6361
},
6462
computed: {
@@ -92,19 +90,20 @@ export default {
9290
},
9391
watch: {
9492
value(newVal) {
95-
const value = this.stringifyValue(newVal)
96-
if (value !== this.localValue) {
97-
this.localValue = value
98-
this.formattedValue = value
93+
const stringifyValue = this.stringifyValue(newVal)
94+
if (stringifyValue !== this.localValue && newVal !== this.vModelValue) {
95+
this.localValue = stringifyValue
96+
this.vModelValue = newVal
9997
}
10098
}
10199
},
102100
mounted() {
103-
const value = this.stringifyValue(this.value)
101+
const value = this.value
102+
const stringifyValue = this.stringifyValue(value)
104103
/* istanbul ignore next */
105-
if (value !== this.localValue) {
106-
this.localValue = value
107-
this.formattedValue = value
104+
if (stringifyValue !== this.localValue && value !== this.vModelValue) {
105+
this.localValue = stringifyValue
106+
this.vModelValue = value
108107
}
109108
},
110109
methods: {
@@ -113,9 +112,12 @@ export default {
113112
},
114113
formatValue(value, evt, force = false) {
115114
value = this.stringifyValue(value)
116-
if (this.lazy && !force) {
117-
return value
115+
if ((!this.lazyFormatter || force) && isFunction(this.formatter)) {
116+
value = this.formatter(value, evt)
118117
}
118+
return value
119+
},
120+
modifyValue(value) {
119121
// Emulate `.trim` modifier behaviour
120122
if (this.trim) {
121123
value = value.trim()
@@ -125,14 +127,12 @@ export default {
125127
const number = parseFloat(value)
126128
value = isNaN(number) ? value : number
127129
}
128-
if ((!this.lazyFormatter || force) && isFunction(this.formatter)) {
129-
value = this.formatter(value, evt)
130-
}
131130
return value
132131
},
133132
updateValue(value, lazy = false) {
134-
if (value !== this.formattedValue) {
135-
this.formattedValue = value
133+
value = this.modifyValue(value)
134+
if (value !== this.vModelValue) {
135+
this.vModelValue = value
136136
if (!lazy) {
137137
this.$emit('update', value)
138138
}
@@ -154,7 +154,7 @@ export default {
154154
evt.preventDefault()
155155
return
156156
}
157-
this.localValue = value
157+
this.localValue = formattedValue
158158
this.updateValue(formattedValue, this.lazy)
159159
this.$emit('input', formattedValue)
160160
},
@@ -179,14 +179,17 @@ export default {
179179
this.$emit('change', formattedValue)
180180
},
181181
onBlur(evt) {
182-
// Lazy v-model handling
183-
if (this.lazy || this.lazyFormatter) {
184-
const value = evt.target.value
185-
const formattedValue = this.formatValue(value, evt, true)
186-
if (formattedValue !== false) {
187-
this.localValue = formattedValue
188-
this.updateValue(formattedValue)
189-
}
182+
// Apply the `localValue` on blur to prevent cursor jumps
183+
// on mobile browsers (e.g. caused by autocomplete)
184+
const value = evt.target.value
185+
const formattedValue = this.formatValue(value, evt, true)
186+
if (formattedValue !== false) {
187+
// We need to use the modified value here to apply the
188+
// `.trim` and `.number` modifiers properly
189+
this.localValue = this.stringifyValue(this.modifyValue(formattedValue))
190+
// We pass the formatted value here since the `updateValue` method
191+
// handles the modifies itself
192+
this.updateValue(formattedValue)
190193
}
191194
// Emit native blur event
192195
this.$emit('blur', evt)

0 commit comments

Comments
 (0)