From acc972a2031740e91808fe883a7eefb2048a38d9 Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 16 Oct 2019 16:37:28 +0800 Subject: [PATCH] feat(Stepper): should limit decimal length when input (#4747) --- src/stepper/index.js | 8 +++++++- src/stepper/test/index.spec.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/stepper/index.js b/src/stepper/index.js index 7ecd149fb..65886a231 100644 --- a/src/stepper/index.js +++ b/src/stepper/index.js @@ -149,7 +149,13 @@ export default createComponent({ return; } - const formatted = this.filter(value); + let formatted = this.filter(value); + + // limit max decimal length + if (isDef(this.decimalLength) && formatted.indexOf('.') !== -1) { + const pair = formatted.split('.'); + formatted = `${pair[0]}.${pair[1].slice(0, this.decimalLength)}`; + } if (!equal(value, formatted)) { event.target.value = formatted; diff --git a/src/stepper/test/index.spec.js b/src/stepper/test/index.spec.js index e6080371f..d3d928b34 100644 --- a/src/stepper/test/index.spec.js +++ b/src/stepper/test/index.spec.js @@ -213,3 +213,19 @@ test('decimal-length prop', () => { plus.trigger('click'); expect(wrapper.emitted('input')[1][0]).toEqual('1.20'); }); + +test('should limit decimal-length when input', () => { + const wrapper = mount(Stepper, { + propsData: { + value: 1, + step: 0.2, + decimalLength: 1 + } + }); + + const input = wrapper.find('input'); + input.element.value = '1.25'; + input.trigger('input'); + + expect(input.element.value).toEqual('1.2'); +});