Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,33 @@ const getDecimalIfValidate = (value: ValueType) => {
type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix';
export interface InputNumberProps<T extends ValueType = ValueType>
extends Omit<
React.InputHTMLAttributes<HTMLInputElement>,
'value' | 'defaultValue' | 'onInput' | 'onChange' | 'prefix' | 'suffix'
> {
React.InputHTMLAttributes<HTMLInputElement>,
| 'value'
| 'defaultValue'
| 'onInput'
| 'onChange'
| 'prefix'
| 'suffix'
| 'onMouseDown'
| 'onClick'
| 'onMouseUp'
| 'onMouseLeave'
| 'onMouseMove'
| 'onMouseEnter'
| 'onMouseOut'
>,
Pick<
React.HTMLAttributes<HTMLDivElement>,
| 'onMouseDown'
| 'onClick'
| 'onMouseUp'
| 'onMouseLeave'
| 'onMouseMove'
| 'onMouseEnter'
| 'onMouseOut'
> {
disabled?: boolean;
readOnly?: boolean;
/** value will show as string */
stringMode?: boolean;

Expand Down Expand Up @@ -143,9 +167,18 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
onPressEnter,
onStep,

// Mouse Events
onMouseDown,
onClick,
onMouseUp,
onMouseLeave,
onMouseMove,
onMouseEnter,
onMouseOut,

changeOnBlur = true,

...inputProps
...restProps
} = props;

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

// >>> Mouse events
const onInternalMouseDown: React.MouseEventHandler<HTMLDivElement> = (event) => {
if (inputRef.current && event.target !== inputRef.current) {
inputRef.current.focus();
}

onMouseDown?.(event);
};

// ========================== Controlled ==========================
// Input by precision & formatter
useLayoutUpdateEffect(() => {
Expand Down Expand Up @@ -624,6 +666,13 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((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);
}}
Expand All @@ -649,14 +698,14 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((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}
value={inputValue}
onChange={onInternalInput}
disabled={disabled}
readOnly={readOnly}
{...restProps}
/>

{suffix !== undefined && (
Expand Down
10 changes: 10 additions & 0 deletions tests/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<InputNumber prefix="prefix" />);
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);
});
});