Skip to content

Commit 25066bb

Browse files
committed
fix: unable to input decimals in distance input
1 parent 7c5e8ec commit 25066bb

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/components/editor/NumericInput2.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
NumericInput,
44
NumericInputProps,
55
} from '@blueprintjs/core'
6+
import { useRef, useState } from 'react'
67

78
type MixedNumericInputProps = HTMLInputProps & NumericInputProps
89

@@ -13,20 +14,34 @@ export interface NumericInput2Props extends MixedNumericInputProps {
1314
export const NumericInput2 = ({
1415
intOnly,
1516
min,
17+
minorStepSize,
18+
value,
1619
onValueChange,
1720
...props
1821
}: NumericInput2Props) => {
1922
const allowNegative = min === undefined || min < 0
2023

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+
2135
return (
2236
<NumericInput
2337
allowNumericCharactersOnly
2438
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)}
2544
onValueChange={(num, str, inputEl) => {
26-
if (!onValueChange) {
27-
return
28-
}
29-
3045
// count hyphens to determine the sign, so that user can toggle the sign
3146
// by pressing hyphen key, regardless of the cursor position
3247
let hyphens = 0
@@ -36,6 +51,14 @@ export const NumericInput2 = ({
3651
return ''
3752
})
3853

54+
const dots = str.split('.').length - 1
55+
56+
if (dots > 1 || (dots === 1 && intOnly)) {
57+
return
58+
}
59+
60+
setEndsWithDot(str.endsWith('.'))
61+
3962
num = parseFloat(str)
4063

4164
if (Number.isNaN(num) || !Number.isFinite(num)) {
@@ -50,7 +73,7 @@ export const NumericInput2 = ({
5073
num = ~~num
5174
}
5275

53-
onValueChange(num, str, inputEl)
76+
onValueChange?.(num, str, inputEl)
5477
}}
5578
{...props}
5679
/>

src/components/editor/action/EditorActionDistance.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ export const EditorActionDistance = ({
7171
>
7272
<div className="flex">
7373
<NumericInput2
74-
intOnly
7574
selectAllOnFocus
7675
className="mr-2"
7776
placeholder="X 距离"
78-
minorStepSize={0.5}
77+
stepSize={0.5}
7978
onValueChange={(value) => onChange(transform.fromX(value))}
8079
onBlur={onBlur}
8180
value={value?.[0]?.toString() ?? ''}
@@ -88,10 +87,9 @@ export const EditorActionDistance = ({
8887
/>
8988

9089
<NumericInput2
91-
intOnly
9290
selectAllOnFocus
9391
placeholder="Y 距离"
94-
minorStepSize={0.5}
92+
stepSize={0.5}
9593
onValueChange={(value) => onChange(transform.fromY(value))}
9694
onBlur={onBlur}
9795
value={value?.[1]?.toString() ?? ''}

src/components/editor/operator/EditorOperatorSkillTimes.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const EditorOperatorSkillTimes = <
2222

2323
return (
2424
<NumericInput2
25+
intOnly
2526
defaultValue={0}
2627
onValueChange={(val) => onChange(Math.min(val, 100))}
2728
onBlur={onBlur}

0 commit comments

Comments
 (0)