[bugfix] Field: maxlength not work when type = number (#1839)

This commit is contained in:
neverland 2018-09-20 21:15:52 +08:00 committed by GitHub
parent 4c7c2d7b76
commit efa264d612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -105,6 +105,7 @@ export default create({
},
mounted() {
this.format();
this.$nextTick(this.adjustSize);
},
@ -129,8 +130,21 @@ export default create({
this.$refs.input && this.$refs.input.blur();
},
// native maxlength not work when type = number
format(target = this.$refs.input) {
let { value } = target;
const { maxlength } = this.$attrs;
if (this.isDef(maxlength) && value.length > maxlength) {
value = value.slice(0, maxlength);
target.value = value;
}
return value;
},
onInput(event) {
this.$emit('input', event.target.value);
this.$emit('input', this.format(event.target));
},
onFocus(event) {

View File

@ -109,3 +109,25 @@ test('blur method', () => {
expect(fn.mock.calls.length).toEqual(1);
});
test('maxlength', async() => {
const wrapper = mount(Field, {
attrs: {
maxlength: 3
},
propsData: {
value: 1234,
type: 'number'
}
});
const input = wrapper.find('input');
expect(input.element.value).toEqual('123');
input.element.value = 1234;
await later();
input.trigger('input');
expect(input.element.value).toEqual('123');
expect(wrapper.emitted('input')[0][0]).toEqual('123');
});