1
1
import { NumberField } from '@base-ui-components/react' ;
2
2
import { ChevronDown , ChevronUp , Minus , Plus } from 'lucide-react' ;
3
- import { ComponentProps } from 'react' ;
3
+ import { ComponentProps , useRef } from 'react' ;
4
4
import { useTranslation } from 'react-i18next' ;
5
5
import { match } from 'ts-pattern' ;
6
6
@@ -27,8 +27,10 @@ export const NumberInput = ({
27
27
locale,
28
28
buttons,
29
29
className,
30
+ onKeyDown,
30
31
...props
31
32
} : NumberInputProps ) => {
33
+ const inputRef = useRef < HTMLInputElement > ( null ) ;
32
34
const { i18n } = useTranslation ( ) ;
33
35
const buttonSize = match ( size )
34
36
. with ( 'default' , undefined , null , ( ) => 'icon' as const )
@@ -39,7 +41,20 @@ export const NumberInput = ({
39
41
const _locale = locale ?? i18n . language ;
40
42
41
43
return (
42
- < NumberField . Root { ...props } locale = { _locale } className = { cn ( className ) } >
44
+ < NumberField . Root
45
+ invalid = { ! ! ariaInvalid }
46
+ { ...props }
47
+ locale = { _locale }
48
+ className = { cn ( className ) }
49
+ onKeyDown = { ( e ) => {
50
+ if ( e . key === 'Enter' ) {
51
+ // Make sure that the value is updated when pressing enter
52
+ inputRef . current ?. blur ( ) ;
53
+ inputRef . current ?. focus ( ) ;
54
+ }
55
+ onKeyDown ?.( e ) ;
56
+ } }
57
+ >
43
58
< NumberField . Group className = "flex gap-2" >
44
59
{ buttons === 'mobile' && (
45
60
< NumberField . Decrement
@@ -51,6 +66,7 @@ export const NumberInput = ({
51
66
< NumberField . Input
52
67
render = {
53
68
< Input
69
+ ref = { inputRef }
54
70
endElement = {
55
71
buttons === 'classic' && (
56
72
< NumberField . Group className = "flex flex-col" >
@@ -65,7 +81,6 @@ export const NumberInput = ({
65
81
}
66
82
size = { size }
67
83
placeholder = { placeholder }
68
- aria-invalid = { ariaInvalid }
69
84
{ ...inputProps }
70
85
/>
71
86
}
0 commit comments