mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(Field): incorrect maxlength slicing (#7284)
* fix(Field): incorrect maxlength slicing * chore: robust
This commit is contained in:
parent
f15534928e
commit
950dd10372
@ -294,10 +294,15 @@ export default createComponent({
|
|||||||
updateValue(value, trigger = 'onChange') {
|
updateValue(value, trigger = 'onChange') {
|
||||||
value = isDef(value) ? String(value) : '';
|
value = isDef(value) ? String(value) : '';
|
||||||
|
|
||||||
// native maxlength not work when type is number
|
// native maxlength have incorrect line-break counting
|
||||||
|
// see: https://github.com/youzan/vant/issues/5033
|
||||||
const { maxlength } = this;
|
const { maxlength } = this;
|
||||||
if (isDef(maxlength) && value.length > maxlength) {
|
if (isDef(maxlength) && value.length > maxlength) {
|
||||||
value = value.slice(0, maxlength);
|
if (this.value && this.value.length === +maxlength) {
|
||||||
|
({ value } = this);
|
||||||
|
} else {
|
||||||
|
value = value.slice(0, maxlength);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.type === 'number' || this.type === 'digit') {
|
if (this.type === 'number' || this.type === 'digit') {
|
||||||
|
@ -171,17 +171,25 @@ test('maxlength', async () => {
|
|||||||
value: 1234,
|
value: 1234,
|
||||||
type: 'number',
|
type: 'number',
|
||||||
},
|
},
|
||||||
|
listeners: {
|
||||||
|
input(value) {
|
||||||
|
wrapper && wrapper.setProps({ value });
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const input = wrapper.find('input');
|
const input = wrapper.find('input');
|
||||||
expect(input.element.value).toEqual('123');
|
expect(input.element.value).toEqual('123');
|
||||||
|
|
||||||
input.element.value = 1234;
|
input.element.value = 1234;
|
||||||
await later();
|
|
||||||
input.trigger('input');
|
input.trigger('input');
|
||||||
|
|
||||||
expect(input.element.value).toEqual('123');
|
expect(input.element.value).toEqual('123');
|
||||||
expect(wrapper.emitted('input')[0][0]).toEqual('123');
|
expect(wrapper.emitted('input')[0][0]).toEqual('123');
|
||||||
|
|
||||||
|
// see: https://github.com/youzan/vant/issues/7265
|
||||||
|
input.element.value = 1423;
|
||||||
|
input.trigger('input');
|
||||||
|
expect(input.element.value).toEqual('123');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('clearable prop', () => {
|
test('clearable prop', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user