3
3
NumericInput ,
4
4
NumericInputProps ,
5
5
} from '@blueprintjs/core'
6
+ import { useRef , useState } from 'react'
6
7
7
8
type MixedNumericInputProps = HTMLInputProps & NumericInputProps
8
9
@@ -13,20 +14,34 @@ export interface NumericInput2Props extends MixedNumericInputProps {
13
14
export const NumericInput2 = ( {
14
15
intOnly,
15
16
min,
17
+ minorStepSize,
18
+ value,
16
19
onValueChange,
17
20
...props
18
21
} : NumericInput2Props ) => {
19
22
const allowNegative = min === undefined || min < 0
20
23
24
+ const inputRef = useRef < HTMLInputElement > ( null )
25
+ const [ endsWithDot , setEndsWithDot ] = useState ( false )
26
+
27
+ if ( minorStepSize && minorStepSize < 0.001 ) {
28
+ // not yet fixed in current version: https://github.com/palantir/blueprint/issues/4497
29
+ process . env . NODE_ENV === 'development' &&
30
+ console . warn ( 'minorStepSize cannot be smaller than 0.001' )
31
+
32
+ minorStepSize = 0.001
33
+ }
34
+
21
35
return (
22
36
< NumericInput
23
37
allowNumericCharactersOnly
24
38
min = { min }
39
+ minorStepSize = { minorStepSize }
40
+ inputRef = { inputRef }
41
+ value = { endsWithDot ? value + '.' : value }
42
+ onBlur = { ( ) => setEndsWithDot ( false ) }
43
+ onButtonClick = { ( num , str ) => onValueChange ?.( num , str , inputRef . current ) }
25
44
onValueChange = { ( num , str , inputEl ) => {
26
- if ( ! onValueChange ) {
27
- return
28
- }
29
-
30
45
// count hyphens to determine the sign, so that user can toggle the sign
31
46
// by pressing hyphen key, regardless of the cursor position
32
47
let hyphens = 0
@@ -36,6 +51,14 @@ export const NumericInput2 = ({
36
51
return ''
37
52
} )
38
53
54
+ const dots = str . split ( '.' ) . length - 1
55
+
56
+ if ( dots > 1 || ( dots === 1 && intOnly ) ) {
57
+ return
58
+ }
59
+
60
+ setEndsWithDot ( str . endsWith ( '.' ) )
61
+
39
62
num = parseFloat ( str )
40
63
41
64
if ( Number . isNaN ( num ) || ! Number . isFinite ( num ) ) {
@@ -50,7 +73,7 @@ export const NumericInput2 = ({
50
73
num = ~ ~ num
51
74
}
52
75
53
- onValueChange ( num , str , inputEl )
76
+ onValueChange ?. ( num , str , inputEl )
54
77
} }
55
78
{ ...props }
56
79
/>
0 commit comments