From 8f8b3a218043a367f8c1498a40cb57f9cf269ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 18 Oct 2023 10:09:01 +0800 Subject: [PATCH] feat: support changeOnBlur --- docs/demo/simple.tsx | 13 ++++++++++++- src/InputNumber.tsx | 12 ++++++++++-- tests/input.test.tsx | 11 +++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/demo/simple.tsx b/docs/demo/simple.tsx index 0377fbff..1a8da31c 100644 --- a/docs/demo/simple.tsx +++ b/docs/demo/simple.tsx @@ -1,6 +1,6 @@ /* eslint no-console:0 */ -import React from 'react'; import InputNumber from 'rc-input-number'; +import React from 'react'; import '../../assets/index.less'; export default () => { @@ -54,6 +54,17 @@ export default () => { max={99} defaultValue={33} /> + +
+

!changeOnBlur

+ ); }; diff --git a/src/InputNumber.tsx b/src/InputNumber.tsx index dd9a28bc..205096ee 100644 --- a/src/InputNumber.tsx +++ b/src/InputNumber.tsx @@ -102,7 +102,11 @@ export interface InputNumberProps // focusOnUpDown: boolean; // useTouch: boolean; - // size?: ISize; + /** + * Trigger change onBlur event. + * If disabled, user must press enter or click handler to confirm the value update + */ + changeOnBlur?: boolean; } type InternalInputNumberProps = Omit; @@ -138,6 +142,8 @@ const InternalInputNumber = React.forwardRef( onPressEnter, onStep, + changeOnBlur = true, + ...inputProps } = props; @@ -513,7 +519,9 @@ const InternalInputNumber = React.forwardRef( // >>> Focus & Blur const onBlur = () => { - flushInputValue(false); + if (changeOnBlur) { + flushInputValue(false); + } setFocus(false); diff --git a/tests/input.test.tsx b/tests/input.test.tsx index 83b9b773..3a77f6bb 100644 --- a/tests/input.test.tsx +++ b/tests/input.test.tsx @@ -212,4 +212,15 @@ describe('InputNumber.Input', () => { expect(onChange).toHaveBeenLastCalledWith(null); }); }); + + it('!changeOnBlur', () => { + const onChange = jest.fn(); + + const { container } = render( + , + ); + + fireEvent.blur(container.querySelector('input')); + expect(onChange).not.toHaveBeenCalled(); + }); });