diff --git a/packages/input-number/__tests__/input-number.spec.ts b/packages/input-number/__tests__/input-number.spec.ts index 72cb97227d..9e5641f3de 100644 --- a/packages/input-number/__tests__/input-number.spec.ts +++ b/packages/input-number/__tests__/input-number.spec.ts @@ -242,4 +242,59 @@ describe('InputNumber.vue', () => { await nextTick() expect(wrapper.vm.num).toBe(1) }) + + test('check increase and decrease button when modelValue not in [min, max]', async () => { + const wrapper = _mount({ + template: ` + + `, + setup() { + const num1 = ref(-5) + const num2 = ref(15) + const inputNumber1 = ref(null) + const inputNumber2 = ref(null) + return { + num1, + num2, + inputNumber1, + inputNumber2, + } + }, + }) + const elInputNumber1 = wrapper.vm.inputNumber1 + const elInputNumber2 = wrapper.vm.inputNumber2 + expect(wrapper.vm.num1).toBe(1) + expect(wrapper.vm.num2).toBe(10) + + elInputNumber1.decrease() + await nextTick() + expect(wrapper.vm.num1).toBe(1) + elInputNumber1.increase() + await nextTick() + expect(wrapper.vm.num1).toBe(2) + elInputNumber1.increase() + await nextTick() + expect(wrapper.vm.num1).toBe(3) + + + elInputNumber2.increase() + await nextTick() + expect(wrapper.vm.num2).toBe(10) + elInputNumber2.decrease() + await nextTick() + expect(wrapper.vm.num2).toBe(9) + elInputNumber2.decrease() + await nextTick() + expect(wrapper.vm.num2).toBe(8) + }) }) diff --git a/packages/input-number/src/index.vue b/packages/input-number/src/index.vue index 67d7b9755e..79259db213 100644 --- a/packages/input-number/src/index.vue +++ b/packages/input-number/src/index.vue @@ -269,8 +269,14 @@ export default defineComponent({ newVal = toPrecision(newVal, props.precision) } } - if (newVal !== undefined && newVal >= props.max) newVal = props.max - if (newVal !== undefined && newVal <= props.min) newVal = props.min + if (newVal !== undefined && newVal >= props.max) { + newVal = props.max + emit('update:modelValue', newVal) + } + if (newVal !== undefined && newVal <= props.min) { + newVal = props.min + emit('update:modelValue', newVal) + } data.currentValue = newVal data.userInput = null }, @@ -284,7 +290,7 @@ export default defineComponent({ innerInput.setAttribute('aria-valuenow', data.currentValue) innerInput.setAttribute('aria-disabled', inputNumberDisabled.value) if (toRawType(props.modelValue) !== 'Number' && props.modelValue !== undefined) { - setCurrentValue(undefined) + emit('update:modelValue', undefined) } }) onUpdated(() => {