mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(Field): formatter not work when v-model changed (#6490)
This commit is contained in:
parent
9adbfcb26c
commit
1035503d65
@ -1,14 +1,14 @@
|
|||||||
// Utils
|
// Utils
|
||||||
import { preventDefault } from '../utils/dom/event';
|
|
||||||
import { formatNumber } from '../utils/format/number';
|
|
||||||
import { resetScroll } from '../utils/dom/reset-scroll';
|
import { resetScroll } from '../utils/dom/reset-scroll';
|
||||||
|
import { formatNumber } from '../utils/format/number';
|
||||||
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import {
|
import {
|
||||||
createNamespace,
|
|
||||||
isObject,
|
|
||||||
isDef,
|
isDef,
|
||||||
addUnit,
|
addUnit,
|
||||||
|
isObject,
|
||||||
isPromise,
|
isPromise,
|
||||||
isFunction,
|
isFunction,
|
||||||
|
createNamespace,
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
@ -53,6 +53,10 @@ export default createComponent({
|
|||||||
errorMessage: String,
|
errorMessage: String,
|
||||||
errorMessageAlign: String,
|
errorMessageAlign: String,
|
||||||
showWordLimit: Boolean,
|
showWordLimit: Boolean,
|
||||||
|
value: {
|
||||||
|
type: [String, Number],
|
||||||
|
default: '',
|
||||||
|
},
|
||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'text',
|
default: 'text',
|
||||||
@ -76,6 +80,7 @@ export default createComponent({
|
|||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
value() {
|
value() {
|
||||||
|
this.updateValue(this.value);
|
||||||
this.resetValidation();
|
this.resetValidation();
|
||||||
this.validateWithTrigger('onChange');
|
this.validateWithTrigger('onChange');
|
||||||
this.$nextTick(this.adjustSize);
|
this.$nextTick(this.adjustSize);
|
||||||
@ -83,7 +88,7 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.format();
|
this.updateValue(this.value);
|
||||||
this.$nextTick(this.adjustSize);
|
this.$nextTick(this.adjustSize);
|
||||||
|
|
||||||
if (this.vanForm) {
|
if (this.vanForm) {
|
||||||
@ -270,14 +275,13 @@ export default createComponent({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
format(target = this.$refs.input) {
|
updateValue(value) {
|
||||||
if (!target) {
|
value = String(value);
|
||||||
|
|
||||||
|
if (value === this.currentValue) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { value } = target;
|
|
||||||
const originValue = value;
|
|
||||||
|
|
||||||
// native maxlength not work when type is number
|
// native maxlength not work when type is number
|
||||||
const { maxlength } = this;
|
const { maxlength } = this;
|
||||||
if (isDef(maxlength) && value.length > maxlength) {
|
if (isDef(maxlength) && value.length > maxlength) {
|
||||||
@ -293,11 +297,16 @@ export default createComponent({
|
|||||||
value = this.formatter(value);
|
value = this.formatter(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value !== originValue) {
|
const { input } = this.$refs;
|
||||||
target.value = value;
|
if (input && value !== input.value) {
|
||||||
|
input.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
if (value !== this.value) {
|
||||||
|
this.$emit('input', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.currentValue = value;
|
||||||
},
|
},
|
||||||
|
|
||||||
onInput(event) {
|
onInput(event) {
|
||||||
@ -306,7 +315,7 @@ export default createComponent({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.$emit('input', this.format(event.target));
|
this.updateValue(event.target.value);
|
||||||
},
|
},
|
||||||
|
|
||||||
onFocus(event) {
|
onFocus(event) {
|
||||||
|
@ -289,11 +289,9 @@ test('formatter prop', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const input = wrapper.find('input');
|
|
||||||
|
|
||||||
input.trigger('input');
|
|
||||||
expect(wrapper.emitted('input')[0][0]).toEqual('abc');
|
expect(wrapper.emitted('input')[0][0]).toEqual('abc');
|
||||||
|
|
||||||
|
const input = wrapper.find('input');
|
||||||
input.element.value = '123efg';
|
input.element.value = '123efg';
|
||||||
input.trigger('input');
|
input.trigger('input');
|
||||||
expect(wrapper.emitted('input')[1][0]).toEqual('efg');
|
expect(wrapper.emitted('input')[1][0]).toEqual('efg');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user