From 18a77d5829df0b7db064d634cd45645befe77435 Mon Sep 17 00:00:00 2001 From: KESHAOYE <30970336+KESHAOYE@users.noreply.github.com> Date: Tue, 17 Sep 2024 13:36:36 +0800 Subject: [PATCH] fix(components): `step-strictly` is true and should keep the initial value and step matching (#18277) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(components): 修复step-strictly为true时初次渲染组件v-model与输入框中显示的值不同的问题 When step strictly is set to true, the initial rendering of component v-model differs from the value displayed in the input box。See issue # 18275 for details。 closed #18275 * fix: unit test error * test(components): [input-number] test cases with fixes added --- .../input-number/__tests__/input-number.test.tsx | 10 ++++++++++ packages/components/input-number/src/input-number.vue | 3 +++ 2 files changed, 13 insertions(+) diff --git a/packages/components/input-number/__tests__/input-number.test.tsx b/packages/components/input-number/__tests__/input-number.test.tsx index c258ec4474..2adcdbb8de 100755 --- a/packages/components/input-number/__tests__/input-number.test.tsx +++ b/packages/components/input-number/__tests__/input-number.test.tsx @@ -594,4 +594,14 @@ describe('InputNumber.vue', () => { expect(increase.classes()).toContain('el-icon') expect(decrease.classes()).toContain('el-icon') }) + + // fix: #18275 + test('step-strictly is true and should keep the initial value and step matching', () => { + const num = ref(2.6) + const wrapper = mount(() => ( + + )) + expect(wrapper.find('input').element.value).toBe(num.value.toString()) + wrapper.unmount() + }) }) diff --git a/packages/components/input-number/src/input-number.vue b/packages/components/input-number/src/input-number.vue index 94765077c8..6c59974d34 100755 --- a/packages/components/input-number/src/input-number.vue +++ b/packages/components/input-number/src/input-number.vue @@ -222,6 +222,9 @@ const verifyValue = ( } if (stepStrictly) { newVal = toPrecision(Math.round(newVal / step) * step, precision) + if (newVal !== value) { + update && emit(UPDATE_MODEL_EVENT, newVal) + } } if (!isUndefined(precision)) { newVal = toPrecision(newVal, precision)