From 96898fe38aaa11be525c16c991d16cdb3d7a2d7e 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: Sat, 15 Nov 2025 23:15:05 +0800 Subject: [PATCH 1/2] fix: Click should focus input --- src/InputNumber.tsx | 59 ++++++++++++++++++++++++++++++++++++++++---- tests/input.test.tsx | 10 ++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/InputNumber.tsx b/src/InputNumber.tsx index 353c724f..d40b2bc3 100644 --- a/src/InputNumber.tsx +++ b/src/InputNumber.tsx @@ -53,9 +53,33 @@ const getDecimalIfValidate = (value: ValueType) => { type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix'; export interface InputNumberProps extends Omit< - React.InputHTMLAttributes, - 'value' | 'defaultValue' | 'onInput' | 'onChange' | 'prefix' | 'suffix' - > { + React.InputHTMLAttributes, + | 'value' + | 'defaultValue' + | 'onInput' + | 'onChange' + | 'prefix' + | 'suffix' + | 'onMouseDown' + | 'onClick' + | 'onMouseUp' + | 'onMouseLeave' + | 'onMouseMove' + | 'onMouseEnter' + | 'onMouseOut' + >, + Pick< + React.HTMLAttributes, + | 'onMouseDown' + | 'onClick' + | 'onMouseUp' + | 'onMouseLeave' + | 'onMouseMove' + | 'onMouseEnter' + | 'onMouseOut' + > { + disabled?: boolean; + readOnly?: boolean; /** value will show as string */ stringMode?: boolean; @@ -143,9 +167,18 @@ const InputNumber = React.forwardRef((props, r onPressEnter, onStep, + // Mouse Events + onMouseDown, + onClick, + onMouseUp, + onMouseLeave, + onMouseMove, + onMouseEnter, + onMouseOut, + changeOnBlur = true, - ...inputProps + ...restProps } = props; const [focus, setFocus] = React.useState(false); @@ -561,6 +594,15 @@ const InputNumber = React.forwardRef((props, r userTypingRef.current = false; }; + // >>> Mouse events + const onInternalMouseDown: React.MouseEventHandler = (event) => { + if (event.target !== inputRef.current) { + inputRef.current.focus(); + } + + onMouseDown?.(event); + }; + // ========================== Controlled ========================== // Input by precision & formatter useLayoutUpdateEffect(() => { @@ -624,6 +666,13 @@ const InputNumber = React.forwardRef((props, r [`${prefixCls}-out-of-range`]: !decimalValue.isInvalidate() && !isInRange(decimalValue), })} style={{ ...styles?.root, ...style }} + onMouseDown={onInternalMouseDown} + onMouseUp={onMouseUp} + onMouseLeave={onMouseLeave} + onMouseMove={onMouseMove} + onMouseEnter={onMouseEnter} + onMouseOut={onMouseOut} + onClick={onClick} onFocus={() => { setFocus(true); }} @@ -649,7 +698,6 @@ const InputNumber = React.forwardRef((props, r aria-valuemax={max as any} aria-valuenow={decimalValue.isInvalidate() ? null : (decimalValue.toString() as any)} step={step} - {...inputProps} ref={inputRef} className={clsx(`${prefixCls}-input`, classNames?.input)} style={styles?.input} @@ -657,6 +705,7 @@ const InputNumber = React.forwardRef((props, r onChange={onInternalInput} disabled={disabled} readOnly={readOnly} + {...restProps} /> {suffix !== undefined && ( diff --git a/tests/input.test.tsx b/tests/input.test.tsx index c38654e4..df9aa462 100644 --- a/tests/input.test.tsx +++ b/tests/input.test.tsx @@ -237,4 +237,14 @@ describe('InputNumber.Input', () => { expect(ref.current.nativeElement).toBe(container.querySelector('.rc-input-number')); }); }); + + it('click prefix should focus input', () => { + const { container } = render(); + const input = container.querySelector('input'); + const prefix = container.querySelector('.rc-input-number-prefix'); + + expect(document.activeElement).not.toBe(input); + fireEvent.mouseDown(prefix); + expect(document.activeElement).toBe(input); + }); }); From 44914eb9bb9604a794752b6e69b7705fdf478c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Sat, 15 Nov 2025 23:21:06 +0800 Subject: [PATCH 2/2] Update src/InputNumber.tsx Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- src/InputNumber.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InputNumber.tsx b/src/InputNumber.tsx index d40b2bc3..eb1552b7 100644 --- a/src/InputNumber.tsx +++ b/src/InputNumber.tsx @@ -596,7 +596,7 @@ const InputNumber = React.forwardRef((props, r // >>> Mouse events const onInternalMouseDown: React.MouseEventHandler = (event) => { - if (event.target !== inputRef.current) { + if (inputRef.current && event.target !== inputRef.current) { inputRef.current.focus(); }