Skip to content

Commit 32e0ed8

Browse files
fix: Click should focus input (#727)
* fix: Click should focus input * Update src/InputNumber.tsx Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 5613cbc commit 32e0ed8

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/InputNumber.tsx

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,33 @@ const getDecimalIfValidate = (value: ValueType) => {
5353
type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix';
5454
export interface InputNumberProps<T extends ValueType = ValueType>
5555
extends Omit<
56-
React.InputHTMLAttributes<HTMLInputElement>,
57-
'value' | 'defaultValue' | 'onInput' | 'onChange' | 'prefix' | 'suffix'
58-
> {
56+
React.InputHTMLAttributes<HTMLInputElement>,
57+
| 'value'
58+
| 'defaultValue'
59+
| 'onInput'
60+
| 'onChange'
61+
| 'prefix'
62+
| 'suffix'
63+
| 'onMouseDown'
64+
| 'onClick'
65+
| 'onMouseUp'
66+
| 'onMouseLeave'
67+
| 'onMouseMove'
68+
| 'onMouseEnter'
69+
| 'onMouseOut'
70+
>,
71+
Pick<
72+
React.HTMLAttributes<HTMLDivElement>,
73+
| 'onMouseDown'
74+
| 'onClick'
75+
| 'onMouseUp'
76+
| 'onMouseLeave'
77+
| 'onMouseMove'
78+
| 'onMouseEnter'
79+
| 'onMouseOut'
80+
> {
81+
disabled?: boolean;
82+
readOnly?: boolean;
5983
/** value will show as string */
6084
stringMode?: boolean;
6185

@@ -143,9 +167,18 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
143167
onPressEnter,
144168
onStep,
145169

170+
// Mouse Events
171+
onMouseDown,
172+
onClick,
173+
onMouseUp,
174+
onMouseLeave,
175+
onMouseMove,
176+
onMouseEnter,
177+
onMouseOut,
178+
146179
changeOnBlur = true,
147180

148-
...inputProps
181+
...restProps
149182
} = props;
150183

151184
const [focus, setFocus] = React.useState(false);
@@ -561,6 +594,15 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
561594
userTypingRef.current = false;
562595
};
563596

597+
// >>> Mouse events
598+
const onInternalMouseDown: React.MouseEventHandler<HTMLDivElement> = (event) => {
599+
if (inputRef.current && event.target !== inputRef.current) {
600+
inputRef.current.focus();
601+
}
602+
603+
onMouseDown?.(event);
604+
};
605+
564606
// ========================== Controlled ==========================
565607
// Input by precision & formatter
566608
useLayoutUpdateEffect(() => {
@@ -624,6 +666,13 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
624666
[`${prefixCls}-out-of-range`]: !decimalValue.isInvalidate() && !isInRange(decimalValue),
625667
})}
626668
style={{ ...styles?.root, ...style }}
669+
onMouseDown={onInternalMouseDown}
670+
onMouseUp={onMouseUp}
671+
onMouseLeave={onMouseLeave}
672+
onMouseMove={onMouseMove}
673+
onMouseEnter={onMouseEnter}
674+
onMouseOut={onMouseOut}
675+
onClick={onClick}
627676
onFocus={() => {
628677
setFocus(true);
629678
}}
@@ -649,14 +698,14 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
649698
aria-valuemax={max as any}
650699
aria-valuenow={decimalValue.isInvalidate() ? null : (decimalValue.toString() as any)}
651700
step={step}
652-
{...inputProps}
653701
ref={inputRef}
654702
className={clsx(`${prefixCls}-input`, classNames?.input)}
655703
style={styles?.input}
656704
value={inputValue}
657705
onChange={onInternalInput}
658706
disabled={disabled}
659707
readOnly={readOnly}
708+
{...restProps}
660709
/>
661710

662711
{suffix !== undefined && (

tests/input.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,14 @@ describe('InputNumber.Input', () => {
237237
expect(ref.current.nativeElement).toBe(container.querySelector('.rc-input-number'));
238238
});
239239
});
240+
241+
it('click prefix should focus input', () => {
242+
const { container } = render(<InputNumber prefix="prefix" />);
243+
const input = container.querySelector('input');
244+
const prefix = container.querySelector('.rc-input-number-prefix');
245+
246+
expect(document.activeElement).not.toBe(input);
247+
fireEvent.mouseDown(prefix);
248+
expect(document.activeElement).toBe(input);
249+
});
240250
});

0 commit comments

Comments
 (0)