Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.

Commit 17bae97

Browse files
committed
feat: handle only valid numbers on UnitsInput
1 parent ffb5fa6 commit 17bae97

File tree

8 files changed

+77
-27
lines changed

8 files changed

+77
-27
lines changed

dist/index.js

Lines changed: 28 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.modern.js

Lines changed: 24 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.modern.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/utils.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
declare const shortenAddress: (address: string) => string;
22
declare const removeEmptySpaces: (str: string) => string;
33
declare const maxSupportedNumber = 99999999999999;
4-
declare const minSupportedNumber = 1e-12;
4+
declare const minSupportedNumber = 0.000001;
55
declare const validatedNumber: (num: number) => number;
66
export { shortenAddress, removeEmptySpaces, maxSupportedNumber, minSupportedNumber, validatedNumber, };

src/components/molecules/RangeSliderWithInputs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const RangeSliderWithInputs: FC<RangeSliderWithInputsProps> = ({
4747
const handleStartInputChange = (
4848
event: React.ChangeEvent<HTMLInputElement>,
4949
): void => {
50-
const newStartValue = validatedNumber(Number(event.target.value)) || startValue
50+
const newStartValue = validatedNumber(Number(event.target.value)) || minValue
5151
handleChange({ min: newStartValue, max: endValue })
5252
}
5353

src/components/molecules/UnitsInput.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ const UnitsInput: FC<UnitsInputProps> = ({
5555
handleOnChange,
5656
...rest
5757
}) => {
58-
5958
const classes = useStyles()
59+
60+
const handleChange = (event) => {
61+
const { target: { value: newValue } } = event
62+
const numberValue = Number(newValue)
63+
64+
// we only handle the change if it's a number
65+
if (numberValue || numberValue === 0) handleOnChange(event)
66+
}
67+
6068
return (
6169
<React.Fragment>
6270
<Grid className={classes.root} container spacing={1}>
@@ -65,7 +73,7 @@ const UnitsInput: FC<UnitsInputProps> = ({
6573
className={classes.input}
6674
classes={{ input: classes.input }}
6775
value={value}
68-
onChange={handleOnChange}
76+
onChange={handleChange}
6977
onBlur={handleOnBlur}
7078
inputProps={{
7179
step,

src/utils.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ const shortenAddress = (address: string): string => `${address.substr(0, 6)}...$
33
const removeEmptySpaces = (str: string): string => str.replace(/\s/g, '')
44

55
const maxSupportedNumber = 99999999999999
6-
const minSupportedNumber = 0.000000000001
6+
const minSupportedNumber = 0.000001
77

88
const validatedNumber = (num: number): number => {
9-
if (num > maxSupportedNumber) return maxSupportedNumber
9+
if (num > 0) {
10+
if (num > maxSupportedNumber) return maxSupportedNumber
1011

11-
if (num < minSupportedNumber) return minSupportedNumber
12+
if (num < minSupportedNumber) return minSupportedNumber
13+
}
14+
15+
if (num < 0) {
16+
if (num < -maxSupportedNumber) return -maxSupportedNumber
17+
18+
if (num > -minSupportedNumber) return -minSupportedNumber
19+
}
1220
return num
1321
}
1422

0 commit comments

Comments
 (0)