🌐 AI搜索 & 代理 主页
Skip to content

Commit 954176d

Browse files
fix(b-form-input, b-form-textarea): properly handle out-of-sync values (closes #4695) (#4701)
* fix(form-input/form-textarea): properly handle out-of-sync values * Update form-text.js * Update form-text.js Co-authored-by: Troy Morehouse <troymore@nbnet.nb.ca>
1 parent cedf702 commit 954176d

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/components/form-input/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ Formatting does not occur if a `formatter` is not provided.
354354
id="input-formatter"
355355
v-model="text1"
356356
placeholder="Enter your name"
357-
:formatter="format"
357+
:formatter="formatter"
358358
></b-form-input>
359359
</b-form-group>
360360
<p><b>Value:</b> {{ text1 }}</p>
@@ -370,7 +370,7 @@ Formatting does not occur if a `formatter` is not provided.
370370
v-model="text2"
371371
placeholder="Enter your name"
372372
lazy-formatter
373-
:formatter="format"
373+
:formatter="formatter"
374374
></b-form-input>
375375
</b-form-group>
376376
<p class="mb-0"><b>Value:</b> {{ text2 }}</p>
@@ -386,7 +386,7 @@ Formatting does not occur if a `formatter` is not provided.
386386
}
387387
},
388388
methods: {
389-
format(value, event) {
389+
formatter(value) {
390390
return value.toLowerCase()
391391
}
392392
}

src/components/form-textarea/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Formatting does not occur if a `formatter` is not provided.
270270
id="textarea-formatter"
271271
v-model="text1"
272272
placeholder="Enter your text"
273-
:formatter="format"
273+
:formatter="formatter"
274274
></b-form-textarea>
275275
</b-form-group>
276276
<p style="white-space: pre-line"><b>Value:</b> {{ text1 }}</p>
@@ -286,7 +286,7 @@ Formatting does not occur if a `formatter` is not provided.
286286
v-model="text2"
287287
placeholder="Enter your text"
288288
lazy-formatter
289-
:formatter="format"
289+
:formatter="formatter"
290290
></b-form-textarea>
291291
</b-form-group>
292292
<p class="mb-0" style="white-space: pre-line"><b>Value:</b> {{ text2 }}</p>
@@ -302,7 +302,7 @@ Formatting does not occur if a `formatter` is not provided.
302302
}
303303
},
304304
methods: {
305-
format(value, event) {
305+
formatter(value) {
306306
return value.toLowerCase()
307307
}
308308
}

src/mixins/form-text.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ export default {
6767
}
6868
},
6969
computed: {
70-
computedDebounce() {
71-
// Ensure we have a positive number equal to or greater than 0
72-
return Math.max(toInteger(this.debounce) || 0, 0)
73-
},
7470
computedClass() {
7571
return [
7672
{
@@ -98,6 +94,13 @@ export default {
9894
}
9995
// Most likely a string value (which could be the string 'true')
10096
return this.ariaInvalid
97+
},
98+
computedDebounce() {
99+
// Ensure we have a positive number equal to or greater than 0
100+
return Math.max(toInteger(this.debounce) || 0, 0)
101+
},
102+
hasFormatter() {
103+
return isFunction(this.formatter)
101104
}
102105
},
103106
watch: {
@@ -132,7 +135,7 @@ export default {
132135
},
133136
formatValue(value, evt, force = false) {
134137
value = toString(value)
135-
if ((!this.lazyFormatter || force) && isFunction(this.formatter)) {
138+
if (this.hasFormatter && (!this.lazyFormatter || force)) {
136139
value = this.formatter(value, evt)
137140
}
138141
return value
@@ -151,7 +154,6 @@ export default {
151154
},
152155
updateValue(value, force = false) {
153156
const lazy = this.lazy
154-
const ms = this.computedDebounce
155157
if (lazy && !force) {
156158
return
157159
}
@@ -162,13 +164,28 @@ export default {
162164
this.vModelValue = value
163165
this.$emit('update', value)
164166
}
165-
if (ms > 0 && !lazy && !force) {
166-
// Change/Blur/Force will not be debounced
167-
this.$_inputDebounceTimer = setTimeout(doUpdate, ms)
167+
const debounce = this.computedDebounce
168+
// Only debounce the value update when a value greater than `0`
169+
// is set and we are not in lazy mode or this is a forced update
170+
if (debounce > 0 && !lazy && !force) {
171+
this.$_inputDebounceTimer = setTimeout(doUpdate, debounce)
168172
} else {
169173
// Immediately update the v-model
170174
doUpdate()
171175
}
176+
} else if (this.hasFormatter) {
177+
// When the `vModelValue` hasn't changed but the actual input value
178+
// is out of sync, make sure to change it to the given one
179+
// Usually caused by browser autocomplete and how it triggers the
180+
// change or input event, or depending on the formatter function
181+
// https://github.com/bootstrap-vue/bootstrap-vue/issues/2657
182+
// https://github.com/bootstrap-vue/bootstrap-vue/issues/3498
183+
/* istanbul ignore next: hard to test */
184+
const $input = this.$refs.input
185+
/* istanbul ignore if: hard to test outof sync value */
186+
if ($input && value !== $input.value) {
187+
$input.value = value
188+
}
172189
}
173190
},
174191
onInput(evt) {

0 commit comments

Comments
 (0)